Skip to content

Commit

Permalink
more events
Browse files Browse the repository at this point in the history
  • Loading branch information
AnyBananaGAME committed Jan 13, 2025
1 parent 5d0808e commit 64f04b4
Show file tree
Hide file tree
Showing 13 changed files with 217 additions and 168 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"name": "@sanctumterra/raknet",
"version": "1.3.68",
"version": "1.3.69",
"description": "",
"main": "dist/index.js",
"scripts": {
"build": "tsc",
"watch": "tsc --watch",
"format": "npx @biomejs/biome format ./src",
"lint": "npx @biomejs/biome lint ./src",
"lint-write": "npx @biomejs/biome lint --write ./src",
Expand Down
2 changes: 2 additions & 0 deletions src/client/client-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ type ClientOptions = {
clientId: bigint;
debug: boolean;
timeout: number;
loggerDisabled: boolean;
};

const defaultClientOptions: ClientOptions = {
Expand All @@ -16,6 +17,7 @@ const defaultClientOptions: ClientOptions = {
clientId: BigInt(Math.floor(Math.random() * 1000000000000000000)),
debug: false,
timeout: 5000,
loggerDisabled: false,
};

export { type ClientOptions, defaultClientOptions };
2 changes: 2 additions & 0 deletions src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ export class Client extends Emitter<ClientEvents> {
this.socket.on("message", (payload, rinfo) => {
this.framer.incommingMessage(payload, rinfo);
});
Logger.disabled = this.options.loggerDisabled;

} catch (error) {
Logger.error(`Failed to create socket: ${error}`);
}
Expand Down
26 changes: 26 additions & 0 deletions src/client/framer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import type { Client } from "./client";
import { BinaryStream } from "@serenityjs/binarystream";
import { ConnectionRequestAccepted } from "../proto/packets/connection-request-accepted";
import type { RemoteInfo } from "node:dgram";
import DisconnectionNotification from "../proto/packets/disconnect";

export class Framer {
private client: Client;
Expand All @@ -44,6 +45,7 @@ export class Framer {
protected outputReliableIndex = 0;
protected outputFrames = new Set<Frame>();
public outputBackup = new Map<number, Frame[]>();
public _tickCount = 0;

constructor(client: Client) {
this.client = client;
Expand All @@ -58,6 +60,7 @@ export class Framer {
}

public tick() {
this._tickCount++;
if (
this.client.status === Status.Disconnected ||
this.client.status === Status.Disconnecting
Expand All @@ -80,6 +83,12 @@ export class Framer {
this.client.send(pk.serialize());
}

if (this._tickCount % 10 === 0) {
const ping = new ConnectedPing();
ping.timestamp = BigInt(Date.now());
this.frameAndSend(ping.serialize(), Priority.Normal);
}

this.sendQueue(this.outputFrames.size);
}

Expand Down Expand Up @@ -150,6 +159,9 @@ export class Framer {
this.client.framer.handle(frameset);
break;
}
default: {
Logger.error(`Unknown packet header: ${header}`);
}
}
}

Expand Down Expand Up @@ -179,6 +191,13 @@ export class Framer {
SystemAddress.count = 0;
break;
}
case Packet.DisconnectionNotification: {
const packet = new DisconnectionNotification(
frame.payload,
).deserialize();
this.client.cleanup();
break;
}
}
}

Expand All @@ -197,6 +216,13 @@ export class Framer {
this.client.emit("encapsulated", frame.payload);
break;
}
case Packet.DisconnectionNotification: {
const packet = new DisconnectionNotification(
frame.payload,
).deserialize();
this.client.cleanup();
break;
}
}
}
}
Expand Down
1 change: 0 additions & 1 deletion src/proto/decorators/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export function Create(id: number) {
if (!properties.includes("serialize")) {
target.prototype.serialize = function () {
this.clear();
if (id < 1) throw new Error("Packet ID cannot be less than 1.");
if (id <= 255) this.writeUint8(id);
else if (id <= 65535) this.writeUint16(id);
else if (id <= 4294967295) this.writeUint32(id);
Expand Down
Loading

0 comments on commit 64f04b4

Please sign in to comment.