My favorites | Sign in
ovw
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174

var Socket = function()
{
this.bOpen = false;
this.bConnected = false;
this.nCheckAliveCnt = 0;
this.checkAliveTimer = null;

this.transportService = Components.classes["@mozilla.org/network/socket-transport-service;1"].getService(Components.interfaces.nsISocketTransportService);
if (!this.transportService) { throw ("Socket: getService(nsISocketTransportService) failed"); }

this.transport = null;
this.outStream = null;
this.asciiInStream = null;
this.inPump = null;
this.observer = null;
}

Socket.kConnectionRefused = 2152398861;

// The observer gets events for connect, disconnect and received data
//
// var observer = {
// onConnected: function () { },
// onDisconnected: function () { },
// onReceiveData: function (data) {}
// }
Socket.prototype.connect = function(sHostname, nPort, observer)
{
anLogVerbose('Socket.connect ' + sHostname + ':' + nPort);

this.observer = observer;

this.transport = this.transportService.createTransport(null, 0, sHostname, nPort, null);
if (!this.transport) { throw ("Socket.connect: createTransport() failed"); }

this.outStream = this.transport.openOutputStream(0,0,0);
if (!this.outStream) { throw ("Socket.connect: transport.openOutputStream() failed"); }

var inStream = this.transport.openInputStream(0,0,0);
if (!inStream) { throw ("Socket.connect: transport.openInputStream() failed"); }

this.asciiInStream = Components.classes["@mozilla.org/scriptableinputstream;1"].createInstance(Components.interfaces.nsIScriptableInputStream);
if (!this.asciiInStream) { throw ("Socket.connect: createInstance(nsIScriptableInputStream) failed"); }

this.asciiInStream.init(inStream);

this.inPump = Components.classes["@mozilla.org/network/input-stream-pump;1"].createInstance(Components.interfaces.nsIInputStreamPump);
if (!this.inPump) { throw ("Socket.connect: createInstance(nsIInputStreamPump) failed"); }

this.inPump.init(inStream, -1, -1, 0, 0, false);

var streamListener = {
socket: this,
onStartRequest: function(request, context)
{
//anLogTrace('Socket.onStartRequest');
},
onStopRequest: function(request, context, status)
{
//anLogTrace('Socket.onStopRequest status=' + status);
this.socket.stopCheckAliveTimer();
this.socket.observer.onDisconnected();
this.socket.bConnected = false;

if (this.socket.inPump) {
this.socket.inPump.cancel(null);
}

if (this.socket.asciiInStream) {
this.socket.asciiInStream.close(status);
}

if (this.socket.outStream) {
this.socket.outStream.close(null);
}

if (this.socket.transport) {
this.socket.transport.close(null);
this.socket.transport = null;
}
},
onDataAvailable: function(request, context, inputStream, offset, count)
{
try {
var sData = this.socket.asciiInStream.read(count);
//anLogTrace('Socket.onDataAvailable sData="' + sData + '"');
this.socket.observer.onReceiveData(sData);
} catch (ex) {
anLogError('Socket.onDataAvailable Exception: ' + ex);
}
},
}

this.inPump.asyncRead(streamListener, null);
//anLogTrace('Socket.connect inPump.asyncRead');

this.bOpen = true;
this.nCheckAliveCnt = 0;
this.startCheckAliveTimer();
}

// -------------------------------

// The weird checkAliveTimer thing generates a reliable async onConnect
// notification because streamListener.onStartRequest does not fire on
// connect. Rather it comes on the first data and just before disconnect,
// which is quite stupid. Yes, onStopRequest and onStartRequest are
// balanced. Thanks a lot. Still, sending a onStartRequest/onStopRequest
// pair when connect fails is a strange idea. We want to know when
// the connect succeeds, so that we can send a HELO. Therefore we have
// to care for ourself, as always, sigh.

Socket.prototype.startCheckAliveTimer = function()
{
this.stopCheckAliveTimer();
this.checkAliveTimer = setTimeout(this.onCheckAlive.bind(this), 200);
}

Socket.prototype.stopCheckAliveTimer = function()
{
if (this.checkAliveTimer) {
clearTimeout(this.checkAliveTimer);
this.checkAliveTimer = null;
}
}

Socket.prototype.onCheckAlive = function()
{

this.checkAliveTimer = null;
this.nCheckAliveCnt++;
anLogTrace('Socket.onCheckAlive ' + this.nCheckAliveCnt);

if (this.bOpen && this.transport && this.transport.isAlive()) {
var bWasConnected = this.bConnected;
if (!bWasConnected) {
this.bConnected = true;
this.observer.onConnected();
}
} else {
if (this.nCheckAliveCnt < 100) {
this.startCheckAliveTimer();
} else {
this.disconnect();
}
}
}

// -------------------------------

Socket.prototype.send = function(sData)
{
if (this.bConnected) {
anLogTrace('Socket.send: ' + sData);
this.outStream.write(sData, sData.length);
} else {
anLogTrace('Socket.send not connected');
}
}

Socket.prototype.disconnect = function()
{
anLogTrace('Socket.disconnect');

this.bOpen = false;
this.bConnected = false;

if (this.transport) {
this.transport.close(null);
this.transport = null;
}
}

Change log

r232 by wolf.heiner on Jun 4, 2011   Diff
Firefox tracking
Go to: 
Project members, sign in to write a code review

Older revisions

r2 by wolf.heiner on May 18, 2010   Diff
import
All revisions of this file

File info

Size: 5391 bytes, 174 lines
Powered by Google Project Hosting