From 7bdcc54bc1ceecac0b27423391cc38caacb2a4d0 Mon Sep 17 00:00:00 2001 From: DavidVentura Date: Wed, 31 Jan 2024 12:01:00 +0100 Subject: [PATCH] abstract server a bit better --- http_server.ts | 31 +------------------------------ server.ts | 32 +++++++++++++++++++++++++------- 2 files changed, 26 insertions(+), 37 deletions(-) diff --git a/http_server.ts b/http_server.ts index 27f8801..92849b8 100644 --- a/http_server.ts +++ b/http_server.ts @@ -1,23 +1,9 @@ -import { RemoteInfo } from "node:dgram"; import { createWriteStream } from "node:fs"; import http from "node:http"; -import { create_LanSearch, create_P2pAlive } from "./impl.js"; import { Handlers, makeSession } from "./server.js"; -const s = makeSession( - 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 }, -); +const s = makeSession(Handlers, { debug: false, ansi: false }); let BOUNDARY = "a very good boundary line"; let responses = []; @@ -36,21 +22,6 @@ s.eventEmitter.on("audio", (frame: Buffer) => { 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) => { if (s.ticket.every((x) => x == 0)) { res.writeHead(400); diff --git a/server.ts b/server.ts index d03962c..e0a9589 100644 --- a/server.ts +++ b/server.ts @@ -1,4 +1,5 @@ import { createSocket, RemoteInfo } from "node:dgram"; +import { create_LanSearch, create_P2pAlive } from "./impl.js"; import { Commands, CommandsByValue } from "./datatypes.js"; import { handle_P2PAlive, handle_PunchPkt, handle_P2PRdy, handle_Drw, notImpl, noop } from "./handlers.js"; import { hexdump } from "./hexdump.js"; @@ -28,7 +29,6 @@ type msgCb = ( rinfo: RemoteInfo, options: opt, ) => void; -type connCb = (session: Session) => void; const handleIncoming: msgCb = (session, handlers, msg, rinfo, options) => { const ab = new Uint8Array(msg).buffer; @@ -42,11 +42,7 @@ const handleIncoming: msgCb = (session, handlers, msg, rinfo, options) => { session.lastReceivedPacket = Date.now(); }; -export const makeSession = ( - handlers: Record, - connCb: connCb, - options: opt, -): Session => { +export const makeSession = (handlers: Record, options: opt): Session => { const sock = createSocket("udp4"); sock.on("error", (err) => { @@ -60,7 +56,14 @@ export const makeSession = ( const address = sock.address(); console.log(`sock listening ${address.address}:${address.port}`); 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? @@ -93,6 +96,21 @@ export const makeSession = ( }, 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; };