mirror of
https://github.com/EchoSixHIYA/WebSpeak-client-for-TeamSpeak.git
synced 2026-12-03 12:02:50 +08:00
Merge pull request #33 from LainHE/fix/acceleration-relay-reconnect-upstream
fix(acceleration-relay): rebuild a self-closed tunnel on reconnect
This commit is contained in:
3 files changed
+78
No files matched your search
@@ -40,6 +40,14 @@ export interface AccelerationRelayClient {
|
||||
readonly localHost: string;
|
||||
readonly localPort: number;
|
||||
readonly sessionId: number;
|
||||
/**
|
||||
* True once the tunnel has torn itself down, whether by {@link close} or on
|
||||
* its own after a socket error, a relay OPEN_ERR/CLOSE frame, or an open
|
||||
* timeout. Callers that route a transport through this tunnel must watch this
|
||||
* flag: after it flips, every sendFrame() call is a silent no-op and both UDP
|
||||
* sockets are closed, so the tunnel can never recover on its own.
|
||||
*/
|
||||
isClosed(): boolean;
|
||||
close(): void;
|
||||
}
|
||||
|
||||
@@ -150,6 +158,7 @@ export async function createAccelerationRelayClient(
|
||||
localHost: "127.0.0.1",
|
||||
localPort: typeof address === "object" ? address.port : 0,
|
||||
sessionId,
|
||||
isClosed: (): boolean => closed,
|
||||
close(): void {
|
||||
if (!closed && ready) {
|
||||
try { sendFrame(FrameType.CLOSE); } catch { /* best effort */ }
|
||||
|
||||
@@ -410,3 +410,47 @@ for (const [name, payload] of retiredEvents) {
|
||||
assert.equal(observed.length, 1);
|
||||
});
|
||||
}
|
||||
|
||||
interface RelayFixtureState {
|
||||
accelerationClient: { isClosed(): boolean; close(): void } | null;
|
||||
adapter: { disconnect(): Promise<void> } | null;
|
||||
client: SdkStub | null;
|
||||
dropStaleAccelerationTransport(): void;
|
||||
}
|
||||
|
||||
function relayFixture(tunnelClosed: boolean) {
|
||||
const client = new TSClient({
|
||||
target: { host: "voice.example.invalid", port: 9987 },
|
||||
nickname: "Test",
|
||||
acceleration: { relayHost: "127.0.0.1", relayPort: 39087, token: "test-acceleration-relay-token" },
|
||||
}, pino({ enabled: false }));
|
||||
let disconnectCalls = 0;
|
||||
let closeCalls = 0;
|
||||
const state = client as unknown as RelayFixtureState;
|
||||
state.accelerationClient = {
|
||||
isClosed: () => tunnelClosed,
|
||||
close: () => { closeCalls++; },
|
||||
};
|
||||
state.adapter = { disconnect: async () => { disconnectCalls++; } };
|
||||
state.client = new SdkStub();
|
||||
return { client, state, counts: () => ({ disconnectCalls, closeCalls }) };
|
||||
}
|
||||
|
||||
test("a self-closed relay tunnel is dropped with its adapter so connect() rebuilds both", () => {
|
||||
// Regression: reconnecting reused the adapter bound to the dead tunnel's local
|
||||
// port. Every frame the tunnel sent was a silent no-op, so auto-reconnect
|
||||
// could never recover and nothing reported an error.
|
||||
const f = relayFixture(true);
|
||||
f.state.dropStaleAccelerationTransport();
|
||||
assert.equal(f.state.accelerationClient, null, "the dead tunnel must not be reused");
|
||||
assert.equal(f.state.adapter, null, "the adapter bound to the dead local port must be dropped");
|
||||
assert.equal(f.state.client, null, "the SDK client for the dead transport must be dropped");
|
||||
});
|
||||
|
||||
test("a live relay tunnel survives the reconnect guard untouched", () => {
|
||||
const f = relayFixture(false);
|
||||
f.state.dropStaleAccelerationTransport();
|
||||
assert.notEqual(f.state.accelerationClient, null, "an open tunnel stays usable");
|
||||
assert.notEqual(f.state.adapter, null, "an open tunnel's adapter stays usable");
|
||||
assert.notEqual(f.state.client, null, "an open tunnel's SDK client stays usable");
|
||||
});
|
||||
@@ -127,6 +127,25 @@ export class TSClient extends EventEmitter {
|
||||
this.identity = options.identity ?? generateIdentity(8);
|
||||
}
|
||||
|
||||
/**
|
||||
* Discards an acceleration tunnel that closed itself (socket error, relay
|
||||
* OPEN_ERR/CLOSE, or open timeout) together with the adapter and SDK client
|
||||
* bound to its now-dead local port, so the next connect() builds all three
|
||||
* from scratch. A closed tunnel silently drops every frame, so reusing the
|
||||
* adapter would make reconnection fail forever without an error.
|
||||
*/
|
||||
private dropStaleAccelerationTransport(): void {
|
||||
if (!this.accelerationClient?.isClosed()) return;
|
||||
const staleAdapter = this.adapter;
|
||||
// Detach before teardown: every SDK listener bails when it no longer owns
|
||||
// this.client, so a late "disconnected" from the stale transport cannot
|
||||
// re-enter connect() through the reconnect path.
|
||||
this.accelerationClient = null;
|
||||
this.adapter = null;
|
||||
this.client = null;
|
||||
try { void staleAdapter?.disconnect().catch(() => undefined); } catch { /* already gone */ }
|
||||
}
|
||||
|
||||
async connect(): Promise<void> {
|
||||
this.connectionGeneration++;
|
||||
this.inputState = { muted: false, accompaniment: false };
|
||||
@@ -135,6 +154,12 @@ export class TSClient extends EventEmitter {
|
||||
this.selfLeaveReasonId = null;
|
||||
this.channelCapacities.clear();
|
||||
this.channelPasswordProtection.clear();
|
||||
// A relay tunnel that tore itself down (socket error, OPEN_ERR/CLOSE frame,
|
||||
// open timeout) reports nothing and no-ops every send, while adapter and
|
||||
// client still look reusable. Reconnecting through the dead local port would
|
||||
// silently fail forever, so drop the whole stack and let the block below
|
||||
// build a fresh tunnel plus adapter.
|
||||
this.dropStaleAccelerationTransport();
|
||||
if (!this.adapter || !this.client) {
|
||||
let transportTarget = this.options.target;
|
||||
if (this.options.acceleration && !this.accelerationClient) {
|
||||
|
||||
Reference in new issue
Block a user