]>
Commit | Line | Data |
---|---|---|
72f1348b JM |
1 | /* |
2 | * Websock: high-performance binary WebSockets | |
84586c0f | 3 | * Copyright (C) 2018 The noVNC Authors |
1d728ace | 4 | * Licensed under MPL 2.0 (see LICENSE.txt) |
72f1348b | 5 | * |
d2467189 PO |
6 | * Websock is similar to the standard WebSocket object but with extra |
7 | * buffer handling. | |
72f1348b JM |
8 | * |
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. | |
13 | */ | |
14 | ||
6d6f0db0 | 15 | import * as Log from './util/logging.js'; |
ae510306 | 16 | |
6d6f0db0 SR |
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. | |
2b5f94fa | 20 | const ENABLE_COPYWITHIN = false; |
2b5f94fa | 21 | const MAX_RQ_GROW_SIZE = 40 * 1024 * 1024; // 40 MiB |
6d6f0db0 | 22 | |
0e4808bf JD |
23 | export default class Websock { |
24 | constructor() { | |
25 | this._websocket = null; // WebSocket object | |
26 | ||
27 | this._rQi = 0; // Receive queue index | |
28 | this._rQlen = 0; // Next write position in the receive queue | |
29 | this._rQbufferSize = 1024 * 1024 * 4; // Receive queue buffer size (4 MiB) | |
0e4808bf JD |
30 | // called in init: this._rQ = new Uint8Array(this._rQbufferSize); |
31 | this._rQ = null; // Receive queue | |
32 | ||
33 | this._sQbufferSize = 1024 * 10; // 10 KiB | |
34 | // called in init: this._sQ = new Uint8Array(this._sQbufferSize); | |
35 | this._sQlen = 0; | |
36 | this._sQ = null; // Send queue | |
37 | ||
38 | this._eventHandlers = { | |
39 | message: () => {}, | |
40 | open: () => {}, | |
41 | close: () => {}, | |
42 | error: () => {} | |
43 | }; | |
44 | } | |
45 | ||
6d6f0db0 | 46 | // Getters and Setters |
8a189a62 | 47 | get sQ() { |
6d6f0db0 | 48 | return this._sQ; |
0e4808bf | 49 | } |
6d6f0db0 | 50 | |
8a189a62 | 51 | get rQ() { |
6d6f0db0 | 52 | return this._rQ; |
0e4808bf | 53 | } |
6d6f0db0 | 54 | |
8a189a62 | 55 | get rQi() { |
6d6f0db0 | 56 | return this._rQi; |
0e4808bf | 57 | } |
6d6f0db0 | 58 | |
8a189a62 | 59 | set rQi(val) { |
6d6f0db0 | 60 | this._rQi = val; |
0e4808bf | 61 | } |
6d6f0db0 SR |
62 | |
63 | // Receive Queue | |
8a189a62 | 64 | get rQlen() { |
6d6f0db0 | 65 | return this._rQlen - this._rQi; |
0e4808bf | 66 | } |
6d6f0db0 | 67 | |
0e4808bf | 68 | rQpeek8() { |
6d6f0db0 | 69 | return this._rQ[this._rQi]; |
0e4808bf | 70 | } |
6d6f0db0 | 71 | |
8a189a62 JD |
72 | rQskipBytes(bytes) { |
73 | this._rQi += bytes; | |
74 | } | |
75 | ||
0e4808bf | 76 | rQshift8() { |
879e33ab | 77 | return this._rQshift(1); |
0e4808bf | 78 | } |
6d6f0db0 | 79 | |
0e4808bf | 80 | rQshift16() { |
879e33ab | 81 | return this._rQshift(2); |
0e4808bf | 82 | } |
6d6f0db0 | 83 | |
0e4808bf | 84 | rQshift32() { |
879e33ab JD |
85 | return this._rQshift(4); |
86 | } | |
87 | ||
88 | // TODO(directxman12): test performance with these vs a DataView | |
89 | _rQshift(bytes) { | |
90 | let res = 0; | |
91 | for (let byte = bytes - 1; byte >= 0; byte--) { | |
92 | res += this._rQ[this._rQi++] << (byte * 8); | |
93 | } | |
94 | return res; | |
0e4808bf | 95 | } |
6d6f0db0 | 96 | |
0e4808bf | 97 | rQshiftStr(len) { |
8a189a62 | 98 | if (typeof(len) === 'undefined') { len = this.rQlen; } |
db9daa98 SM |
99 | let str = ""; |
100 | // Handle large arrays in steps to avoid long strings on the stack | |
101 | for (let i = 0; i < len; i += 4096) { | |
f90c2a6d | 102 | let part = this.rQshiftBytes(Math.min(4096, len - i)); |
d9814c06 | 103 | str += String.fromCharCode.apply(null, part); |
db9daa98 SM |
104 | } |
105 | return str; | |
0e4808bf | 106 | } |
6d6f0db0 | 107 | |
0e4808bf | 108 | rQshiftBytes(len) { |
8a189a62 | 109 | if (typeof(len) === 'undefined') { len = this.rQlen; } |
6d6f0db0 SR |
110 | this._rQi += len; |
111 | return new Uint8Array(this._rQ.buffer, this._rQi - len, len); | |
0e4808bf | 112 | } |
6d6f0db0 | 113 | |
0e4808bf | 114 | rQshiftTo(target, len) { |
8a189a62 | 115 | if (len === undefined) { len = this.rQlen; } |
6d6f0db0 SR |
116 | // TODO: make this just use set with views when using a ArrayBuffer to store the rQ |
117 | target.set(new Uint8Array(this._rQ.buffer, this._rQi, len)); | |
118 | this._rQi += len; | |
0e4808bf | 119 | } |
6d6f0db0 | 120 | |
8a189a62 JD |
121 | rQslice(start, end = this.rQlen) { |
122 | return new Uint8Array(this._rQ.buffer, this._rQi + start, end - start); | |
0e4808bf | 123 | } |
6d6f0db0 SR |
124 | |
125 | // Check to see if we must wait for 'num' bytes (default to FBU.bytes) | |
126 | // to be available in the receive queue. Return true if we need to | |
127 | // wait (and possibly print a debug message), otherwise false. | |
0e4808bf | 128 | rQwait(msg, num, goback) { |
8a189a62 | 129 | if (this.rQlen < num) { |
6d6f0db0 SR |
130 | if (goback) { |
131 | if (this._rQi < goback) { | |
132 | throw new Error("rQwait cannot backup " + goback + " bytes"); | |
2cccf753 | 133 | } |
6d6f0db0 | 134 | this._rQi -= goback; |
2cccf753 | 135 | } |
6d6f0db0 SR |
136 | return true; // true means need more data |
137 | } | |
138 | return false; | |
0e4808bf | 139 | } |
72f1348b | 140 | |
6d6f0db0 | 141 | // Send Queue |
72f1348b | 142 | |
0e4808bf | 143 | flush() { |
6d6f0db0 SR |
144 | if (this._sQlen > 0 && this._websocket.readyState === WebSocket.OPEN) { |
145 | this._websocket.send(this._encode_message()); | |
146 | this._sQlen = 0; | |
147 | } | |
0e4808bf | 148 | } |
6d6f0db0 | 149 | |
0e4808bf | 150 | send(arr) { |
6d6f0db0 SR |
151 | this._sQ.set(arr, this._sQlen); |
152 | this._sQlen += arr.length; | |
153 | this.flush(); | |
0e4808bf | 154 | } |
6d6f0db0 | 155 | |
0e4808bf | 156 | send_string(str) { |
651c23ec | 157 | this.send(str.split('').map(chr => chr.charCodeAt(0))); |
0e4808bf | 158 | } |
6d6f0db0 SR |
159 | |
160 | // Event Handlers | |
0e4808bf | 161 | off(evt) { |
651c23ec | 162 | this._eventHandlers[evt] = () => {}; |
0e4808bf | 163 | } |
6d6f0db0 | 164 | |
0e4808bf | 165 | on(evt, handler) { |
6d6f0db0 | 166 | this._eventHandlers[evt] = handler; |
0e4808bf | 167 | } |
6d6f0db0 | 168 | |
0e4808bf | 169 | _allocate_buffers() { |
6d6f0db0 SR |
170 | this._rQ = new Uint8Array(this._rQbufferSize); |
171 | this._sQ = new Uint8Array(this._sQbufferSize); | |
0e4808bf | 172 | } |
6d6f0db0 | 173 | |
0e4808bf | 174 | init() { |
6d6f0db0 SR |
175 | this._allocate_buffers(); |
176 | this._rQi = 0; | |
177 | this._websocket = null; | |
0e4808bf | 178 | } |
6d6f0db0 | 179 | |
0e4808bf | 180 | open(uri, protocols) { |
6d6f0db0 SR |
181 | this.init(); |
182 | ||
183 | this._websocket = new WebSocket(uri, protocols); | |
184 | this._websocket.binaryType = 'arraybuffer'; | |
185 | ||
186 | this._websocket.onmessage = this._recv_message.bind(this); | |
651c23ec | 187 | this._websocket.onopen = () => { |
6d6f0db0 SR |
188 | Log.Debug('>> WebSock.onopen'); |
189 | if (this._websocket.protocol) { | |
190 | Log.Info("Server choose sub-protocol: " + this._websocket.protocol); | |
2cccf753 | 191 | } |
2cccf753 | 192 | |
6d6f0db0 SR |
193 | this._eventHandlers.open(); |
194 | Log.Debug("<< WebSock.onopen"); | |
651c23ec JD |
195 | }; |
196 | this._websocket.onclose = (e) => { | |
6d6f0db0 SR |
197 | Log.Debug(">> WebSock.onclose"); |
198 | this._eventHandlers.close(e); | |
199 | Log.Debug("<< WebSock.onclose"); | |
651c23ec JD |
200 | }; |
201 | this._websocket.onerror = (e) => { | |
6d6f0db0 SR |
202 | Log.Debug(">> WebSock.onerror: " + e); |
203 | this._eventHandlers.error(e); | |
204 | Log.Debug("<< WebSock.onerror: " + e); | |
651c23ec | 205 | }; |
0e4808bf | 206 | } |
6d6f0db0 | 207 | |
0e4808bf | 208 | close() { |
6d6f0db0 SR |
209 | if (this._websocket) { |
210 | if ((this._websocket.readyState === WebSocket.OPEN) || | |
211 | (this._websocket.readyState === WebSocket.CONNECTING)) { | |
212 | Log.Info("Closing WebSocket connection"); | |
213 | this._websocket.close(); | |
fcff386b | 214 | } |
6d6f0db0 | 215 | |
651c23ec | 216 | this._websocket.onmessage = () => {}; |
6d6f0db0 | 217 | } |
0e4808bf | 218 | } |
6d6f0db0 SR |
219 | |
220 | // private methods | |
0e4808bf | 221 | _encode_message() { |
6d6f0db0 SR |
222 | // Put in a binary arraybuffer |
223 | // according to the spec, you can send ArrayBufferViews with the send method | |
224 | return new Uint8Array(this._sQ.buffer, 0, this._sQlen); | |
0e4808bf | 225 | } |
6d6f0db0 | 226 | |
0e4808bf | 227 | _expand_compact_rQ(min_fit) { |
7d755d10 JAD |
228 | // if we're using less than 1/8th of the buffer even with the incoming bytes, compact in place |
229 | // instead of resizing | |
230 | const required_buffer_size = (this._rQlen - this._rQi + min_fit) * 8; | |
231 | const resizeNeeded = this._rQbufferSize < required_buffer_size; | |
232 | ||
6d6f0db0 | 233 | if (resizeNeeded) { |
7d755d10 JAD |
234 | // Make sure we always *at least* double the buffer size, and have at least space for 8x |
235 | // the current amount of data | |
236 | this._rQbufferSize = Math.max(this._rQbufferSize * 2, required_buffer_size); | |
6d6f0db0 | 237 | } |
40037b6a | 238 | |
6d6f0db0 SR |
239 | // we don't want to grow unboundedly |
240 | if (this._rQbufferSize > MAX_RQ_GROW_SIZE) { | |
241 | this._rQbufferSize = MAX_RQ_GROW_SIZE; | |
8a189a62 | 242 | if (this._rQbufferSize - this.rQlen < min_fit) { |
8727f598 | 243 | throw new Error("Receive Queue buffer exceeded " + MAX_RQ_GROW_SIZE + " bytes, and the new message could not fit"); |
40037b6a | 244 | } |
6d6f0db0 | 245 | } |
40037b6a | 246 | |
6d6f0db0 | 247 | if (resizeNeeded) { |
2b5f94fa | 248 | const old_rQbuffer = this._rQ.buffer; |
6d6f0db0 | 249 | this._rQ = new Uint8Array(this._rQbufferSize); |
08567b08 | 250 | this._rQ.set(new Uint8Array(old_rQbuffer, this._rQi, this._rQlen - this._rQi)); |
6d6f0db0 SR |
251 | } else { |
252 | if (ENABLE_COPYWITHIN) { | |
08567b08 | 253 | this._rQ.copyWithin(0, this._rQi, this._rQlen); |
40037b6a | 254 | } else { |
08567b08 | 255 | this._rQ.set(new Uint8Array(this._rQ.buffer, this._rQi, this._rQlen - this._rQi)); |
40037b6a | 256 | } |
6d6f0db0 | 257 | } |
40037b6a | 258 | |
6d6f0db0 SR |
259 | this._rQlen = this._rQlen - this._rQi; |
260 | this._rQi = 0; | |
0e4808bf | 261 | } |
40037b6a | 262 | |
0e4808bf | 263 | _decode_message(data) { |
6d6f0db0 | 264 | // push arraybuffer values onto the end |
2b5f94fa | 265 | const u8 = new Uint8Array(data); |
6d6f0db0 SR |
266 | if (u8.length > this._rQbufferSize - this._rQlen) { |
267 | this._expand_compact_rQ(u8.length); | |
268 | } | |
269 | this._rQ.set(u8, this._rQlen); | |
270 | this._rQlen += u8.length; | |
0e4808bf | 271 | } |
fcff386b | 272 | |
0e4808bf | 273 | _recv_message(e) { |
1678bf86 | 274 | this._decode_message(e.data); |
8a189a62 | 275 | if (this.rQlen > 0) { |
1678bf86 PO |
276 | this._eventHandlers.message(); |
277 | // Compact the receive queue | |
278 | if (this._rQlen == this._rQi) { | |
279 | this._rQlen = 0; | |
280 | this._rQi = 0; | |
2cccf753 | 281 | } |
1678bf86 PO |
282 | } else { |
283 | Log.Debug("Ignoring empty message"); | |
72f1348b | 284 | } |
6d6f0db0 | 285 | } |
0e4808bf | 286 | } |