mirror of
https://github.com/gamedig/node-gamedig.git
synced 2024-09-08 11:51:32 +02:00
65 lines
1.5 KiB
JavaScript
65 lines
1.5 KiB
JavaScript
var dgram = require('dgram'),
|
|
EventEmitter = require('events').EventEmitter,
|
|
util = require('util'),
|
|
dns = require('dns'),
|
|
TypeResolver = require('./typeresolver');
|
|
|
|
var activeQueries = [];
|
|
|
|
var udpSocket = dgram.createSocket('udp4');
|
|
udpSocket.unref();
|
|
udpSocket.bind(21943);
|
|
udpSocket.on('message', function(buffer, rinfo) {
|
|
if(Gamedig.debug) console.log(rinfo.address+':'+rinfo.port+" <--UDP "+buffer.toString('hex'));
|
|
for(var i = 0; i < activeQueries.length; i++) {
|
|
var query = activeQueries[i];
|
|
if(
|
|
query.options.address != rinfo.address
|
|
&& query.options.altaddress != rinfo.address
|
|
) continue;
|
|
if(query.options.port != rinfo.port) continue;
|
|
query._udpResponse(buffer);
|
|
break;
|
|
}
|
|
});
|
|
udpSocket.on('error', function(e) {
|
|
if(Gamedig.debug) console.log("UDP ERROR: "+e);
|
|
});
|
|
|
|
Gamedig = {
|
|
|
|
query: function(options,callback) {
|
|
if(callback) options.callback = callback;
|
|
|
|
var query = TypeResolver(options.type);
|
|
if(!query) {
|
|
process.nextTick(function() {
|
|
callback({error:'Invalid server type: '+options.type});
|
|
});
|
|
return;
|
|
}
|
|
query.debug = Gamedig.debug;
|
|
query.udpSocket = udpSocket;
|
|
query.type = options.type;
|
|
|
|
// copy over options
|
|
for(var i in options) query.options[i] = options[i];
|
|
|
|
activeQueries.push(query);
|
|
|
|
query.on('finished',function(state) {
|
|
var i = activeQueries.indexOf(query);
|
|
if(i >= 0) activeQueries.splice(i, 1);
|
|
});
|
|
|
|
process.nextTick(function() {
|
|
query.start();
|
|
});
|
|
|
|
return query;
|
|
}
|
|
|
|
};
|
|
|
|
module.exports = Gamedig;
|