Improve source protocol to support all available features

This commit is contained in:
Michael Morrison 2013-07-10 07:13:04 -05:00
parent ef0f0075d3
commit f062e03271
5 changed files with 56 additions and 10 deletions

View file

@ -28,6 +28,7 @@
"dependencies": {
"iconv": ">=2.0.6",
"bignum": ">=0.6.1",
"async": ">=0.2.9"
"async": ">=0.2.9",
"compressjs": ">=1.0.0"
}
}

View file

@ -2,7 +2,6 @@ var EventEmitter = require('events').EventEmitter,
dns = require('dns'),
async = require('async'),
Class = require('../Class'),
Bignum = require('bignum'),
Reader = require('../reader');
module.exports = Class.extend(EventEmitter,{

View file

@ -80,7 +80,7 @@ module.exports = require('./core').extend({
id = id & 0x7f;
if(last) numPackets = id+1;
packets[id] = buffer;
packets[id] = buffer.slice(16);
if(!numPackets || Object.keys(packets).length != numPackets) return;
@ -91,7 +91,7 @@ module.exports = require('./core').extend({
self.error('Missing packet #'+i);
return true;
}
list.push(packets[i].slice(16));
list.push(packets[i]);
}
var assembled = Buffer.concat(list);
c(assembled);

View file

@ -1,4 +1,5 @@
var async = require('async');
var async = require('async'),
Bzip2 = require('compressjs').Bzip2;
module.exports = require('./core').extend({
init: function() {
@ -15,7 +16,7 @@ module.exports = require('./core').extend({
async.series([
function(c) {
self.sendPacket(
0x54,false,new Buffer('Source Engine Query'),
0x54,false,new Buffer('Source Engine Query\0'),
self.goldsrc ? 0x6D : 0x49,
function(b) {
var reader = self.reader(b);
@ -136,13 +137,54 @@ module.exports = require('./core').extend({
return true;
}
var numPackets = 0;
var packets = [];
var bzip = false;
this.udpSend(b,function(buffer) {
var header = buffer.readInt32LE(0);
if(header == -1) return received(buffer.slice(4));
if(header == -1) {
// full package
return received(buffer.slice(4));
}
if(header == -2) {
// partial package
var uid = buffer.readUInt32LE(4);
if(!self.goldsrc && uid & 0x80000000) bzip = true;
// partial pack
console.log(buffer);
var id,payload;
if(self.goldsrc) {
id = buffer.readUInt8(8);
numPackets = id & 0x0f;
id = id & 0xf0 >> 4;
payload = buffer.slice(9);
} else {
numPackets = buffer.readUInt8(8);
id = buffer.readUInt8(9);
if(id == 0 && bzip) payload = buffer.slice(20);
else payload = buffer.slice(12);
}
console.log(id,numPackets);
packets[id] = payload;
if(!numPackets || Object.keys(packets).length != numPackets) return;
// assemble the parts
var list = [];
for(var i = 0; i < numPackets; i++) {
if(!(i in packets)) {
self.error('Missing packet #'+i);
return true;
}
list.push(packets[i]);
}
var assembled = Buffer.concat(list);
var payload = assembled.slice(4);
if(bzip) payload = Bzip2.uncompressFile(payload);
return received(payload);
}
});
}
});

View file

@ -1,4 +1,8 @@
var Iconv = require('iconv').Iconv;
var Iconv = require('iconv').Iconv,
Bignum = require('bignum');
var Iconv_converters = {};
function getIconv(from) {
var to = 'utf-8';