diff --git a/deno.json b/deno.json index 94ff92b..b07ece0 100644 --- a/deno.json +++ b/deno.json @@ -6,5 +6,8 @@ "@std/io": "jsr:@std/io@^0.225.0" }, "version": "0.0.2", - "exports": "./mod.ts" + "exports": "./mod.ts", + "publish": { + "include": ["README.md", "mod.ts", "src/"] + } } diff --git a/src/rcon.ts b/src/rcon.ts index 64c11ed..c9c84aa 100644 --- a/src/rcon.ts +++ b/src/rcon.ts @@ -1,6 +1,6 @@ import protocol from "./protocol.ts"; -import { iterateReader } from "@std/io"; import { concat } from "@std/bytes"; +import { createConnection, type Socket } from "node:net"; import { encode, decode } from "./packet.ts"; import { NotAuthenticatedException, @@ -34,7 +34,7 @@ import type { RconOptions } from "./types.ts"; export default class Rcon { #host: string; #port: number; - #connection?: Deno.Conn; + #connection?: Socket; #connected = false; #authenticated = false; #maxPacketSize = 4096; @@ -74,7 +74,7 @@ export default class Rcon { */ public async authenticate(password: string): Promise { if (!this.#connected) { - await this.#connect(); + this.#connect(); } const response = await this.#send( @@ -116,16 +116,17 @@ export default class Rcon { public disconnect() { this.#authenticated = false; this.#connected = false; - this.#connection?.close(); + this.#connection?.end(); } /** * Connects to the SRCDS server */ - async #connect() { - this.#connection = await Deno.connect({ - hostname: this.#host, + #connect() { + this.#connection = createConnection({ + host: this.#host, port: this.#port, + timeout: 1000, }); this.#connected = true; @@ -144,12 +145,16 @@ export default class Rcon { throw new PacketSizeTooBigException(); } - await this.#connection!.write(encodedPacket); + this.#connection!.write(encodedPacket); let potentialMultiPacketResponse = new Uint8Array(); - for await (const response of iterateReader(this.#connection!)) { - const decodedPacket = decode(response); + const socketIterator = this.#connection![Symbol.asyncIterator](); + + while (true) { + const { value } = await socketIterator.next(); + + const decodedPacket = decode(value); if (decodedPacket.size < 10) { throw new UnableToParseResponseException(); @@ -190,14 +195,12 @@ export default class Rcon { "" ); - await this.#connection!.write(encodedTerminationPacket); + this.#connection!.write(encodedTerminationPacket); } else if (decodedPacket.size <= 3700) { // no need to check for ID_TERM here, since this packet will always be < 3700 return new TextDecoder().decode(potentialMultiPacketResponse); } } } - - throw new Error("Unreachable"); } }