2013-01-04 01:45:57 -05:00
|
|
|
var net = require('net')
|
|
|
|
, EventEmitter = require('events').EventEmitter
|
|
|
|
, util = require('util')
|
|
|
|
, assert = require('assert')
|
|
|
|
, Client = require('./client')
|
2013-12-30 10:05:22 -05:00
|
|
|
, states = require('./protocol').states
|
2013-11-19 18:06:57 -05:00
|
|
|
;
|
2013-01-04 01:45:57 -05:00
|
|
|
|
|
|
|
module.exports = Server;
|
|
|
|
|
2013-01-07 23:36:14 -05:00
|
|
|
function Server() {
|
2013-01-04 01:45:57 -05:00
|
|
|
EventEmitter.call(this);
|
|
|
|
|
2013-01-04 20:22:19 -05:00
|
|
|
this.socketServer = null;
|
2013-01-04 01:45:57 -05:00
|
|
|
this.cipher = null;
|
|
|
|
this.decipher = null;
|
2013-01-04 20:22:19 -05:00
|
|
|
this.clients = {};
|
2013-01-04 01:45:57 -05:00
|
|
|
}
|
|
|
|
util.inherits(Server, EventEmitter);
|
|
|
|
|
|
|
|
Server.prototype.listen = function(port, host) {
|
|
|
|
var self = this;
|
2013-01-04 20:22:19 -05:00
|
|
|
var nextId = 0;
|
|
|
|
self.socketServer = net.createServer();
|
|
|
|
self.socketServer.on('connection', function(socket) {
|
2013-01-07 23:36:14 -05:00
|
|
|
var client = new Client(true);
|
2013-01-27 06:47:35 -05:00
|
|
|
client._end = client.end;
|
|
|
|
client.end = function end(endReason) {
|
2013-12-30 10:05:22 -05:00
|
|
|
if (client.state === states.PLAY) {
|
|
|
|
client.write(0x40, {reason: endReason});
|
|
|
|
} else if (client.state === states.LOGIN) {
|
|
|
|
client.write(0x00, {reason: endReason});
|
|
|
|
}
|
2013-02-10 17:51:47 -05:00
|
|
|
client._end(endReason);
|
|
|
|
};
|
2013-01-04 20:22:19 -05:00
|
|
|
client.id = nextId++;
|
|
|
|
self.clients[client.id] = client;
|
2013-01-04 01:45:57 -05:00
|
|
|
client.on('error', function(err) {
|
|
|
|
self.emit('error', err);
|
|
|
|
});
|
|
|
|
client.on('end', function() {
|
2013-01-04 20:22:19 -05:00
|
|
|
delete self.clients[client.id];
|
2013-01-04 01:45:57 -05:00
|
|
|
});
|
2013-01-04 20:55:53 -05:00
|
|
|
client.setSocket(socket);
|
|
|
|
self.emit('connection', client);
|
2013-01-04 01:45:57 -05:00
|
|
|
});
|
2013-01-04 20:22:19 -05:00
|
|
|
self.socketServer.on('error', function(err) {
|
2013-01-04 01:45:57 -05:00
|
|
|
self.emit('error', err);
|
|
|
|
});
|
2013-01-04 20:22:19 -05:00
|
|
|
self.socketServer.on('close', function() {
|
|
|
|
self.emit('close');
|
2013-01-04 01:45:57 -05:00
|
|
|
});
|
2013-01-04 20:22:19 -05:00
|
|
|
self.socketServer.on('listening', function() {
|
2013-01-04 01:45:57 -05:00
|
|
|
self.emit('listening');
|
|
|
|
});
|
2013-01-04 20:22:19 -05:00
|
|
|
self.socketServer.listen(port, host);
|
|
|
|
};
|
|
|
|
|
|
|
|
Server.prototype.close = function() {
|
|
|
|
var client;
|
|
|
|
for(var clientId in this.clients) {
|
2013-11-19 18:06:57 -05:00
|
|
|
if (!this.clients.hasOwnProperty(clientId)) continue;
|
|
|
|
|
2013-01-04 20:22:19 -05:00
|
|
|
client = this.clients[clientId];
|
2013-01-04 20:55:53 -05:00
|
|
|
client.end('ServerShutdown');
|
2013-01-04 20:22:19 -05:00
|
|
|
}
|
|
|
|
this.socketServer.close();
|
2013-01-04 01:45:57 -05:00
|
|
|
};
|