reuse port
This commit is contained in:
parent
b2a7ecd39b
commit
5ddd85e14e
37
lib/relay.js
37
lib/relay.js
|
@ -4,6 +4,7 @@ var url = require('url');
|
|||
var PromiseA = require('bluebird');
|
||||
var jwt = require('jsonwebtoken');
|
||||
var Packer = require('proxy-packer');
|
||||
var portServers = {};
|
||||
|
||||
function timeoutPromise(duration) {
|
||||
return new PromiseA(function (resolve) {
|
||||
|
@ -258,9 +259,7 @@ module.exports.create = function (state) {
|
|||
Devices.add(state.deviceLists, domainname, token);
|
||||
});
|
||||
|
||||
function onDynTcpReady() {
|
||||
var serviceport = this.address().port;
|
||||
console.info('[DynTcpConn] Port', serviceport, 'now open for', token.deviceId);
|
||||
function onDynTcpReadyHelper(serviceport) {
|
||||
//token.dynamicPorts.push(serviceport);
|
||||
Devices.add(state.deviceLists, serviceport, token);
|
||||
//var hri = require('human-readable-ids').hri;
|
||||
|
@ -300,16 +299,28 @@ module.exports.create = function (state) {
|
|||
//token.dynamicNames = [];
|
||||
|
||||
var onePortForNow = parseInt(token.ports[0], 10) || 0;
|
||||
// TODO try again with random port
|
||||
try {
|
||||
token.server = require('net').createServer(onDynTcpConn).listen(onePortForNow, onDynTcpReady);
|
||||
token.server.on('error', function (e) {
|
||||
console.error("Server Error assigning a dynamic port to a new connection:", e);
|
||||
});
|
||||
} catch(e) {
|
||||
// what a wonderful problem it will be the day that this bug needs to be fixed
|
||||
// (i.e. there are enough users to run out of ports)
|
||||
console.error("Error assigning a dynamic port to a new connection:", e);
|
||||
if (portServers[onePortForNow]) {
|
||||
//token.ports = [];
|
||||
token.server = portServers[onePortForNow];
|
||||
token.server.on('connection', onDynTcpConn);
|
||||
onDynTcpReadyHelper(onePortForNow);
|
||||
} else {
|
||||
try {
|
||||
token.server = require('net').createServer(onDynTcpConn).listen(onePortForNow, function () {
|
||||
var serviceport = this.address().port;
|
||||
portServers[serviceport] = this;
|
||||
console.info('[DynTcpConn] Port', serviceport, 'now open for', token.deviceId);
|
||||
onDynTcpReadyHelper(serviceport);
|
||||
});
|
||||
token.server.on('error', function (e) {
|
||||
// TODO try again with random port
|
||||
console.error("Server Error assigning a dynamic port to a new connection:", e);
|
||||
});
|
||||
} catch(e) {
|
||||
// what a wonderful problem it will be the day that this bug needs to be fixed
|
||||
// (i.e. there are enough users to run out of ports)
|
||||
console.error("Error assigning a dynamic port to a new connection:", e);
|
||||
}
|
||||
}
|
||||
|
||||
remotes[jwtoken] = token;
|
||||
|
|
Loading…
Reference in New Issue