Skip to content

Commit

Permalink
Merge pull request #475 from Sadzurami/fix/uncaught-exceptions-with-s…
Browse files Browse the repository at this point in the history
…team-messages

Fix uncaught exceptions when reading and writing messages via TCP
  • Loading branch information
DoctorMcKay authored Sep 3, 2024
2 parents 3112479 + 2e64a1a commit f59af6d
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions components/connection_protocols/tcp.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,21 @@ class TCPConnection extends BaseConnection {
data = SteamCrypto.symmetricEncryptWithHmacIv(data, this.sessionKey);
}

if (!this.stream) {
this._debug('Tried to send message, but there is no stream');
return;
}

let buf = Buffer.alloc(4 + 4 + data.length);
buf.writeUInt32LE(data.length, 0);
buf.write(MAGIC, 4);
data.copy(buf, 8);
this.stream.write(buf);

try {
this.stream.write(buf);
} catch (error) {
this._debug('Error writing to socket: ' + error.message);
}
}

/**
Expand All @@ -189,7 +199,18 @@ class TCPConnection extends BaseConnection {
}
}

let message = this.stream.read(this._messageLength);
if (!this.stream) {
this._debug('Tried to read message, but there is no stream');
return;
}

let message;
try {
message = this.stream.read(this._messageLength);
} catch (error) {
this._debug('Error reading from socket: ' + error.message);
}

if (!message) {
this._debug('Got incomplete message; expecting ' + this._messageLength + ' more bytes');
return;
Expand Down

0 comments on commit f59af6d

Please sign in to comment.