mirror of
https://github.com/tribufu/node-gamedig
synced 2026-05-06 15:17:36 +00:00
Add support for udp bind port override (3.0.5) Fixes #149
This commit is contained in:
parent
68b8dfd684
commit
ce4e728493
8 changed files with 59 additions and 26 deletions
|
|
@ -1,25 +1,29 @@
|
|||
const dgram = require('dgram'),
|
||||
HexUtil = require('./HexUtil'),
|
||||
Logger = require('./Logger');
|
||||
const dgram = require('dgram');
|
||||
const HexUtil = require('./HexUtil');
|
||||
const Logger = require('./Logger');
|
||||
const util = require('util');
|
||||
|
||||
class GlobalUdpSocket {
|
||||
constructor() {
|
||||
constructor({port}) {
|
||||
this.socket = null;
|
||||
this.callbacks = new Set();
|
||||
this.debuggingCallbacks = new Set();
|
||||
this.logger = new Logger();
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
_getSocket() {
|
||||
async _getSocket() {
|
||||
if (!this.socket) {
|
||||
const udpSocket = this.socket = dgram.createSocket('udp4');
|
||||
const udpSocket = dgram.createSocket({
|
||||
type: 'udp4',
|
||||
reuseAddr: true
|
||||
});
|
||||
udpSocket.unref();
|
||||
udpSocket.bind();
|
||||
udpSocket.on('message', (buffer, rinfo) => {
|
||||
const fromAddress = rinfo.address;
|
||||
const fromPort = rinfo.port;
|
||||
this.logger.debug(log => {
|
||||
log(fromAddress + ':' + fromPort + " <--UDP");
|
||||
log(fromAddress + ':' + fromPort + " <--UDP(" + this.port + ")");
|
||||
log(HexUtil.debugDump(buffer));
|
||||
});
|
||||
for (const cb of this.callbacks) {
|
||||
|
|
@ -29,12 +33,22 @@ class GlobalUdpSocket {
|
|||
udpSocket.on('error', e => {
|
||||
this.logger.debug("UDP ERROR:", e);
|
||||
});
|
||||
await util.promisify(udpSocket.bind).bind(udpSocket)(this.port);
|
||||
this.port = udpSocket.address().port;
|
||||
this.socket = udpSocket;
|
||||
}
|
||||
return this.socket;
|
||||
}
|
||||
|
||||
send(buffer, address, port) {
|
||||
this._getSocket().send(buffer,0,buffer.length,port,address);
|
||||
async send(buffer, address, port, debug) {
|
||||
const socket = await this._getSocket();
|
||||
if (debug) {
|
||||
this.logger._print(log => {
|
||||
log(address + ':' + port + " UDP(" + this.port + ")-->");
|
||||
log(HexUtil.debugDump(buffer));
|
||||
});
|
||||
}
|
||||
await util.promisify(socket.send).bind(socket)(buffer,0,buffer.length,port,address);
|
||||
}
|
||||
|
||||
addCallback(callback, debug) {
|
||||
|
|
|
|||
|
|
@ -9,8 +9,10 @@ const defaultOptions = {
|
|||
};
|
||||
|
||||
class QueryRunner {
|
||||
constructor() {
|
||||
this.udpSocket = new GlobalUdpSocket();
|
||||
constructor(runnerOpts = {}) {
|
||||
this.udpSocket = new GlobalUdpSocket({
|
||||
port: runnerOpts.listenUdpPort
|
||||
});
|
||||
this.gameResolver = new GameResolver();
|
||||
this.protocolResolver = new ProtocolResolver();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,8 +3,8 @@ const QueryRunner = require('./QueryRunner');
|
|||
let singleton = null;
|
||||
|
||||
class Gamedig {
|
||||
constructor() {
|
||||
this.queryRunner = new QueryRunner();
|
||||
constructor(runnerOpts) {
|
||||
this.queryRunner = new QueryRunner(runnerOpts);
|
||||
}
|
||||
|
||||
async query(userOptions) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue