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