-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
42 lines (32 loc) · 1.12 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
/*global require: true*/
/*global exports: true*/
/*global console: true*/
/*global module: true*/
var net = require('net');
var EventEmitter = require('events').EventEmitter;
var SmpProtocol = require('./protocol').SmpProtocol;
function SmpConnection(tcpSocket, config)
{
var protocol = this.protocol = new SmpProtocol(this, tcpSocket, config.protocol);
tcpSocket.on('connect', protocol.connect);
tcpSocket.on('data', protocol.onData);
tcpSocket.on('close', protocol.close);
}
require('util').inherits(SmpConnection, EventEmitter);
function SmpServer(config)
{
var tcpServer = net.createServer(config.socket);
function socketListening()
{
function socketConnection(tcpSocket)
{
var connection = new SmpConnection(tcpSocket, config);
this.emit('connection', connection);
}
tcpServer.on('connection', socketConnection);
}
tcpServer.listen(config.port, '0.0.0.0');
tcpServer.on('listening', socketListening);
}
require('util').inherits(SmpServer, EventEmitter);
module.exports.SmpServer = SmpServer;