Skip to content

Instantly share code, notes, and snippets.

@bnoordhuis
Created October 6, 2011 20:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bnoordhuis/a7182192d0bf451c1cd0 to your computer and use it in GitHub Desktop.
Save bnoordhuis/a7182192d0bf451c1cd0 to your computer and use it in GitHub Desktop.
diff --git a/lib/net_uv.js b/lib/net_uv.js
index 277acd2..2113bfc 100644
--- a/lib/net_uv.js
+++ b/lib/net_uv.js
@@ -65,6 +65,9 @@ function initSocketHandle(self) {
self.bytesRead = 0;
self.bytesWritten = 0;
+ self.writeData = '';
+ self.writeCallbacks = [];
+
// Handle creation may be deferred to bind() or connect() time.
if (self._handle) {
self._handle.socket = self;
@@ -365,12 +368,28 @@ Socket.prototype.write = function(data /* [encoding], [fd], [cb] */) {
}
}
+ if (data.length == 0)
+ return true; // FIXME
+
+ if (!this.writeData)
+ process.nextTick(this._flush.bind(this));
+
+ if (cb)
+ this.writeCallbacks.push(cb);
+
this.bytesWritten += data.length;
+ this.writeData += data;
- // Change strings to buffers. SLOW
- if (typeof data == 'string') {
- data = new Buffer(data, encoding);
- }
+ return false;
+};
+
+
+Socket.prototype._flush = function() {
+ var data = Buffer(this.writeData);
+ var callbacks = this.writeCallbacks;
+
+ this.writeData = '';
+ this.writeCallbacks = [];
// If we are still connecting, then buffer this for later.
if (this._connecting) {
@@ -391,7 +410,10 @@ Socket.prototype.write = function(data /* [encoding], [fd], [cb] */) {
}
writeReq.oncomplete = afterWrite;
- writeReq.cb = cb;
+ writeReq.cb = function() {
+ for (var i = 0; callbacks[i]; ++i)
+ (callbacks[i])();
+ };
this._pendingWriteReqs++;
return this._handle.writeQueueSize == 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment