mirror of
https://github.com/tribufu/node-gamedig
synced 2026-06-01 09:42:41 +00:00
Additional async rewrite
This commit is contained in:
parent
efe12a00aa
commit
29ce0b82d0
24 changed files with 654 additions and 470 deletions
42
lib/GlobalUdpSocket.js
Normal file
42
lib/GlobalUdpSocket.js
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
const dgram = require('dgram'),
|
||||
HexUtil = require('./HexUtil');
|
||||
|
||||
class GlobalUdpSocket {
|
||||
constructor() {
|
||||
this.socket = null;
|
||||
this.callbacks = new Set();
|
||||
this.debug = false;
|
||||
}
|
||||
|
||||
_getSocket() {
|
||||
if (!this.socket) {
|
||||
const udpSocket = this.socket = dgram.createSocket('udp4');
|
||||
udpSocket.unref();
|
||||
udpSocket.bind();
|
||||
udpSocket.on('message', (buffer, rinfo) => {
|
||||
for (const cb of this.callbacks) {
|
||||
cb(rinfo.address, rinfo.port, buffer);
|
||||
}
|
||||
});
|
||||
udpSocket.on('error', (e) => {
|
||||
if (this.debug) {
|
||||
console.log("UDP ERROR: " + e);
|
||||
}
|
||||
});
|
||||
}
|
||||
return this.socket;
|
||||
}
|
||||
|
||||
send(buffer, address, port) {
|
||||
this._getSocket().send(buffer,0,buffer.length,port,address);
|
||||
}
|
||||
|
||||
addCallback(callback) {
|
||||
this.callbacks.add(callback);
|
||||
}
|
||||
removeCallback(callback) {
|
||||
this.callbacks.delete(callback);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = GlobalUdpSocket;
|
||||
Loading…
Add table
Add a link
Reference in a new issue