Initial es6 async conversion work

This commit is contained in:
mmorrison 2019-01-07 00:52:29 -06:00
parent a054557f10
commit 77b2cc1c7f
10 changed files with 773 additions and 748 deletions

View file

@ -2,7 +2,7 @@ const dgram = require('dgram'),
TypeResolver = require('./typeresolver'),
HexUtil = require('./HexUtil');
const activeQueries = [];
const activeQueries = new Set();
const udpSocket = dgram.createSocket('udp4');
udpSocket.unref();
@ -13,12 +13,9 @@ udpSocket.on('message', (buffer, rinfo) => {
console.log(HexUtil.debugDump(buffer));
}
for(const query of activeQueries) {
if(
query.options.address !== rinfo.address
&& query.options.altaddress !== rinfo.address
) continue;
if(query.options.address !== rinfo.address) continue;
if(query.options.port_query !== rinfo.port) continue;
query._udpResponse(buffer);
query._udpIncoming(buffer);
break;
}
});
@ -29,27 +26,14 @@ udpSocket.on('error', (e) => {
class Gamedig {
static query(options,callback) {
const promise = new Promise((resolve,reject) => {
const promise = (async () => {
for (const key of Object.keys(options)) {
if (['port_query', 'port'].includes(key)) {
options[key] = parseInt(options[key]);
}
}
options.callback = (state) => {
if (state.error) reject(state.error);
else resolve(state);
};
let query;
try {
query = TypeResolver.lookup(options.type);
} catch(e) {
process.nextTick(() => {
options.callback({error:e});
});
return;
}
let query = TypeResolver.lookup(options.type);
query.debug = Gamedig.debug;
query.udpSocket = udpSocket;
query.type = options.type;
@ -73,17 +57,13 @@ class Gamedig {
query.options[key] = options[key];
}
activeQueries.push(query);
query.on('finished',() => {
const i = activeQueries.indexOf(query);
if(i >= 0) activeQueries.splice(i, 1);
});
process.nextTick(() => {
query.start();
});
});
activeQueries.add(query);
try {
return await query.runAll();
} finally {
activeQueries.delete(query);
}
})();
if (callback && callback instanceof Function) {
if(callback.length === 2) {

View file

@ -1,5 +1,6 @@
const Path = require('path'),
fs = require('fs');
fs = require('fs'),
Core = require('../protocols/core');
const protocolDir = Path.normalize(__dirname+'/../protocols');
const gamesFile = Path.normalize(__dirname+'/../games.txt');
@ -55,6 +56,10 @@ function createProtocolInstance(type) {
}
class TypeResolver {
/**
* @param {string} type
* @returns Core
*/
static lookup(type) {
if(!type) throw Error('No game specified');