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