Skip to content

Commit 38e51aa

Browse files
authored
Add proper support for multipacket responses (#25)
* Add ID for termination packets This adds a static ID for empty packets, that are sent to find the end of a multipacket response * Add hack for multipacket answers
1 parent a143926 commit 38e51aa

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

src/protocol.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ const protocol = Object.freeze({
1111
SERVERDATA_RESPONSE_VALUE: 0x00, // Response of SERVERDATA_EXECCOMMAND
1212

1313
ID_AUTH: 0x999,
14-
ID_REQUEST: 0x123
14+
ID_REQUEST: 0x123,
15+
ID_TERM:0x777
1516
})
1617

1718
export default protocol

src/rcon.ts

+14-8
Original file line numberDiff line numberDiff line change
@@ -161,14 +161,20 @@ class RCON {
161161
}
162162

163163
this.connection.removeListener('data', onData)
164-
} else if (id === decodedPacket.id) {
165-
response = response.concat(decodedPacket.body.replace(/\n$/, '\n')) // remove last line break
166-
167-
// Check the response if it's defined rather than if it contains 'command ${body}'
168-
// Reason for this is because we no longer need to check if it starts with 'command', testing shows it never will
169-
if (response) {
170-
this.connection.removeListener('data', onData)
171-
resolve(response)
164+
} else if (id === decodedPacket.id || decodedPacket.id === protocol.ID_TERM) {
165+
// don't add the termination packet.
166+
if (decodedPacket.id != protocol.ID_TERM) {
167+
response = response.concat(decodedPacket.body.replace(/\n$/, '\n')) // remove last line break
168+
}
169+
170+
// Hack to cope with multipacket responses.
171+
// see https://developer.valvesoftware.com/wiki/Talk:Source_RCON_Protocol#How_to_receive_split_response?
172+
if (decodedPacket.size > 3700) {
173+
let encodedTerminationPacket = packets.encode(protocol.SERVERDATA_RESPONSE_VALUE, protocol.ID_TERM, '');
174+
this.connection.write(encodedTerminationPacket);
175+
} else if (decodedPacket.size <= 3700) { // no need to check for ID_TERM here, since this packet will always be < 3700
176+
this.connection.removeListener('data', onData);
177+
resolve(response);
172178
}
173179
}
174180

0 commit comments

Comments
 (0)