Fix race
This commit is contained in:
parent
36e28ef081
commit
b466a6b2a2
1 changed files with 10 additions and 1 deletions
11
index.js
11
index.js
|
@ -122,6 +122,7 @@ class Connection {
|
|||
/** @type {NodeJS.Timer | null} */
|
||||
reachabilityTimeout = null;
|
||||
|
||||
connected = false;
|
||||
writing = false;
|
||||
|
||||
/**
|
||||
|
@ -136,14 +137,22 @@ class Connection {
|
|||
}
|
||||
|
||||
initTargetSocket() {
|
||||
this.connected = false;
|
||||
clearTimeout(this.retryTimeout);
|
||||
|
||||
this.writeSocket.on("connect", () => {
|
||||
this.connected = true;
|
||||
console.log(
|
||||
"Opened new connection to target from",
|
||||
this.source.address,
|
||||
this.source.port
|
||||
);
|
||||
|
||||
if (this.msgBuffer.length > 0) {
|
||||
requestIdleCallback(() => {
|
||||
this.send();
|
||||
});
|
||||
}
|
||||
});
|
||||
this.writeSocket.on("message", (fromMsg) => {
|
||||
this.#resetIdleTimeout();
|
||||
|
@ -180,7 +189,7 @@ class Connection {
|
|||
send(msg) {
|
||||
this.#resetIdleTimeout();
|
||||
|
||||
if (msg && (this.writing || !this.writeSocket)) {
|
||||
if (msg && (!this.connected || this.writing || !this.writeSocket)) {
|
||||
this.msgBuffer.push(msg);
|
||||
return;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue