Skip to content

Commit

Permalink
use connection state to optimise packet type detection
Browse files Browse the repository at this point in the history
  • Loading branch information
William Duncan committed Aug 12, 2023
1 parent 280f4af commit de50d07
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 8 deletions.
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.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;
}

0 comments on commit de50d07

Please sign in to comment.