From 068065e46567fcb58327d29f550b1a03d4327ef9 Mon Sep 17 00:00:00 2001 From: Joel Martin Date: Thu, 16 Aug 2012 12:21:21 -0500 Subject: [PATCH] Simpler (but working) binary support. Instead of trying to handle the receive queue as a typed array, just replace the base64 encode/decode with conversion from/to typed arrays and handle the receive and send queue as before (plain Javascript arrays). There is a lot of opportunity here for optimization of course, but for now it's more important that it work properly. --- include/websock.js | 56 +++++++++++++--------------------------------- 1 file changed, 16 insertions(+), 40 deletions(-) diff --git a/include/websock.js b/include/websock.js index e3f9d2a..ce4b931 100644 --- a/include/websock.js +++ b/include/websock.js @@ -125,46 +125,26 @@ function rQshift32() { (rQ[rQi++] << 8) + (rQ[rQi++] ); } -function rQslice(start, end) { - if (mode === 'binary') { - if (end) { - return rQ.subarray(rQi + start, rQi + end); - } else { - return rQ.subarray(rQi + start); - } - } else { - if (end) { - return rQ.slice(rQi + start, rQi + end); - } else { - return rQ.slice(rQi + start); - } - } -} - function rQshiftStr(len) { if (typeof(len) === 'undefined') { len = rQlen(); } - var arr = rQslice(0, len); + var arr = rQ.slice(rQi, rQi + len); rQi += len; - return String.fromCharCode.apply(null, arr); + return arr.map(function (num) { + return String.fromCharCode(num); } ).join(''); + } function rQshiftBytes(len) { if (typeof(len) === 'undefined') { len = rQlen(); } - var a = rQslice(0, len), b = []; - if (mode === 'binary') { - // Convert to plain array - b.push.apply(b, a); - } else { - // Already plain array, just return the original - b = a - } rQi += len; - return b; + return rQ.slice(rQi-len, rQi); } -function rQshiftArray(len) { - if (typeof(len) === 'undefined') { len = rQlen(); } - var a = rQslice(0, len); - rQi += len; - return a; + +function rQslice(start, end) { + if (end) { + return rQ.slice(rQi + start, rQi + end); + } else { + return rQ.slice(rQi + start); + } } // Check to see if we must wait for 'num' bytes (default to FBU.bytes) @@ -202,14 +182,10 @@ function encode_message() { function decode_message(data) { //Util.Debug(">> decode_message: " + data); if (mode === 'binary') { - // Create new arraybuffer and dump old and new data into it - // TODO: this could be far more efficient and re-use the array - var new_rQ = new Uint8Array(rQ.length + data.byteLength); - new_rQ.set(rQ); - new_rQ.set(new Uint8Array(data), rQ.length); - rQ = new_rQ; + // push arraybuffer values onto the end + rQ.push.apply(rQ, (new Uint8Array(data))); } else { - /* base64 decode and concat to the end */ + // base64 decode and concat to the end rQ = rQ.concat(Base64.decode(data, 0)); } //Util.Debug(">> decode_message, rQ: " + rQ); @@ -265,7 +241,7 @@ function recv_message(e) { // Compact the receive queue if (rQ.length > rQmax) { //Util.Debug("Compacting receive queue"); - rQ = rQslice(rQi); + rQ = rQ.slice(rQi); rQi = 0; } } else {