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:
parent
376872d993
commit
068065e465
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Reference in New Issue