Repeat buffer reads as long as there is data
ci/woodpecker/tag/ociImageTag Pipeline was successful Details

This commit is contained in:
Natty 2024-12-24 17:05:41 +01:00
parent 5f4ab23b8d
commit a060a1ad29
1 changed files with 31 additions and 28 deletions

View File

@ -60,37 +60,39 @@ async function getRpcClient(): Promise<Socket> {
client.on("data", (recv) => {
buf.writeBuffer(recv);
if (buf.length < 1 + 4 + 8) {
return;
}
const header = String.fromCharCode(buf.readUInt8());
if (!["M", "F"].includes(header)) {
logger.error(`Invalid header: ${header}`);
return;
}
const serial = buf.readBigUInt64BE();
let dataDecoded;
if (header === "M") {
const dataLen = buf.readUInt32BE();
if (buf.remaining() < dataLen) {
buf.readOffset = 0;
while (true) {
if (buf.length < 1 + 4 + 8) {
return;
}
const data = buf.readBuffer(dataLen);
dataDecoded = decode(data);
} else {
dataDecoded = {success: false, data: "RPC returned failure"};
const header = String.fromCharCode(buf.readUInt8());
if (!["M", "F"].includes(header)) {
logger.error(`Invalid header: ${header}`);
return;
}
const serial = buf.readBigUInt64BE();
let dataDecoded;
if (header === "M") {
const dataLen = buf.readUInt32BE();
if (buf.remaining() < dataLen) {
buf.readOffset = 0;
return;
}
const data = buf.readBuffer(dataLen);
dataDecoded = decode(data);
} else {
dataDecoded = {success: false, data: "RPC returned failure"};
}
// Move the rest of the data to the beginning of the buffer
const rest = buf.readBuffer();
buf.clear();
buf.writeBuffer(rest);
client!.emit(`mag-data:${serial}`, dataDecoded);
}
// Move the rest of the data to the beginning of the buffer
const rest = buf.readBuffer();
buf.clear();
buf.writeBuffer(rest);
client!.emit(`mag-data:${serial}`, dataDecoded);
});
client.connect(port, host);
@ -154,6 +156,7 @@ async function rpcCall<D, T>(method: string, data: D): Promise<T> {
if (!data.success) {
reject(data.data);
return;
}
resolve(data.data);