net.js: make Socket.bytesWritten work again
authorBert Belder <bertbelder@gmail.com>
Tue, 8 May 2012 18:19:38 +0000 (20:19 +0200)
committerBert Belder <bertbelder@gmail.com>
Wed, 9 May 2012 02:00:07 +0000 (04:00 +0200)
Earlier string write optimizations broke it.

lib/net.js

index ebf3962e8f717b412adc1ec8d3e167732b3f4223..fa6258ba4354ae02f9efd3537307b7fd2614eb34 100644 (file)
@@ -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;