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} */
|
/** @type {NodeJS.Timer | null} */
|
||||||
reachabilityTimeout = null;
|
reachabilityTimeout = null;
|
||||||
|
|
||||||
|
connected = false;
|
||||||
writing = false;
|
writing = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -136,14 +137,22 @@ class Connection {
|
||||||
}
|
}
|
||||||
|
|
||||||
initTargetSocket() {
|
initTargetSocket() {
|
||||||
|
this.connected = false;
|
||||||
clearTimeout(this.retryTimeout);
|
clearTimeout(this.retryTimeout);
|
||||||
|
|
||||||
this.writeSocket.on("connect", () => {
|
this.writeSocket.on("connect", () => {
|
||||||
|
this.connected = true;
|
||||||
console.log(
|
console.log(
|
||||||
"Opened new connection to target from",
|
"Opened new connection to target from",
|
||||||
this.source.address,
|
this.source.address,
|
||||||
this.source.port
|
this.source.port
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if (this.msgBuffer.length > 0) {
|
||||||
|
requestIdleCallback(() => {
|
||||||
|
this.send();
|
||||||
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
this.writeSocket.on("message", (fromMsg) => {
|
this.writeSocket.on("message", (fromMsg) => {
|
||||||
this.#resetIdleTimeout();
|
this.#resetIdleTimeout();
|
||||||
|
@ -180,7 +189,7 @@ class Connection {
|
||||||
send(msg) {
|
send(msg) {
|
||||||
this.#resetIdleTimeout();
|
this.#resetIdleTimeout();
|
||||||
|
|
||||||
if (msg && (this.writing || !this.writeSocket)) {
|
if (msg && (!this.connected || this.writing || !this.writeSocket)) {
|
||||||
this.msgBuffer.push(msg);
|
this.msgBuffer.push(msg);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue