Browse Source

TCP clients should buffer writes before connection

v0.7.4-release
Ryan Dahl 14 years ago
parent
commit
1d3142a882
  1. 18
      lib/net.js
  2. 2
      test/disabled/pipe-test.js

18
lib/net.js

@ -652,13 +652,20 @@ Object.defineProperty(Stream.prototype, 'readyState', {
// something was queued. If data was queued, then the "drain" event will
// signal when it has been finally flushed to socket.
Stream.prototype.write = function (data, encoding, fd) {
if (this._writeQueue && this._writeQueue.length) {
if (this._connecting || (this._writeQueue && this._writeQueue.length)) {
if (!this._writeQueue) {
this._writeQueue = [];
this._writeQueueEncoding = [];
this._writeQueueFD = [];
}
// Slow. There is already a write queue, so let's append to it.
if (this._writeQueueLast() === END_OF_FILE) {
throw new Error('Stream.end() called already; cannot write.');
}
if (typeof data == 'string' &&
this._writeQueueEncoding.length &&
this._writeQueueEncoding[this._writeQueueEncoding.length-1] === encoding) {
// optimization - concat onto last
this._writeQueue[this._writeQueue.length-1] += data;
@ -864,6 +871,14 @@ function doConnect (socket, port, host) {
socket.destroy(e);
return;
}
if (socket._writeQueue && socket._writeQueue.length) {
// Flush socket in case any writes are queued up while connecting.
// ugly
_doFlush.call(socket._writeWatcher);
}
} else if (errno != EINPROGRESS) {
socket.destroy(errnoException(errno, 'connect'));
}
@ -886,6 +901,7 @@ Stream.prototype.connect = function () {
timeout.active(socket);
self._connecting = true; // set false in doConnect
self.writable = true;
var port = toPort(arguments[0]);
if (port === false) {

2
test/disabled/pipe-test.js

@ -32,9 +32,9 @@ var web = http.Server(function (req, res) {
socket.on('connect', function () {
console.log('socket connected');
req.pipe(socket);
});
req.pipe(socket);
req.on('end', function () {
res.writeHead(200);

Loading…
Cancel
Save