send P2pAlive if havent heard from the other side for 500ms
This commit is contained in:
+12
-4
@@ -1,11 +1,11 @@
|
|||||||
import http from "node:http";
|
|
||||||
|
|
||||||
import { RemoteInfo } from "node:dgram";
|
import { RemoteInfo } from "node:dgram";
|
||||||
import { createWriteStream } from "node:fs";
|
import { createWriteStream } from "node:fs";
|
||||||
|
import http from "node:http";
|
||||||
|
|
||||||
import { CommandsByValue } from "./datatypes.js";
|
import { CommandsByValue } from "./datatypes.js";
|
||||||
import { create_LanSearch } from "./func_replacements.js";
|
import { create_LanSearch } from "./func_replacements.js";
|
||||||
import { hexdump } from "./hexdump.js";
|
import { hexdump } from "./hexdump.js";
|
||||||
|
import { create_P2pAlive } from "./impl.js";
|
||||||
import { Handlers, makeSession } from "./server.js";
|
import { Handlers, makeSession } from "./server.js";
|
||||||
|
|
||||||
const s = makeSession(
|
const s = makeSession(
|
||||||
@@ -18,10 +18,11 @@ const s = makeSession(
|
|||||||
console.log(hexdump(msg.buffer, { ansi: options.ansi, ansiColor: 1 }));
|
console.log(hexdump(msg.buffer, { ansi: options.ansi, ansiColor: 1 }));
|
||||||
}
|
}
|
||||||
Handlers[cmd](session, dv, rinfo);
|
Handlers[cmd](session, dv, rinfo);
|
||||||
|
session.lastReceivedPacket = Date.now();
|
||||||
},
|
},
|
||||||
(session) => {
|
(session) => {
|
||||||
// ther should be a better way of executing periodic status update requests
|
// ther should be a better way of executing periodic status update
|
||||||
// per device
|
// requests per device
|
||||||
const int = setInterval(() => {
|
const int = setInterval(() => {
|
||||||
let buf = new DataView(new Uint8Array(4).buffer);
|
let buf = new DataView(new Uint8Array(4).buffer);
|
||||||
create_LanSearch(buf);
|
create_LanSearch(buf);
|
||||||
@@ -52,6 +53,13 @@ s.eventEmitter.on("connect", (name: string, rinfo: RemoteInfo) => {
|
|||||||
console.log(`Connected to ${name} - ${rinfo.address}`);
|
console.log(`Connected to ${name} - ${rinfo.address}`);
|
||||||
s.outgoingCommandId = 0;
|
s.outgoingCommandId = 0;
|
||||||
s.dst_ip = rinfo.address;
|
s.dst_ip = rinfo.address;
|
||||||
|
|
||||||
|
const int = setInterval(() => {
|
||||||
|
let buf = create_P2pAlive();
|
||||||
|
if (Date.now() - s.lastReceivedPacket > 500) {
|
||||||
|
s.send(buf);
|
||||||
|
}
|
||||||
|
}, 1000);
|
||||||
});
|
});
|
||||||
|
|
||||||
const server = http.createServer((req, res) => {
|
const server = http.createServer((req, res) => {
|
||||||
|
|||||||
@@ -92,3 +92,10 @@ export const create_P2pRdy = (inbuf: DataView): DataView => {
|
|||||||
outbuf.add(4).writeByteArray(new Uint8Array(inbuf.readByteArray(P2PRDY_SIZE).buffer));
|
outbuf.add(4).writeByteArray(new Uint8Array(inbuf.readByteArray(P2PRDY_SIZE).buffer));
|
||||||
return outbuf;
|
return outbuf;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const create_P2pAlive = (): DataView => {
|
||||||
|
const outbuf = new DataView(new Uint8Array(4).buffer);
|
||||||
|
outbuf.writeU16(Commands.P2PAlive);
|
||||||
|
outbuf.add(2).writeU16(0);
|
||||||
|
return outbuf;
|
||||||
|
};
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ export type Session = {
|
|||||||
ticket: number[];
|
ticket: number[];
|
||||||
eventEmitter: EventEmitter;
|
eventEmitter: EventEmitter;
|
||||||
dst_ip: string;
|
dst_ip: string;
|
||||||
|
lastReceivedPacket: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type PacketHandler = (session: Session, dv: DataView, rinfo: RemoteInfo) => void;
|
export type PacketHandler = (session: Session, dv: DataView, rinfo: RemoteInfo) => void;
|
||||||
@@ -49,6 +50,7 @@ export const makeSession = (cb: msgCb, connCb: connCb, options: opt): Session =>
|
|||||||
const session: Session = {
|
const session: Session = {
|
||||||
outgoingCommandId: 0,
|
outgoingCommandId: 0,
|
||||||
ticket: [0, 0, 0, 0],
|
ticket: [0, 0, 0, 0],
|
||||||
|
lastReceivedPacket: 0,
|
||||||
eventEmitter: new EventEmitter(),
|
eventEmitter: new EventEmitter(),
|
||||||
send: (msg: DataView) => {
|
send: (msg: DataView) => {
|
||||||
const raw = msg.readU16();
|
const raw = msg.readU16();
|
||||||
|
|||||||
Reference in New Issue
Block a user