refactor curimage into a slice of buffers & pass options via session

This commit is contained in:
DavidVentura
2024-02-01 17:56:08 +01:00
parent 92dbd8354c
commit 2f2b6d302a
3 changed files with 17 additions and 14 deletions
+6 -6
View File
@@ -148,21 +148,21 @@ const deal_with_data = (session: Session, dv: DataView) => {
} else { } else {
const data = dv.add(8).readByteArray(pkt_len - 4); const data = dv.add(8).readByteArray(pkt_len - 4);
if (is_new_image) { if (is_new_image) {
if (session.curImage != null && !session.frame_is_bad) { if (session.curImage.length > 0 && !session.frame_is_bad) {
session.eventEmitter.emit("frame"); session.eventEmitter.emit("frame");
} }
session.frame_is_bad = false; session.frame_is_bad = false;
session.curImage = Buffer.from(data.buffer); session.curImage = [Buffer.from(data.buffer)];
session.rcvSeqId = pkt_id; session.rcvSeqId = pkt_id;
} else { } else {
if (pkt_id <= session.rcvSeqId) { if (pkt_id <= session.rcvSeqId) {
// retransmit // retransmit
return; return;
} }
if (session.frame_is_bad) {
return; let b = Buffer.from(data.buffer);
}
if (pkt_id > session.rcvSeqId + 1) { if (pkt_id > session.rcvSeqId + 1) {
// missed some packets -- filling with zeroes still produces a broken // missed some packets -- filling with zeroes still produces a broken
// image // image
@@ -172,7 +172,7 @@ const deal_with_data = (session: Session, dv: DataView) => {
} }
session.rcvSeqId = pkt_id; session.rcvSeqId = pkt_id;
if (session.curImage != null) { if (session.curImage != null) {
session.curImage = Buffer.concat([session.curImage, Buffer.from(data.buffer)]); session.curImage.push(b);
} }
} }
} }
+3 -1
View File
@@ -63,9 +63,11 @@ devEv.on("discover", (rinfo: RemoteInfo, dev: DevSerial) => {
const header = Buffer.from(`--${BOUNDARY}\r\nContent-Type: image/jpeg\r\n\r\n`); const header = Buffer.from(`--${BOUNDARY}\r\nContent-Type: image/jpeg\r\n\r\n`);
s.eventEmitter.on("frame", () => { s.eventEmitter.on("frame", () => {
const assembled = Buffer.concat(s.curImage);
responses[dev.devId].forEach((res) => { responses[dev.devId].forEach((res) => {
res.write(header); res.write(header);
res.write(s.curImage); res.write(assembled);
}); });
}); });
+8 -7
View File
@@ -17,9 +17,10 @@ export type Session = {
connected: boolean; connected: boolean;
devName: string; devName: string;
timers: ReturnType<typeof setInterval>[]; timers: ReturnType<typeof setInterval>[];
curImage: Buffer | null; curImage: Buffer[];
rcvSeqId: number; rcvSeqId: number;
frame_is_bad: boolean; frame_is_bad: boolean;
options: opt;
}; };
export type PacketHandler = (session: Session, dv: DataView, rinfo: RemoteInfo) => void; export type PacketHandler = (session: Session, dv: DataView, rinfo: RemoteInfo) => void;
@@ -29,16 +30,15 @@ type msgCb = (
handlers: Record<keyof typeof Commands, PacketHandler>, handlers: Record<keyof typeof Commands, PacketHandler>,
msg: Buffer, msg: Buffer,
rinfo: RemoteInfo, rinfo: RemoteInfo,
options: opt,
) => void; ) => void;
const handleIncoming: msgCb = (session, handlers, msg, rinfo, options) => { const handleIncoming: msgCb = (session, handlers, msg, rinfo) => {
const ab = new Uint8Array(msg).buffer; const ab = new Uint8Array(msg).buffer;
const dv = new DataView(ab); const dv = new DataView(ab);
const cmd = CommandsByValue[dv.readU16()]; const cmd = CommandsByValue[dv.readU16()];
if (options.debug) { if (session.options.debug) {
console.log(`<< ${cmd}`); console.log(`<< ${cmd}`);
console.log(hexdump(msg.buffer, { ansi: options.ansi, ansiColor: 1 })); console.log(hexdump(msg.buffer, { ansi: session.options.ansi, ansiColor: 1 }));
} }
handlers[cmd](session, dv, rinfo); handlers[cmd](session, dv, rinfo);
session.lastReceivedPacket = Date.now(); session.lastReceivedPacket = Date.now();
@@ -58,7 +58,7 @@ export const makeSession = (
sock.close(); sock.close();
}); });
sock.on("message", (msg, rinfo) => handleIncoming(session, handlers, msg, rinfo, options)); sock.on("message", (msg, rinfo) => handleIncoming(session, handlers, msg, rinfo));
sock.on("listening", () => { sock.on("listening", () => {
const buf = makePunchPkt(dev); const buf = makePunchPkt(dev);
@@ -98,9 +98,10 @@ export const makeSession = (
sock.send(new Uint8Array(msg.buffer), SEND_PORT, session.dst_ip); sock.send(new Uint8Array(msg.buffer), SEND_PORT, session.dst_ip);
}, },
dst_ip: ra.address, dst_ip: ra.address,
curImage: null, curImage: [],
rcvSeqId: 0, rcvSeqId: 0,
frame_is_bad: false, frame_is_bad: false,
options: options,
}; };
session.eventEmitter.on("disconnect", () => { session.eventEmitter.on("disconnect", () => {