Fix VICE binary protocol packet format

The request ID field is 4 bytes (not 1), and the packet structure was
incorrect:
- Request: STX(1) + API(1) + Length(4) + ReqID(4) + Cmd(1) + Body
- Response: STX(1) + API(1) + Length(4) + Type(1) + Error(1) + ReqID(4) + Body

This was causing all commands after connect() to timeout because response
matching was reading the wrong bytes for the request ID.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Simen Svale 2025-12-30 02:44:27 +01:00
parent 0f5db6162f
commit 3fece5cb1e

View file

@ -146,7 +146,7 @@ export class ViceClient {
} }
private nextRequestId(): number { private nextRequestId(): number {
this.requestId = (this.requestId + 1) & 0xff; this.requestId = (this.requestId + 1) & 0xffffffff;
return this.requestId; return this.requestId;
} }
@ -154,8 +154,8 @@ export class ViceClient {
this.responseBuffer = Buffer.concat([this.responseBuffer, data]); this.responseBuffer = Buffer.concat([this.responseBuffer, data]);
// Process complete packets // Process complete packets
while (this.responseBuffer.length >= 9) { // Response header: STX(1) + API(1) + bodyLength(4) + responseType(1) + errorCode(1) + requestId(4) = 12 bytes
// Minimum header size while (this.responseBuffer.length >= 12) {
const stx = this.responseBuffer[0]; const stx = this.responseBuffer[0];
if (stx !== STX) { if (stx !== STX) {
// Protocol error, skip byte // Protocol error, skip byte
@ -164,7 +164,7 @@ export class ViceClient {
} }
const bodyLength = this.responseBuffer.readUInt32LE(2); const bodyLength = this.responseBuffer.readUInt32LE(2);
const totalLength = 6 + bodyLength; // Header (6) + body const totalLength = 12 + bodyLength; // Header (12) + body
if (this.responseBuffer.length < totalLength) { if (this.responseBuffer.length < totalLength) {
// Wait for more data // Wait for more data
@ -172,10 +172,11 @@ export class ViceClient {
} }
// Parse complete packet // Parse complete packet
// Response format: STX(1) + API(1) + bodyLen(4) + type(1) + error(1) + reqId(4) + body
const responseType = this.responseBuffer[6] as ResponseType; const responseType = this.responseBuffer[6] as ResponseType;
const errorCode = this.responseBuffer[7] as ErrorCode; const errorCode = this.responseBuffer[7] as ErrorCode;
const requestId = this.responseBuffer[8]; const requestId = this.responseBuffer.readUInt32LE(8);
const body = this.responseBuffer.subarray(9, totalLength); const body = this.responseBuffer.subarray(12, totalLength);
const response: ViceResponse = { const response: ViceResponse = {
responseType, responseType,
@ -250,13 +251,14 @@ export class ViceClient {
const requestId = this.nextRequestId(); const requestId = this.nextRequestId();
// Build packet: STX (1) + API (1) + Length (4) + RequestID (1) + Command (1) + Body // Build packet: STX(1) + API(1) + Length(4) + RequestID(4) + Command(1) + Body
const header = Buffer.alloc(8); // Length field includes: RequestID(4) + Command(1) + Body
const header = Buffer.alloc(11);
header[0] = STX; header[0] = STX;
header[1] = API_VERSION; header[1] = API_VERSION;
header.writeUInt32LE(body.length + 2, 2); // Body length includes request ID and command header.writeUInt32LE(body.length + 5, 2); // Body length includes request ID (4) and command (1)
header[6] = requestId; header.writeUInt32LE(requestId, 6);
header[7] = command; header[10] = command;
const packet = Buffer.concat([header, body]); const packet = Buffer.concat([header, body]);