forked from raceconditions/StellarSocks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
76 lines (59 loc) · 1.98 KB
/
server.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
74
75
76
/*
* (C) Copyright 2014 Travis Miller (http://raceconditions.net/).
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Lesser General Public License
* (LGPL) version 2.1 which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/lgpl-2.1.html
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
var net = require('net');
var util = require('util');
var EventEmitter = require('events').EventEmitter;
var Server = function(port) {
var self = this;
this.start = function () {
self.server = net.createServer(function(sock) {
self.sock = sock;
self.isStarted = true;
var remoteAddress = sock.remoteAddress;
var remotePort = sock.remotePort;
sock.socketIsOpen = true;
console.log('CONNECTED: ' + remoteAddress +':'+ remotePort);
sock.on('error', function(err) {
if(err != undefined)
console.log("ERROR: ", err);
});
sock.on('data', function(data) {
sock.lastCommand = data;
console.log('DATA ' + sock.remoteAddress + ': ' + data);
self.emit('data', data);
});
sock.on('close', function(data) {
console.log('CLOSED: ' + remoteAddress +':'+ remotePort);
sock.socketIsOpen = false;
});
});
self.server.listen(port, function() {
console.log("STARTED server on port:", port);
});
};
this.stop = function() {
if(self.isStarted) {
self.server.close(function() {
console.log("STOPPED server on port:", port);
});
self.isStarted = false;
}
};
this.write = function(buffer) {
if(self.sock && self.sock.socketIsOpen)
self.sock.write(buffer);
};
};
util.inherits(Server, EventEmitter);
module.exports = Server;