Skip to content

Commit

Permalink
lints
Browse files Browse the repository at this point in the history
  • Loading branch information
AnyBananaGAME committed Feb 19, 2025
1 parent b2c6be9 commit d99f5c2
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 118 deletions.
132 changes: 77 additions & 55 deletions src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ const TICK_INTERVAL = 50;
const REQUEST_INTERVAL = 50;

export class Client extends Emitter<ClientEvents> {
public socket!: Socket;
public framer!: Framer;
public socket!: Socket | null;
public framer!: Framer | null;
public options: ClientOptions;
private tickTimer?: NodeJS.Timeout;
private connectionTimeout?: NodeJS.Timeout;
private requestInterval?: NodeJS.Timeout;
public serverAddress!: Address;
public serverAddress!: Address | null;

public status = Status.Disconnected;

Expand All @@ -45,12 +45,12 @@ export class Client extends Emitter<ClientEvents> {
this.socket = createSocket("udp4");
this.framer = new Framer(this);

this.remove("tick", () => this.framer.tick());
this.on("tick", () => this.framer.tick());
this.remove("tick", () => this.framer?.tick());
this.on("tick", () => this.framer?.tick());

this.socket.removeAllListeners("message");
this.socket.removeAllListeners();
this.socket.on("message", (payload, rinfo) => {
this.framer.incommingMessage(payload, rinfo);
this.framer?.incommingMessage(payload, rinfo);
});

this.socket.on("error", (err) => {
Expand Down Expand Up @@ -171,6 +171,10 @@ export class Client extends Emitter<ClientEvents> {

public sendFrame(frame: Frame, priority: Priority): void {
try {
if (!this.framer) {
Logger.error("[Client] Cannot send frame: framer is null");
return;
}
this.framer.sendFrame(frame, priority);
} catch (error) {
Logger.error("[Client] Failed to send frame", error);
Expand All @@ -190,6 +194,11 @@ export class Client extends Emitter<ClientEvents> {
return;
}

if (!this.socket) {
Logger.error("[Client] Cannot send packet: socket is null");
return;
}

Logger.debug(
`[Client] Sending packet ${buffer[0]}, ${buffer.length} bytes to ${this.options.address}:${this.options.port}`,
);
Expand All @@ -212,6 +221,62 @@ export class Client extends Emitter<ClientEvents> {
}
}

private cleanupSocket() {
if (!this.socket) return;
try {
this.socket.removeAllListeners();
Logger.cleanup();
if (this.status === Status.Connected) {
const disconnect = new DisconnectionNotification();
this.socket.send(
disconnect.serialize(),
0,
disconnect.serialize().length,
this.options.port,
this.options.address,
);
}

const stateSymbol = Symbol.for("state symbol");
const socketWithState = this.socket as unknown as {
[key: symbol]: { handle: { close: () => void } | null };
};
const state = socketWithState[stateSymbol];
if (state?.handle) {
state.handle.close();
state.handle = null;
}

this.socket.close(() => {
console.log("socket closed");
this.socket?.removeAllListeners();
});

// (this.socket as { _handle?: unknown })._handle = undefined;
} catch (err) {
Logger.error("[Client] Error during socket cleanup", err as Error);
try {
const stateSymbol = Symbol.for("state symbol");
const socketWithState = this.socket as unknown as {
[key: symbol]: { handle: { close: () => void } | null };
};
const state = socketWithState[stateSymbol];
if (state?.handle) {
state.handle.close();
state.handle = null;
}
} catch (_) {}
this.socket = null;
}
}

private cleanupFramer() {
if (!this.framer) return;
(this.framer as { _events?: unknown })._events = undefined;
(this.framer as { _eventsCount?: unknown })._eventsCount = undefined;
this.framer = null;
}

public cleanup(): void {
if (this.status === Status.Disconnected) return;

Expand All @@ -237,56 +302,13 @@ export class Client extends Emitter<ClientEvents> {
this.removeAll();
this.removeAllAfter();
this.removeAllBefore();

if (this.socket) {
try {
this.socket.removeAllListeners();
Logger.cleanup();
if (wasConnected) {
const disconnect = new DisconnectionNotification();
this.socket.send(
disconnect.serialize(),
0,
disconnect.serialize().length,
this.options.port,
this.options.address
);
}

const state = (this.socket as any)[Symbol.for('state symbol')];
if (state?.handle) {
state.handle.close();
state.handle = null;
}

this.socket.close(() => {
console.log("socket closed");
this.socket.removeAllListeners();
});

delete (this.socket as any)._handle;
} catch (err) {
Logger.error("[Client] Error during socket cleanup", err as Error);
try {
const state = (this.socket as any)[Symbol.for('state symbol')];
if (state?.handle) {
state.handle.close();
state.handle = null;
}
} catch (_) {}
this.socket = undefined as any;
}
}

if (this.framer) {
delete (this.framer as any)._events;
delete (this.framer as any)._eventsCount;
this.framer = undefined as any;
}
this.cleanupSocket();
this.cleanupFramer();

this.serverAddress = undefined as any;
delete (this as any)._events;
delete (this as any)._eventsCount;
this.serverAddress = null;
(this as { _events?: unknown })._events = undefined;
(this as { _eventsCount?: unknown })._eventsCount = undefined;
this.status = Status.Disconnected;

Logger.debug("[Client] Cleanup complete");
Expand Down
Loading

0 comments on commit d99f5c2

Please sign in to comment.