]>
Commit | Line | Data |
---|---|---|
d595e656 JM |
1 | /* |
2 | * noVNC: HTML5 VNC client | |
d58f8b51 | 3 | * Copyright (C) 2012 Joel Martin |
1d728ace | 4 | * Licensed under MPL 2.0 (see LICENSE.txt) |
d595e656 JM |
5 | */ |
6 | ||
7 | "use strict"; | |
8 | /*jslint browser: true, white: false */ | |
9 | /*global Util, VNC_frame_data, finish */ | |
10 | ||
b8bd88d0 | 11 | var rfb, mode, test_state, frame_idx, frame_length, |
d595e656 JM |
12 | iteration, iterations, istart_time, |
13 | ||
14 | // Pre-declarations for jslint | |
efed2eea | 15 | send_array, next_iteration, queue_next_packet, do_packet, enable_test_mode; |
b8bd88d0 JM |
16 | |
17 | // Override send_array | |
18 | send_array = function (arr) { | |
19 | // Stub out send_array | |
d595e656 | 20 | }; |
b8bd88d0 | 21 | |
efed2eea SR |
22 | enable_test_mode = function () { |
23 | rfb._sock._mode = VNC_frame_encoding; | |
24 | rfb._sock.send = send_array; | |
25 | rfb._sock.close = function () {}; | |
26 | rfb._sock.flush = function () {}; | |
27 | rfb._checkEvents = function () {}; | |
28 | rfb.connect = function (host, port, password, path) { | |
29 | this._rfb_host = host; | |
30 | this._rfb_port = port; | |
31 | this._rfb_password = (password !== undefined) ? password : ""; | |
32 | this._rfb_path = (path !== undefined) ? path : ""; | |
38781d93 | 33 | this._sock.init('binary', 'ws'); |
efed2eea SR |
34 | this._updateState('ProtocolVersion', "Starting VNC handshake"); |
35 | }; | |
36 | }; | |
37 | ||
d595e656 | 38 | next_iteration = function () { |
efed2eea SR |
39 | rfb = new RFB({'target': $D('VNC_canvas'), |
40 | 'onUpdateState': updateState}); | |
41 | enable_test_mode(); | |
42 | ||
b8bd88d0 JM |
43 | if (iteration === 0) { |
44 | frame_length = VNC_frame_data.length; | |
45 | test_state = 'running'; | |
b8bd88d0 | 46 | } |
38781d93 | 47 | |
b8bd88d0 JM |
48 | if (test_state !== 'running') { return; } |
49 | ||
d595e656 | 50 | iteration += 1; |
b8bd88d0 JM |
51 | if (iteration > iterations) { |
52 | finish(); | |
53 | return; | |
54 | } | |
55 | ||
56 | frame_idx = 0; | |
57 | istart_time = (new Date()).getTime(); | |
58 | rfb.connect('test', 0, "bogus"); | |
59 | ||
60 | queue_next_packet(); | |
61 | ||
d595e656 | 62 | }; |
b8bd88d0 | 63 | |
d595e656 JM |
64 | queue_next_packet = function () { |
65 | var frame, foffset, toffset, delay; | |
b8bd88d0 JM |
66 | if (test_state !== 'running') { return; } |
67 | ||
68 | frame = VNC_frame_data[frame_idx]; | |
69 | while ((frame_idx < frame_length) && (frame.charAt(0) === "}")) { | |
70 | //Util.Debug("Send frame " + frame_idx); | |
71 | frame_idx += 1; | |
72 | frame = VNC_frame_data[frame_idx]; | |
73 | } | |
74 | ||
75 | if (frame === 'EOF') { | |
76 | Util.Debug("Finished, found EOF"); | |
77 | next_iteration(); | |
78 | return; | |
79 | } | |
80 | if (frame_idx >= frame_length) { | |
81 | Util.Debug("Finished, no more frames"); | |
82 | next_iteration(); | |
83 | return; | |
84 | } | |
85 | ||
86 | if (mode === 'realtime') { | |
87 | foffset = frame.slice(1, frame.indexOf('{', 1)); | |
88 | toffset = (new Date()).getTime() - istart_time; | |
89 | delay = foffset - toffset; | |
90 | if (delay < 1) { | |
91 | delay = 1; | |
92 | } | |
93 | ||
94 | setTimeout(do_packet, delay); | |
95 | } else { | |
96 | setTimeout(do_packet, 1); | |
97 | } | |
d595e656 | 98 | }; |
b8bd88d0 | 99 | |
e7e66602 JM |
100 | var bytes_processed = 0; |
101 | ||
d595e656 | 102 | do_packet = function () { |
b8bd88d0 | 103 | //Util.Debug("Processing frame: " + frame_idx); |
e7e66602 JM |
104 | var frame = VNC_frame_data[frame_idx], |
105 | start = frame.indexOf('{', 1) + 1; | |
106 | bytes_processed += frame.length - start; | |
107 | if (VNC_frame_encoding === 'binary') { | |
108 | var u8 = new Uint8Array(frame.length - start); | |
109 | for (var i = 0; i < frame.length - start; i++) { | |
110 | u8[i] = frame.charCodeAt(start + i); | |
111 | } | |
efed2eea | 112 | rfb._sock._recv_message({'data' : u8}); |
e7e66602 | 113 | } else { |
efed2eea | 114 | rfb._sock._recv_message({'data' : frame.slice(start)}); |
e7e66602 | 115 | } |
b8bd88d0 JM |
116 | frame_idx += 1; |
117 | ||
118 | queue_next_packet(); | |
d595e656 | 119 | }; |
b8bd88d0 | 120 |