Additional async rewrite

This commit is contained in:
mmorrison 2019-01-12 04:43:36 -06:00
parent efe12a00aa
commit 29ce0b82d0
24 changed files with 654 additions and 470 deletions

View file

@ -1,87 +1,48 @@
const dgram = require('dgram'),
TypeResolver = require('./typeresolver'),
HexUtil = require('./HexUtil');
const QueryRunner = require('./QueryRunner'),
GlobalUdpSocket = require('./GlobalUdpSocket');
const activeQueries = new Set();
const udpSocket = dgram.createSocket('udp4');
udpSocket.unref();
udpSocket.bind();
udpSocket.on('message', (buffer, rinfo) => {
if(Gamedig.debug) {
console.log(rinfo.address+':'+rinfo.port+" <--UDP");
console.log(HexUtil.debugDump(buffer));
}
for(const query of activeQueries) {
if(query.options.address !== rinfo.address) continue;
if(query.options.port_query !== rinfo.port) continue;
query._udpIncoming(buffer);
break;
}
});
udpSocket.on('error', (e) => {
if(Gamedig.debug) console.log("UDP ERROR: "+e);
});
let singleton = null;
class Gamedig {
constructor() {
this.udpSocket = new GlobalUdpSocket();
this.queryRunner = new QueryRunner(this.udpSocket);
this._debug = false;
}
static query(options,callback) {
const promise = (async () => {
for (const key of Object.keys(options)) {
if (['port_query', 'port'].includes(key)) {
options[key] = parseInt(options[key]);
}
}
setDebug(on) {
this.udpSocket.debug = on;
this._debug = on;
this.queryRunner.debug = on;
}
let query = TypeResolver.lookup(options.type);
query.debug = Gamedig.debug;
query.udpSocket = udpSocket;
query.type = options.type;
if(!('port' in query.options) && ('port_query' in query.options)) {
if(Gamedig.isCommandLine) {
process.stderr.write(
"Warning! This game is so old, that we don't know"
+" what the server's connection port is. We've guessed that"
+" the query port for "+query.type+" is "+query.options.port_query+"."
+" If you know the connection port for this type of server, please let"
+" us know on the GameDig issue tracker, thanks!\n"
);
}
query.options.port = query.options.port_query;
delete query.options.port_query;
}
// copy over options
for(const key of Object.keys(options)) {
query.options[key] = options[key];
}
activeQueries.add(query);
try {
return await query.runAll();
} finally {
activeQueries.delete(query);
}
})();
async query(userOptions) {
userOptions.debug |= this._debug;
return await this.queryRunner.run(userOptions);
}
static getInstance() {
if (!singleton) {
singleton = new Gamedig();
}
return singleton;
}
static query(userOptions, callback) {
const promise = Gamedig.getInstance().query(userOptions);
if (callback && callback instanceof Function) {
if(callback.length === 2) {
if (callback.length === 2) {
promise
.then((state) => callback(null,state))
.then((state) => callback(null, state))
.catch((error) => callback(error));
} else if (callback.length === 1) {
promise
.then((state) => callback(state))
.catch((error) => callback({error:error}));
.catch((error) => callback({error: error}));
}
}
return promise;
}
}
Gamedig.debug = false;
Gamedig.isCommandLine = false;
Object.defineProperty(Gamedig, "debug", { set: on => Gamedig.getInstance().setDebug(on) });
module.exports = Gamedig;