| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 02e6c84 commit 4571c84
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -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(); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,18 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + // Ensure that if a dgram socket is closed before the sendQueue is drained | ||
| 3 | + // will not crash | ||
| 4 | + | ||
| 5 | + const common = require('../common'); | ||
| 6 | + const dgram = require('dgram'); | ||
| 7 | + | ||
| 8 | + const buf = Buffer.alloc(1024, 42); | ||
| 9 | + | ||
| 10 | + const socket = dgram.createSocket('udp4'); | ||
| 11 | + | ||
| 12 | + socket.on('listening', function() { | ||
| 13 | + socket.close(); | ||
| 14 | + }); | ||
| 15 | + | ||
| 16 | + // adds a listener to 'listening' to send the data when | ||
| 17 | + // the socket is available | ||
| 18 | + socket.send(buf, 0, buf.length, common.PORT, 'localhost'); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments