mirror of
https://github.com/tribufu/node-gamedig
synced 2026-05-06 07:07:33 +00:00
Additional async rewrite
This commit is contained in:
parent
efe12a00aa
commit
29ce0b82d0
24 changed files with 654 additions and 470 deletions
103
lib/QueryRunner.js
Normal file
103
lib/QueryRunner.js
Normal file
|
|
@ -0,0 +1,103 @@
|
|||
const GameResolver = require('./GameResolver'),
|
||||
ProtocolResolver = require('./ProtocolResolver');
|
||||
|
||||
const defaultOptions = {
|
||||
socketTimeout: 2000,
|
||||
attemptTimeout: 10000,
|
||||
maxAttempts: 1
|
||||
};
|
||||
|
||||
class QueryRunner {
|
||||
constructor(udpSocket, debug) {
|
||||
this.debug = debug;
|
||||
this.udpSocket = udpSocket;
|
||||
this.gameResolver = new GameResolver();
|
||||
this.protocolResolver = new ProtocolResolver();
|
||||
}
|
||||
async run(userOptions) {
|
||||
for (const key of Object.keys(userOptions)) {
|
||||
const value = userOptions[key];
|
||||
if (['port'].includes(key)) {
|
||||
userOptions[key] = parseInt(value);
|
||||
}
|
||||
}
|
||||
|
||||
const {
|
||||
port_query: gameQueryPort,
|
||||
port_query_offset: gameQueryPortOffset,
|
||||
...gameOptions
|
||||
} = this.gameResolver.lookup(userOptions.type);
|
||||
const attempts = [];
|
||||
|
||||
if (userOptions.port) {
|
||||
if (gameQueryPortOffset) {
|
||||
attempts.push({
|
||||
...defaultOptions,
|
||||
...gameOptions,
|
||||
...userOptions,
|
||||
port: userOptions.port + gameQueryPortOffset
|
||||
});
|
||||
}
|
||||
if (userOptions.port === gameOptions.port && gameQueryPort) {
|
||||
attempts.push({
|
||||
...defaultOptions,
|
||||
...gameOptions,
|
||||
...userOptions,
|
||||
port: gameQueryPort
|
||||
});
|
||||
}
|
||||
attempts.push({
|
||||
...defaultOptions,
|
||||
...gameOptions,
|
||||
...userOptions
|
||||
});
|
||||
} else if (gameQueryPort) {
|
||||
attempts.push({
|
||||
...defaultOptions,
|
||||
...gameOptions,
|
||||
...userOptions,
|
||||
port: gameQueryPort
|
||||
});
|
||||
} else if (gameOptions.port) {
|
||||
attempts.push({
|
||||
...defaultOptions,
|
||||
...gameOptions,
|
||||
...userOptions
|
||||
});
|
||||
} else {
|
||||
throw new Error("Could not determine port to query. Did you provide a port or gameid?");
|
||||
}
|
||||
|
||||
if (attempts.length === 1) {
|
||||
return await this._attempt(attempts[0]);
|
||||
} else {
|
||||
const errors = [];
|
||||
for (const attempt of attempts) {
|
||||
try {
|
||||
return await this._attempt(attempt);
|
||||
} catch(e) {
|
||||
const e2 = new Error('Failed to query port ' + attempt.port);
|
||||
e2.stack += "\nCaused by:\n" + e.stack;
|
||||
errors.push(e2);
|
||||
}
|
||||
}
|
||||
|
||||
const err = new Error('Failed all port attempts');
|
||||
err.stack = errors.map(e => e.stack).join('\n');
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
async _attempt(options) {
|
||||
if (this.debug) {
|
||||
console.log("Running attempt with options:");
|
||||
console.log(options);
|
||||
}
|
||||
const core = this.protocolResolver.create(options.protocol);
|
||||
core.options = options;
|
||||
core.udpSocket = this.udpSocket;
|
||||
return await core.runAllAttempts();
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = QueryRunner;
|
||||
Loading…
Add table
Add a link
Reference in a new issue