Skip to content

Commit

Permalink
Simpler (but working) binary support.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kanaka committed Aug 16, 2012
1 parent 376872d commit 068065e
Showing 1 changed file with 16 additions and 40 deletions.
56 changes: 16 additions & 40 deletions include/websock.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 068065e

Please sign in to comment.