-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathBaseServer.js
74 lines (55 loc) · 1.84 KB
/
BaseServer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
const net = require('net'),
util = require('util'),
events = require('events'),
uuid = require('uuid');
const i2putil = require("./i2putil");
const Sam = require("./Sam");
const ServerConnection = require("./ServerConnection");
module.exports = function() {
var self = this;
Sam.call(this);
self.base_forward_options = i2putil.copyObj(self.base_forward_options);
self.forward_port = new net.Server();
self.forward_port.on('error', self.emit.bind(self, "error"));
self.forward_port.on('connection', self.handleConnection.bind(self));
self.on("cmdStreamStatus", self.emit.bind(self, "listening"));
self.on("end", self.handleEnd.bind(self));
}
util.inherits(module.exports, Sam);
module.exports.prototype.base_forward_options = {
ID: undefined,
PORT: undefined
// , HOST: undefined
}
module.exports.prototype.listen = function (base_forward_options, sam_options) {
var self = this;
self.forward_port.listen(
0, 'localhost',
function () {
var addr = self.forward_port.address();
i2putil.copyObj(base_forward_options, self.base_forward_options);
self.base_forward_options.HOST = addr.address;
self.base_forward_options.PORT = addr.port;
Sam.prototype.connect.call(self, sam_options);
}
);
}
module.exports.prototype.close = function () {
var self = this;
self.end();
}
module.exports.prototype.handleConnection = function (socket) {
var self = this;
server_connection = new ServerConnection(socket, function () {
self.emit("connection", server_connection);
});
};
module.exports.prototype.handleCmdHelloReply = function(data) {
var self = this;
Sam.prototype.handleCmdHelloReply.call(self, data);
self.sendCmd(["STREAM", "FORWARD"], self.base_forward_options);
}
module.exports.prototype.handleEnd = function () {
var self = this;
self.forward_port.close();
}