Skip to content

Commit

Permalink
Merge pull request #4 from Macil/factorio
Browse files Browse the repository at this point in the history
Add multiPacketResponses option for Factorio compatibility
  • Loading branch information
ribizli authored Feb 19, 2022
2 parents dac8299 + 5f213d5 commit 5e039b4
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions rcon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ class ResolvablePromise {
});
}

export interface Options {
/** Support multi-packet responses from the server. On by default.
* May be turned off in order to read responses from servers that
* don't support it, like Factorio. */
multiPacketResponses: boolean;
}

const defaultOptions: Options = {
multiPacketResponses: true
};

export class Rcon {
private authed?: ResolvablePromise;

Expand All @@ -39,11 +50,16 @@ export class Rcon {

private requests = new Map<number, Request>();

private options: Options;

constructor(
private host = 'localhost',
private port = 27015,
private password = ''
) {}
private password = '',
options: Partial<Options> = {}
) {
this.options = {...defaultOptions, ...options};
}

sendCmd(cmd: string) {
return this.send(cmd, PacketType.COMMAND);
Expand Down Expand Up @@ -120,7 +136,11 @@ export class Rcon {

const request = this.requests.get(id);
if (request) {
if (equals(FrameBuffer, payload)) {
if (!this.options.multiPacketResponses) {
const str = new TextDecoder().decode(payload);
request.resolve(str);
this.requests.delete(id);
} else if (equals(FrameBuffer, payload)) {
const str = new TextDecoder().decode(request.data);
request.resolve(str);
this.requests.delete(id);
Expand Down

0 comments on commit 5e039b4

Please sign in to comment.