-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
no longer reuse UDP sockets, since that seems to break randomly in so…
…me setups
- Loading branch information
1 parent
4b6effc
commit 2bdf979
Showing
9 changed files
with
101 additions
and
222 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,16 @@ | ||
import UDPClient from "../UDPSocket/UDPClient.js"; | ||
import UnconnectedPing from "../Packet/BedrockPing/UnconnectedPing.js"; | ||
import UnconnectedPong from "../Packet/BedrockPing/UnconnectedPong.js"; | ||
import * as crypto from "node:crypto"; | ||
|
||
export default class BedrockPing extends UDPClient { | ||
/** @type {BigInt} */ sessionId; | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
appliesTo(message) { | ||
let data = message.getData(); | ||
if (data.byteLength < 9) { | ||
return false; | ||
} | ||
|
||
let timestamp = data.readBigInt64BE(1); | ||
return timestamp === this.sessionId; | ||
} | ||
|
||
/** | ||
* @return {Promise<UnconnectedPong>} | ||
*/ | ||
async ping() { | ||
// Normally, the time field is used for the current ms timestamp, but we're using it as a session ID | ||
// to identify which reply belongs to which request since we're using the same socket for multiple requests. | ||
this.sessionId = crypto.randomBytes(8).readBigInt64BE(); | ||
let startTime = BigInt(Date.now()); | ||
await this.send(new UnconnectedPing().setTime(this.sessionId).generateClientGUID().write()); | ||
await this.sendPacket(new UnconnectedPing().setTime(startTime).generateClientGUID()); | ||
this.signal?.throwIfAborted(); | ||
|
||
// The time field in the response contains the session ID, but we replace it with the start time | ||
// in case anyone relies on the time field containing an actual timestamp. | ||
return new UnconnectedPong().read(await this.readData()).setTime(startTime); | ||
return new UnconnectedPong().read(await this.readData()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,36 @@ | ||
import UDPSocket from "../UDPSocket/UDPSocket.js"; | ||
import BedrockPing from "./BedrockPing.js"; | ||
|
||
export default class BedrockPingClient extends UDPSocket { | ||
export default class BedrockPingClient { | ||
/** @type {import("node:dgram").SocketOptions}} */ socketOptions; | ||
/** @type {import("node:dgram").BindOptions}} */ bindOptions; | ||
|
||
/** | ||
* @param {import("node:dgram").SocketOptions} socketOptions | ||
* @param {import("node:dgram").BindOptions} bindOptions | ||
*/ | ||
constructor(socketOptions = {type: "udp4"}, bindOptions = {}) { | ||
this.socketOptions = socketOptions; | ||
this.bindOptions = bindOptions; | ||
} | ||
|
||
/** | ||
* @param {string} address | ||
* @param {number} port | ||
* @param {?AbortSignal} signal | ||
* @return {Promise<UnconnectedPong>} | ||
*/ | ||
async ping(address, port, signal = null) { | ||
let ping = new BedrockPing(address, port, this, signal); | ||
await ping.connect(); | ||
let ping = new BedrockPing(address, port, signal, this.socketOptions, this.bindOptions); | ||
await ping.bind(signal); | ||
let result; | ||
try { | ||
result = await ping.ping(); | ||
} catch (e) { | ||
ping.close(); | ||
await ping.close(); | ||
throw e; | ||
} | ||
ping.close(); | ||
await ping.close(); | ||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.