1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2024-12-29 17:45:21 +00:00
webvirtcloud/static/js/novnc/core/websock.js

295 lines
8.9 KiB
JavaScript
Raw Normal View History

/*
* Websock: high-performance binary WebSockets
2020-08-11 12:05:09 +00:00
* 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
* buffer handling.
*
* Websock has built-in receive queue buffering; the message event
* does not contain actual data but is simply a notification that
* there is new data available. Several rQ* methods are available to
* read binary data off of the receive queue.
*/
import * as Log from './util/logging.js';
// 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.
2020-08-11 12:05:09 +00:00
// Also copyWithin() for TypedArrays is not supported in IE 11 or
// Safari 13 (at the moment we want to support Safari 11).
2019-06-24 11:26:25 +00:00
const ENABLE_COPYWITHIN = false;
const MAX_RQ_GROW_SIZE = 40 * 1024 * 1024; // 40 MiB
export default class Websock {
constructor() {
this._websocket = null; // WebSocket object
this._rQi = 0; // Receive queue index
this._rQlen = 0; // Next write position in the receive queue
this._rQbufferSize = 1024 * 1024 * 4; // Receive queue buffer size (4 MiB)
// called in init: this._rQ = new Uint8Array(this._rQbufferSize);
this._rQ = null; // Receive queue
this._sQbufferSize = 1024 * 10; // 10 KiB
// called in init: this._sQ = new Uint8Array(this._sQbufferSize);
this._sQlen = 0;
this._sQ = null; // Send queue
this._eventHandlers = {
message: () => {},
open: () => {},
close: () => {},
error: () => {}
};
}
// Getters and Setters
2019-06-24 11:26:25 +00:00
get sQ() {
return this._sQ;
2019-06-24 11:26:25 +00:00
}
2019-06-24 11:26:25 +00:00
get rQ() {
return this._rQ;
2019-06-24 11:26:25 +00:00
}
2019-06-24 11:26:25 +00:00
get rQi() {
return this._rQi;
2019-06-24 11:26:25 +00:00
}
2019-06-24 11:26:25 +00:00
set rQi(val) {
this._rQi = val;
2019-06-24 11:26:25 +00:00
}
// Receive Queue
2019-06-24 11:26:25 +00:00
get rQlen() {
return this._rQlen - this._rQi;
2019-06-24 11:26:25 +00:00
}
2019-06-24 11:26:25 +00:00
rQpeek8() {
return this._rQ[this._rQi];
2019-06-24 11:26:25 +00:00
}
2019-06-24 11:26:25 +00:00
rQskipBytes(bytes) {
this._rQi += bytes;
}
rQshift8() {
return this._rQshift(1);
}
2019-06-24 11:26:25 +00:00
rQshift16() {
return this._rQshift(2);
}
2019-06-24 11:26:25 +00:00
rQshift32() {
return this._rQshift(4);
}
// TODO(directxman12): test performance with these vs a DataView
2019-06-24 11:26:25 +00:00
_rQshift(bytes) {
let res = 0;
for (let byte = bytes - 1; byte >= 0; byte--) {
res += this._rQ[this._rQi++] << (byte * 8);
}
return res;
}
rQshiftStr(len) {
if (typeof(len) === 'undefined') { len = this.rQlen; }
let str = "";
// Handle large arrays in steps to avoid long strings on the stack
for (let i = 0; i < len; i += 4096) {
let part = this.rQshiftBytes(Math.min(4096, len - i));
str += String.fromCharCode.apply(null, part);
}
return str;
}
2019-06-24 11:26:25 +00:00
rQshiftBytes(len) {
if (typeof(len) === 'undefined') { len = this.rQlen; }
this._rQi += len;
return new Uint8Array(this._rQ.buffer, this._rQi - len, len);
2019-06-24 11:26:25 +00:00
}
2019-06-24 11:26:25 +00:00
rQshiftTo(target, len) {
if (len === undefined) { len = this.rQlen; }
// TODO: make this just use set with views when using a ArrayBuffer to store the rQ
target.set(new Uint8Array(this._rQ.buffer, this._rQi, len));
this._rQi += len;
2019-06-24 11:26:25 +00:00
}
2019-06-24 11:26:25 +00:00
rQslice(start, end = this.rQlen) {
return new Uint8Array(this._rQ.buffer, this._rQi + start, end - start);
}
// Check to see if we must wait for 'num' bytes (default to FBU.bytes)
// to be available in the receive queue. Return true if we need to
// wait (and possibly print a debug message), otherwise false.
2019-06-24 11:26:25 +00:00
rQwait(msg, num, goback) {
if (this.rQlen < num) {
if (goback) {
if (this._rQi < goback) {
throw new Error("rQwait cannot backup " + goback + " bytes");
}
this._rQi -= goback;
}
return true; // true means need more data
}
return false;
2019-06-24 11:26:25 +00:00
}
// Send Queue
2019-06-24 11:26:25 +00:00
flush() {
if (this._sQlen > 0 && this._websocket.readyState === WebSocket.OPEN) {
2020-08-11 12:05:09 +00:00
this._websocket.send(this._encodeMessage());
this._sQlen = 0;
}
2019-06-24 11:26:25 +00:00
}
2019-06-24 11:26:25 +00:00
send(arr) {
this._sQ.set(arr, this._sQlen);
this._sQlen += arr.length;
this.flush();
2019-06-24 11:26:25 +00:00
}
2020-08-11 12:05:09 +00:00
sendString(str) {
2019-06-24 11:26:25 +00:00
this.send(str.split('').map(chr => chr.charCodeAt(0)));
}
// Event Handlers
2019-06-24 11:26:25 +00:00
off(evt) {
this._eventHandlers[evt] = () => {};
}
2019-06-24 11:26:25 +00:00
on(evt, handler) {
this._eventHandlers[evt] = handler;
2019-06-24 11:26:25 +00:00
}
2020-08-11 12:05:09 +00:00
_allocateBuffers() {
this._rQ = new Uint8Array(this._rQbufferSize);
this._sQ = new Uint8Array(this._sQbufferSize);
2019-06-24 11:26:25 +00:00
}
2019-06-24 11:26:25 +00:00
init() {
2020-08-11 12:05:09 +00:00
this._allocateBuffers();
this._rQi = 0;
this._websocket = null;
2019-06-24 11:26:25 +00:00
}
2019-06-24 11:26:25 +00:00
open(uri, protocols) {
this.init();
this._websocket = new WebSocket(uri, protocols);
this._websocket.binaryType = 'arraybuffer';
2020-08-11 12:05:09 +00:00
this._websocket.onmessage = this._recvMessage.bind(this);
2019-06-24 11:26:25 +00:00
this._websocket.onopen = () => {
Log.Debug('>> WebSock.onopen');
if (this._websocket.protocol) {
Log.Info("Server choose sub-protocol: " + this._websocket.protocol);
}
this._eventHandlers.open();
Log.Debug("<< WebSock.onopen");
2019-06-24 11:26:25 +00:00
};
this._websocket.onclose = (e) => {
Log.Debug(">> WebSock.onclose");
this._eventHandlers.close(e);
Log.Debug("<< WebSock.onclose");
2019-06-24 11:26:25 +00:00
};
this._websocket.onerror = (e) => {
Log.Debug(">> WebSock.onerror: " + e);
this._eventHandlers.error(e);
Log.Debug("<< WebSock.onerror: " + e);
2019-06-24 11:26:25 +00:00
};
}
2019-06-24 11:26:25 +00:00
close() {
if (this._websocket) {
if ((this._websocket.readyState === WebSocket.OPEN) ||
(this._websocket.readyState === WebSocket.CONNECTING)) {
Log.Info("Closing WebSocket connection");
this._websocket.close();
}
2019-06-24 11:26:25 +00:00
this._websocket.onmessage = () => {};
}
2019-06-24 11:26:25 +00:00
}
// private methods
2020-08-11 12:05:09 +00:00
_encodeMessage() {
// Put in a binary arraybuffer
// according to the spec, you can send ArrayBufferViews with the send method
return new Uint8Array(this._sQ.buffer, 0, this._sQlen);
2019-06-24 11:26:25 +00:00
}
2020-08-11 12:05:09 +00:00
// 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.
_expandCompactRQ(minFit) {
// if we're using less than 1/8th of the buffer even with the incoming bytes, compact in place
// instead of resizing
const requiredBufferSize = (this._rQlen - this._rQi + minFit) * 8;
const resizeNeeded = this._rQbufferSize < requiredBufferSize;
if (resizeNeeded) {
2020-08-11 12:05:09 +00:00
// Make sure we always *at least* double the buffer size, and have at least space for 8x
// the current amount of data
this._rQbufferSize = Math.max(this._rQbufferSize * 2, requiredBufferSize);
}
// we don't want to grow unboundedly
if (this._rQbufferSize > MAX_RQ_GROW_SIZE) {
this._rQbufferSize = MAX_RQ_GROW_SIZE;
2020-08-11 12:05:09 +00:00
if (this._rQbufferSize - this.rQlen < minFit) {
2019-06-24 11:26:25 +00:00
throw new Error("Receive Queue buffer exceeded " + MAX_RQ_GROW_SIZE + " bytes, and the new message could not fit");
}
}
if (resizeNeeded) {
2020-08-11 12:05:09 +00:00
const oldRQbuffer = this._rQ.buffer;
this._rQ = new Uint8Array(this._rQbufferSize);
2020-08-11 12:05:09 +00:00
this._rQ.set(new Uint8Array(oldRQbuffer, this._rQi, this._rQlen - this._rQi));
} else {
if (ENABLE_COPYWITHIN) {
2020-08-11 12:05:09 +00:00
this._rQ.copyWithin(0, this._rQi, this._rQlen);
} else {
2020-08-11 12:05:09 +00:00
this._rQ.set(new Uint8Array(this._rQ.buffer, this._rQi, this._rQlen - this._rQi));
}
}
this._rQlen = this._rQlen - this._rQi;
this._rQi = 0;
2019-06-24 11:26:25 +00:00
}
2020-08-11 12:05:09 +00:00
// push arraybuffer values onto the end of the receive que
_DecodeMessage(data) {
2019-06-24 11:26:25 +00:00
const u8 = new Uint8Array(data);
if (u8.length > this._rQbufferSize - this._rQlen) {
2020-08-11 12:05:09 +00:00
this._expandCompactRQ(u8.length);
}
this._rQ.set(u8, this._rQlen);
this._rQlen += u8.length;
2019-06-24 11:26:25 +00:00
}
2020-08-11 12:05:09 +00:00
_recvMessage(e) {
this._DecodeMessage(e.data);
2019-06-24 11:26:25 +00:00
if (this.rQlen > 0) {
this._eventHandlers.message();
if (this._rQlen == this._rQi) {
2020-08-11 12:05:09 +00:00
// All data has now been processed, this means we
// can reset the receive queue.
this._rQlen = 0;
this._rQi = 0;
}
} else {
Log.Debug("Ignoring empty message");
}
}
2019-06-24 11:26:25 +00:00
}