Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add proper support for multipacket responses #25

Merged
merged 2 commits into from
Nov 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ const protocol = Object.freeze({
SERVERDATA_RESPONSE_VALUE: 0x00, // Response of SERVERDATA_EXECCOMMAND

ID_AUTH: 0x999,
ID_REQUEST: 0x123
ID_REQUEST: 0x123,
ID_TERM:0x777
})

export default protocol
22 changes: 14 additions & 8 deletions src/rcon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,14 +161,20 @@ class RCON {
}

this.connection.removeListener('data', onData)
} else if (id === decodedPacket.id) {
response = response.concat(decodedPacket.body.replace(/\n$/, '\n')) // remove last line break

// Check the response if it's defined rather than if it contains 'command ${body}'
// Reason for this is because we no longer need to check if it starts with 'command', testing shows it never will
if (response) {
this.connection.removeListener('data', onData)
resolve(response)
} else if (id === decodedPacket.id || decodedPacket.id === protocol.ID_TERM) {
// don't add the termination packet.
if (decodedPacket.id != protocol.ID_TERM) {
response = response.concat(decodedPacket.body.replace(/\n$/, '\n')) // remove last line break
}

// Hack to cope with multipacket responses.
// see https://developer.valvesoftware.com/wiki/Talk:Source_RCON_Protocol#How_to_receive_split_response?
if (decodedPacket.size > 3700) {
let encodedTerminationPacket = packets.encode(protocol.SERVERDATA_RESPONSE_VALUE, protocol.ID_TERM, '');
this.connection.write(encodedTerminationPacket);
} else if (decodedPacket.size <= 3700) { // no need to check for ID_TERM here, since this packet will always be < 3700
this.connection.removeListener('data', onData);
resolve(response);
}
}

Expand Down