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