From 58266bcb7fc7405fe00f47ee3dc4077d6c4b0726 Mon Sep 17 00:00:00 2001 From: Natty Date: Fri, 15 Nov 2024 23:12:21 +0100 Subject: [PATCH] RPC fixes --- packages/backend/src/mag/rpc-client.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/packages/backend/src/mag/rpc-client.ts b/packages/backend/src/mag/rpc-client.ts index 0863653b17..3276b1aef7 100644 --- a/packages/backend/src/mag/rpc-client.ts +++ b/packages/backend/src/mag/rpc-client.ts @@ -13,7 +13,7 @@ let serial: bigint = BigInt(0); const logger = new Logger("RpcLog"); function getRpcClient(): Socket { - if (client) { + if (client != null) { return client; } @@ -85,6 +85,9 @@ function getRpcClient(): Socket { } async function rpcCall(method: string, data: D): Promise { + const currentSerial = serial; + serial = serial + BigInt(1); + const header = new Uint8Array([77]); const textEncoder = new TextEncoder(); const methodBuf = textEncoder.encode(method); @@ -99,7 +102,7 @@ async function rpcCall(method: string, data: D): Promise { packetBuf.set(dataBuf, header.length + serialLength + sizeLength + methodBuf.length + sizeLength); const packetDataView = new DataView(packetBuf.buffer); - packetDataView.setBigUint64(header.length, serial); + packetDataView.setBigUint64(header.length, currentSerial); packetDataView.setUint32(header.length + serialLength, methodBuf.length); packetDataView.setUint32(header.length + serialLength + sizeLength + methodBuf.length, dataBuf.length); @@ -107,13 +110,11 @@ async function rpcCall(method: string, data: D): Promise { client.write(packetBuf); const fut = new Promise((resolve, reject) => { - client.once(`mag-data:${serial}`, resolve); + client.once(`mag-data:${currentSerial}`, resolve); client.once("close", reject); client.once("error", reject); }); - serial++; - const result = await fut as { success: false, data: any } | { success: true, data: T }; if (!result.success) {