/*
* Websock: high-performance binary WebSockets
- * Copyright (C) 2018 The noVNC Authors
+ * Copyright (C) 2019 The noVNC Authors
* Licensed under MPL 2.0 (see LICENSE.txt)
*
* Websock is similar to the standard WebSocket object but with extra
// this has performance issues in some versions Chromium, and
// doesn't gain a tremendous amount of performance increase in Firefox
// at the moment. It may be valuable to turn it on in the future.
+// Also copyWithin() for TypedArrays is not supported in IE 11 or
+// Safari 13 (at the moment we want to support Safari 11).
const ENABLE_COPYWITHIN = false;
const MAX_RQ_GROW_SIZE = 40 * 1024 * 1024; // 40 MiB
return new Uint8Array(this._sQ.buffer, 0, this._sQlen);
}
+ // We want to move all the unread data to the start of the queue,
+ // e.g. compacting.
+ // The function also expands the receive que if needed, and for
+ // performance reasons we combine these two actions to avoid
+ // unneccessary copying.
_expand_compact_rQ(min_fit) {
// if we're using less than 1/8th of the buffer even with the incoming bytes, compact in place
// instead of resizing
if (resizeNeeded) {
const old_rQbuffer = this._rQ.buffer;
this._rQ = new Uint8Array(this._rQbufferSize);
- this._rQ.set(new Uint8Array(old_rQbuffer, this._rQi));
+ this._rQ.set(new Uint8Array(old_rQbuffer, this._rQi, this._rQlen - this._rQi));
} else {
if (ENABLE_COPYWITHIN) {
- this._rQ.copyWithin(0, this._rQi);
+ this._rQ.copyWithin(0, this._rQi, this._rQlen);
} else {
- this._rQ.set(new Uint8Array(this._rQ.buffer, this._rQi));
+ this._rQ.set(new Uint8Array(this._rQ.buffer, this._rQi, this._rQlen - this._rQi));
}
}
this._rQi = 0;
}
+ // push arraybuffer values onto the end of the receive que
_decode_message(data) {
- // push arraybuffer values onto the end
const u8 = new Uint8Array(data);
if (u8.length > this._rQbufferSize - this._rQlen) {
this._expand_compact_rQ(u8.length);
this._decode_message(e.data);
if (this.rQlen > 0) {
this._eventHandlers.message();
- // Compact the receive queue
if (this._rQlen == this._rQi) {
+ // All data has now been processed, this means we
+ // can reset the receive queue.
this._rQlen = 0;
this._rQi = 0;
}