2013-08-22 08:17:40 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This object contains helpers buffering incoming data & deconstructing it
|
|
|
|
* into parcels as well as buffering outgoing data & constructing parcels.
|
|
|
|
* For that it maintains two buffers and corresponding uint8 views, indexes.
|
|
|
|
*
|
|
|
|
* The incoming buffer is a circular buffer where we store incoming data.
|
|
|
|
* As soon as a complete parcel is received, it is processed right away, so
|
|
|
|
* the buffer only needs to be large enough to hold one parcel.
|
|
|
|
*
|
|
|
|
* The outgoing buffer is to prepare outgoing parcels. The index is reset
|
|
|
|
* every time a parcel is sent.
|
|
|
|
*/
|
|
|
|
|
2015-09-15 21:19:45 +03:00
|
|
|
var Buf = {
|
2013-09-14 02:12:25 +04:00
|
|
|
INT32_MAX: 2147483647,
|
|
|
|
UINT8_SIZE: 1,
|
|
|
|
UINT16_SIZE: 2,
|
|
|
|
UINT32_SIZE: 4,
|
|
|
|
PARCEL_SIZE_SIZE: 4,
|
|
|
|
PDU_HEX_OCTET_SIZE: 4,
|
2013-09-04 14:58:26 +04:00
|
|
|
|
2013-09-06 13:03:51 +04:00
|
|
|
incomingBufferLength: 1024,
|
|
|
|
incomingBuffer: null,
|
|
|
|
incomingBytes: null,
|
|
|
|
incomingWriteIndex: 0,
|
|
|
|
incomingReadIndex: 0,
|
|
|
|
readIncoming: 0,
|
|
|
|
readAvailable: 0,
|
|
|
|
currentParcelSize: 0,
|
|
|
|
|
|
|
|
outgoingBufferLength: 1024,
|
|
|
|
outgoingBuffer: null,
|
|
|
|
outgoingBytes: null,
|
|
|
|
outgoingIndex: 0,
|
|
|
|
outgoingBufferCalSizeQueue: null,
|
2013-08-22 08:17:40 +04:00
|
|
|
|
2014-01-13 06:44:40 +04:00
|
|
|
_init: function() {
|
2013-09-06 13:03:51 +04:00
|
|
|
this.incomingBuffer = new ArrayBuffer(this.incomingBufferLength);
|
|
|
|
this.outgoingBuffer = new ArrayBuffer(this.outgoingBufferLength);
|
2013-08-22 08:17:40 +04:00
|
|
|
|
2013-09-06 13:03:51 +04:00
|
|
|
this.incomingBytes = new Uint8Array(this.incomingBuffer);
|
|
|
|
this.outgoingBytes = new Uint8Array(this.outgoingBuffer);
|
2013-08-22 08:17:40 +04:00
|
|
|
|
2013-09-04 14:58:23 +04:00
|
|
|
// Track where incoming data is read from and written to.
|
2013-09-06 13:03:51 +04:00
|
|
|
this.incomingWriteIndex = 0;
|
|
|
|
this.incomingReadIndex = 0;
|
2013-08-22 08:17:40 +04:00
|
|
|
|
2013-09-04 14:58:23 +04:00
|
|
|
// Leave room for the parcel size for outgoing parcels.
|
2013-09-06 13:03:51 +04:00
|
|
|
this.outgoingIndex = this.PARCEL_SIZE_SIZE;
|
2013-08-22 08:17:40 +04:00
|
|
|
|
2013-09-04 14:58:23 +04:00
|
|
|
// How many bytes we've read for this parcel so far.
|
2013-09-06 13:03:51 +04:00
|
|
|
this.readIncoming = 0;
|
2013-08-22 08:17:40 +04:00
|
|
|
|
2013-09-04 14:58:23 +04:00
|
|
|
// How many bytes available as parcel data.
|
2013-09-06 13:03:51 +04:00
|
|
|
this.readAvailable = 0;
|
2013-08-22 08:17:40 +04:00
|
|
|
|
2013-09-04 14:58:23 +04:00
|
|
|
// Size of the incoming parcel. If this is zero, we're expecting a new
|
|
|
|
// parcel.
|
2013-09-06 13:03:51 +04:00
|
|
|
this.currentParcelSize = 0;
|
2013-08-22 08:17:40 +04:00
|
|
|
|
2013-09-04 14:58:23 +04:00
|
|
|
// Queue for storing outgoing override points
|
2013-09-06 13:03:51 +04:00
|
|
|
this.outgoingBufferCalSizeQueue = [];
|
2013-09-04 14:58:23 +04:00
|
|
|
},
|
2013-08-22 08:17:40 +04:00
|
|
|
|
2013-09-04 14:58:23 +04:00
|
|
|
/**
|
2013-09-06 13:03:51 +04:00
|
|
|
* Mark current outgoingIndex as start point for calculation length of data
|
|
|
|
* written to outgoingBuffer.
|
2013-09-04 14:58:23 +04:00
|
|
|
* Mark can be nested for here uses queue to remember marks.
|
|
|
|
*
|
|
|
|
* @param writeFunction
|
2013-09-06 13:03:51 +04:00
|
|
|
* Function to write data length into outgoingBuffer, this function is
|
2013-09-04 14:58:23 +04:00
|
|
|
* also used to allocate buffer for data length.
|
|
|
|
* Raw data size(in Uint8) is provided as parameter calling writeFunction.
|
|
|
|
* If raw data size is not in proper unit for writing, user can adjust
|
|
|
|
* the length value in writeFunction before writing.
|
|
|
|
**/
|
2014-01-13 06:44:40 +04:00
|
|
|
startCalOutgoingSize: function(writeFunction) {
|
2013-09-06 13:03:51 +04:00
|
|
|
let sizeInfo = {index: this.outgoingIndex,
|
2013-09-04 14:58:23 +04:00
|
|
|
write: writeFunction};
|
|
|
|
|
|
|
|
// Allocate buffer for data lemgtj.
|
|
|
|
writeFunction.call(0);
|
|
|
|
|
|
|
|
// Get size of data length buffer for it is not counted into data size.
|
2013-09-06 13:03:51 +04:00
|
|
|
sizeInfo.size = this.outgoingIndex - sizeInfo.index;
|
2013-09-04 14:58:23 +04:00
|
|
|
|
|
|
|
// Enqueue size calculation information.
|
2013-09-06 13:03:51 +04:00
|
|
|
this.outgoingBufferCalSizeQueue.push(sizeInfo);
|
2013-09-04 14:58:23 +04:00
|
|
|
},
|
2013-08-22 08:17:40 +04:00
|
|
|
|
2013-09-04 14:58:23 +04:00
|
|
|
/**
|
|
|
|
* Calculate data length since last mark, and write it into mark position.
|
|
|
|
**/
|
2014-01-13 06:44:40 +04:00
|
|
|
stopCalOutgoingSize: function() {
|
2013-09-06 13:03:51 +04:00
|
|
|
let sizeInfo = this.outgoingBufferCalSizeQueue.pop();
|
2013-08-22 08:17:40 +04:00
|
|
|
|
2013-09-06 13:03:51 +04:00
|
|
|
// Remember current outgoingIndex.
|
|
|
|
let currentOutgoingIndex = this.outgoingIndex;
|
2013-09-04 14:58:23 +04:00
|
|
|
// Calculate data length, in uint8.
|
2013-09-06 13:03:51 +04:00
|
|
|
let writeSize = this.outgoingIndex - sizeInfo.index - sizeInfo.size;
|
2013-08-22 08:17:40 +04:00
|
|
|
|
2013-09-04 14:58:23 +04:00
|
|
|
// Write data length to mark, use same function for allocating buffer to make
|
|
|
|
// sure there is no buffer overloading.
|
2013-09-06 13:03:51 +04:00
|
|
|
this.outgoingIndex = sizeInfo.index;
|
2013-09-04 14:58:23 +04:00
|
|
|
sizeInfo.write(writeSize);
|
2013-08-22 08:17:40 +04:00
|
|
|
|
2013-09-06 13:03:51 +04:00
|
|
|
// Restore outgoingIndex.
|
|
|
|
this.outgoingIndex = currentOutgoingIndex;
|
2013-09-04 14:58:23 +04:00
|
|
|
},
|
2013-08-22 08:17:40 +04:00
|
|
|
|
2013-09-04 14:58:23 +04:00
|
|
|
/**
|
|
|
|
* Grow the incoming buffer.
|
|
|
|
*
|
|
|
|
* @param min_size
|
|
|
|
* Minimum new size. The actual new size will be the the smallest
|
|
|
|
* power of 2 that's larger than this number.
|
|
|
|
*/
|
2014-01-13 06:44:40 +04:00
|
|
|
growIncomingBuffer: function(min_size) {
|
2013-09-04 14:58:23 +04:00
|
|
|
if (DEBUG) {
|
2013-09-06 13:03:51 +04:00
|
|
|
debug("Current buffer of " + this.incomingBufferLength +
|
2013-09-04 14:58:23 +04:00
|
|
|
" can't handle incoming " + min_size + " bytes.");
|
|
|
|
}
|
2013-09-06 13:03:51 +04:00
|
|
|
let oldBytes = this.incomingBytes;
|
|
|
|
this.incomingBufferLength =
|
2013-09-04 14:58:23 +04:00
|
|
|
2 << Math.floor(Math.log(min_size)/Math.log(2));
|
2013-09-06 13:03:51 +04:00
|
|
|
if (DEBUG) debug("New incoming buffer size: " + this.incomingBufferLength);
|
|
|
|
this.incomingBuffer = new ArrayBuffer(this.incomingBufferLength);
|
|
|
|
this.incomingBytes = new Uint8Array(this.incomingBuffer);
|
|
|
|
if (this.incomingReadIndex <= this.incomingWriteIndex) {
|
2013-09-04 14:58:23 +04:00
|
|
|
// Read and write index are in natural order, so we can just copy
|
|
|
|
// the old buffer over to the bigger one without having to worry
|
|
|
|
// about the indexes.
|
2013-09-06 13:03:51 +04:00
|
|
|
this.incomingBytes.set(oldBytes, 0);
|
2013-09-04 14:58:23 +04:00
|
|
|
} else {
|
|
|
|
// The write index has wrapped around but the read index hasn't yet.
|
|
|
|
// Write whatever the read index has left to read until it would
|
|
|
|
// circle around to the beginning of the new buffer, and the rest
|
|
|
|
// behind that.
|
2013-09-06 13:03:51 +04:00
|
|
|
let head = oldBytes.subarray(this.incomingReadIndex);
|
|
|
|
let tail = oldBytes.subarray(0, this.incomingReadIndex);
|
|
|
|
this.incomingBytes.set(head, 0);
|
|
|
|
this.incomingBytes.set(tail, head.length);
|
|
|
|
this.incomingReadIndex = 0;
|
|
|
|
this.incomingWriteIndex += head.length;
|
2013-09-04 14:58:23 +04:00
|
|
|
}
|
|
|
|
if (DEBUG) {
|
2013-09-06 13:03:51 +04:00
|
|
|
debug("New incoming buffer size is " + this.incomingBufferLength);
|
2013-09-04 14:58:23 +04:00
|
|
|
}
|
|
|
|
},
|
2013-08-22 08:17:40 +04:00
|
|
|
|
2013-09-04 14:58:23 +04:00
|
|
|
/**
|
|
|
|
* Grow the outgoing buffer.
|
|
|
|
*
|
|
|
|
* @param min_size
|
|
|
|
* Minimum new size. The actual new size will be the the smallest
|
|
|
|
* power of 2 that's larger than this number.
|
|
|
|
*/
|
2014-01-13 06:44:40 +04:00
|
|
|
growOutgoingBuffer: function(min_size) {
|
2013-09-04 14:58:23 +04:00
|
|
|
if (DEBUG) {
|
2013-09-06 13:03:51 +04:00
|
|
|
debug("Current buffer of " + this.outgoingBufferLength +
|
2013-09-04 14:58:23 +04:00
|
|
|
" is too small.");
|
|
|
|
}
|
2013-09-06 13:03:51 +04:00
|
|
|
let oldBytes = this.outgoingBytes;
|
|
|
|
this.outgoingBufferLength =
|
2013-09-04 14:58:23 +04:00
|
|
|
2 << Math.floor(Math.log(min_size)/Math.log(2));
|
2013-09-06 13:03:51 +04:00
|
|
|
this.outgoingBuffer = new ArrayBuffer(this.outgoingBufferLength);
|
|
|
|
this.outgoingBytes = new Uint8Array(this.outgoingBuffer);
|
|
|
|
this.outgoingBytes.set(oldBytes, 0);
|
2013-09-04 14:58:23 +04:00
|
|
|
if (DEBUG) {
|
2013-09-06 13:03:51 +04:00
|
|
|
debug("New outgoing buffer size is " + this.outgoingBufferLength);
|
2013-09-04 14:58:23 +04:00
|
|
|
}
|
|
|
|
},
|
2013-08-22 08:17:40 +04:00
|
|
|
|
2013-09-04 14:58:23 +04:00
|
|
|
/**
|
|
|
|
* Functions for reading data from the incoming buffer.
|
|
|
|
*
|
|
|
|
* These are all little endian, apart from readParcelSize();
|
|
|
|
*/
|
2013-08-22 08:17:40 +04:00
|
|
|
|
2013-09-04 14:58:23 +04:00
|
|
|
/**
|
|
|
|
* Ensure position specified is readable.
|
|
|
|
*
|
|
|
|
* @param index
|
|
|
|
* Data position in incoming parcel, valid from 0 to
|
2013-09-06 13:03:51 +04:00
|
|
|
* currentParcelSize.
|
2013-09-04 14:58:23 +04:00
|
|
|
*/
|
2014-01-13 06:44:40 +04:00
|
|
|
ensureIncomingAvailable: function(index) {
|
2013-09-06 13:03:51 +04:00
|
|
|
if (index >= this.currentParcelSize) {
|
2013-09-04 14:58:23 +04:00
|
|
|
throw new Error("Trying to read data beyond the parcel end!");
|
|
|
|
} else if (index < 0) {
|
|
|
|
throw new Error("Trying to read data before the parcel begin!");
|
|
|
|
}
|
|
|
|
},
|
2013-08-22 08:17:40 +04:00
|
|
|
|
2013-09-04 14:58:23 +04:00
|
|
|
/**
|
|
|
|
* Seek in current incoming parcel.
|
|
|
|
*
|
|
|
|
* @param offset
|
|
|
|
* Seek offset in relative to current position.
|
|
|
|
*/
|
2014-01-13 06:44:40 +04:00
|
|
|
seekIncoming: function(offset) {
|
2013-09-06 13:03:51 +04:00
|
|
|
// Translate to 0..currentParcelSize
|
|
|
|
let cur = this.currentParcelSize - this.readAvailable;
|
2013-09-04 14:58:23 +04:00
|
|
|
|
|
|
|
let newIndex = cur + offset;
|
|
|
|
this.ensureIncomingAvailable(newIndex);
|
|
|
|
|
2013-09-06 13:03:51 +04:00
|
|
|
// ... incomingReadIndex -->|
|
|
|
|
// 0 new cur currentParcelSize
|
2013-09-04 14:58:23 +04:00
|
|
|
// |================|=======|====================|
|
2013-09-06 13:03:51 +04:00
|
|
|
// |<-- cur -->|<- readAvailable ->|
|
|
|
|
// |<-- newIndex -->|<-- new readAvailable -->|
|
|
|
|
this.readAvailable = this.currentParcelSize - newIndex;
|
2013-09-04 14:58:23 +04:00
|
|
|
|
|
|
|
// Translate back:
|
2013-09-06 13:03:51 +04:00
|
|
|
if (this.incomingReadIndex < cur) {
|
|
|
|
// The incomingReadIndex is wrapped.
|
|
|
|
newIndex += this.incomingBufferLength;
|
2013-09-04 14:58:23 +04:00
|
|
|
}
|
2013-09-06 13:03:51 +04:00
|
|
|
newIndex += (this.incomingReadIndex - cur);
|
|
|
|
newIndex %= this.incomingBufferLength;
|
|
|
|
this.incomingReadIndex = newIndex;
|
2013-09-04 14:58:23 +04:00
|
|
|
},
|
|
|
|
|
2014-01-13 06:44:40 +04:00
|
|
|
readUint8Unchecked: function() {
|
2013-09-06 13:03:51 +04:00
|
|
|
let value = this.incomingBytes[this.incomingReadIndex];
|
|
|
|
this.incomingReadIndex = (this.incomingReadIndex + 1) %
|
|
|
|
this.incomingBufferLength;
|
2013-09-04 14:58:23 +04:00
|
|
|
return value;
|
|
|
|
},
|
|
|
|
|
2014-01-13 06:44:40 +04:00
|
|
|
readUint8: function() {
|
2013-09-06 13:03:51 +04:00
|
|
|
// Translate to 0..currentParcelSize
|
|
|
|
let cur = this.currentParcelSize - this.readAvailable;
|
2013-09-04 14:58:23 +04:00
|
|
|
this.ensureIncomingAvailable(cur);
|
|
|
|
|
2013-09-06 13:03:51 +04:00
|
|
|
this.readAvailable--;
|
2013-09-04 14:58:23 +04:00
|
|
|
return this.readUint8Unchecked();
|
|
|
|
},
|
|
|
|
|
2014-01-13 06:44:40 +04:00
|
|
|
readUint8Array: function(length) {
|
2013-09-06 13:03:51 +04:00
|
|
|
// Translate to 0..currentParcelSize
|
|
|
|
let last = this.currentParcelSize - this.readAvailable;
|
2013-09-04 14:58:23 +04:00
|
|
|
last += (length - 1);
|
|
|
|
this.ensureIncomingAvailable(last);
|
|
|
|
|
|
|
|
let array = new Uint8Array(length);
|
|
|
|
for (let i = 0; i < length; i++) {
|
|
|
|
array[i] = this.readUint8Unchecked();
|
|
|
|
}
|
2013-08-22 08:17:40 +04:00
|
|
|
|
2013-09-06 13:03:51 +04:00
|
|
|
this.readAvailable -= length;
|
2013-09-04 14:58:23 +04:00
|
|
|
return array;
|
|
|
|
},
|
2013-08-22 08:17:40 +04:00
|
|
|
|
2014-01-13 06:44:40 +04:00
|
|
|
readUint16: function() {
|
2013-09-04 14:58:23 +04:00
|
|
|
return this.readUint8() | this.readUint8() << 8;
|
|
|
|
},
|
2013-08-22 08:17:40 +04:00
|
|
|
|
2014-01-13 06:44:40 +04:00
|
|
|
readInt32: function() {
|
2013-09-04 14:58:23 +04:00
|
|
|
return this.readUint8() | this.readUint8() << 8 |
|
|
|
|
this.readUint8() << 16 | this.readUint8() << 24;
|
|
|
|
},
|
2013-08-22 08:17:40 +04:00
|
|
|
|
2014-07-21 14:21:44 +04:00
|
|
|
readInt64: function() {
|
|
|
|
// Avoid using bitwise operators as the operands of all bitwise operators
|
|
|
|
// are converted to signed 32-bit integers.
|
|
|
|
return this.readUint8() +
|
|
|
|
this.readUint8() * Math.pow(2, 8) +
|
|
|
|
this.readUint8() * Math.pow(2, 16) +
|
|
|
|
this.readUint8() * Math.pow(2, 24) +
|
|
|
|
this.readUint8() * Math.pow(2, 32) +
|
|
|
|
this.readUint8() * Math.pow(2, 40) +
|
|
|
|
this.readUint8() * Math.pow(2, 48) +
|
|
|
|
this.readUint8() * Math.pow(2, 56);
|
|
|
|
},
|
|
|
|
|
2014-01-13 06:44:40 +04:00
|
|
|
readInt32List: function() {
|
2013-09-26 10:53:50 +04:00
|
|
|
let length = this.readInt32();
|
2013-09-04 14:58:23 +04:00
|
|
|
let ints = [];
|
|
|
|
for (let i = 0; i < length; i++) {
|
2013-09-26 10:53:50 +04:00
|
|
|
ints.push(this.readInt32());
|
2013-08-22 08:17:40 +04:00
|
|
|
}
|
2013-09-04 14:58:23 +04:00
|
|
|
return ints;
|
|
|
|
},
|
2013-08-22 08:17:40 +04:00
|
|
|
|
2014-01-13 06:44:40 +04:00
|
|
|
readString: function() {
|
2013-09-26 10:53:50 +04:00
|
|
|
let string_len = this.readInt32();
|
2013-09-14 02:12:25 +04:00
|
|
|
if (string_len < 0 || string_len >= this.INT32_MAX) {
|
2013-09-04 14:58:23 +04:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
let s = "";
|
|
|
|
for (let i = 0; i < string_len; i++) {
|
|
|
|
s += String.fromCharCode(this.readUint16());
|
|
|
|
}
|
|
|
|
// Strings are \0\0 delimited, but that isn't part of the length. And
|
|
|
|
// if the string length is even, the delimiter is two characters wide.
|
|
|
|
// It's insane, I know.
|
|
|
|
this.readStringDelimiter(string_len);
|
|
|
|
return s;
|
|
|
|
},
|
|
|
|
|
2014-01-13 06:44:40 +04:00
|
|
|
readStringList: function() {
|
2013-09-26 10:53:50 +04:00
|
|
|
let num_strings = this.readInt32();
|
2013-09-04 14:58:23 +04:00
|
|
|
let strings = [];
|
|
|
|
for (let i = 0; i < num_strings; i++) {
|
|
|
|
strings.push(this.readString());
|
|
|
|
}
|
|
|
|
return strings;
|
|
|
|
},
|
2013-08-22 08:17:40 +04:00
|
|
|
|
2014-01-13 06:44:40 +04:00
|
|
|
readStringDelimiter: function(length) {
|
2013-09-04 14:58:23 +04:00
|
|
|
let delimiter = this.readUint16();
|
|
|
|
if (!(length & 1)) {
|
|
|
|
delimiter |= this.readUint16();
|
|
|
|
}
|
|
|
|
if (DEBUG) {
|
|
|
|
if (delimiter !== 0) {
|
|
|
|
debug("Something's wrong, found string delimiter: " + delimiter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2013-08-22 08:17:40 +04:00
|
|
|
|
2014-01-13 06:44:40 +04:00
|
|
|
readParcelSize: function() {
|
2013-09-04 14:58:23 +04:00
|
|
|
return this.readUint8Unchecked() << 24 |
|
|
|
|
this.readUint8Unchecked() << 16 |
|
|
|
|
this.readUint8Unchecked() << 8 |
|
|
|
|
this.readUint8Unchecked();
|
|
|
|
},
|
2013-08-22 08:17:40 +04:00
|
|
|
|
|
|
|
/**
|
2013-09-04 14:58:23 +04:00
|
|
|
* Functions for writing data to the outgoing buffer.
|
2013-08-22 08:17:40 +04:00
|
|
|
*/
|
|
|
|
|
2013-09-04 14:58:23 +04:00
|
|
|
/**
|
|
|
|
* Ensure position specified is writable.
|
|
|
|
*
|
|
|
|
* @param index
|
|
|
|
* Data position in outgoing parcel, valid from 0 to
|
2013-09-06 13:03:51 +04:00
|
|
|
* outgoingBufferLength.
|
2013-09-04 14:58:23 +04:00
|
|
|
*/
|
2014-01-13 06:44:40 +04:00
|
|
|
ensureOutgoingAvailable: function(index) {
|
2013-09-06 13:03:51 +04:00
|
|
|
if (index >= this.outgoingBufferLength) {
|
2013-09-04 14:58:23 +04:00
|
|
|
this.growOutgoingBuffer(index + 1);
|
2013-08-22 08:17:40 +04:00
|
|
|
}
|
2013-09-04 14:58:23 +04:00
|
|
|
},
|
|
|
|
|
2014-01-13 06:44:40 +04:00
|
|
|
writeUint8: function(value) {
|
2013-09-06 13:03:51 +04:00
|
|
|
this.ensureOutgoingAvailable(this.outgoingIndex);
|
2013-09-04 14:58:23 +04:00
|
|
|
|
2013-09-06 13:03:51 +04:00
|
|
|
this.outgoingBytes[this.outgoingIndex] = value;
|
|
|
|
this.outgoingIndex++;
|
2013-09-04 14:58:23 +04:00
|
|
|
},
|
|
|
|
|
2014-01-13 06:44:40 +04:00
|
|
|
writeUint16: function(value) {
|
2013-09-04 14:58:23 +04:00
|
|
|
this.writeUint8(value & 0xff);
|
|
|
|
this.writeUint8((value >> 8) & 0xff);
|
|
|
|
},
|
|
|
|
|
2014-01-13 06:44:40 +04:00
|
|
|
writeInt32: function(value) {
|
2013-09-04 14:58:23 +04:00
|
|
|
this.writeUint8(value & 0xff);
|
|
|
|
this.writeUint8((value >> 8) & 0xff);
|
|
|
|
this.writeUint8((value >> 16) & 0xff);
|
|
|
|
this.writeUint8((value >> 24) & 0xff);
|
|
|
|
},
|
|
|
|
|
2014-01-13 06:44:40 +04:00
|
|
|
writeString: function(value) {
|
2013-09-04 14:58:23 +04:00
|
|
|
if (value == null) {
|
2013-09-26 10:53:50 +04:00
|
|
|
this.writeInt32(-1);
|
2013-09-04 14:58:23 +04:00
|
|
|
return;
|
|
|
|
}
|
2013-09-26 10:53:50 +04:00
|
|
|
this.writeInt32(value.length);
|
2013-09-04 14:58:23 +04:00
|
|
|
for (let i = 0; i < value.length; i++) {
|
|
|
|
this.writeUint16(value.charCodeAt(i));
|
|
|
|
}
|
|
|
|
// Strings are \0\0 delimited, but that isn't part of the length. And
|
|
|
|
// if the string length is even, the delimiter is two characters wide.
|
|
|
|
// It's insane, I know.
|
|
|
|
this.writeStringDelimiter(value.length);
|
|
|
|
},
|
|
|
|
|
2014-01-13 06:44:40 +04:00
|
|
|
writeStringList: function(strings) {
|
2013-09-26 10:53:50 +04:00
|
|
|
this.writeInt32(strings.length);
|
2013-09-04 14:58:23 +04:00
|
|
|
for (let i = 0; i < strings.length; i++) {
|
|
|
|
this.writeString(strings[i]);
|
|
|
|
}
|
|
|
|
},
|
2013-08-22 08:17:40 +04:00
|
|
|
|
2014-01-13 06:44:40 +04:00
|
|
|
writeStringDelimiter: function(length) {
|
2013-09-04 14:58:23 +04:00
|
|
|
this.writeUint16(0);
|
|
|
|
if (!(length & 1)) {
|
|
|
|
this.writeUint16(0);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-01-13 06:44:40 +04:00
|
|
|
writeParcelSize: function(value) {
|
2013-09-04 14:58:23 +04:00
|
|
|
/**
|
|
|
|
* Parcel size will always be the first thing in the parcel byte
|
|
|
|
* array, but the last thing written. Store the current index off
|
|
|
|
* to a temporary to be reset after we write the size.
|
|
|
|
*/
|
2013-09-06 13:03:51 +04:00
|
|
|
let currentIndex = this.outgoingIndex;
|
|
|
|
this.outgoingIndex = 0;
|
2013-09-04 14:58:23 +04:00
|
|
|
this.writeUint8((value >> 24) & 0xff);
|
|
|
|
this.writeUint8((value >> 16) & 0xff);
|
|
|
|
this.writeUint8((value >> 8) & 0xff);
|
|
|
|
this.writeUint8(value & 0xff);
|
2013-09-06 13:03:51 +04:00
|
|
|
this.outgoingIndex = currentIndex;
|
2013-09-04 14:58:23 +04:00
|
|
|
},
|
|
|
|
|
2014-01-13 06:44:40 +04:00
|
|
|
copyIncomingToOutgoing: function(length) {
|
2013-09-04 14:58:23 +04:00
|
|
|
if (!length || (length < 0)) {
|
|
|
|
return;
|
|
|
|
}
|
2013-08-22 08:17:40 +04:00
|
|
|
|
2013-09-04 14:58:23 +04:00
|
|
|
let translatedReadIndexEnd =
|
2013-09-06 13:03:51 +04:00
|
|
|
this.currentParcelSize - this.readAvailable + length - 1;
|
2013-09-04 14:58:23 +04:00
|
|
|
this.ensureIncomingAvailable(translatedReadIndexEnd);
|
|
|
|
|
2013-09-06 13:03:51 +04:00
|
|
|
let translatedWriteIndexEnd = this.outgoingIndex + length - 1;
|
2013-09-04 14:58:23 +04:00
|
|
|
this.ensureOutgoingAvailable(translatedWriteIndexEnd);
|
|
|
|
|
2013-09-06 13:03:51 +04:00
|
|
|
let newIncomingReadIndex = this.incomingReadIndex + length;
|
|
|
|
if (newIncomingReadIndex < this.incomingBufferLength) {
|
2013-09-04 14:58:23 +04:00
|
|
|
// Reading won't cause wrapping, go ahead with builtin copy.
|
2013-09-06 13:03:51 +04:00
|
|
|
this.outgoingBytes
|
|
|
|
.set(this.incomingBytes.subarray(this.incomingReadIndex,
|
|
|
|
newIncomingReadIndex),
|
|
|
|
this.outgoingIndex);
|
2013-09-04 14:58:23 +04:00
|
|
|
} else {
|
|
|
|
// Not so lucky.
|
2013-09-06 13:03:51 +04:00
|
|
|
newIncomingReadIndex %= this.incomingBufferLength;
|
|
|
|
this.outgoingBytes
|
|
|
|
.set(this.incomingBytes.subarray(this.incomingReadIndex,
|
|
|
|
this.incomingBufferLength),
|
|
|
|
this.outgoingIndex);
|
2013-09-04 14:58:23 +04:00
|
|
|
if (newIncomingReadIndex) {
|
2013-09-06 13:03:51 +04:00
|
|
|
let firstPartLength = this.incomingBufferLength - this.incomingReadIndex;
|
|
|
|
this.outgoingBytes.set(this.incomingBytes.subarray(0, newIncomingReadIndex),
|
|
|
|
this.outgoingIndex + firstPartLength);
|
2013-09-04 14:58:23 +04:00
|
|
|
}
|
|
|
|
}
|
2013-08-22 08:17:40 +04:00
|
|
|
|
2013-09-06 13:03:51 +04:00
|
|
|
this.incomingReadIndex = newIncomingReadIndex;
|
|
|
|
this.readAvailable -= length;
|
|
|
|
this.outgoingIndex += length;
|
2013-09-04 14:58:23 +04:00
|
|
|
},
|
2013-08-22 08:17:40 +04:00
|
|
|
|
2013-09-04 14:58:23 +04:00
|
|
|
/**
|
|
|
|
* Parcel management
|
|
|
|
*/
|
2013-08-22 08:17:40 +04:00
|
|
|
|
2013-09-04 14:58:23 +04:00
|
|
|
/**
|
|
|
|
* Write incoming data to the circular buffer.
|
|
|
|
*
|
|
|
|
* @param incoming
|
|
|
|
* Uint8Array containing the incoming data.
|
|
|
|
*/
|
2014-01-13 06:44:40 +04:00
|
|
|
writeToIncoming: function(incoming) {
|
2013-09-04 14:58:23 +04:00
|
|
|
// We don't have to worry about the head catching the tail since
|
|
|
|
// we process any backlog in parcels immediately, before writing
|
|
|
|
// new data to the buffer. So the only edge case we need to handle
|
|
|
|
// is when the incoming data is larger than the buffer size.
|
2013-09-06 13:03:51 +04:00
|
|
|
let minMustAvailableSize = incoming.length + this.readIncoming;
|
|
|
|
if (minMustAvailableSize > this.incomingBufferLength) {
|
2013-09-04 14:58:23 +04:00
|
|
|
this.growIncomingBuffer(minMustAvailableSize);
|
|
|
|
}
|
2013-08-22 08:17:40 +04:00
|
|
|
|
2013-09-04 14:58:23 +04:00
|
|
|
// We can let the typed arrays do the copying if the incoming data won't
|
|
|
|
// wrap around the edges of the circular buffer.
|
2013-09-06 13:03:51 +04:00
|
|
|
let remaining = this.incomingBufferLength - this.incomingWriteIndex;
|
2013-09-04 14:58:23 +04:00
|
|
|
if (remaining >= incoming.length) {
|
2013-09-06 13:03:51 +04:00
|
|
|
this.incomingBytes.set(incoming, this.incomingWriteIndex);
|
2013-09-04 14:58:23 +04:00
|
|
|
} else {
|
|
|
|
// The incoming data would wrap around it.
|
|
|
|
let head = incoming.subarray(0, remaining);
|
|
|
|
let tail = incoming.subarray(remaining);
|
2013-09-06 13:03:51 +04:00
|
|
|
this.incomingBytes.set(head, this.incomingWriteIndex);
|
|
|
|
this.incomingBytes.set(tail, 0);
|
2013-08-22 08:17:40 +04:00
|
|
|
}
|
2013-09-06 13:03:51 +04:00
|
|
|
this.incomingWriteIndex = (this.incomingWriteIndex + incoming.length) %
|
|
|
|
this.incomingBufferLength;
|
2013-09-04 14:58:23 +04:00
|
|
|
},
|
2013-08-22 08:17:40 +04:00
|
|
|
|
2013-09-04 14:58:23 +04:00
|
|
|
/**
|
|
|
|
* Process incoming data.
|
|
|
|
*
|
|
|
|
* @param incoming
|
|
|
|
* Uint8Array containing the incoming data.
|
|
|
|
*/
|
2014-01-13 06:44:40 +04:00
|
|
|
processIncoming: function(incoming) {
|
2013-09-04 14:58:23 +04:00
|
|
|
if (DEBUG) {
|
|
|
|
debug("Received " + incoming.length + " bytes.");
|
2013-09-06 13:03:51 +04:00
|
|
|
debug("Already read " + this.readIncoming);
|
2013-08-22 08:17:40 +04:00
|
|
|
}
|
|
|
|
|
2013-09-04 14:58:23 +04:00
|
|
|
this.writeToIncoming(incoming);
|
2013-09-06 13:03:51 +04:00
|
|
|
this.readIncoming += incoming.length;
|
2013-09-04 14:58:23 +04:00
|
|
|
while (true) {
|
2013-09-06 13:03:51 +04:00
|
|
|
if (!this.currentParcelSize) {
|
2013-09-04 14:58:23 +04:00
|
|
|
// We're expecting a new parcel.
|
2013-09-06 13:03:51 +04:00
|
|
|
if (this.readIncoming < this.PARCEL_SIZE_SIZE) {
|
2013-09-04 14:58:23 +04:00
|
|
|
// We don't know how big the next parcel is going to be, need more
|
|
|
|
// data.
|
|
|
|
if (DEBUG) debug("Next parcel size unknown, going to sleep.");
|
|
|
|
return;
|
|
|
|
}
|
2013-09-06 13:03:51 +04:00
|
|
|
this.currentParcelSize = this.readParcelSize();
|
2013-09-04 14:58:23 +04:00
|
|
|
if (DEBUG) {
|
2013-09-06 13:03:51 +04:00
|
|
|
debug("New incoming parcel of size " + this.currentParcelSize);
|
2013-09-04 14:58:23 +04:00
|
|
|
}
|
|
|
|
// The size itself is not included in the size.
|
2013-09-06 13:03:51 +04:00
|
|
|
this.readIncoming -= this.PARCEL_SIZE_SIZE;
|
2013-09-04 14:58:23 +04:00
|
|
|
}
|
2013-08-22 08:17:40 +04:00
|
|
|
|
2013-09-06 13:03:51 +04:00
|
|
|
if (this.readIncoming < this.currentParcelSize) {
|
2013-09-04 14:58:23 +04:00
|
|
|
// We haven't read enough yet in order to be able to process a parcel.
|
2013-09-06 13:03:51 +04:00
|
|
|
if (DEBUG) debug("Read " + this.readIncoming + ", but parcel size is "
|
|
|
|
+ this.currentParcelSize + ". Going to sleep.");
|
2013-09-04 14:58:23 +04:00
|
|
|
return;
|
2013-08-22 08:17:40 +04:00
|
|
|
}
|
|
|
|
|
2013-09-04 14:58:23 +04:00
|
|
|
// Alright, we have enough data to process at least one whole parcel.
|
|
|
|
// Let's do that.
|
2013-09-06 13:03:51 +04:00
|
|
|
let expectedAfterIndex = (this.incomingReadIndex + this.currentParcelSize)
|
|
|
|
% this.incomingBufferLength;
|
2013-08-22 08:17:40 +04:00
|
|
|
|
|
|
|
if (DEBUG) {
|
2013-09-04 14:58:23 +04:00
|
|
|
let parcel;
|
2013-09-06 13:03:51 +04:00
|
|
|
if (expectedAfterIndex < this.incomingReadIndex) {
|
|
|
|
let head = this.incomingBytes.subarray(this.incomingReadIndex);
|
|
|
|
let tail = this.incomingBytes.subarray(0, expectedAfterIndex);
|
2013-09-04 14:58:23 +04:00
|
|
|
parcel = Array.slice(head).concat(Array.slice(tail));
|
|
|
|
} else {
|
2013-09-06 13:03:51 +04:00
|
|
|
parcel = Array.slice(this.incomingBytes.subarray(
|
|
|
|
this.incomingReadIndex, expectedAfterIndex));
|
2013-09-04 14:58:23 +04:00
|
|
|
}
|
2013-09-06 13:03:51 +04:00
|
|
|
debug("Parcel (size " + this.currentParcelSize + "): " + parcel);
|
2013-09-04 14:58:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (DEBUG) debug("We have at least one complete parcel.");
|
|
|
|
try {
|
2013-09-06 13:03:51 +04:00
|
|
|
this.readAvailable = this.currentParcelSize;
|
2013-09-04 14:58:23 +04:00
|
|
|
this.processParcel();
|
|
|
|
} catch (ex) {
|
|
|
|
if (DEBUG) debug("Parcel handling threw " + ex + "\n" + ex.stack);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that the whole parcel was consumed.
|
2013-09-06 13:03:51 +04:00
|
|
|
if (this.incomingReadIndex != expectedAfterIndex) {
|
2013-09-04 14:58:23 +04:00
|
|
|
if (DEBUG) {
|
|
|
|
debug("Parcel handler didn't consume whole parcel, " +
|
2013-09-06 13:03:51 +04:00
|
|
|
Math.abs(expectedAfterIndex - this.incomingReadIndex) +
|
2013-09-04 14:58:23 +04:00
|
|
|
" bytes left over");
|
|
|
|
}
|
2013-09-06 13:03:51 +04:00
|
|
|
this.incomingReadIndex = expectedAfterIndex;
|
2013-08-22 08:17:40 +04:00
|
|
|
}
|
2013-09-06 13:03:51 +04:00
|
|
|
this.readIncoming -= this.currentParcelSize;
|
|
|
|
this.readAvailable = 0;
|
|
|
|
this.currentParcelSize = 0;
|
2013-08-22 08:17:40 +04:00
|
|
|
}
|
2013-09-04 14:58:23 +04:00
|
|
|
},
|
2013-08-22 08:17:40 +04:00
|
|
|
|
2013-09-04 14:58:23 +04:00
|
|
|
/**
|
2013-09-04 14:58:26 +04:00
|
|
|
* Communicate with the IPC thread.
|
2013-09-04 14:58:23 +04:00
|
|
|
*/
|
2014-01-13 06:44:40 +04:00
|
|
|
sendParcel: function() {
|
2013-09-04 14:58:23 +04:00
|
|
|
// Compute the size of the parcel and write it to the front of the parcel
|
|
|
|
// where we left room for it. Note that he parcel size does not include
|
|
|
|
// the size itself.
|
2013-09-06 13:03:51 +04:00
|
|
|
let parcelSize = this.outgoingIndex - this.PARCEL_SIZE_SIZE;
|
2013-09-04 14:58:23 +04:00
|
|
|
this.writeParcelSize(parcelSize);
|
|
|
|
|
|
|
|
// This assumes that postRILMessage will make a copy of the ArrayBufferView
|
|
|
|
// right away!
|
2013-09-06 13:03:51 +04:00
|
|
|
let parcel = this.outgoingBytes.subarray(0, this.outgoingIndex);
|
2013-09-04 14:58:23 +04:00
|
|
|
if (DEBUG) debug("Outgoing parcel: " + Array.slice(parcel));
|
|
|
|
this.onSendParcel(parcel);
|
2013-09-06 13:03:51 +04:00
|
|
|
this.outgoingIndex = this.PARCEL_SIZE_SIZE;
|
2013-09-04 14:58:23 +04:00
|
|
|
},
|
|
|
|
|
2014-01-13 06:44:40 +04:00
|
|
|
getCurrentParcelSize: function() {
|
2013-09-06 13:03:51 +04:00
|
|
|
return this.currentParcelSize;
|
2013-09-04 14:58:23 +04:00
|
|
|
},
|
|
|
|
|
2014-01-13 06:44:40 +04:00
|
|
|
getReadAvailable: function() {
|
2013-09-06 13:03:51 +04:00
|
|
|
return this.readAvailable;
|
2013-08-22 08:17:40 +04:00
|
|
|
}
|
|
|
|
|
2013-09-04 14:58:26 +04:00
|
|
|
/**
|
|
|
|
* Process one parcel.
|
|
|
|
*
|
|
|
|
* |processParcel| is an implementation provided incoming parcel processing
|
|
|
|
* function invoked when we have received a complete parcel. Implementation
|
|
|
|
* may call multiple read functions to extract data from the incoming buffer.
|
|
|
|
*/
|
2014-01-13 06:44:40 +04:00
|
|
|
//processParcel: function() {
|
2013-09-26 10:53:50 +04:00
|
|
|
// let something = this.readInt32();
|
2013-09-04 14:58:26 +04:00
|
|
|
// ...
|
|
|
|
//},
|
|
|
|
|
2013-09-04 14:58:23 +04:00
|
|
|
/**
|
|
|
|
* Write raw data out to underlying channel.
|
|
|
|
*
|
|
|
|
* |onSendParcel| is an implementation provided stream output function
|
|
|
|
* invoked when we're really going to write something out. We assume the
|
|
|
|
* data are completely copied to some output buffer in this call and may
|
|
|
|
* be destroyed when it's done.
|
|
|
|
*
|
|
|
|
* @param parcel
|
|
|
|
* An array of numeric octet data.
|
|
|
|
*/
|
2014-01-13 06:44:40 +04:00
|
|
|
//onSendParcel: function(parcel) {
|
2013-09-04 14:58:23 +04:00
|
|
|
// ...
|
|
|
|
//}
|
2013-08-22 08:17:40 +04:00
|
|
|
};
|
2013-09-04 14:58:23 +04:00
|
|
|
|
|
|
|
module.exports = { Buf: Buf };
|