Remove callback support and global Gamedig.debug option

This commit is contained in:
mmorrison 2019-01-12 22:38:49 -06:00
parent fdc08b5c09
commit dfa5c95efc
5 changed files with 28 additions and 46 deletions

View File

@ -27,8 +27,9 @@ if (argv._.length >= 1) {
options.port = split[1];
}
}
if(debug) Gamedig.debug = true;
if (debug) {
options.debug = true;
}
Gamedig.query(options)
.then((state) => {

View File

@ -5,7 +5,7 @@ class GlobalUdpSocket {
constructor() {
this.socket = null;
this.callbacks = new Set();
this.debug = false;
this.debuggingCallbacks = new Set();
}
_getSocket() {
@ -14,12 +14,18 @@ class GlobalUdpSocket {
udpSocket.unref();
udpSocket.bind();
udpSocket.on('message', (buffer, rinfo) => {
const fromAddress = rinfo.address;
const fromPort = rinfo.port;
if (this.debuggingCallbacks.size) {
console.log(fromAddress + ':' + fromPort + " <--UDP");
console.log(HexUtil.debugDump(buffer));
}
for (const cb of this.callbacks) {
cb(rinfo.address, rinfo.port, buffer);
cb(fromAddress, fromPort, buffer);
}
});
udpSocket.on('error', (e) => {
if (this.debug) {
if (this.debuggingCallbacks.size) {
console.log("UDP ERROR: " + e);
}
});
@ -31,11 +37,15 @@ class GlobalUdpSocket {
this._getSocket().send(buffer,0,buffer.length,port,address);
}
addCallback(callback) {
addCallback(callback, debug) {
this.callbacks.add(callback);
if (debug) {
this.debuggingCallbacks.add(callback);
}
}
removeCallback(callback) {
this.callbacks.delete(callback);
this.debuggingCallbacks.delete(callback);
}
}

View File

@ -1,5 +1,6 @@
const GameResolver = require('./GameResolver'),
ProtocolResolver = require('./ProtocolResolver');
ProtocolResolver = require('./ProtocolResolver'),
GlobalUdpSocket = require('./GlobalUdpSocket');
const defaultOptions = {
socketTimeout: 2000,
@ -8,9 +9,8 @@ const defaultOptions = {
};
class QueryRunner {
constructor(udpSocket, debug) {
this.debug = debug;
this.udpSocket = udpSocket;
constructor() {
this.udpSocket = new GlobalUdpSocket();
this.gameResolver = new GameResolver();
this.protocolResolver = new ProtocolResolver();
}
@ -90,7 +90,7 @@ class QueryRunner {
}
async _attempt(options) {
if (this.debug) {
if (options.debug) {
console.log("Running attempt with options:");
console.log(options);
}

View File

@ -1,48 +1,23 @@
const QueryRunner = require('./QueryRunner'),
GlobalUdpSocket = require('./GlobalUdpSocket');
const QueryRunner = require('./QueryRunner');
let singleton = null;
class Gamedig {
constructor() {
this.udpSocket = new GlobalUdpSocket();
this.queryRunner = new QueryRunner(this.udpSocket);
this._debug = false;
}
setDebug(on) {
this.udpSocket.debug = on;
this._debug = on;
this.queryRunner.debug = on;
this.queryRunner = new QueryRunner();
}
async query(userOptions) {
userOptions.debug |= this._debug;
return await this.queryRunner.run(userOptions);
}
static getInstance() {
if (!singleton) {
singleton = new Gamedig();
}
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) {
promise
.then((state) => callback(null, state))
.catch((error) => callback(error));
} else if (callback.length === 1) {
promise
.then((state) => callback(state))
.catch((error) => callback({error: error}));
}
}
return promise;
static async query(...args) {
return await Gamedig.getInstance().query(...args);
}
}
Object.defineProperty(Gamedig, "debug", { set: on => Gamedig.getInstance().setDebug(on) });
module.exports = Gamedig;

View File

@ -312,10 +312,6 @@ class Core extends EventEmitter {
const rtt = end-start;
this.registerRtt(rtt);
}
this.debugLog(log => {
log(fromAddress + ':' + fromPort + " <--UDP");
log(HexUtil.debugDump(buffer));
});
const result = onPacket(buffer);
if (result !== undefined) {
this.debugLog("UDP send finished by callback");
@ -325,7 +321,7 @@ class Core extends EventEmitter {
reject(e);
}
};
socket.addCallback(socketCallback);
socket.addCallback(socketCallback, this.options.debug);
});
timeout = Promises.createTimeout(this.options.socketTimeout, 'UDP');
const wrappedTimeout = new Promise((resolve, reject) => {