Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use connection state to optimise packet type detection #38

Merged
merged 3 commits into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class Connection {
/** @internal */
public incomingPacketFragment(data: number) {
if (this.currentPacketFragment.push(data)) {
const p = this.currentPacketFragment.getTypedClient();
const p = this.currentPacketFragment.getTypedClient(this);
if (p) {
p.execute(this, this.server);
this.server.emit("packet", p, this);
Expand Down
5 changes: 3 additions & 2 deletions src/Packet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import ParsedPacket from "./ParsedPacket.js";
import {TypedClientPacket, TypedClientPacketStatic} from "./types/TypedPacket";
import HandshakePacket from "./packet/client/HandshakePacket.js";
import LoginPacket from "./packet/client/LoginPacket.js";
import Connection from "./Connection";

export default class Packet {
readonly #data: number[];
Expand Down Expand Up @@ -197,9 +198,9 @@ export default class Packet {
/**
* Get typed client packet
*/
public getTypedClient(): TypedClientPacket | null {
public getTypedClient(conn: Connection): TypedClientPacket | null {
for (const type of Packet.clientTypes) {
const p = type.isThisPacket(this.parse());
const p = type.isThisPacket(this.parse(), conn);
if (p !== null) return p;
}
return null;
Expand Down
5 changes: 3 additions & 2 deletions src/packet/client/HandshakePacket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ export default class HandshakePacket {

public static readonly id = 0x00;

public static isThisPacket(data: ParsedPacket): TypedClientPacket | null {
const p = new this(data);
public static isThisPacket(data: ParsedPacket, conn: Connection): TypedClientPacket | null {
if (conn.state !== Connection.State.NONE) return null;
try {
const p = new this(data);
return (p.packet.id === this.id && p.data.nextState === 2) ? p : null;
}
catch {
Expand Down
5 changes: 3 additions & 2 deletions src/packet/client/LoginPacket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ export default class LoginPacket {

public static readonly id = 0x00;

public static isThisPacket(data: ParsedPacket): TypedClientPacket | null {
const p = new this(data);
public static isThisPacket(data: ParsedPacket, conn: Connection): TypedClientPacket | null {
if (conn.state !== Connection.State.LOGIN) return null;
try {
const p = new this(data);
return (p.packet.id === this.id && p.data.username !== null && p.data.username.match(/^[.*]?[A-Za-z0-9_]{3,16}$/) !== null) ? p : null;
}
catch {
Expand Down
2 changes: 1 addition & 1 deletion src/types/TypedPacket.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ export interface TypedClientPacketStatic {

readonly id: number;

isThisPacket(data: ParsedPacket): TypedClientPacket | null;
isThisPacket(data: ParsedPacket, conn: Connection): TypedClientPacket | null;
}
Loading