reuse port
This commit is contained in:
parent
b2a7ecd39b
commit
5ddd85e14e
21
lib/relay.js
21
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,10 +299,21 @@ module.exports.create = function (state) {
|
|||
//token.dynamicNames = [];
|
||||
|
||||
var onePortForNow = parseInt(token.ports[0], 10) || 0;
|
||||
// TODO try again with random port
|
||||
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, onDynTcpReady);
|
||||
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) {
|
||||
|
@ -311,6 +321,7 @@ module.exports.create = function (state) {
|
|||
// (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;
|
||||
console.info("[ws] authorized", socketId, "for", token.deviceId);
|
||||
|
|
Loading…
Reference in New Issue