Commit ce2b6909 authored by Joel Martin's avatar Joel Martin

rfb.js: fail() wrapper for common failed pattern.

parent 8b17ac27
...@@ -79,7 +79,7 @@ var that = {}, // Public API interface ...@@ -79,7 +79,7 @@ var that = {}, // Public API interface
// Frame buffer update state // Frame buffer update state
FBU = { FBU = {
rects : 0, rects : 0,
subrects : 0, // RRE and HEXTILE subrects : 0, // RRE
lines : 0, // RAW lines : 0, // RAW
tiles : 0, // HEXTILE tiles : 0, // HEXTILE
bytes : 0, bytes : 0,
...@@ -289,7 +289,7 @@ function init_ws() { ...@@ -289,7 +289,7 @@ function init_ws() {
if (rfb_state === "connect") { if (rfb_state === "connect") {
updateState('ProtocolVersion', "Starting VNC handshake"); updateState('ProtocolVersion', "Starting VNC handshake");
} else { } else {
updateState('failed', "Got unexpected WebSockets connection"); fail("Got unexpected WebSockets connection");
} }
Util.Debug("<< WebSocket.onopen"); Util.Debug("<< WebSocket.onopen");
}; };
...@@ -298,17 +298,17 @@ function init_ws() { ...@@ -298,17 +298,17 @@ function init_ws() {
if (rfb_state === 'disconnect') { if (rfb_state === 'disconnect') {
updateState('disconnected', 'VNC disconnected'); updateState('disconnected', 'VNC disconnected');
} else if (rfb_state === 'ProtocolVersion') { } else if (rfb_state === 'ProtocolVersion') {
updateState('failed', 'Failed to connect to server'); fail('Failed to connect to server');
} else if (rfb_state in {'failed':1, 'disconnected':1}) { } else if (rfb_state in {'failed':1, 'disconnected':1}) {
Util.Error("Received onclose while disconnected"); Util.Error("Received onclose while disconnected");
} else { } else {
updateState('failed', 'Server disconnected'); fail('Server disconnected');
} }
Util.Debug("<< WebSocket.onclose"); Util.Debug("<< WebSocket.onclose");
}; };
ws.onerror = function(e) { ws.onerror = function(e) {
Util.Debug(">> WebSocket.onerror"); Util.Debug(">> WebSocket.onerror");
updateState('failed', "WebSocket error"); fail("WebSocket error");
Util.Debug("<< WebSocket.onerror"); Util.Debug("<< WebSocket.onerror");
}; };
...@@ -466,7 +466,7 @@ updateState = function(state, statusMsg) { ...@@ -466,7 +466,7 @@ updateState = function(state, statusMsg) {
case 'connect': case 'connect':
connTimer = setTimeout(function () { connTimer = setTimeout(function () {
updateState('failed', "Connect timeout"); fail("Connect timeout");
}, conf.connectTimeout * 1000); }, conf.connectTimeout * 1000);
init_vars(); init_vars();
...@@ -480,7 +480,7 @@ updateState = function(state, statusMsg) { ...@@ -480,7 +480,7 @@ updateState = function(state, statusMsg) {
if (! test_mode) { if (! test_mode) {
disconnTimer = setTimeout(function () { disconnTimer = setTimeout(function () {
updateState('failed', "Disconnect timeout"); fail("Disconnect timeout");
}, conf.disconnectTimeout * 1000); }, conf.disconnectTimeout * 1000);
} }
...@@ -519,6 +519,10 @@ updateState = function(state, statusMsg) { ...@@ -519,6 +519,10 @@ updateState = function(state, statusMsg) {
conf.updateState(that, state, oldstate, statusMsg); conf.updateState(that, state, oldstate, statusMsg);
} }
}; };
function fail(msg) {
updateState('failed', msg);
return false;
}
function encode_message(arr) { function encode_message(arr) {
/* base64 encode */ /* base64 encode */
...@@ -589,9 +593,9 @@ recv_message = function(e) { ...@@ -589,9 +593,9 @@ recv_message = function(e) {
Util.Warn("recv_message, caught exception:" + exc); Util.Warn("recv_message, caught exception:" + exc);
} }
if (typeof exc.name !== 'undefined') { if (typeof exc.name !== 'undefined') {
updateState('failed', exc.name + ": " + exc.message); fail(exc.name + ": " + exc.message);
} else { } else {
updateState('failed', exc); fail(exc);
} }
} }
//Util.Debug("<< recv_message"); //Util.Debug("<< recv_message");
...@@ -695,9 +699,7 @@ init_msg = function() { ...@@ -695,9 +699,7 @@ init_msg = function() {
case 'ProtocolVersion' : case 'ProtocolVersion' :
if (rQlen() < 12) { if (rQlen() < 12) {
updateState('failed', return fail("Incomplete protocol version");
"Disconnected: incomplete protocol version");
return;
} }
sversion = rQshiftStr(12).substr(4,7); sversion = rQshiftStr(12).substr(4,7);
Util.Info("Server ProtocolVersion: " + sversion); Util.Info("Server ProtocolVersion: " + sversion);
...@@ -707,9 +709,7 @@ init_msg = function() { ...@@ -707,9 +709,7 @@ init_msg = function() {
case "003.007": rfb_version = 3.7; break; case "003.007": rfb_version = 3.7; break;
case "003.008": rfb_version = 3.8; break; case "003.008": rfb_version = 3.8; break;
default: default:
updateState('failed', return fail("Invalid server version " + sversion);
"Invalid server version " + sversion);
return;
} }
if (rfb_version > rfb_max_version) { if (rfb_version > rfb_max_version) {
rfb_version = rfb_max_version; rfb_version = rfb_max_version;
...@@ -744,9 +744,7 @@ init_msg = function() { ...@@ -744,9 +744,7 @@ init_msg = function() {
if (num_types === 0) { if (num_types === 0) {
strlen = rQshift32(); strlen = rQshift32();
reason = rQshiftStr(strlen); reason = rQshiftStr(strlen);
updateState('failed', return fail("Security failure: " + reason);
"Disconnected: security failure: " + reason);
return;
} }
rfb_auth_scheme = 0; rfb_auth_scheme = 0;
types = rQshiftBytes(num_types); types = rQshiftBytes(num_types);
...@@ -757,9 +755,7 @@ init_msg = function() { ...@@ -757,9 +755,7 @@ init_msg = function() {
} }
} }
if (rfb_auth_scheme === 0) { if (rfb_auth_scheme === 0) {
updateState('failed', return fail("Unsupported security types: " + types);
"Disconnected: unsupported security types: " + types);
return;
} }
send_array([rfb_auth_scheme]); send_array([rfb_auth_scheme]);
...@@ -779,9 +775,7 @@ init_msg = function() { ...@@ -779,9 +775,7 @@ init_msg = function() {
if (rQwait("auth reason", 4)) { return false; } if (rQwait("auth reason", 4)) { return false; }
strlen = rQshift32(); strlen = rQshift32();
reason = rQshiftStr(strlen); reason = rQshiftStr(strlen);
updateState('failed', return fail("Auth failure: " + reason);
"Disconnected: auth failure: " + reason);
return;
case 1: // no authentication case 1: // no authentication
updateState('SecurityResult'); updateState('SecurityResult');
break; break;
...@@ -804,17 +798,14 @@ init_msg = function() { ...@@ -804,17 +798,14 @@ init_msg = function() {
updateState('SecurityResult'); updateState('SecurityResult');
break; break;
default: default:
updateState('failed', fail("Unsupported auth scheme: " + rfb_auth_scheme);
"Disconnected: unsupported auth scheme: " +
rfb_auth_scheme);
return; return;
} }
break; break;
case 'SecurityResult' : case 'SecurityResult' :
if (rQlen() < 4) { if (rQlen() < 4) {
updateState('failed', "Invalid VNC auth response"); return fail("Invalid VNC auth response");
return;
} }
switch (rQshift32()) { switch (rQshift32()) {
case 0: // OK case 0: // OK
...@@ -827,23 +818,20 @@ init_msg = function() { ...@@ -827,23 +818,20 @@ init_msg = function() {
return false; return false;
} }
reason = rQshiftStr(reason_len); reason = rQshiftStr(reason_len);
updateState('failed', reason); fail(reason);
} else { } else {
updateState('failed', "Authentication failed"); fail("Authentication failed");
} }
return; return;
case 2: // too-many case 2: // too-many
updateState('failed', return fail("Too many auth attempts");
"Disconnected: too many auth attempts");
return;
} }
send_array([rfb_shared]); // ClientInitialisation send_array([rfb_shared]); // ClientInitialisation
break; break;
case 'ServerInitialisation' : case 'ServerInitialisation' :
if (rQlen() < 24) { if (rQlen() < 24) {
updateState('failed', "Invalid server initialisation"); return fail("Invalid server initialisation");
return;
} }
/* Screen size */ /* Screen size */
...@@ -944,8 +932,7 @@ normal_msg = function() { ...@@ -944,8 +932,7 @@ normal_msg = function() {
conf.clipboardReceive(that, rQshiftStr(length)); conf.clipboardReceive(that, rQshiftStr(length));
break; break;
default: default:
updateState('failed', fail("Disconnected: illegal server message type " + msg_type);
"Disconnected: illegal server message type " + msg_type);
Util.Debug("rQ.slice(0,30):" + rQ.slice(0,30)); Util.Debug("rQ.slice(0,30):" + rQ.slice(0,30));
break; break;
} }
...@@ -954,7 +941,7 @@ normal_msg = function() { ...@@ -954,7 +941,7 @@ normal_msg = function() {
}; };
framebufferUpdate = function() { framebufferUpdate = function() {
var now, hdr, fbu_rt_diff, ret = true, ctx; var now, hdr, fbu_rt_diff, ret = true;
if (FBU.rects === 0) { if (FBU.rects === 0) {
//Util.Debug("New FBU: rQ.slice(0,20): " + rQ.slice(0,20)); //Util.Debug("New FBU: rQ.slice(0,20): " + rQ.slice(0,20));
...@@ -1006,9 +993,8 @@ framebufferUpdate = function() { ...@@ -1006,9 +993,8 @@ framebufferUpdate = function() {
Util.Debug(msg); Util.Debug(msg);
*/ */
} else { } else {
updateState('failed', fail("Disconnected: unsupported encoding " +
"Disconnected: unsupported encoding " + FBU.encoding);
FBU.encoding);
return false; return false;
} }
} }
...@@ -1052,10 +1038,10 @@ framebufferUpdate = function() { ...@@ -1052,10 +1038,10 @@ framebufferUpdate = function() {
} }
} }
if (! ret) { if (! ret) {
break; // false ret means need more data return ret; // false ret means need more data
} }
} }
return ret; return true; // We finished this FBU
}; };
// //
...@@ -1155,8 +1141,7 @@ encHandlers.HEXTILE = function display_hextile() { ...@@ -1155,8 +1141,7 @@ encHandlers.HEXTILE = function display_hextile() {
//Util.Debug(" 2 rQ length: " + rQlen() + " rQ[rQi]: " + rQ[rQi] + " rQ.slice(rQi,rQi+20): " + rQ.slice(rQi,rQi+20) + ", FBU.rects: " + FBU.rects + ", FBU.tiles: " + FBU.tiles); //Util.Debug(" 2 rQ length: " + rQlen() + " rQ[rQi]: " + rQ[rQi] + " rQ.slice(rQi,rQi+20): " + rQ.slice(rQi,rQi+20) + ", FBU.rects: " + FBU.rects + ", FBU.tiles: " + FBU.tiles);
subencoding = rQ[rQi]; // Peek subencoding = rQ[rQi]; // Peek
if (subencoding > 30) { // Raw if (subencoding > 30) { // Raw
updateState('failed', fail("Disconnected: illegal hextile subencoding " + subencoding);
"Disconnected: illegal hextile subencoding " + subencoding);
//Util.Debug("rQ.slice(0,30):" + rQ.slice(0,30)); //Util.Debug("rQ.slice(0,30):" + rQ.slice(0,30));
return false; return false;
} }
...@@ -1543,8 +1528,7 @@ that.connect = function(host, port, password) { ...@@ -1543,8 +1528,7 @@ that.connect = function(host, port, password) {
rfb_password = (password !== undefined) ? password : ""; rfb_password = (password !== undefined) ? password : "";
if ((!rfb_host) || (!rfb_port)) { if ((!rfb_host) || (!rfb_port)) {
updateState('failed', "Must set host and port"); return fail("Must set host and port");
return;
} }
updateState('connect'); updateState('connect');
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment