mirror of
https://github.com/gamedig/node-gamedig.git
synced 2024-11-17 17:25:19 +01:00
Remove callback support and global Gamedig.debug option
This commit is contained in:
parent
fdc08b5c09
commit
dfa5c95efc
5 changed files with 28 additions and 46 deletions
|
@ -27,8 +27,9 @@ if (argv._.length >= 1) {
|
||||||
options.port = split[1];
|
options.port = split[1];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (debug) {
|
||||||
if(debug) Gamedig.debug = true;
|
options.debug = true;
|
||||||
|
}
|
||||||
|
|
||||||
Gamedig.query(options)
|
Gamedig.query(options)
|
||||||
.then((state) => {
|
.then((state) => {
|
||||||
|
|
|
@ -5,7 +5,7 @@ class GlobalUdpSocket {
|
||||||
constructor() {
|
constructor() {
|
||||||
this.socket = null;
|
this.socket = null;
|
||||||
this.callbacks = new Set();
|
this.callbacks = new Set();
|
||||||
this.debug = false;
|
this.debuggingCallbacks = new Set();
|
||||||
}
|
}
|
||||||
|
|
||||||
_getSocket() {
|
_getSocket() {
|
||||||
|
@ -14,12 +14,18 @@ class GlobalUdpSocket {
|
||||||
udpSocket.unref();
|
udpSocket.unref();
|
||||||
udpSocket.bind();
|
udpSocket.bind();
|
||||||
udpSocket.on('message', (buffer, rinfo) => {
|
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) {
|
for (const cb of this.callbacks) {
|
||||||
cb(rinfo.address, rinfo.port, buffer);
|
cb(fromAddress, fromPort, buffer);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
udpSocket.on('error', (e) => {
|
udpSocket.on('error', (e) => {
|
||||||
if (this.debug) {
|
if (this.debuggingCallbacks.size) {
|
||||||
console.log("UDP ERROR: " + e);
|
console.log("UDP ERROR: " + e);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -31,11 +37,15 @@ class GlobalUdpSocket {
|
||||||
this._getSocket().send(buffer,0,buffer.length,port,address);
|
this._getSocket().send(buffer,0,buffer.length,port,address);
|
||||||
}
|
}
|
||||||
|
|
||||||
addCallback(callback) {
|
addCallback(callback, debug) {
|
||||||
this.callbacks.add(callback);
|
this.callbacks.add(callback);
|
||||||
|
if (debug) {
|
||||||
|
this.debuggingCallbacks.add(callback);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
removeCallback(callback) {
|
removeCallback(callback) {
|
||||||
this.callbacks.delete(callback);
|
this.callbacks.delete(callback);
|
||||||
|
this.debuggingCallbacks.delete(callback);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
const GameResolver = require('./GameResolver'),
|
const GameResolver = require('./GameResolver'),
|
||||||
ProtocolResolver = require('./ProtocolResolver');
|
ProtocolResolver = require('./ProtocolResolver'),
|
||||||
|
GlobalUdpSocket = require('./GlobalUdpSocket');
|
||||||
|
|
||||||
const defaultOptions = {
|
const defaultOptions = {
|
||||||
socketTimeout: 2000,
|
socketTimeout: 2000,
|
||||||
|
@ -8,9 +9,8 @@ const defaultOptions = {
|
||||||
};
|
};
|
||||||
|
|
||||||
class QueryRunner {
|
class QueryRunner {
|
||||||
constructor(udpSocket, debug) {
|
constructor() {
|
||||||
this.debug = debug;
|
this.udpSocket = new GlobalUdpSocket();
|
||||||
this.udpSocket = udpSocket;
|
|
||||||
this.gameResolver = new GameResolver();
|
this.gameResolver = new GameResolver();
|
||||||
this.protocolResolver = new ProtocolResolver();
|
this.protocolResolver = new ProtocolResolver();
|
||||||
}
|
}
|
||||||
|
@ -90,7 +90,7 @@ class QueryRunner {
|
||||||
}
|
}
|
||||||
|
|
||||||
async _attempt(options) {
|
async _attempt(options) {
|
||||||
if (this.debug) {
|
if (options.debug) {
|
||||||
console.log("Running attempt with options:");
|
console.log("Running attempt with options:");
|
||||||
console.log(options);
|
console.log(options);
|
||||||
}
|
}
|
||||||
|
|
35
lib/index.js
35
lib/index.js
|
@ -1,48 +1,23 @@
|
||||||
const QueryRunner = require('./QueryRunner'),
|
const QueryRunner = require('./QueryRunner');
|
||||||
GlobalUdpSocket = require('./GlobalUdpSocket');
|
|
||||||
|
|
||||||
let singleton = null;
|
let singleton = null;
|
||||||
|
|
||||||
class Gamedig {
|
class Gamedig {
|
||||||
constructor() {
|
constructor() {
|
||||||
this.udpSocket = new GlobalUdpSocket();
|
this.queryRunner = new QueryRunner();
|
||||||
this.queryRunner = new QueryRunner(this.udpSocket);
|
|
||||||
this._debug = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
setDebug(on) {
|
|
||||||
this.udpSocket.debug = on;
|
|
||||||
this._debug = on;
|
|
||||||
this.queryRunner.debug = on;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async query(userOptions) {
|
async query(userOptions) {
|
||||||
userOptions.debug |= this._debug;
|
|
||||||
return await this.queryRunner.run(userOptions);
|
return await this.queryRunner.run(userOptions);
|
||||||
}
|
}
|
||||||
|
|
||||||
static getInstance() {
|
static getInstance() {
|
||||||
if (!singleton) {
|
if (!singleton) singleton = new Gamedig();
|
||||||
singleton = new Gamedig();
|
|
||||||
}
|
|
||||||
return singleton;
|
return singleton;
|
||||||
}
|
}
|
||||||
static query(userOptions, callback) {
|
static async query(...args) {
|
||||||
const promise = Gamedig.getInstance().query(userOptions);
|
return await Gamedig.getInstance().query(...args);
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Object.defineProperty(Gamedig, "debug", { set: on => Gamedig.getInstance().setDebug(on) });
|
|
||||||
|
|
||||||
module.exports = Gamedig;
|
module.exports = Gamedig;
|
||||||
|
|
|
@ -312,10 +312,6 @@ class Core extends EventEmitter {
|
||||||
const rtt = end-start;
|
const rtt = end-start;
|
||||||
this.registerRtt(rtt);
|
this.registerRtt(rtt);
|
||||||
}
|
}
|
||||||
this.debugLog(log => {
|
|
||||||
log(fromAddress + ':' + fromPort + " <--UDP");
|
|
||||||
log(HexUtil.debugDump(buffer));
|
|
||||||
});
|
|
||||||
const result = onPacket(buffer);
|
const result = onPacket(buffer);
|
||||||
if (result !== undefined) {
|
if (result !== undefined) {
|
||||||
this.debugLog("UDP send finished by callback");
|
this.debugLog("UDP send finished by callback");
|
||||||
|
@ -325,7 +321,7 @@ class Core extends EventEmitter {
|
||||||
reject(e);
|
reject(e);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
socket.addCallback(socketCallback);
|
socket.addCallback(socketCallback, this.options.debug);
|
||||||
});
|
});
|
||||||
timeout = Promises.createTimeout(this.options.socketTimeout, 'UDP');
|
timeout = Promises.createTimeout(this.options.socketTimeout, 'UDP');
|
||||||
const wrappedTimeout = new Promise((resolve, reject) => {
|
const wrappedTimeout = new Promise((resolve, reject) => {
|
||||||
|
|
Loading…
Reference in a new issue