From dfa5c95efc29fd2b1199ad3484e85cbb952ea4f6 Mon Sep 17 00:00:00 2001 From: mmorrison Date: Sat, 12 Jan 2019 22:38:49 -0600 Subject: [PATCH] Remove callback support and global Gamedig.debug option --- bin/gamedig.js | 5 +++-- lib/GlobalUdpSocket.js | 18 ++++++++++++++---- lib/QueryRunner.js | 10 +++++----- lib/index.js | 35 +++++------------------------------ protocols/core.js | 6 +----- 5 files changed, 28 insertions(+), 46 deletions(-) diff --git a/bin/gamedig.js b/bin/gamedig.js index 64e2411..d12a80b 100644 --- a/bin/gamedig.js +++ b/bin/gamedig.js @@ -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) => { diff --git a/lib/GlobalUdpSocket.js b/lib/GlobalUdpSocket.js index a59fff7..af45ead 100644 --- a/lib/GlobalUdpSocket.js +++ b/lib/GlobalUdpSocket.js @@ -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); } } diff --git a/lib/QueryRunner.js b/lib/QueryRunner.js index 1cb16b8..fe4fb19 100644 --- a/lib/QueryRunner.js +++ b/lib/QueryRunner.js @@ -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); } diff --git a/lib/index.js b/lib/index.js index a5b6ddd..4ded359 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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; diff --git a/protocols/core.js b/protocols/core.js index 246c660..38f7722 100644 --- a/protocols/core.js +++ b/protocols/core.js @@ -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) => {