rearranged some of the handler functions

This commit is contained in:
tigerbot 2017-04-07 17:38:55 -06:00
parent 61a8d29c5e
commit cbdff3e55b
1 changed files with 62 additions and 73 deletions

View File

@ -9,13 +9,52 @@ var authenticated = false;
function run(copts) { function run(copts) {
var tunnelUrl = copts.stunneld.replace(/\/$/, '') + '/?access_token=' + copts.token; var tunnelUrl = copts.stunneld.replace(/\/$/, '') + '/?access_token=' + copts.token;
var wstunneler; var wstunneler;
var localclients = {}; var localclients = {};
// BaaS / Backendless / noBackend / horizon.io var clientHandlers = {
// user authentication onClose: function (cid, opts, err) {
// a place to store data try {
// file management wstunneler.send(Packer.pack(opts, null, err && 'error' || 'end'), { binary: true });
// Synergy Teamwork Paradigm = Jabberwocky } catch(e) {
var handlers = { // ignore
}
delete localclients[cid];
console.log('[local onClose] closed "' + cid + '" (' + clientHandlers.count() + ' clients)');
}
, onError: function(cid, opts, err) {
console.info("[local onError] closing '" + cid + "' because '" + err.message + "'");
clientHandlers.onClose(cid, opts, err);
}
, closeSingle: function (cid) {
console.log('[closeSingle]', cid);
if (!localclients[cid]) {
return;
}
try {
localclients[cid].end();
} catch(e) {
// ignore
}
delete localclients[cid];
}
, closeAll: function () {
console.log('[close clients]');
Object.keys(localclients).forEach(function (cid) {
try {
localclients[cid].end();
} catch(e) {
// ignore
}
delete localclients[cid];
});
}
, count: function () {
return Object.keys(localclients).length;
}
};
var packerHandlers = {
onmessage: function (opts) { onmessage: function (opts) {
var net = copts.net || require('net'); var net = copts.net || require('net');
var cid = Packer.addrToId(opts); var cid = Packer.addrToId(opts);
@ -34,7 +73,7 @@ function run(copts) {
return; return;
} }
if (!portList) { if (!portList) {
handlers._onLocalError(cid, opts, new Error("unsupported service '" + service + "'")); packerHandlers._onConnectError(cid, opts, new Error("unsupported service '" + service + "'"));
return; return;
} }
@ -52,13 +91,11 @@ function run(copts) {
if (!servername) { if (!servername) {
//console.warn(opts.data.toString()); //console.warn(opts.data.toString());
handlers._onLocalError(cid, opts, new Error("missing servername for '" + cid + "' " + opts.data.byteLength)); packerHandlers._onConnectError(cid, opts, new Error("missing servername for '" + cid + "' " + opts.data.byteLength));
return; return;
} }
port = portList[servername] || portList['*']; port = portList[servername] || portList['*'];
console.info("[connect] new client '" + cid + "' for '" + servername + "' (" + (handlers._numClients() + 1) + " clients)");
console.log('port', port, opts.port, service, portList); console.log('port', port, opts.port, service, portList);
localclients[cid] = net.createConnection({ localclients[cid] = net.createConnection({
port: port port: port
@ -74,15 +111,8 @@ function run(copts) {
// this will happen before 'data' is triggered // this will happen before 'data' is triggered
localclients[cid].write(opts.data); localclients[cid].write(opts.data);
}); });
// 'data' console.info("[connect] new client '" + cid + "' for '" + servername + "' (" + clientHandlers.count() + " clients)");
/*
localclients[cid].on('data', function (chunk) {
//console.log("[<=] local '" + opts.service + "' sent to '" + cid + "' <= ", chunk.byteLength, "bytes");
//console.log(JSON.stringify(chunk.toString()));
wstunneler.send(Packer.pack(opts, chunk), { binary: true });
});
//*/
///*
localclients[cid].on('readable', function (size) { localclients[cid].on('readable', function (size) {
var chunk; var chunk;
@ -98,58 +128,31 @@ function run(copts) {
do { do {
chunk = localclients[cid].read(size); chunk = localclients[cid].read(size);
//console.log("[<=] local '" + opts.service + "' sent to '" + cid + "' <= ", chunk.byteLength, "bytes");
//console.log(JSON.stringify(chunk.toString()));
if (chunk) { if (chunk) {
wstunneler.send(Packer.pack(opts, chunk), { binary: true }); wstunneler.send(Packer.pack(opts, chunk), { binary: true });
} }
} while (chunk); } while (chunk);
}); });
//*/ localclients[cid].on('error', clientHandlers.onError.bind(null, cid, opts));
localclients[cid].on('error', function (err) { localclients[cid].on('end', clientHandlers.onClose.bind(null, cid, opts));
handlers._onLocalError(cid, opts, err);
});
localclients[cid].on('end', function () {
console.info("[end] closing client '" + cid + "' for '" + servername + "' (" + (handlers._numClients() - 1) + " clients)");
handlers._onLocalClose(cid, opts);
});
} }
, onend: function (opts) { , onend: function (opts) {
var cid = Packer.addrToId(opts); var cid = Packer.addrToId(opts);
//console.log("[end] '" + cid + "'"); //console.log("[end] '" + cid + "'");
handlers._onend(cid); clientHandlers.closeSingle(cid);
} }
, onerror: function (opts) { , onerror: function (opts) {
var cid = Packer.addrToId(opts); var cid = Packer.addrToId(opts);
//console.log("[error] '" + cid + "'", opts.code || '', opts.message); //console.log("[error] '" + cid + "'", opts.code || '', opts.message);
handlers._onend(cid); clientHandlers.closeSingle(cid);
} }
, _onend: function (cid) { , _onConnectError: function (cid, opts, err) {
console.log('[_onend]'); console.info("[_onConnectError] opening '" + cid + "' failed because " + err.message);
if (localclients[cid]) {
try {
localclients[cid].end();
} catch(e) {
// ignore
}
}
delete localclients[cid];
}
, _onLocalClose: function (cid, opts, err) {
console.log('[_onLocalClose]');
try { try {
wstunneler.send(Packer.pack(opts, null, err && 'error' || 'end'), { binary: true }); wstunneler.send(Packer.pack(opts, null, 'error'), { binary: true });
} catch(e) { } catch(e) {
// ignore // ignore
} }
delete localclients[cid];
}
, _onLocalError: function (cid, opts, err) {
console.info("[error] closing '" + cid + "' because '" + err.message + "' (" + (handlers._numClients() - 1) + " clients)");
handlers._onLocalClose(cid, opts, err);
}
, _numClients: function () {
return Object.keys(localclients).length;
} }
}; };
var wsHandlers = { var wsHandlers = {
@ -158,20 +161,10 @@ function run(copts) {
} }
, retry: true , retry: true
, closeClients: function () {
console.log('[close clients]');
Object.keys(localclients).forEach(function (cid) {
try {
localclients[cid].end();
} catch(e) {
// ignore
}
delete localclients[cid];
});
}
, onClose: function () { , onClose: function () {
console.log('ON CLOSE'); console.log('ON CLOSE');
clientHandlers.closeAll();
if (!authenticated) { if (!authenticated) {
console.info('[close] failed on first attempt... check authentication.'); console.info('[close] failed on first attempt... check authentication.');
} }
@ -182,10 +175,6 @@ function run(copts) {
else { else {
console.info('[close] closing tunnel to exit...'); console.info('[close] closing tunnel to exit...');
} }
process.removeListener('exit', wsHandlers.onExit);
process.removeListener('SIGINT', wsHandlers.onExit);
wsHandlers.closeClients();
} }
, onError: function (err) { , onError: function (err) {
@ -196,7 +185,7 @@ function run(copts) {
, onExit: function () { , onExit: function () {
console.log('[wait] closing wstunneler...'); console.log('[wait] closing wstunneler...');
wsHandlers.retry = false; wsHandlers.retry = false;
wsHandlers.closeClients(); clientHandlers.closeAll();
try { try {
wstunneler.close(); wstunneler.close();
} catch(e) { } catch(e) {
@ -206,7 +195,7 @@ function run(copts) {
} }
} }
}; };
var machine = require('tunnel-packer').create(handlers); var machine = require('tunnel-packer').create(packerHandlers);
console.info("[connect] '" + copts.stunneld + "'"); console.info("[connect] '" + copts.stunneld + "'");