From a77850f8a5116d7c50d536ece179e6ba10a5081f Mon Sep 17 00:00:00 2001 From: Adrian Wann Date: Wed, 6 Nov 2024 15:08:14 +1100 Subject: [PATCH 1/9] Bump version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 9462a38..8f83403 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "nuclearnet.js", - "version": "1.7.1", + "version": "1.7.2", "description": "Node.js module for interacting with the NUClear network", "main": "index.js", "types": "index.d.ts", From c2f77fc33c134dd187e84b2b9d9208f2a26a87b6 Mon Sep 17 00:00:00 2001 From: Adrian Wann Date: Wed, 6 Nov 2024 15:09:03 +1100 Subject: [PATCH 2/9] Revert "Bump version" This reverts commit a77850f8a5116d7c50d536ece179e6ba10a5081f. --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 8f83403..9462a38 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "nuclearnet.js", - "version": "1.7.2", + "version": "1.7.1", "description": "Node.js module for interacting with the NUClear network", "main": "index.js", "types": "index.d.ts", From 0cd944399f51500a7df1c17f83ac70852d73a4fb Mon Sep 17 00:00:00 2001 From: Adrian Wann Date: Mon, 26 May 2025 15:05:25 +1000 Subject: [PATCH 3/9] Add disconnect event --- index.d.ts | 3 +++ index.js | 8 +++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index 3a1a047..7d294c6 100644 --- a/index.d.ts +++ b/index.d.ts @@ -140,6 +140,9 @@ export declare class NUClearNet { /** Emitted when NUClearNet receives any packet */ public on(event: 'nuclear_packet', callback: (packet: NUClearNetMaybeTypedPacket) => void): this; + /** Emitted when connection to the network is lost */ + public on(event: 'disconnect', callback: () => void): this; + /** Emitted when the given packet is received */ public on(event: string, callback: (packet: NUClearNetTypedPacket) => void): this; diff --git a/index.js b/index.js index f98d7d7..ffbd998 100644 --- a/index.js +++ b/index.js @@ -124,7 +124,11 @@ class NUClearNet extends EventEmitter { // Only process if we're active if (this._active) { - this._net.process(); + try { + this._net.process(); + } catch (err) { + this.disconnect(); + } } // Sometimes due to weird timing artifacts we run out of these @@ -165,6 +169,8 @@ class NUClearNet extends EventEmitter { this._active = false; this._net.shutdown(); + + this.emit('disconnect'); } send(options) { From bcb497a74b1a262880a321ab5682ea90150eca66 Mon Sep 17 00:00:00 2001 From: Adrian Wann Date: Mon, 26 May 2025 15:13:07 +1000 Subject: [PATCH 4/9] Only trigger auto disconnect once --- index.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index ffbd998..99342af 100644 --- a/index.js +++ b/index.js @@ -127,7 +127,9 @@ class NUClearNet extends EventEmitter { try { this._net.process(); } catch (err) { - this.disconnect(); + if (this._active) { + this.disconnect(); + } } } From 90b162151e1122380c42d52908badadb460a2884 Mon Sep 17 00:00:00 2001 From: Adrian Wann Date: Tue, 27 May 2025 09:21:11 +1000 Subject: [PATCH 5/9] Fix repeated disconnect attempts on fail --- index.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 99342af..24aa45c 100644 --- a/index.js +++ b/index.js @@ -127,6 +127,8 @@ class NUClearNet extends EventEmitter { try { this._net.process(); } catch (err) { + // An error occurred during processing, disconnect. + // Check if active again, since process happens asynchronously. if (this._active) { this.disconnect(); } @@ -159,11 +161,13 @@ class NUClearNet extends EventEmitter { const mtu = options.mtu === undefined ? 1500 : options.mtu; // Connect to the network - this._active = true; this._net.reset(name, address, port, mtu); // Run our first "process" to kick things off this._net.process(); + + // If the first process is successful we are active + this._active = true; } disconnect() { From 75861889ae266d8c0b9a3dd0374c82a33dfa4c58 Mon Sep 17 00:00:00 2001 From: Adrian Wann Date: Tue, 27 May 2025 09:47:45 +1000 Subject: [PATCH 6/9] Check for actual error --- index.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 24aa45c..4a8c3d6 100644 --- a/index.js +++ b/index.js @@ -127,10 +127,12 @@ class NUClearNet extends EventEmitter { try { this._net.process(); } catch (err) { - // An error occurred during processing, disconnect. - // Check if active again, since process happens asynchronously. - if (this._active) { - this.disconnect(); + if (err instanceof Error) { + // An error occurred during processing, disconnect. + // Check if active again, since process happens asynchronously. + if (this._active) { + this.disconnect(); + } } } } From 11c6521e4ca1053f0e1c8b02d5a7a3adfccf9e75 Mon Sep 17 00:00:00 2001 From: Adrian Wann Date: Tue, 27 May 2025 11:52:40 +1000 Subject: [PATCH 7/9] Don't hash the disconnect event --- index.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/index.js b/index.js index 4a8c3d6..6d393f2 100644 --- a/index.js +++ b/index.js @@ -44,6 +44,7 @@ class NUClearNet extends EventEmitter { event !== 'nuclear_packet' && event !== 'newListener' && event !== 'removeListener' && + event !== 'disconnect' && this.listenerCount(event) === 0 ) { const hash = this._net.hash(event); @@ -60,6 +61,7 @@ class NUClearNet extends EventEmitter { event !== 'nuclear_packet' && event !== 'newListener' && event !== 'removeListener' && + event !== 'disconnect' && this.listenerCount(event) === 0 ) { // Get our hash and delete it From 52be374fa1d1373d124886f1781a0c8b7e666ad9 Mon Sep 17 00:00:00 2001 From: Adrian Wann Date: Tue, 27 May 2025 11:58:16 +1000 Subject: [PATCH 8/9] Maybe don't get the error? --- index.js | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 6d393f2..7185935 100644 --- a/index.js +++ b/index.js @@ -128,13 +128,11 @@ class NUClearNet extends EventEmitter { if (this._active) { try { this._net.process(); - } catch (err) { - if (err instanceof Error) { - // An error occurred during processing, disconnect. - // Check if active again, since process happens asynchronously. - if (this._active) { - this.disconnect(); - } + } catch { + // An error occurred during processing, disconnect. + // Check if active again, since process happens asynchronously. + if (this._active) { + this.disconnect(); } } } From fa5017cbe63dd05a18c3aa6a14c7697679263237 Mon Sep 17 00:00:00 2001 From: Adrian Wann Date: Tue, 27 May 2025 15:03:44 +1000 Subject: [PATCH 9/9] Update comment --- index.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 7185935..3794087 100644 --- a/index.js +++ b/index.js @@ -130,7 +130,9 @@ class NUClearNet extends EventEmitter { this._net.process(); } catch { // An error occurred during processing, disconnect. - // Check if active again, since process happens asynchronously. + // This needs to check again if this is still active, as multiple + // `_onWait` calls run concurrently, and only the first one to fail + // should disconnect. if (this._active) { this.disconnect(); }