abstract server a bit better

This commit is contained in:
DavidVentura
2024-01-31 12:01:00 +01:00
parent 9d671f18c9
commit 7bdcc54bc1
2 changed files with 26 additions and 37 deletions
+1 -30
View File
@@ -1,23 +1,9 @@
import { RemoteInfo } from "node:dgram";
import { createWriteStream } from "node:fs"; import { createWriteStream } from "node:fs";
import http from "node:http"; import http from "node:http";
import { create_LanSearch, create_P2pAlive } from "./impl.js";
import { Handlers, makeSession } from "./server.js"; import { Handlers, makeSession } from "./server.js";
const s = makeSession( const s = makeSession(Handlers, { debug: false, ansi: false });
Handlers,
(session) => {
// ther should be a better way of executing periodic status update
// requests per device
let buf = create_LanSearch();
const int = setInterval(() => {
session.broadcast(buf);
}, 2000);
session.broadcast(buf);
},
{ debug: false, ansi: false },
);
let BOUNDARY = "a very good boundary line"; let BOUNDARY = "a very good boundary line";
let responses = []; let responses = [];
@@ -36,21 +22,6 @@ s.eventEmitter.on("audio", (frame: Buffer) => {
audioFd.write(frame); audioFd.write(frame);
}); });
s.eventEmitter.on("connect", (name: string, rinfo: RemoteInfo) => {
console.log(`Connected to ${name} - ${rinfo.address}`);
s.outgoingCommandId = 0;
s.dst_ip = rinfo.address;
if (s.ticket.every((x) => x == 0)) {
const int = setInterval(() => {
let buf = create_P2pAlive();
if (Date.now() - s.lastReceivedPacket > 600) {
s.send(buf);
}
}, 400);
}
});
const server = http.createServer((req, res) => { const server = http.createServer((req, res) => {
if (s.ticket.every((x) => x == 0)) { if (s.ticket.every((x) => x == 0)) {
res.writeHead(400); res.writeHead(400);
+25 -7
View File
@@ -1,4 +1,5 @@
import { createSocket, RemoteInfo } from "node:dgram"; import { createSocket, RemoteInfo } from "node:dgram";
import { create_LanSearch, create_P2pAlive } from "./impl.js";
import { Commands, CommandsByValue } from "./datatypes.js"; import { Commands, CommandsByValue } from "./datatypes.js";
import { handle_P2PAlive, handle_PunchPkt, handle_P2PRdy, handle_Drw, notImpl, noop } from "./handlers.js"; import { handle_P2PAlive, handle_PunchPkt, handle_P2PRdy, handle_Drw, notImpl, noop } from "./handlers.js";
import { hexdump } from "./hexdump.js"; import { hexdump } from "./hexdump.js";
@@ -28,7 +29,6 @@ type msgCb = (
rinfo: RemoteInfo, rinfo: RemoteInfo,
options: opt, options: opt,
) => void; ) => void;
type connCb = (session: Session) => void;
const handleIncoming: msgCb = (session, handlers, msg, rinfo, options) => { const handleIncoming: msgCb = (session, handlers, msg, rinfo, options) => {
const ab = new Uint8Array(msg).buffer; const ab = new Uint8Array(msg).buffer;
@@ -42,11 +42,7 @@ const handleIncoming: msgCb = (session, handlers, msg, rinfo, options) => {
session.lastReceivedPacket = Date.now(); session.lastReceivedPacket = Date.now();
}; };
export const makeSession = ( export const makeSession = (handlers: Record<keyof typeof Commands, PacketHandler>, options: opt): Session => {
handlers: Record<keyof typeof Commands, PacketHandler>,
connCb: connCb,
options: opt,
): Session => {
const sock = createSocket("udp4"); const sock = createSocket("udp4");
sock.on("error", (err) => { sock.on("error", (err) => {
@@ -60,7 +56,14 @@ export const makeSession = (
const address = sock.address(); const address = sock.address();
console.log(`sock listening ${address.address}:${address.port}`); console.log(`sock listening ${address.address}:${address.port}`);
sock.setBroadcast(true); sock.setBroadcast(true);
connCb(session);
// ther should be a better way of executing periodic status update
// requests per device
let buf = create_LanSearch();
const int = setInterval(() => {
session.broadcast(buf);
}, 2000);
session.broadcast(buf);
}); });
const RECV_PORT = 49512; // important? const RECV_PORT = 49512; // important?
@@ -93,6 +96,21 @@ export const makeSession = (
}, },
dst_ip: BCAST_IP, dst_ip: BCAST_IP,
}; };
session.eventEmitter.on("connect", (name: string, rinfo: RemoteInfo) => {
console.log(`Connected to ${name} - ${rinfo.address}`);
session.outgoingCommandId = 0;
session.dst_ip = rinfo.address;
if (session.ticket.every((x) => x == 0)) {
const int = setInterval(() => {
if (Date.now() - session.lastReceivedPacket > 600) {
let buf = create_P2pAlive();
session.send(buf);
}
}, 400);
}
});
return session; return session;
}; };