Repository: thrift
Updated Branches:
  refs/heads/master 7878f1102 -> 35c8b97fb


fix TFramedTransport residual continuation

There were many bugs in the current TFramedTransport.receiver caused by
merge mistakes and bad patches. Simplify the logic so it is easier to
reason about and prevent future issues.

- THRIFT-2194 Fixed one bug with residual not being set
- THRIFT-2205 Reverted the above fix (by accident) and broke it further
  by including InputBufferUnderrunError in TFramedTransport (which is
  incorrect)

This patch cleans up TFramedTransport.receiver by only have one hold
over buffer instead of two (frame + residual).


Project: http://git-wip-us.apache.org/repos/asf/thrift/repo
Commit: http://git-wip-us.apache.org/repos/asf/thrift/commit/35c8b97f
Tree: http://git-wip-us.apache.org/repos/asf/thrift/tree/35c8b97f
Diff: http://git-wip-us.apache.org/repos/asf/thrift/diff/35c8b97f

Branch: refs/heads/master
Commit: 35c8b97fbfd277cbb195486e29be17aba714a7b1
Parents: 7878f11
Author: Wade Simmons <[email protected]>
Authored: Fri Jun 27 11:55:06 2014 -0700
Committer: Roger Meier <[email protected]>
Committed: Sun Jun 29 22:53:23 2014 +0200

----------------------------------------------------------------------
 lib/nodejs/lib/thrift/transport.js | 49 ++++++++++++---------------------
 1 file changed, 18 insertions(+), 31 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/thrift/blob/35c8b97f/lib/nodejs/lib/thrift/transport.js
----------------------------------------------------------------------
diff --git a/lib/nodejs/lib/thrift/transport.js 
b/lib/nodejs/lib/thrift/transport.js
index 8423f89..900472c 100644
--- a/lib/nodejs/lib/thrift/transport.js
+++ b/lib/nodejs/lib/thrift/transport.js
@@ -34,49 +34,36 @@ var TFramedTransport = exports.TFramedTransport = 
function(buffer, callback) {
   this.onFlush = callback;
 };
 TFramedTransport.receiver = function(callback) {
-  var frameLeft = 0,
-      framePos = 0,
-      frame = null;
   var residual = null;
 
   return function(data) {
     // Prepend any residual data from our previous read
     if (residual) {
-      var dat = new Buffer(data.length + residual.length);
-      residual.copy(dat, 0, 0);
-      data.copy(dat, residual.length, 0);
+      data = Buffer.concat([residual, data]);
       residual = null;
     }
 
     // framed transport
     while (data.length) {
-      if (frameLeft === 0) {
-        // TODO assumes we have all 4 bytes
-        if (data.length < 4) {
-          console.log("Expecting > 4 bytes, found only " + data.length);
-          residual = data;
-          throw new InputBufferUnderrunError();
-          //throw Error("Expecting > 4 bytes, found only " + data.length);
-        }
-        frameLeft = binary.readI32(data, 0);
-        frame = new Buffer(frameLeft);
-        framePos = 0;
-        data = data.slice(4, data.length);
+      if (data.length < 4) {
+        // Not enough bytes to continue, save and resume on next packet
+        residual = data;
+        return;
       }
-      
-      if (data.length >= frameLeft) {
-        data.copy(frame, framePos, 0, frameLeft);
-        data = data.slice(frameLeft, data.length);
-      
-        frameLeft = 0;
-        callback(new TFramedTransport(frame));
-      } else if (data.length) {
-        data.copy(frame, framePos, 0, data.length);
-        frameLeft -= data.length;
-        framePos += data.length;
-        data = data.slice(data.length, data.length);
-        throw new InputBufferUnderrunError();
+      var frameSize = binary.readI32(data, 0);
+      if (data.length < 4 + frameSize) {
+        // Not enough bytes to continue, save and resume on next packet
+        residual = data;
+        return;
       }
+
+      var frame = data.slice(4, 4 + frameSize);
+      residual = data.slice(4 + frameSize);
+
+      callback(new TFramedTransport(frame));
+
+      data = residual;
+      residual = null;
     }
   };
 };

Reply via email to