]> git.proxmox.com Git - mirror_novnc.git/blob - core/websock.js
Merge pull request #603 from WardF/master
[mirror_novnc.git] / core / websock.js
1 /*
2 * Websock: high-performance binary WebSockets
3 * Copyright (C) 2012 Joel Martin
4 * Licensed under MPL 2.0 (see LICENSE.txt)
5 *
6 * Websock is similar to the standard WebSocket object but Websock
7 * enables communication with raw TCP sockets (i.e. the binary stream)
8 * via websockify. This is accomplished by base64 encoding the data
9 * stream between Websock and websockify.
10 *
11 * Websock has built-in receive queue buffering; the message event
12 * does not contain actual data but is simply a notification that
13 * there is new data available. Several rQ* methods are available to
14 * read binary data off of the receive queue.
15 */
16
17 /* [module]
18 * import Util from "./util";
19 * import Base64 from "./base64";
20 */
21
22 /*jslint browser: true, bitwise: true */
23 /*global Util*/
24
25 /* [module] export default */ function Websock() {
26 "use strict";
27
28 this._websocket = null; // WebSocket object
29
30 this._rQi = 0; // Receive queue index
31 this._rQlen = 0; // Next write position in the receive queue
32 this._rQbufferSize = 1024 * 1024 * 4; // Receive queue buffer size (4 MiB)
33 this._rQmax = this._rQbufferSize / 8;
34 // called in init: this._rQ = new Uint8Array(this._rQbufferSize);
35 this._rQ = null; // Receive queue
36
37 this._sQbufferSize = 1024 * 10; // 10 KiB
38 // called in init: this._sQ = new Uint8Array(this._sQbufferSize);
39 this._sQlen = 0;
40 this._sQ = null; // Send queue
41
42 this._mode = 'binary'; // Current WebSocket mode: 'binary', 'base64'
43 this.maxBufferedAmount = 200;
44
45 this._eventHandlers = {
46 'message': function () {},
47 'open': function () {},
48 'close': function () {},
49 'error': function () {}
50 };
51 };
52
53 (function () {
54 "use strict";
55 // this has performance issues in some versions Chromium, and
56 // doesn't gain a tremendous amount of performance increase in Firefox
57 // at the moment. It may be valuable to turn it on in the future.
58 var ENABLE_COPYWITHIN = false;
59
60 var MAX_RQ_GROW_SIZE = 40 * 1024 * 1024; // 40 MiB
61
62 var typedArrayToString = (function () {
63 // This is only for PhantomJS, which doesn't like apply-ing
64 // with Typed Arrays
65 try {
66 var arr = new Uint8Array([1, 2, 3]);
67 String.fromCharCode.apply(null, arr);
68 return function (a) { return String.fromCharCode.apply(null, a); };
69 } catch (ex) {
70 return function (a) {
71 return String.fromCharCode.apply(
72 null, Array.prototype.slice.call(a));
73 };
74 }
75 })();
76
77 Websock.prototype = {
78 // Getters and Setters
79 get_sQ: function () {
80 return this._sQ;
81 },
82
83 get_rQ: function () {
84 return this._rQ;
85 },
86
87 get_rQi: function () {
88 return this._rQi;
89 },
90
91 set_rQi: function (val) {
92 this._rQi = val;
93 },
94
95 // Receive Queue
96 rQlen: function () {
97 return this._rQlen - this._rQi;
98 },
99
100 rQpeek8: function () {
101 return this._rQ[this._rQi];
102 },
103
104 rQshift8: function () {
105 return this._rQ[this._rQi++];
106 },
107
108 rQskip8: function () {
109 this._rQi++;
110 },
111
112 rQskipBytes: function (num) {
113 this._rQi += num;
114 },
115
116 // TODO(directxman12): test performance with these vs a DataView
117 rQshift16: function () {
118 return (this._rQ[this._rQi++] << 8) +
119 this._rQ[this._rQi++];
120 },
121
122 rQshift32: function () {
123 return (this._rQ[this._rQi++] << 24) +
124 (this._rQ[this._rQi++] << 16) +
125 (this._rQ[this._rQi++] << 8) +
126 this._rQ[this._rQi++];
127 },
128
129 rQshiftStr: function (len) {
130 if (typeof(len) === 'undefined') { len = this.rQlen(); }
131 var arr = new Uint8Array(this._rQ.buffer, this._rQi, len);
132 this._rQi += len;
133 return typedArrayToString(arr);
134 },
135
136 rQshiftBytes: function (len) {
137 if (typeof(len) === 'undefined') { len = this.rQlen(); }
138 this._rQi += len;
139 return new Uint8Array(this._rQ.buffer, this._rQi - len, len);
140 },
141
142 rQshiftTo: function (target, len) {
143 if (len === undefined) { len = this.rQlen(); }
144 // TODO: make this just use set with views when using a ArrayBuffer to store the rQ
145 target.set(new Uint8Array(this._rQ.buffer, this._rQi, len));
146 this._rQi += len;
147 },
148
149 rQwhole: function () {
150 return new Uint8Array(this._rQ.buffer, 0, this._rQlen);
151 },
152
153 rQslice: function (start, end) {
154 if (end) {
155 return new Uint8Array(this._rQ.buffer, this._rQi + start, end - start);
156 } else {
157 return new Uint8Array(this._rQ.buffer, this._rQi + start, this._rQlen - this._rQi - start);
158 }
159 },
160
161 // Check to see if we must wait for 'num' bytes (default to FBU.bytes)
162 // to be available in the receive queue. Return true if we need to
163 // wait (and possibly print a debug message), otherwise false.
164 rQwait: function (msg, num, goback) {
165 var rQlen = this._rQlen - this._rQi; // Skip rQlen() function call
166 if (rQlen < num) {
167 if (goback) {
168 if (this._rQi < goback) {
169 throw new Error("rQwait cannot backup " + goback + " bytes");
170 }
171 this._rQi -= goback;
172 }
173 return true; // true means need more data
174 }
175 return false;
176 },
177
178 // Send Queue
179
180 flush: function () {
181 if (this._websocket.bufferedAmount !== 0) {
182 Util.Debug("bufferedAmount: " + this._websocket.bufferedAmount);
183 }
184
185 if (this._websocket.bufferedAmount < this.maxBufferedAmount) {
186 if (this._sQlen > 0 && this._websocket.readyState === WebSocket.OPEN) {
187 this._websocket.send(this._encode_message());
188 this._sQlen = 0;
189 }
190
191 return true;
192 } else {
193 Util.Info("Delaying send, bufferedAmount: " +
194 this._websocket.bufferedAmount);
195 return false;
196 }
197 },
198
199 send: function (arr) {
200 this._sQ.set(arr, this._sQlen);
201 this._sQlen += arr.length;
202 return this.flush();
203 },
204
205 send_string: function (str) {
206 this.send(str.split('').map(function (chr) {
207 return chr.charCodeAt(0);
208 }));
209 },
210
211 // Event Handlers
212 off: function (evt) {
213 this._eventHandlers[evt] = function () {};
214 },
215
216 on: function (evt, handler) {
217 this._eventHandlers[evt] = handler;
218 },
219
220 _allocate_buffers: function () {
221 this._rQ = new Uint8Array(this._rQbufferSize);
222 this._sQ = new Uint8Array(this._sQbufferSize);
223 },
224
225 init: function (protocols, ws_schema) {
226 this._allocate_buffers();
227 this._rQi = 0;
228 this._websocket = null;
229
230 // Check for full typed array support
231 var bt = false;
232 if (('Uint8Array' in window) &&
233 ('set' in Uint8Array.prototype)) {
234 bt = true;
235 }
236
237 // Check for full binary type support in WebSockets
238 // Inspired by:
239 // https://github.com/Modernizr/Modernizr/issues/370
240 // https://github.com/Modernizr/Modernizr/blob/master/feature-detects/websockets/binary.js
241 var wsbt = false;
242 try {
243 if (bt && ('binaryType' in WebSocket.prototype ||
244 !!(new WebSocket(ws_schema + '://.').binaryType))) {
245 Util.Info("Detected binaryType support in WebSockets");
246 wsbt = true;
247 }
248 } catch (exc) {
249 // Just ignore failed test localhost connection
250 }
251
252 // Default protocols if not specified
253 if (typeof(protocols) === "undefined") {
254 protocols = 'binary';
255 }
256
257 if (Array.isArray(protocols) && protocols.indexOf('binary') > -1) {
258 protocols = 'binary';
259 }
260
261 if (!wsbt) {
262 throw new Error("noVNC no longer supports base64 WebSockets. " +
263 "Please use a browser which supports binary WebSockets.");
264 }
265
266 if (protocols != 'binary') {
267 throw new Error("noVNC no longer supports base64 WebSockets. Please " +
268 "use the binary subprotocol instead.");
269 }
270
271 return protocols;
272 },
273
274 open: function (uri, protocols) {
275 var ws_schema = uri.match(/^([a-z]+):\/\//)[1];
276 protocols = this.init(protocols, ws_schema);
277
278 this._websocket = new WebSocket(uri, protocols);
279
280 if (protocols.indexOf('binary') >= 0) {
281 this._websocket.binaryType = 'arraybuffer';
282 }
283
284 this._websocket.onmessage = this._recv_message.bind(this);
285 this._websocket.onopen = (function () {
286 Util.Debug('>> WebSock.onopen');
287 if (this._websocket.protocol) {
288 this._mode = this._websocket.protocol;
289 Util.Info("Server choose sub-protocol: " + this._websocket.protocol);
290 } else {
291 this._mode = 'binary';
292 Util.Error('Server select no sub-protocol!: ' + this._websocket.protocol);
293 }
294
295 if (this._mode != 'binary') {
296 throw new Error("noVNC no longer supports base64 WebSockets. Please " +
297 "use the binary subprotocol instead.");
298
299 }
300
301 this._eventHandlers.open();
302 Util.Debug("<< WebSock.onopen");
303 }).bind(this);
304 this._websocket.onclose = (function (e) {
305 Util.Debug(">> WebSock.onclose");
306 this._eventHandlers.close(e);
307 Util.Debug("<< WebSock.onclose");
308 }).bind(this);
309 this._websocket.onerror = (function (e) {
310 Util.Debug(">> WebSock.onerror: " + e);
311 this._eventHandlers.error(e);
312 Util.Debug("<< WebSock.onerror: " + e);
313 }).bind(this);
314 },
315
316 close: function () {
317 if (this._websocket) {
318 if ((this._websocket.readyState === WebSocket.OPEN) ||
319 (this._websocket.readyState === WebSocket.CONNECTING)) {
320 Util.Info("Closing WebSocket connection");
321 this._websocket.close();
322 }
323
324 this._websocket.onmessage = function (e) { return; };
325 }
326 },
327
328 // private methods
329 _encode_message: function () {
330 // Put in a binary arraybuffer
331 // according to the spec, you can send ArrayBufferViews with the send method
332 return new Uint8Array(this._sQ.buffer, 0, this._sQlen);
333 },
334
335 _expand_compact_rQ: function (min_fit) {
336 var resizeNeeded = min_fit || this._rQlen - this._rQi > this._rQbufferSize / 2;
337 if (resizeNeeded) {
338 if (!min_fit) {
339 // just double the size if we need to do compaction
340 this._rQbufferSize *= 2;
341 } else {
342 // otherwise, make sure we satisy rQlen - rQi + min_fit < rQbufferSize / 8
343 this._rQbufferSize = (this._rQlen - this._rQi + min_fit) * 8;
344 }
345 }
346
347 // we don't want to grow unboundedly
348 if (this._rQbufferSize > MAX_RQ_GROW_SIZE) {
349 this._rQbufferSize = MAX_RQ_GROW_SIZE;
350 if (this._rQbufferSize - this._rQlen - this._rQi < min_fit) {
351 throw new Exception("Receive Queue buffer exceeded " + MAX_RQ_GROW_SIZE + " bytes, and the new message could not fit");
352 }
353 }
354
355 if (resizeNeeded) {
356 var old_rQbuffer = this._rQ.buffer;
357 this._rQmax = this._rQbufferSize / 8;
358 this._rQ = new Uint8Array(this._rQbufferSize);
359 this._rQ.set(new Uint8Array(old_rQbuffer, this._rQi));
360 } else {
361 if (ENABLE_COPYWITHIN) {
362 this._rQ.copyWithin(0, this._rQi);
363 } else {
364 this._rQ.set(new Uint8Array(this._rQ.buffer, this._rQi));
365 }
366 }
367
368 this._rQlen = this._rQlen - this._rQi;
369 this._rQi = 0;
370 },
371
372 _decode_message: function (data) {
373 // push arraybuffer values onto the end
374 var u8 = new Uint8Array(data);
375 if (u8.length > this._rQbufferSize - this._rQlen) {
376 this._expand_compact_rQ(u8.length);
377 }
378 this._rQ.set(u8, this._rQlen);
379 this._rQlen += u8.length;
380 },
381
382 _recv_message: function (e) {
383 try {
384 this._decode_message(e.data);
385 if (this.rQlen() > 0) {
386 this._eventHandlers.message();
387 // Compact the receive queue
388 if (this._rQlen == this._rQi) {
389 this._rQlen = 0;
390 this._rQi = 0;
391 } else if (this._rQlen > this._rQmax) {
392 this._expand_compact_rQ();
393 }
394 } else {
395 Util.Debug("Ignoring empty message");
396 }
397 } catch (exc) {
398 var exception_str = "";
399 if (exc.name) {
400 exception_str += "\n name: " + exc.name + "\n";
401 exception_str += " message: " + exc.message + "\n";
402 }
403
404 if (typeof exc.description !== 'undefined') {
405 exception_str += " description: " + exc.description + "\n";
406 }
407
408 if (typeof exc.stack !== 'undefined') {
409 exception_str += exc.stack;
410 }
411
412 if (exception_str.length > 0) {
413 Util.Error("recv_message, caught exception: " + exception_str);
414 } else {
415 Util.Error("recv_message, caught exception: " + exc);
416 }
417
418 if (typeof exc.name !== 'undefined') {
419 this._eventHandlers.error(exc.name + ": " + exc.message);
420 } else {
421 this._eventHandlers.error(exc);
422 }
423 }
424 }
425 };
426 })();