Skip to content

Phase 2: Protocol-specific base classes for byte order enforcement #2802

@drazisil

Description

@drazisil

Summary

Create protocol-specific base classes that enforce byte order at the type level to prevent mixing Big Endian (NPS) and Little Endian (MCOTS) protocols.

Background

From the packet architecture analysis:

  • NPS Protocol (ports 7003, 8226, 8228): Uses Big Endian
  • MCOTS Protocol (port 43300): Uses Little Endian
  • Current issue: BytableBase.coerceValueToBuffer uses LE, but BytableMessage.htonl uses BE - this inconsistency can cause bugs

Proposed Solution

Create new base classes:

// libs/@rustymotors/binary/src/lib/NPSPacketBase.ts
export abstract class NPSPacketBase extends BytableMessage {
    protected static readonly ENDIANNESS = 'BE' as const;
    
    protected writeUInt16(buffer: Buffer, value: number, offset: number): void {
        buffer.writeUInt16BE(value, offset);
    }
    
    protected writeUInt32(buffer: Buffer, value: number, offset: number): void {
        buffer.writeUInt32BE(value, offset);
    }
}

// libs/@rustymotors/binary/src/lib/MCOTSPacketBase.ts
export abstract class MCOTSPacketBase extends BytableServerMessage {
    protected static readonly ENDIANNESS = 'LE' as const;
    
    protected writeUInt16(buffer: Buffer, value: number, offset: number): void {
        buffer.writeUInt16LE(value, offset);
    }
    
    protected writeUInt32(buffer: Buffer, value: number, offset: number): void {
        buffer.writeUInt32LE(value, offset);
    }
}

Tasks

  • Create NPSPacketBase class in libs/@rustymotors/binary/
  • Create MCOTSPacketBase class in libs/@rustymotors/binary/
  • Fix BytableBase.coerceValueToBuffer to be protocol-aware or deprecate it
  • Add tests for byte order enforcement
  • Update documentation

Dependencies

Related

Part of the packet architecture improvement initiative. See PR #2801 for context.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions