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.
This commit is contained in:
Joel Martin 2012-08-16 12:21:21 -05:00
parent 376872d993
commit 068065e465
1 changed files with 16 additions and 40 deletions

View File

@ -125,46 +125,26 @@ function rQshift32() {
(rQ[rQi++] << 8) + (rQ[rQi++] << 8) +
(rQ[rQi++] ); (rQ[rQi++] );
} }
function rQshiftStr(len) {
if (typeof(len) === 'undefined') { len = rQlen(); }
var arr = rQ.slice(rQi, rQi + len);
rQi += len;
return arr.map(function (num) {
return String.fromCharCode(num); } ).join('');
}
function rQshiftBytes(len) {
if (typeof(len) === 'undefined') { len = rQlen(); }
rQi += len;
return rQ.slice(rQi-len, rQi);
}
function rQslice(start, end) { 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) { if (end) {
return rQ.slice(rQi + start, rQi + end); return rQ.slice(rQi + start, rQi + end);
} else { } else {
return rQ.slice(rQi + start); return rQ.slice(rQi + start);
} }
}
}
function rQshiftStr(len) {
if (typeof(len) === 'undefined') { len = rQlen(); }
var arr = rQslice(0, len);
rQi += len;
return String.fromCharCode.apply(null, arr);
}
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;
}
function rQshiftArray(len) {
if (typeof(len) === 'undefined') { len = rQlen(); }
var a = rQslice(0, len);
rQi += len;
return a;
} }
// Check to see if we must wait for 'num' bytes (default to FBU.bytes) // 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) { function decode_message(data) {
//Util.Debug(">> decode_message: " + data); //Util.Debug(">> decode_message: " + data);
if (mode === 'binary') { if (mode === 'binary') {
// Create new arraybuffer and dump old and new data into it // push arraybuffer values onto the end
// TODO: this could be far more efficient and re-use the array rQ.push.apply(rQ, (new Uint8Array(data)));
var new_rQ = new Uint8Array(rQ.length + data.byteLength);
new_rQ.set(rQ);
new_rQ.set(new Uint8Array(data), rQ.length);
rQ = new_rQ;
} else { } else {
/* base64 decode and concat to the end */ // base64 decode and concat to the end
rQ = rQ.concat(Base64.decode(data, 0)); rQ = rQ.concat(Base64.decode(data, 0));
} }
//Util.Debug(">> decode_message, rQ: " + rQ); //Util.Debug(">> decode_message, rQ: " + rQ);
@ -265,7 +241,7 @@ function recv_message(e) {
// Compact the receive queue // Compact the receive queue
if (rQ.length > rQmax) { if (rQ.length > rQmax) {
//Util.Debug("Compacting receive queue"); //Util.Debug("Compacting receive queue");
rQ = rQslice(rQi); rQ = rQ.slice(rQi);
rQi = 0; rQi = 0;
} }
} else { } else {