consider remoteinfo on connect

This commit is contained in:
DavidVentura
2024-01-29 11:39:01 +01:00
parent 1cfe82ee7a
commit 0857d220d5
+21 -12
View File
@@ -1,4 +1,4 @@
import dgram from "node:dgram"; import { createSocket, RemoteInfo } from "node:dgram";
import { createWriteStream } from "node:fs"; import { createWriteStream } from "node:fs";
import { create_LanSearch } from "./func_replacements.js"; import { create_LanSearch } from "./func_replacements.js";
import { Commands, CommandsByValue } from "./datatypes.js"; import { Commands, CommandsByValue } from "./datatypes.js";
@@ -12,20 +12,21 @@ export type Session = {
outgoingCommandId: number; outgoingCommandId: number;
ticket: number[]; ticket: number[];
eventEmitter: EventEmitter; eventEmitter: EventEmitter;
dst_ip: string;
}; };
export type PacketHandler = (session: Session, dv: DataView) => void; export type PacketHandler = (session: Session, dv: DataView, rinfo: RemoteInfo) => void;
type opt = { type opt = {
debug: boolean; debug: boolean;
ansi: boolean; ansi: boolean;
}; };
type msgCb = (session: Session, msg: Buffer, rinfo: any, options: opt) => void; type msgCb = (session: Session, msg: Buffer, rinfo: RemoteInfo, options: opt) => void;
type connCb = (session: Session) => void; type connCb = (session: Session) => void;
const makeSession = (cb: msgCb, connCb: connCb, options?: opt): Session => { const makeSession = (cb: msgCb, connCb: connCb, options: opt): Session => {
const sock = dgram.createSocket("udp4"); const sock = createSocket("udp4");
sock.on("error", (err) => { sock.on("error", (err) => {
console.error(`sock error:\n${err.stack}`); console.error(`sock error:\n${err.stack}`);
@@ -42,7 +43,6 @@ const makeSession = (cb: msgCb, connCb: connCb, options?: opt): Session => {
}); });
const RECV_PORT = 49512; // important? const RECV_PORT = 49512; // important?
const DST_IP = "192.168.1.1";
const BCAST_IP = "192.168.1.255"; const BCAST_IP = "192.168.1.255";
const SEND_PORT = 32108; const SEND_PORT = 32108;
sock.bind(RECV_PORT); sock.bind(RECV_PORT);
@@ -62,9 +62,10 @@ const makeSession = (cb: msgCb, connCb: connCb, options?: opt): Session => {
// not sure why cmd == Commands.Drw does not work // not sure why cmd == Commands.Drw does not work
session.outgoingCommandId++; session.outgoingCommandId++;
} }
sock.send(new Uint8Array(msg.buffer), SEND_PORT, DST_IP); sock.send(new Uint8Array(msg.buffer), SEND_PORT, session.dst_ip);
}, },
broadcast: (msg: DataView) => sock.send(new Uint8Array(msg.buffer), SEND_PORT, BCAST_IP), broadcast: (msg: DataView) => sock.send(new Uint8Array(msg.buffer), SEND_PORT, BCAST_IP),
dst_ip: BCAST_IP,
}; };
return session; return session;
}; };
@@ -96,7 +97,7 @@ const Handlers: Record<keyof typeof Commands, PacketHandler> = {
}; };
const s = makeSession( const s = makeSession(
(session, msg, _, options) => { (session, msg, rinfo, options) => {
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()];
@@ -104,7 +105,7 @@ const s = makeSession(
console.log(`<< ${cmd}`); console.log(`<< ${cmd}`);
console.log(hexdump(msg.buffer, { ansi: options.ansi, ansiColor: 1 })); console.log(hexdump(msg.buffer, { ansi: options.ansi, ansiColor: 1 }));
} }
Handlers[cmd](session, dv); Handlers[cmd](session, dv, rinfo);
}, },
(session) => { (session) => {
const int = setInterval(() => { const int = setInterval(() => {
@@ -124,14 +125,22 @@ s.eventEmitter.on("frame", (frame: Buffer) => {
cur_image_index++; cur_image_index++;
cur_image.write(frame); cur_image.write(frame);
cur_image.close(); cur_image.close();
console.log("got an entire frame", frame.length); // console.log("got an entire frame", frame.length);
}); });
let i = 0;
s.eventEmitter.on("audio", (frame: Buffer) => { s.eventEmitter.on("audio", (frame: Buffer) => {
audioFd.write(frame); audioFd.write(frame);
if (i == 20) {
const buf = SendDevStatus(s);
s.send(buf);
i = 0;
}
i++;
}); });
s.eventEmitter.on("connect", (name: string) => { s.eventEmitter.on("connect", (name: string, rinfo: RemoteInfo) => {
console.log(`Connected to ${name}`); console.log(`Connected to ${name} - ${rinfo.address}`);
s.outgoingCommandId = 0; s.outgoingCommandId = 0;
s.dst_ip = rinfo.address;
}); });