@@ -14,13 +14,25 @@ if (process.env.NODE_DEBUG && /net/.test(process.env.NODE_DEBUG)) {
14
14
}
15
15
16
16
17
+ exports . createServer = function ( ) {
18
+ return new Server ( arguments [ 0 ] , arguments [ 1 ] ) ;
19
+ } ;
20
+
21
+
22
+ exports . connect = exports . createConnection = function ( port , host ) {
23
+ var s = new Socket ( ) ;
24
+ s . connect ( port , host ) ;
25
+ return s ;
26
+ } ;
27
+
28
+
17
29
function Socket ( options ) {
18
30
if ( ! ( this instanceof Socket ) ) return new Socket ( options ) ;
19
31
20
32
stream . Stream . call ( this ) ;
21
33
22
34
// private
23
- if ( options . handle ) {
35
+ if ( options && options . handle ) {
24
36
this . _handle = options . handle ;
25
37
} else {
26
38
this . _handle = new TCP ( ) ;
@@ -46,6 +58,39 @@ Socket.prototype.setTimeout = function(msecs, callback) {
46
58
} ;
47
59
48
60
61
+ Socket . prototype . setNoDelay = function ( ) {
62
+ /* TODO implement me */
63
+ } ;
64
+
65
+
66
+ Object . defineProperty ( Socket . prototype , 'readyState' , {
67
+ get : function ( ) {
68
+ if ( this . _connecting ) {
69
+ return 'opening' ;
70
+ } else if ( this . readable && this . writable ) {
71
+ assert ( typeof this . fd === 'number' ) ;
72
+ return 'open' ;
73
+ } else if ( this . readable && ! this . writable ) {
74
+ assert ( typeof this . fd === 'number' ) ;
75
+ return 'readOnly' ;
76
+ } else if ( ! this . readable && this . writable ) {
77
+ assert ( typeof this . fd === 'number' ) ;
78
+ return 'writeOnly' ;
79
+ } else {
80
+ assert ( typeof this . fd !== 'number' ) ;
81
+ return 'closed' ;
82
+ }
83
+ }
84
+ } ) ;
85
+
86
+
87
+ Object . defineProperty ( Socket . prototype , 'bufferSize' , {
88
+ get : function ( ) {
89
+ return this . _handle . writeQueueSize ;
90
+ }
91
+ } ) ;
92
+
93
+
49
94
Socket . prototype . pause = function ( ) {
50
95
this . _handle . readStop ( ) ;
51
96
} ;
@@ -204,6 +249,8 @@ Socket.prototype.connect = function(port, host) {
204
249
throw new Error ( "ipv6 addresses not yet supported by libuv" ) ;
205
250
}
206
251
252
+ ip = ip || '127.0.0.1' ;
253
+
207
254
self . remoteAddress = ip ;
208
255
self . remotePort = port ;
209
256
@@ -214,11 +261,11 @@ Socket.prototype.connect = function(port, host) {
214
261
215
262
self . _connectReq = self . _handle . connect ( ip , port ) ;
216
263
217
- if ( ! self . _connectReq ) {
264
+ if ( self . _connectReq ) {
265
+ self . _connectReq . oncomplete = afterConnect ;
266
+ } else {
218
267
self . destroy ( errnoException ( errno , 'connect' ) ) ;
219
268
}
220
-
221
- self . _connectReq . oncomplete = afterConnect ;
222
269
}
223
270
} ) ;
224
271
} ;
@@ -330,8 +377,12 @@ function onconnection(clientHandle) {
330
377
var self = handle . socket ;
331
378
332
379
var socket = new Socket ( { handle : clientHandle } ) ;
380
+ socket . readable = socket . writable = true ;
333
381
socket . resume ( ) ;
334
382
383
+ self . connections ++ ;
384
+ socket . server = self ;
385
+
335
386
DTRACE_NET_SERVER_CONNECTION ( socket ) ;
336
387
self . emit ( 'connection' , socket ) ;
337
388
}
0 commit comments