2015-03-29 02:24:23 -04:00
|
|
|
var EventEmitter = require('events').EventEmitter
|
2013-01-04 01:45:57 -05:00
|
|
|
, util = require('util')
|
|
|
|
, protocol = require('./protocol')
|
|
|
|
, createPacketBuffer = protocol.createPacketBuffer
|
2014-12-30 03:55:04 -05:00
|
|
|
, compressPacketBuffer = protocol.compressPacketBuffer
|
2015-01-01 17:20:47 -05:00
|
|
|
, oldStylePacket = protocol.oldStylePacket
|
|
|
|
, newStylePacket = protocol.newStylePacket
|
2015-02-21 19:41:41 -05:00
|
|
|
, parsePacketData = protocol.parsePacketData
|
2015-01-02 12:50:54 -05:00
|
|
|
, parseNewStylePacket = protocol.parseNewStylePacket
|
2015-01-01 17:20:47 -05:00
|
|
|
, packetIds = protocol.packetIds
|
|
|
|
, packetNames = protocol.packetNames
|
2013-12-30 10:05:22 -05:00
|
|
|
, states = protocol.states
|
2015-05-13 08:47:28 -04:00
|
|
|
, debug = require('./debug')
|
2015-05-14 16:08:49 -04:00
|
|
|
;
|
2013-01-04 01:45:57 -05:00
|
|
|
|
|
|
|
module.exports = Client;
|
|
|
|
|
2013-01-07 23:36:14 -05:00
|
|
|
function Client(isServer) {
|
2013-01-04 01:45:57 -05:00
|
|
|
EventEmitter.call(this);
|
|
|
|
|
2013-12-30 10:05:22 -05:00
|
|
|
this._state = states.HANDSHAKING;
|
|
|
|
Object.defineProperty(this, "state", {
|
|
|
|
get: function() {
|
|
|
|
return this._state;
|
|
|
|
},
|
|
|
|
set: function(newProperty) {
|
|
|
|
var oldProperty = this._state;
|
|
|
|
this._state = newProperty;
|
|
|
|
this.emit('state', newProperty, oldProperty);
|
|
|
|
}
|
|
|
|
});
|
2013-01-07 23:36:14 -05:00
|
|
|
this.isServer = !!isServer;
|
2013-01-04 01:45:57 -05:00
|
|
|
this.socket = null;
|
|
|
|
this.encryptionEnabled = false;
|
|
|
|
this.cipher = null;
|
|
|
|
this.decipher = null;
|
2015-01-01 17:20:47 -05:00
|
|
|
this.compressionThreshold = -2;
|
2014-04-10 19:48:06 -04:00
|
|
|
this.packetsToParse = {};
|
|
|
|
this.on('newListener', function(event, listener) {
|
|
|
|
var direction = this.isServer ? 'toServer' : 'toClient';
|
2015-05-14 16:08:49 -04:00
|
|
|
if(protocol.packetStates[direction].hasOwnProperty(event) || event === "packet") {
|
|
|
|
if(typeof this.packetsToParse[event] === "undefined") this.packetsToParse[event] = 1;
|
2014-04-10 19:48:06 -04:00
|
|
|
else this.packetsToParse[event] += 1;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
this.on('removeListener', function(event, listener) {
|
|
|
|
var direction = this.isServer ? 'toServer' : 'toClient';
|
2015-05-14 16:08:49 -04:00
|
|
|
if(protocol.packetStates[direction].hasOwnProperty(event) || event === "packet") {
|
2014-04-10 19:48:06 -04:00
|
|
|
this.packetsToParse[event] -= 1;
|
|
|
|
}
|
|
|
|
});
|
2013-01-04 01:45:57 -05:00
|
|
|
}
|
2014-04-10 19:48:06 -04:00
|
|
|
|
2013-01-04 01:45:57 -05:00
|
|
|
util.inherits(Client, EventEmitter);
|
|
|
|
|
2014-04-10 19:48:06 -04:00
|
|
|
// Transform weird "packet" types into string representing their type. Should be mostly retro-compatible
|
|
|
|
Client.prototype.on = function(type, func) {
|
2015-05-14 16:08:49 -04:00
|
|
|
var direction = this.isServer ? 'toServer' : 'toClient';
|
|
|
|
if(Array.isArray(type)) {
|
|
|
|
arguments[0] = protocol.packetNames[type[0]][direction][type[1]];
|
|
|
|
} else if(typeof type === "number") {
|
|
|
|
arguments[0] = protocol.packetNames[this.state][direction][type];
|
|
|
|
}
|
|
|
|
EventEmitter.prototype.on.apply(this, arguments);
|
2014-04-10 19:48:06 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
Client.prototype.onRaw = function(type, func) {
|
2015-05-14 16:08:49 -04:00
|
|
|
var arg = "raw.";
|
|
|
|
if(Array.isArray(type)) {
|
|
|
|
arg += protocol.packetNames[type[0]][direction][type[1]];
|
|
|
|
} else if(typeof type === "number") {
|
|
|
|
arg += protocol.packetNames[this.state][direction][type];
|
|
|
|
} else {
|
|
|
|
arg += type;
|
|
|
|
}
|
|
|
|
arguments[0] = arg;
|
|
|
|
EventEmitter.prototype.on.apply(this, arguments);
|
2014-04-10 19:48:06 -04:00
|
|
|
};
|
|
|
|
|
2013-01-04 01:45:57 -05:00
|
|
|
Client.prototype.setSocket = function(socket) {
|
|
|
|
var self = this;
|
2015-05-14 16:08:49 -04:00
|
|
|
|
2015-02-05 13:02:34 -05:00
|
|
|
function afterParse(err, parsed) {
|
2015-05-14 16:08:49 -04:00
|
|
|
if(err || (parsed && parsed.error)) {
|
2015-02-19 20:04:43 -05:00
|
|
|
self.emit('error', err || parsed.error);
|
|
|
|
self.end("ProtocolError");
|
2015-02-05 13:02:34 -05:00
|
|
|
return;
|
|
|
|
}
|
2015-05-14 16:08:49 -04:00
|
|
|
if(!parsed) {
|
|
|
|
return;
|
|
|
|
}
|
2015-02-19 20:04:43 -05:00
|
|
|
var packet = parsed.results;
|
2015-02-21 19:41:41 -05:00
|
|
|
//incomingBuffer = incomingBuffer.slice(parsed.size); TODO: Already removed in prepare
|
2015-02-05 13:02:34 -05:00
|
|
|
|
|
|
|
var packetName = protocol.packetNames[self.state][self.isServer ? 'toServer' : 'toClient'][packet.id];
|
2015-03-03 07:35:51 -05:00
|
|
|
var packetState = self.state;
|
2015-02-05 13:02:34 -05:00
|
|
|
self.emit(packetName, packet);
|
|
|
|
self.emit('packet', packet);
|
2015-03-03 07:35:51 -05:00
|
|
|
self.emit('raw.' + packetName, parsed.buffer, packetState);
|
|
|
|
self.emit('raw', parsed.buffer, packetState);
|
2015-02-19 15:36:37 -05:00
|
|
|
prepareParse();
|
2015-02-05 13:02:34 -05:00
|
|
|
}
|
2015-02-19 15:36:37 -05:00
|
|
|
|
|
|
|
function prepareParse() {
|
2015-02-21 19:41:41 -05:00
|
|
|
var packetLengthField = protocol.types["varint"][0](incomingBuffer, 0);
|
2015-05-14 16:08:49 -04:00
|
|
|
if(packetLengthField && packetLengthField.size + packetLengthField.value <= incomingBuffer.length) {
|
2015-02-21 19:41:41 -05:00
|
|
|
var buf = incomingBuffer.slice(packetLengthField.size, packetLengthField.size + packetLengthField.value);
|
|
|
|
// TODO : Slice as early as possible to avoid processing same data twice.
|
|
|
|
incomingBuffer = incomingBuffer.slice(packetLengthField.size + packetLengthField.value);
|
2015-05-14 16:08:49 -04:00
|
|
|
if(self.compressionThreshold == -2) {
|
2015-04-26 09:44:03 -04:00
|
|
|
afterParse(null, parsePacketData(buf, self.state, self.isServer, self.packetsToParse));
|
2015-02-21 19:41:41 -05:00
|
|
|
} else {
|
|
|
|
parseNewStylePacket(buf, self.state, self.isServer, self.packetsToParse, afterParse);
|
|
|
|
}
|
|
|
|
}
|
2015-02-19 15:36:37 -05:00
|
|
|
}
|
|
|
|
|
2013-01-04 01:45:57 -05:00
|
|
|
self.socket = socket;
|
2015-05-14 16:08:49 -04:00
|
|
|
if(self.socket.setNoDelay)
|
2014-10-28 19:30:33 -04:00
|
|
|
self.socket.setNoDelay(true);
|
2013-01-04 01:45:57 -05:00
|
|
|
var incomingBuffer = new Buffer(0);
|
|
|
|
self.socket.on('data', function(data) {
|
2015-05-14 16:08:49 -04:00
|
|
|
if(self.encryptionEnabled) data = new Buffer(self.decipher.update(data), 'binary');
|
2013-01-04 01:45:57 -05:00
|
|
|
incomingBuffer = Buffer.concat([incomingBuffer, data]);
|
2015-02-19 15:36:37 -05:00
|
|
|
prepareParse()
|
2013-01-04 01:45:57 -05:00
|
|
|
});
|
|
|
|
|
2013-01-04 21:33:19 -05:00
|
|
|
self.socket.on('connect', function() {
|
|
|
|
self.emit('connect');
|
|
|
|
});
|
2013-02-03 15:36:55 -05:00
|
|
|
|
|
|
|
self.socket.on('error', onError);
|
|
|
|
self.socket.on('close', endSocket);
|
|
|
|
self.socket.on('end', endSocket);
|
|
|
|
self.socket.on('timeout', endSocket);
|
|
|
|
|
|
|
|
function onError(err) {
|
|
|
|
self.emit('error', err);
|
|
|
|
endSocket();
|
|
|
|
}
|
|
|
|
|
2013-02-03 22:13:15 -05:00
|
|
|
var ended = false;
|
2015-05-14 16:08:49 -04:00
|
|
|
|
2013-02-03 15:36:55 -05:00
|
|
|
function endSocket() {
|
2015-05-14 16:08:49 -04:00
|
|
|
if(ended) return;
|
2013-02-03 22:13:15 -05:00
|
|
|
ended = true;
|
2013-02-03 15:36:55 -05:00
|
|
|
self.socket.removeListener('close', endSocket);
|
|
|
|
self.socket.removeListener('end', endSocket);
|
|
|
|
self.socket.removeListener('timeout', endSocket);
|
|
|
|
self.emit('end', self._endReason);
|
|
|
|
}
|
2013-01-04 01:45:57 -05:00
|
|
|
};
|
|
|
|
|
2013-01-04 20:55:53 -05:00
|
|
|
Client.prototype.end = function(reason) {
|
|
|
|
this._endReason = reason;
|
2013-01-04 01:45:57 -05:00
|
|
|
this.socket.end();
|
|
|
|
};
|
|
|
|
|
|
|
|
Client.prototype.write = function(packetId, params) {
|
2015-05-14 16:08:49 -04:00
|
|
|
if(Array.isArray(packetId)) {
|
|
|
|
if(packetId[0] !== this.state)
|
2013-12-30 10:05:22 -05:00
|
|
|
return false;
|
|
|
|
packetId = packetId[1];
|
|
|
|
}
|
2015-05-14 16:08:49 -04:00
|
|
|
if(typeof packetId === "string")
|
2015-01-01 17:20:47 -05:00
|
|
|
packetId = packetIds[this.state][this.isServer ? "toClient" : "toServer"][packetId];
|
2014-12-31 13:26:59 -05:00
|
|
|
var that = this;
|
|
|
|
|
2015-02-21 19:41:41 -05:00
|
|
|
var finishWriting = function(err, buffer) {
|
2015-05-14 16:08:49 -04:00
|
|
|
if(err) {
|
2015-02-21 19:41:41 -05:00
|
|
|
console.log(err);
|
|
|
|
throw err; // TODO : Handle errors gracefully, if possible
|
|
|
|
}
|
2015-01-01 17:20:47 -05:00
|
|
|
var packetName = packetNames[that.state][that.isServer ? "toClient" : "toServer"][packetId];
|
|
|
|
debug("writing packetId " + that.state + "." + packetName + " (0x" + packetId.toString(16) + ")");
|
|
|
|
debug(params);
|
|
|
|
var out = that.encryptionEnabled ? new Buffer(that.cipher.update(buffer), 'binary') : buffer;
|
|
|
|
that.socket.write(out);
|
|
|
|
return true;
|
2014-12-30 03:55:04 -05:00
|
|
|
}
|
|
|
|
|
2013-12-30 10:05:22 -05:00
|
|
|
var buffer = createPacketBuffer(packetId, this.state, params, this.isServer);
|
2015-05-14 16:08:49 -04:00
|
|
|
if(this.compressionThreshold >= 0 && buffer.length >= this.compressionThreshold) {
|
2015-01-01 17:20:47 -05:00
|
|
|
debug("Compressing packet");
|
|
|
|
compressPacketBuffer(buffer, finishWriting);
|
2015-05-14 16:08:49 -04:00
|
|
|
} else if(this.compressionThreshold >= -1) {
|
2015-01-01 17:20:47 -05:00
|
|
|
debug("New-styling packet");
|
2015-05-18 12:24:10 -04:00
|
|
|
newStylePacket(buffer, 0, finishWriting);
|
2014-12-30 03:55:04 -05:00
|
|
|
} else {
|
2015-01-01 17:20:47 -05:00
|
|
|
debug("Old-styling packet");
|
2015-02-21 19:41:41 -05:00
|
|
|
oldStylePacket(buffer, finishWriting);
|
2014-12-30 03:55:04 -05:00
|
|
|
}
|
2013-01-04 01:45:57 -05:00
|
|
|
};
|
2014-04-10 19:48:06 -04:00
|
|
|
|
2015-01-01 17:20:47 -05:00
|
|
|
// TODO : Perhaps this should only accept buffers without length, so we can
|
|
|
|
// handle compression ourself ? Needs to ask peopl who actually use this feature
|
|
|
|
// like @deathcap
|
2015-03-02 21:01:04 -05:00
|
|
|
Client.prototype.writeRaw = function(buffer) {
|
2015-05-14 16:08:49 -04:00
|
|
|
var self = this;
|
|
|
|
|
|
|
|
var finishWriting = function(error, buffer) {
|
|
|
|
if(error)
|
|
|
|
throw error; // TODO : How do we handle this error ?
|
|
|
|
var out = self.encryptionEnabled ? new Buffer(self.cipher.update(buffer), 'binary') : buffer;
|
|
|
|
self.socket.write(out);
|
|
|
|
};
|
|
|
|
if(this.compressionThreshold >= 0 && buffer.length >= this.compressionThreshold) {
|
|
|
|
compressPacketBuffer(buffer, finishWriting);
|
|
|
|
} else if(this.compressionThreshold >= -1) {
|
2015-05-18 12:24:10 -04:00
|
|
|
newStylePacket(buffer, 0, finishWriting);
|
2015-05-14 16:08:49 -04:00
|
|
|
} else {
|
|
|
|
oldStylePacket(buffer, finishWriting);
|
|
|
|
}
|
2014-10-28 19:30:33 -04:00
|
|
|
};
|