2 * Websock: high-performance buffering wrapper
3 * Copyright (C) 2019 The noVNC Authors
4 * Licensed under MPL 2.0 (see LICENSE.txt)
6 * Websock is similar to the standard WebSocket / RTCDataChannel object
7 * but with extra buffer handling.
9 * Websock has built-in receive queue buffering; the message event
10 * does not contain actual data but is simply a notification that
11 * there is new data available. Several rQ* methods are available to
12 * read binary data off of the receive queue.
15import * as Log from './util/logging.js';
17// this has performance issues in some versions Chromium, and
18// doesn't gain a tremendous amount of performance increase in Firefox
19// at the moment. It may be valuable to turn it on in the future.
20const MAX_RQ_GROW_SIZE = 40 * 1024 * 1024; // 40 MiB
22// Constants pulled from RTCDataChannelState enum
23// https://developer.mozilla.org/en-US/docs/Web/API/RTCDataChannel/readyState#RTCDataChannelState_enum
25 CONNECTING: "connecting",
32 CONNECTING: [WebSocket.CONNECTING, DataChannel.CONNECTING],
33 OPEN: [WebSocket.OPEN, DataChannel.OPEN],
34 CLOSING: [WebSocket.CLOSING, DataChannel.CLOSING],
35 CLOSED: [WebSocket.CLOSED, DataChannel.CLOSED],
38// Properties a raw channel must have, WebSocket and RTCDataChannel are two examples
39const rawChannelProps = [
50export default class Websock {
52 this._websocket = null; // WebSocket or RTCDataChannel object
54 this._rQi = 0; // Receive queue index
55 this._rQlen = 0; // Next write position in the receive queue
56 this._rQbufferSize = 1024 * 1024 * 4; // Receive queue buffer size (4 MiB)
57 // called in init: this._rQ = new Uint8Array(this._rQbufferSize);
58 this._rQ = null; // Receive queue
60 this._sQbufferSize = 1024 * 10; // 10 KiB
61 // called in init: this._sQ = new Uint8Array(this._sQbufferSize);
63 this._sQ = null; // Send queue
65 this._eventHandlers = {
73 // Getters and Setters
78 if (this._websocket === null) {
82 subState = this._websocket.readyState;
84 if (ReadyStates.CONNECTING.includes(subState)) {
86 } else if (ReadyStates.OPEN.includes(subState)) {
88 } else if (ReadyStates.CLOSING.includes(subState)) {
90 } else if (ReadyStates.CLOSED.includes(subState)) {
99 return this._rQ[this._rQi];
107 return this._rQshift(1);
111 return this._rQshift(2);
115 return this._rQshift(4);
118 // TODO(directxman12): test performance with these vs a DataView
121 for (let byte = bytes - 1; byte >= 0; byte--) {
122 res += this._rQ[this._rQi++] << (byte * 8);
129 // Handle large arrays in steps to avoid long strings on the stack
130 for (let i = 0; i < len; i += 4096) {
131 let part = this.rQshiftBytes(Math.min(4096, len - i), false);
132 str += String.fromCharCode.apply(null, part);
137 rQshiftBytes(len, copy=true) {
140 return this._rQ.slice(this._rQi - len, this._rQi);
142 return this._rQ.subarray(this._rQi - len, this._rQi);
146 rQshiftTo(target, len) {
147 // TODO: make this just use set with views when using a ArrayBuffer to store the rQ
148 target.set(new Uint8Array(this._rQ.buffer, this._rQi, len));
152 rQpeekBytes(len, copy=true) {
154 return this._rQ.slice(this._rQi, this._rQi + len);
156 return this._rQ.subarray(this._rQi, this._rQi + len);
160 // Check to see if we must wait for 'num' bytes (default to FBU.bytes)
161 // to be available in the receive queue. Return true if we need to
162 // wait (and possibly print a debug message), otherwise false.
163 rQwait(msg, num, goback) {
164 if (this._rQlen - this._rQi < num) {
166 if (this._rQi < goback) {
167 throw new Error("rQwait cannot backup " + goback + " bytes");
171 return true; // true means need more data
179 this._sQensureSpace(1);
180 this._sQ[this._sQlen++] = num;
184 this._sQensureSpace(2);
185 this._sQ[this._sQlen++] = (num >> 8) & 0xff;
186 this._sQ[this._sQlen++] = (num >> 0) & 0xff;
190 this._sQensureSpace(4);
191 this._sQ[this._sQlen++] = (num >> 24) & 0xff;
192 this._sQ[this._sQlen++] = (num >> 16) & 0xff;
193 this._sQ[this._sQlen++] = (num >> 8) & 0xff;
194 this._sQ[this._sQlen++] = (num >> 0) & 0xff;
198 let bytes = str.split('').map(chr => chr.charCodeAt(0));
199 this.sQpushBytes(new Uint8Array(bytes));
203 for (let offset = 0;offset < bytes.length;) {
204 this._sQensureSpace(1);
206 let chunkSize = this._sQbufferSize - this._sQlen;
207 if (chunkSize > bytes.length - offset) {
208 chunkSize = bytes.length - offset;
211 this._sQ.set(bytes.subarray(offset, offset + chunkSize), this._sQlen);
212 this._sQlen += chunkSize;
218 if (this._sQlen > 0 && this.readyState === 'open') {
219 this._websocket.send(new Uint8Array(this._sQ.buffer, 0, this._sQlen));
224 _sQensureSpace(bytes) {
225 if (this._sQbufferSize - this._sQlen < bytes) {
232 this._eventHandlers[evt] = () => {};
236 this._eventHandlers[evt] = handler;
240 this._rQ = new Uint8Array(this._rQbufferSize);
241 this._sQ = new Uint8Array(this._sQbufferSize);
245 this._allocateBuffers();
247 this._websocket = null;
250 open(uri, protocols) {
251 this.attach(new WebSocket(uri, protocols));
257 // Must get object and class methods to be compatible with the tests.
258 const channelProps = [...Object.keys(rawChannel), ...Object.getOwnPropertyNames(Object.getPrototypeOf(rawChannel))];
259 for (let i = 0; i < rawChannelProps.length; i++) {
260 const prop = rawChannelProps[i];
261 if (channelProps.indexOf(prop) < 0) {
262 throw new Error('Raw channel missing property: ' + prop);
266 this._websocket = rawChannel;
267 this._websocket.binaryType = "arraybuffer";
268 this._websocket.onmessage = this._recvMessage.bind(this);
270 this._websocket.onopen = () => {
271 Log.Debug('>> WebSock.onopen');
272 if (this._websocket.protocol) {
273 Log.Info("Server choose sub-protocol: " + this._websocket.protocol);
276 this._eventHandlers.open();
277 Log.Debug("<< WebSock.onopen");
280 this._websocket.onclose = (e) => {
281 Log.Debug(">> WebSock.onclose");
282 this._eventHandlers.close(e);
283 Log.Debug("<< WebSock.onclose");
286 this._websocket.onerror = (e) => {
287 Log.Debug(">> WebSock.onerror: " + e);
288 this._eventHandlers.error(e);
289 Log.Debug("<< WebSock.onerror: " + e);
294 if (this._websocket) {
295 if (this.readyState === 'connecting' ||
296 this.readyState === 'open') {
297 Log.Info("Closing WebSocket connection");
298 this._websocket.close();
301 this._websocket.onmessage = () => {};
307 // We want to move all the unread data to the start of the queue,
309 // The function also expands the receive que if needed, and for
310 // performance reasons we combine these two actions to avoid
311 // unnecessary copying.
312 _expandCompactRQ(minFit) {
313 // if we're using less than 1/8th of the buffer even with the incoming bytes, compact in place
314 // instead of resizing
315 const requiredBufferSize = (this._rQlen - this._rQi + minFit) * 8;
316 const resizeNeeded = this._rQbufferSize < requiredBufferSize;
319 // Make sure we always *at least* double the buffer size, and have at least space for 8x
320 // the current amount of data
321 this._rQbufferSize = Math.max(this._rQbufferSize * 2, requiredBufferSize);
324 // we don't want to grow unboundedly
325 if (this._rQbufferSize > MAX_RQ_GROW_SIZE) {
326 this._rQbufferSize = MAX_RQ_GROW_SIZE;
327 if (this._rQbufferSize - (this._rQlen - this._rQi) < minFit) {
328 throw new Error("Receive Queue buffer exceeded " + MAX_RQ_GROW_SIZE + " bytes, and the new message could not fit");
333 const oldRQbuffer = this._rQ.buffer;
334 this._rQ = new Uint8Array(this._rQbufferSize);
335 this._rQ.set(new Uint8Array(oldRQbuffer, this._rQi, this._rQlen - this._rQi));
337 this._rQ.copyWithin(0, this._rQi, this._rQlen);
340 this._rQlen = this._rQlen - this._rQi;
344 // push arraybuffer values onto the end of the receive que
346 if (this._rQlen == this._rQi) {
347 // All data has now been processed, this means we
348 // can reset the receive queue.
352 const u8 = new Uint8Array(e.data);
353 if (u8.length > this._rQbufferSize - this._rQlen) {
354 this._expandCompactRQ(u8.length);
356 this._rQ.set(u8, this._rQlen);
357 this._rQlen += u8.length;
359 if (this._rQlen - this._rQi > 0) {
360 this._eventHandlers.message();
362 Log.Debug("Ignoring empty message");