Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

net.js: make Socket.bytesWritten work again

Earlier string write optimizations broke it.
  • Loading branch information...
piscisaureus committed May 8, 2012
1 parent 726ebad commit 27ddd14e9f6bb08e0c19211c752565d3f7355842
Showing with 22 additions and 3 deletions.
  1. +22 −3 lib/net.js
@@ -116,7 +116,7 @@ function initSocketHandle(self) {
self.destroyed = false;
self.errorEmitted = false;
self.bytesRead = 0;
self.bytesWritten = 0;
self._bytesDispatched = 0;

// Handle creation may be deferred to bind() or connect() time.
if (self._handle) {
@@ -497,8 +497,6 @@ Socket.prototype.write = function(data, arg1, arg2) {
throw new TypeError('First argument must be a buffer or a string.');
}

this.bytesWritten += data.length;

// If we are still connecting, then buffer this for later.
if (this._connecting) {
this._connectQueueSize += data.length;
@@ -557,12 +555,33 @@ Socket.prototype._write = function(data, encoding, cb) {

writeReq.oncomplete = afterWrite;
writeReq.cb = cb;

this._pendingWriteReqs++;
this._bytesDispatched += writeReq.bytes;

return this._handle.writeQueueSize == 0;
};


Socket.prototype.__defineGetter__('bytesWritten', function() {
var bytes = this._bytesDispatched,
connectQueue = this._connectQueue;

if (connectQueue) {
connectQueue.forEach(function(el) {
var data = el[0];
if (Buffer.isBuffer(data)) {
bytes += data.length;
} else {
bytes += Buffer.byteLength(data, el[1]);
}
}, this);
}

return bytes;
});


function afterWrite(status, handle, req) {
var self = handle.socket;

0 comments on commit 27ddd14

Please sign in to comment.
You can’t perform that action at this time.