This commit is contained in:
Mikko Ahlroth 2024-07-18 01:24:37 +03:00
parent 04c8630199
commit 359eee9df5
2 changed files with 30 additions and 28 deletions

View file

@ -113,6 +113,9 @@ class Connection {
/** @type {NodeJS.Timer | null} */ /** @type {NodeJS.Timer | null} */
idleTimeout = null; idleTimeout = null;
/** @type {() => void} */
idleTimeoutCallback;
/** @type {NodeJS.Timer | null} */ /** @type {NodeJS.Timer | null} */
retryTimeout = null; retryTimeout = null;
@ -124,10 +127,12 @@ class Connection {
/** /**
* @param {Target} source * @param {Target} source
* @param {Socket} writeSocket * @param {Socket} writeSocket
* @param {() => void} idleTimeoutCallback
*/ */
constructor(source, writeSocket) { constructor(source, writeSocket, idleTimeoutCallback) {
this.source = source; this.source = source;
this.writeSocket = writeSocket; this.writeSocket = writeSocket;
this.idleTimeoutCallback = idleTimeoutCallback;
} }
initTargetSocket() { initTargetSocket() {
@ -136,8 +141,8 @@ class Connection {
this.writeSocket.on("connect", () => { this.writeSocket.on("connect", () => {
console.log( console.log(
"Opened new connection to target from", "Opened new connection to target from",
source.address, this.source.address,
source.port this.source.port
); );
}); });
this.writeSocket.on("message", (fromMsg) => { this.writeSocket.on("message", (fromMsg) => {
@ -146,17 +151,22 @@ class Connection {
"Got", "Got",
fromMsg.byteLength, fromMsg.byteLength,
"bytes from target for", "bytes from target for",
source.address, this.source.address,
source.port this.source.port
); );
READ_SOCKET.send(fromMsg, source.port, source.address); READ_SOCKET.send(fromMsg, this.source.port, this.source.address);
}); });
this.writeSocket.on("error", (err) => { this.writeSocket.on("error", (err) => {
this.#writeError(err); this.#writeError(err);
}); });
this.writeSocket.on("close", () => { this.writeSocket.on("close", () => {
console.log("Socket for", source.address, source.port, "closed."); console.log(
"Socket for",
this.source.address,
this.source.port,
"closed."
);
CONNECTIONS.delete(source); CONNECTIONS.delete(source);
}); });
@ -168,6 +178,8 @@ class Connection {
* @param {?Buffer} msg * @param {?Buffer} msg
*/ */
send(msg) { send(msg) {
this.#resetIdleTimeout();
if (msg && (this.writing || !this.writeSocket)) { if (msg && (this.writing || !this.writeSocket)) {
this.msgBuffer.push(msg); this.msgBuffer.push(msg);
return; return;
@ -213,7 +225,10 @@ class Connection {
#resetIdleTimeout() { #resetIdleTimeout() {
clearTimeout(this.idleTimeout); clearTimeout(this.idleTimeout);
this.idleTimeout = setTimeout(() => idleTimeout(this.source), IDLE_TIMEOUT); this.idleTimeout = setTimeout(
() => this.idleTimeoutCallback(),
IDLE_TIMEOUT
);
} }
/** /**
@ -238,14 +253,6 @@ class Connection {
} }
} }
/**
* @param {Target} source
*/
function idleTimeout(source) {
console.log("Idle timeout for", source.address, source.port);
CONNECTIONS.delete(source);
}
const TARGET_ADDRESS = argv[2]; const TARGET_ADDRESS = argv[2];
const TARGET_PORT = parseInt(argv[3], 10); const TARGET_PORT = parseInt(argv[3], 10);
@ -274,22 +281,16 @@ READ_SOCKET.on("message", (toMsg, rinfo) => {
let connection = CONNECTIONS.get(source); let connection = CONNECTIONS.get(source);
if (!connection) { if (!connection) {
const socket = createSocket(SOCKET_TYPE); const socket = createSocket(SOCKET_TYPE);
connection = new Connection( connection = new Connection(source, socket, () => {
source, console.log("Idle timeout for", source.address, source.port);
socket, CONNECTIONS.delete(source);
setTimeout(() => { });
idleTimeout(source);
}, IDLE_TIMEOUT)
);
connection.initTargetSocket(); connection.initTargetSocket();
CONNECTIONS.set(source, connection); CONNECTIONS.set(source, connection);
} }
clearTimeout(connection.idleTimeout); connection.send(toMsg);
connection.idleTimeout = setTimeout(() => {
idleTimeout(source);
}, IDLE_TIMEOUT);
}); });
READ_SOCKET.on("error", (err) => { READ_SOCKET.on("error", (err) => {

View file

@ -12,5 +12,6 @@
}, },
"author": "Mikko Ahlroth <mikko@ahlroth.fi>", "author": "Mikko Ahlroth <mikko@ahlroth.fi>",
"license": "CC0-1.0", "license": "CC0-1.0",
"private": true "private": true,
"type": "module"
} }