Skip to content

Commit 64f04b4

Browse files
committed
more events
1 parent 5d0808e commit 64f04b4

13 files changed

+217
-168
lines changed

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
{
22
"name": "@sanctumterra/raknet",
3-
"version": "1.3.68",
3+
"version": "1.3.69",
44
"description": "",
55
"main": "dist/index.js",
66
"scripts": {
77
"build": "tsc",
8+
"watch": "tsc --watch",
89
"format": "npx @biomejs/biome format ./src",
910
"lint": "npx @biomejs/biome lint ./src",
1011
"lint-write": "npx @biomejs/biome lint --write ./src",

src/client/client-options.ts

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ type ClientOptions = {
66
clientId: bigint;
77
debug: boolean;
88
timeout: number;
9+
loggerDisabled: boolean;
910
};
1011

1112
const defaultClientOptions: ClientOptions = {
@@ -16,6 +17,7 @@ const defaultClientOptions: ClientOptions = {
1617
clientId: BigInt(Math.floor(Math.random() * 1000000000000000000)),
1718
debug: false,
1819
timeout: 5000,
20+
loggerDisabled: false,
1921
};
2022

2123
export { type ClientOptions, defaultClientOptions };

src/client/client.ts

+2
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ export class Client extends Emitter<ClientEvents> {
5050
this.socket.on("message", (payload, rinfo) => {
5151
this.framer.incommingMessage(payload, rinfo);
5252
});
53+
Logger.disabled = this.options.loggerDisabled;
54+
5355
} catch (error) {
5456
Logger.error(`Failed to create socket: ${error}`);
5557
}

src/client/framer.ts

+26
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import type { Client } from "./client";
2323
import { BinaryStream } from "@serenityjs/binarystream";
2424
import { ConnectionRequestAccepted } from "../proto/packets/connection-request-accepted";
2525
import type { RemoteInfo } from "node:dgram";
26+
import DisconnectionNotification from "../proto/packets/disconnect";
2627

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

4850
constructor(client: Client) {
4951
this.client = client;
@@ -58,6 +60,7 @@ export class Framer {
5860
}
5961

6062
public tick() {
63+
this._tickCount++;
6164
if (
6265
this.client.status === Status.Disconnected ||
6366
this.client.status === Status.Disconnecting
@@ -80,6 +83,12 @@ export class Framer {
8083
this.client.send(pk.serialize());
8184
}
8285

86+
if (this._tickCount % 10 === 0) {
87+
const ping = new ConnectedPing();
88+
ping.timestamp = BigInt(Date.now());
89+
this.frameAndSend(ping.serialize(), Priority.Normal);
90+
}
91+
8392
this.sendQueue(this.outputFrames.size);
8493
}
8594

@@ -150,6 +159,9 @@ export class Framer {
150159
this.client.framer.handle(frameset);
151160
break;
152161
}
162+
default: {
163+
Logger.error(`Unknown packet header: ${header}`);
164+
}
153165
}
154166
}
155167

@@ -179,6 +191,13 @@ export class Framer {
179191
SystemAddress.count = 0;
180192
break;
181193
}
194+
case Packet.DisconnectionNotification: {
195+
const packet = new DisconnectionNotification(
196+
frame.payload,
197+
).deserialize();
198+
this.client.cleanup();
199+
break;
200+
}
182201
}
183202
}
184203

@@ -197,6 +216,13 @@ export class Framer {
197216
this.client.emit("encapsulated", frame.payload);
198217
break;
199218
}
219+
case Packet.DisconnectionNotification: {
220+
const packet = new DisconnectionNotification(
221+
frame.payload,
222+
).deserialize();
223+
this.client.cleanup();
224+
break;
225+
}
200226
}
201227
}
202228
}

src/proto/decorators/create.ts

-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ export function Create(id: number) {
2727
if (!properties.includes("serialize")) {
2828
target.prototype.serialize = function () {
2929
this.clear();
30-
if (id < 1) throw new Error("Packet ID cannot be less than 1.");
3130
if (id <= 255) this.writeUint8(id);
3231
else if (id <= 65535) this.writeUint16(id);
3332
else if (id <= 4294967295) this.writeUint32(id);

0 commit comments

Comments
 (0)