-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathser2netproxy.js
62 lines (52 loc) · 1.52 KB
/
ser2netproxy.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
var util = require("util");
var events = require("events");
var net = require("net");
var g = require("./globals");
function ser2netproxy(netaddr) {
var self = this;
var client = null;
events.EventEmitter.call(this);
this.open = function(cb) {
try {
client = net.connect(netaddr, function() {
this.setEncoding('ascii'); // read data will now return ascii strings, not buffer objects
});
client.on('data', function(data) {
self.emit('data',data);
});
client.on('error', function(e) {
g.log(g.LOG_ERROR,"ser2net got client error: " + e);
client = null;
});
client.on('close', function() {
g.log(g.LOG_ERROR,"ser2net got close");
if (client) {
client.removeAllListeners();
}
client = null;
self.open(cb);
});
}
catch(e) {
g.log(g.LOG_ERROR,"exception in ser2net proxy: " + e);
client = null;
}
cb();
};
this.write = function (data, cb) {
if (!client) {
g.log(g.LOG_ERROR,"ser2net null client write");
if (cb) {
cb();
}
return;
}
client.write(data, "ascii", function() {
if (cb) {
cb();
}
});
};
}
util.inherits(ser2netproxy, events.EventEmitter);
module.exports = ser2netproxy;