Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
diff --git a/lib/net.js b/lib/net.js
index 561ebae..d9e2c10 100644
--- a/lib/net.js
+++ b/lib/net.js
@@ -67,6 +67,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;
@@ -381,12 +384,28 @@ Socket.prototype.write = function(data, arg1, arg2) {
}
}
+ 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) {
@@ -407,7 +426,10 @@ Socket.prototype.write = function(data, arg1, arg2) {
}
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
You can’t perform that action at this time.