| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -196,7 +196,7 @@ context of the defined or default list of trusted CA certificates. | |||
| 196 | 196 | Returns a JSON structure detailing the peer's certificate, containing a dictionary | |
| 197 | 197 | with keys for the certificate `'subject'`, `'issuer'`, `'valid_from'` and `'valid_to'`. | |
| 198 | 198 | ||
| 199 | - #### stream.write(data, [encoding]) | ||
| 199 | + #### stream.write(data, [encoding], [callback]) | ||
| 200 | 200 | ||
| 201 | 201 | Sends data on the stream. The second parameter specifies the encoding in the | |
| 202 | 202 | case of a string--it defaults to UTF8 encoding. | |
@@ -205,7 +205,10 @@ Returns `true` if the entire data was flushed successfully to the kernel | |||
| 205 | 205 | buffer. Returns `false` if all or part of the data was queued in user memory. | |
| 206 | 206 | `'drain'` will be emitted when the buffer is again free. | |
| 207 | 207 | ||
| 208 | - #### stream.write(data, [encoding], [fileDescriptor]) | ||
| 208 | + The optional `callback` parameter will be executed when the data is finally | ||
| 209 | + written out - this may not be immediately. | ||
| 210 | + | ||
| 211 | + #### stream.write(data, [encoding], [fileDescriptor], [callback]) | ||
| 209 | 212 | ||
| 210 | 213 | For UNIX sockets, it is possible to send a file descriptor through the | |
| 211 | 214 | stream. Simply add the `fileDescriptor` argument and listen for the `'fd'` | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -178,6 +178,7 @@ function initStream(self) { | |||
| 178 | 178 | self._writeQueue = []; | |
| 179 | 179 | self._writeQueueEncoding = []; | |
| 180 | 180 | self._writeQueueFD = []; | |
| 181 | + self._writeQueueCallbacks = []; | ||
| 181 | 182 | ||
| 182 | 183 | self._writeWatcher = ioWatchers.alloc(); | |
| 183 | 184 | self._writeWatcher.socket = self; | |
@@ -296,6 +297,7 @@ Stream.prototype.write = function(data /* [encoding], [fd], [cb] */) { | |||
| 296 | 297 | this._writeQueue = []; | |
| 297 | 298 | this._writeQueueEncoding = []; | |
| 298 | 299 | this._writeQueueFD = []; | |
| 300 | + this._writeQueueCallbacks = []; | ||
| 299 | 301 | } | |
| 300 | 302 | ||
| 301 | 303 | // Slow. There is already a write queue, so let's append to it. | |
@@ -311,9 +313,22 @@ Stream.prototype.write = function(data /* [encoding], [fd], [cb] */) { | |||
| 311 | 313 | this._writeQueueEncoding[last] === encoding) { | |
| 312 | 314 | // optimization - concat onto last | |
| 313 | 315 | this._writeQueue[last] += data; | |
| 316 | + | ||
| 317 | + if (cb) { | ||
| 318 | + if (!this._writeQueueCallbacks[last]) { | ||
| 319 | + this._writeQueueCallbacks[last] = cb; | ||
| 320 | + } else { | ||
| 321 | + // awful | ||
| 322 | + this._writeQueueCallbacks[last] = function () { | ||
| 323 | + this._writeQueueCallbacks[last](); | ||
| 324 | + cb(); | ||
| 325 | + }; | ||
| 326 | + } | ||
| 327 | + } | ||
| 314 | 328 | } else { | |
| 315 | 329 | this._writeQueue.push(data); | |
| 316 | 330 | this._writeQueueEncoding.push(encoding); | |
| 331 | + this._writeQueueCallbacks.push(cb); | ||
| 317 | 332 | } | |
| 318 | 333 | ||
| 319 | 334 | if (fd != undefined) { | |
@@ -325,7 +340,7 @@ Stream.prototype.write = function(data /* [encoding], [fd], [cb] */) { | |||
| 325 | 340 | // Fast. | |
| 326 | 341 | // The most common case. There is no write queue. Just push the data | |
| 327 | 342 | // directly to the socket. | |
| 328 | - return this._writeOut(data, encoding, fd); | ||
| 343 | + return this._writeOut(data, encoding, fd, cb); | ||
| 329 | 344 | } | |
| 330 | 345 | }; | |
| 331 | 346 | ||
@@ -337,7 +352,7 @@ Stream.prototype.write = function(data /* [encoding], [fd], [cb] */) { | |||
| 337 | 352 | // 2. Write data to socket. Return true if flushed. | |
| 338 | 353 | // 3. Slice out remaining | |
| 339 | 354 | // 4. Unshift remaining onto _writeQueue. Return false. | |
| 340 | - Stream.prototype._writeOut = function(data, encoding, fd) { | ||
| 355 | + Stream.prototype._writeOut = function(data, encoding, fd, cb) { | ||
| 341 | 356 | if (!this.writable) { | |
| 342 | 357 | throw new Error('Stream is not writable'); | |
| 343 | 358 | } | |
@@ -388,6 +403,7 @@ Stream.prototype._writeOut = function(data, encoding, fd) { | |||
| 388 | 403 | // Unshift whatever didn't fit onto the buffer | |
| 389 | 404 | this._writeQueue.unshift(data.slice(charsWritten)); | |
| 390 | 405 | this._writeQueueEncoding.unshift(encoding); | |
| 406 | + this._writeQueueCallbacks.unshift(cb); | ||
| 391 | 407 | this._writeWatcher.start(); | |
| 392 | 408 | queuedData = true; | |
| 393 | 409 | } | |
@@ -416,6 +432,7 @@ Stream.prototype._writeOut = function(data, encoding, fd) { | |||
| 416 | 432 | if (queuedData) { | |
| 417 | 433 | return false; | |
| 418 | 434 | } else { | |
| 435 | + if (cb) cb(); | ||
| 419 | 436 | return true; | |
| 420 | 437 | } | |
| 421 | 438 | } | |
@@ -434,6 +451,7 @@ Stream.prototype._writeOut = function(data, encoding, fd) { | |||
| 434 | 451 | // data should be the next thing to write. | |
| 435 | 452 | this._writeQueue.unshift(leftOver); | |
| 436 | 453 | this._writeQueueEncoding.unshift(null); | |
| 454 | + this._writeQueueCallbacks.unshift(cb); | ||
| 437 | 455 | ||
| 438 | 456 | // If didn't successfully write any bytes, enqueue our fd and try again | |
| 439 | 457 | if (!bytesWritten) { | |
@@ -450,14 +468,15 @@ Stream.prototype.flush = function() { | |||
| 450 | 468 | while (this._writeQueue && this._writeQueue.length) { | |
| 451 | 469 | var data = this._writeQueue.shift(); | |
| 452 | 470 | var encoding = this._writeQueueEncoding.shift(); | |
| 471 | + var cb = this._writeQueueCallbacks.shift(); | ||
| 453 | 472 | var fd = this._writeQueueFD.shift(); | |
| 454 | 473 | ||
| 455 | 474 | if (data === END_OF_FILE) { | |
| 456 | 475 | this._shutdown(); | |
| 457 | 476 | return true; | |
| 458 | 477 | } | |
| 459 | 478 | ||
| 460 | - var flushed = this._writeOut(data, encoding, fd); | ||
| 479 | + var flushed = this._writeOut(data, encoding, fd, cb); | ||
| 461 | 480 | if (!flushed) return false; | |
| 462 | 481 | } | |
| 463 | 482 | if (this._writeWatcher) this._writeWatcher.stop(); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,6 +3,8 @@ var assert = require('assert'); | |||
| 3 | 3 | var net = require('net'); | |
| 4 | 4 | ||
| 5 | 5 | var tcpPort = common.PORT; | |
| 6 | + var fooWritten = false; | ||
| 7 | + var connectHappened = false; | ||
| 6 | 8 | ||
| 7 | 9 | var tcp = net.Server(function(s) { | |
| 8 | 10 | tcp.close(); | |
@@ -25,23 +27,35 @@ var tcp = net.Server(function(s) { | |||
| 25 | 27 | process.exit(1); | |
| 26 | 28 | }); | |
| 27 | 29 | }); | |
| 28 | - tcp.listen(tcpPort, startClient); | ||
| 29 | 30 | ||
| 30 | - function startClient() { | ||
| 31 | + tcp.listen(common.PORT, function () { | ||
| 31 | 32 | var socket = net.Stream(); | |
| 32 | 33 | ||
| 33 | 34 | console.log('Connecting to socket'); | |
| 34 | 35 | ||
| 35 | 36 | socket.connect(tcpPort); | |
| 36 | 37 | ||
| 38 | + | ||
| 37 | 39 | socket.on('connect', function() { | |
| 38 | 40 | console.log('socket connected'); | |
| 41 | + connectHappened = true; | ||
| 39 | 42 | }); | |
| 40 | 43 | ||
| 41 | 44 | assert.equal('opening', socket.readyState); | |
| 42 | 45 | ||
| 43 | - assert.equal(false, socket.write('foo')); | ||
| 46 | + var r = socket.write('foo', function () { | ||
| 47 | + fooWritten = true; | ||
| 48 | + assert.ok(connectHappened); | ||
| 49 | + console.error("foo written"); | ||
| 50 | + }); | ||
| 51 | + | ||
| 52 | + assert.equal(false, r); | ||
| 44 | 53 | socket.end('bar'); | |
| 45 | 54 | ||
| 46 | 55 | assert.equal('opening', socket.readyState); | |
| 47 | - } | ||
| 56 | + }); | ||
| 57 | + | ||
| 58 | + process.on('exit', function () { | ||
| 59 | + assert.ok(connectHappened); | ||
| 60 | + assert.ok(fooWritten); | ||
| 61 | + }); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -8,6 +8,7 @@ var tests_run = 0; | |||
| 8 | 8 | function pingPongTest(port, host) { | |
| 9 | 9 | var N = 1000; | |
| 10 | 10 | var count = 0; | |
| 11 | + var sentPongs = 0; | ||
| 11 | 12 | var sent_final_ping = false; | |
| 12 | 13 | ||
| 13 | 14 | var server = net.createServer({ allowHalfOpen: true }, function(socket) { | |
@@ -25,7 +26,10 @@ function pingPongTest(port, host) { | |||
| 25 | 26 | assert.equal(true, socket.readable); | |
| 26 | 27 | assert.equal(true, count <= N); | |
| 27 | 28 | if (/PING/.exec(data)) { | |
| 28 | - socket.write('PONG'); | ||
| 29 | + socket.write('PONG', function () { | ||
| 30 | + sentPongs++; | ||
| 31 | + console.error('sent PONG'); | ||
| 32 | + }); | ||
| 29 | 33 | } | |
| 30 | 34 | }); | |
| 31 | 35 | ||
@@ -85,8 +89,9 @@ function pingPongTest(port, host) { | |||
| 85 | 89 | }); | |
| 86 | 90 | ||
| 87 | 91 | client.addListener('close', function() { | |
| 88 | - console.log('client.endd'); | ||
| 92 | + console.log('client.end'); | ||
| 89 | 93 | assert.equal(N + 1, count); | |
| 94 | + assert.equal(N + 1, sentPongs); | ||
| 90 | 95 | assert.equal(true, sent_final_ping); | |
| 91 | 96 | tests_run += 1; | |
| 92 | 97 | }); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments