• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * noVNC: HTML5 VNC client
3 * Copyright (C) 2012 Joel Martin
4 * Licensed under MPL 2.0 (see LICENSE.txt)
5 */
6
7"use strict";
8/*jslint browser: true, white: false */
9/*global Util, VNC_frame_data, finish */
10
11var rfb, mode, test_state, frame_idx, frame_length,
12    iteration, iterations, istart_time,
13
14    // Pre-declarations for jslint
15    send_array, next_iteration, queue_next_packet, do_packet;
16
17// Override send_array
18send_array = function (arr) {
19    // Stub out send_array
20};
21
22next_iteration = function () {
23    if (iteration === 0) {
24        frame_length = VNC_frame_data.length;
25        test_state = 'running';
26    } else {
27        rfb.disconnect();
28    }
29
30    if (test_state !== 'running') { return; }
31
32    iteration += 1;
33    if (iteration > iterations) {
34        finish();
35        return;
36    }
37
38    frame_idx = 0;
39    istart_time = (new Date()).getTime();
40    rfb.connect('test', 0, "bogus");
41
42    queue_next_packet();
43
44};
45
46queue_next_packet = function () {
47    var frame, foffset, toffset, delay;
48    if (test_state !== 'running') { return; }
49
50    frame = VNC_frame_data[frame_idx];
51    while ((frame_idx < frame_length) && (frame.charAt(0) === "}")) {
52        //Util.Debug("Send frame " + frame_idx);
53        frame_idx += 1;
54        frame = VNC_frame_data[frame_idx];
55    }
56
57    if (frame === 'EOF') {
58        Util.Debug("Finished, found EOF");
59        next_iteration();
60        return;
61    }
62    if (frame_idx >= frame_length) {
63        Util.Debug("Finished, no more frames");
64        next_iteration();
65        return;
66    }
67
68    if (mode === 'realtime') {
69        foffset = frame.slice(1, frame.indexOf('{', 1));
70        toffset = (new Date()).getTime() - istart_time;
71        delay = foffset - toffset;
72        if (delay < 1) {
73            delay = 1;
74        }
75
76        setTimeout(do_packet, delay);
77    } else {
78        setTimeout(do_packet, 1);
79    }
80};
81
82var bytes_processed = 0;
83
84do_packet = function () {
85    //Util.Debug("Processing frame: " + frame_idx);
86    var frame = VNC_frame_data[frame_idx],
87        start = frame.indexOf('{', 1) + 1;
88    bytes_processed += frame.length - start;
89    if (VNC_frame_encoding === 'binary') {
90        var u8 = new Uint8Array(frame.length - start);
91        for (var i = 0; i < frame.length - start; i++) {
92            u8[i] = frame.charCodeAt(start + i);
93        }
94        rfb.recv_message({'data' : u8});
95    } else {
96        rfb.recv_message({'data' : frame.slice(start)});
97    }
98    frame_idx += 1;
99
100    queue_next_packet();
101};
102
103