@@ -243,6 +243,32 @@ Socket.prototype.sendto = function(buffer,
243
243
} ;
244
244
245
245
246
+ function enqueue ( self , toEnqueue ) {
247
+ // If the send queue hasn't been initialized yet, do it, and install an
248
+ // event handler that flushes the send queue after binding is done.
249
+ if ( ! self . _queue ) {
250
+ self . _queue = [ ] ;
251
+ self . once ( 'listening' , clearQueue ) ;
252
+ }
253
+ self . _queue . push ( toEnqueue ) ;
254
+ return ;
255
+ }
256
+
257
+
258
+ function clearQueue ( ) {
259
+ const queue = this . _queue ;
260
+ this . _queue = undefined ;
261
+
262
+ // Flush the send queue.
263
+ for ( var i = 0 ; i < queue . length ; i ++ )
264
+ queue [ i ] ( ) ;
265
+ }
266
+
267
+
268
+ // valid combinations
269
+ // send(buffer, offset, length, port, address, callback)
270
+ // send(buffer, offset, length, port, address)
271
+ // send(buffer, offset, length, port)
246
272
Socket . prototype . send = function ( buffer ,
247
273
offset ,
248
274
length ,
@@ -290,18 +316,13 @@ Socket.prototype.send = function(buffer,
290
316
// If the socket hasn't been bound yet, push the outbound packet onto the
291
317
// send queue and send after binding is complete.
292
318
if ( self . _bindState != BIND_STATE_BOUND ) {
293
- // If the send queue hasn't been initialized yet, do it, and install an
294
- // event handler that flushes the send queue after binding is done.
295
- if ( ! self . _sendQueue ) {
296
- self . _sendQueue = [ ] ;
297
- self . once ( 'listening' , function ( ) {
298
- // Flush the send queue.
299
- for ( var i = 0 ; i < self . _sendQueue . length ; i ++ )
300
- self . send . apply ( self , self . _sendQueue [ i ] ) ;
301
- self . _sendQueue = undefined ;
302
- } ) ;
303
- }
304
- self . _sendQueue . push ( [ buffer , offset , length , port , address , callback ] ) ;
319
+ enqueue ( self , self . send . bind ( self ,
320
+ buffer ,
321
+ offset ,
322
+ length ,
323
+ port ,
324
+ address ,
325
+ callback ) ) ;
305
326
return ;
306
327
}
307
328
@@ -347,10 +368,15 @@ function afterSend(err) {
347
368
this . callback ( err , this . length ) ;
348
369
}
349
370
350
-
351
371
Socket . prototype . close = function ( callback ) {
352
372
if ( typeof callback === 'function' )
353
373
this . on ( 'close' , callback ) ;
374
+
375
+ if ( this . _queue ) {
376
+ this . _queue . push ( this . close . bind ( this ) ) ;
377
+ return this ;
378
+ }
379
+
354
380
this . _healthCheck ( ) ;
355
381
this . _stopReceiving ( ) ;
356
382
this . _handle . close ( ) ;
0 commit comments