Skip to content

Commit

Permalink
Prevent redundant connection attempts
Browse files Browse the repository at this point in the history
  • Loading branch information
Hal-9k1 committed Feb 13, 2025
1 parent 3417470 commit 516a42e
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/main/network/RuntimeComms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,6 @@ export default class RuntimeComms {
*/
disconnect() {
this.#tcpDisconnected = true; // Don't reconnect
if (this.#pingInterval) {
clearInterval(this.#pingInterval);
this.#pingInterval = null;
}
this.#disconnectTcp();
}

Expand All @@ -153,6 +149,7 @@ export default class RuntimeComms {
} catch {
return false;
}
this.#disconnectTcp();
this.#connectTcp(); // Reconnect TCP
return true;
}
Expand Down Expand Up @@ -220,6 +217,9 @@ export default class RuntimeComms {
* most recently known Runtime IP and port.
*/
#connectTcp() {
if (this.#tcpSock && (this.#tcpSock.connecting || !this.#tcpSock.pending)) {
return; // Already trying to connect
}
this.#tcpDisconnected = false;
this.#disconnectTcp();
const tcpStream = new PacketStream().on(
Expand Down Expand Up @@ -248,6 +248,10 @@ export default class RuntimeComms {
this.#tcpSock.resetAndDestroy();
this.#tcpSock = null;
}
if (this.#pingInterval) {
clearInterval(this.#pingInterval);
this.#pingInterval = null;
}
}

/**
Expand Down Expand Up @@ -335,6 +339,7 @@ export default class RuntimeComms {
*/
#handleTcpClose() {
this.#commsListener.onRuntimeDisconnect();
this.#disconnectTcp();
if (!this.#tcpDisconnected) {
setTimeout(this.#connectTcp.bind(this), TCP_RECONNECT_DELAY);
}
Expand Down

0 comments on commit 516a42e

Please sign in to comment.