Made protocols a bit more error resistant

This commit is contained in:
Michael Morrison 2014-02-02 14:28:31 -06:00
parent d6d7fee80f
commit a6338df070
4 changed files with 19 additions and 10 deletions

View file

@ -1,5 +1,3 @@
var dns = require('dns');
module.exports = require('./protocols/gamespy3').extend({
init: function() {
this._super();

View file

@ -119,27 +119,32 @@ module.exports = require('./core').extend({
var numPackets = 0;
var packets = {};
this.udpSend(b,function(buffer) {
var iType = buffer.readUInt8(0);
var reader = self.reader(buffer);
var iType = reader.uint(1);
if(iType != type) return;
var iSessionId = buffer.readUInt32BE(1);
var iSessionId = reader.uint(4);
if(iSessionId != self.sessionId) return;
if(!assemble) {
c(buffer.slice(5));
c(reader.rest());
return true;
}
if(self.useOnlySingleSplit) {
// has split headers, but they are worthless and only one packet is used
c([buffer.slice(16)]);
reader.skip(11);
c([buffer.rest()]);
return true;
}
var id = buffer.readUInt16LE(14);
reader.skip(9); // filler data -- usually set to 'splitnum\0'
var id = reader.uint(1);
var last = (id & 0x80);
id = id & 0x7f;
if(last) numPackets = id+1;
reader.skip(1); // "another 'packet number' byte, but isn't understood."
packets[id] = buffer.slice(16);
packets[id] = reader.rest();
if(self.debug) {
console.log("Received packet #"+id);
if(last) console.log("(last)");

View file

@ -119,13 +119,16 @@ module.exports = require('./core').extend({
return out;
},
sendPacket: function(type,required,callback) {
var self = this;
var outbuffer = new Buffer([0x79,0,0,0,type]);
var packets = [];
this.udpSend(outbuffer,function(buffer) {
var iType = buffer.readUInt8(4);
var reader = self.reader(buffer);
var header = reader.uint(4);
var iType = reader.uint(1);
if(iType != type) return;
packets.push(buffer.slice(5));
packets.push(reader.rest());
},function() {
if(!packets.length && required) return;
callback(Buffer.concat(packets));

View file

@ -22,6 +22,9 @@ udpSocket.on('message', function(buffer, rinfo) {
break;
}
});
udpSocket.on('error', function(e) {
if(Gamedig.debug) console.log("UDP ERROR: "+e);
});
Gamedig = {