Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
net.js: make Socket.bytesWritten work again
Browse files Browse the repository at this point in the history
Earlier string write optimizations broke it.
  • Loading branch information
piscisaureus committed May 9, 2012
1 parent 726ebad commit 27ddd14
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions lib/net.js
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit 27ddd14

Please sign in to comment.