Compare commits

..

No commits in common. "eb4a8f1c9e54056ee00e58d7dfb8b08745d4f52e" and "f457294772e5bb3af36c9b463fe7bcc48ad3b462" have entirely different histories.

1 changed files with 10 additions and 85 deletions

View File

@ -51,11 +51,6 @@ class Channel {
}
async open(handshake) {
if (this._slave) {
await this._mux.pullLocal();
await this._mux.pullFree();
}
const freeLen = this._mux._free.length;
const id =
this._mux._free.length > 0
? this._mux._free.pop()
@ -65,14 +60,6 @@ class Channel {
this._localId = id + 1;
this._mux._local[id] = this;
if (this._slave) {
if (freeLen > 0) {
await this._mux.popFree(id);
} else {
await this._mux.pushLocal(id);
}
}
if (this._remoteId === 0) {
this._info.outgoing.push(this._localId);
}
@ -94,6 +81,7 @@ class Channel {
if (this._handshake) this._handshake.encode(state, handshake);
this._mux._write0(state.buffer);
await this._mux.syncState?.();
}
_dec() {
@ -108,6 +96,7 @@ class Channel {
async _fullyOpenSoon() {
this._mux._remote[this._remoteId - 1].session = this;
queueTick(this._fullyOpen.bind(this));
await this._mux.syncState?.();
}
_fullyOpen() {
@ -153,16 +142,9 @@ class Channel {
if (this._remoteId > 0) {
this._mux._remote[this._remoteId - 1] = null;
this._remoteId = 0;
if (this._slave) {
await this._mux.pullFree();
}
// If remote has acked, we can reuse the local id now
// otherwise, we need to wait for the "ack" to arrive
this._mux._free.push(this._localId - 1);
if (this._slave) {
await this._mux.pushFree(this._localId - 1);
}
}
this._mux._local[this._localId - 1] = null;
@ -172,6 +154,7 @@ class Channel {
this._track(this.onclose(isRemote, this));
if (this._active === 0) this._destroy();
await this._mux.syncState?.();
}
_destroy() {
@ -515,7 +498,7 @@ module.exports = class Protomux {
const type = c.uint.decode(state);
if (remoteId === 0) {
this._oncontrolsession(type, state);
await this._oncontrolsession(type, state);
return;
}
@ -530,10 +513,11 @@ module.exports = class Protomux {
return;
}
await this.syncState?.();
r.session._recv(type, state);
}
_oncontrolsession(type, state) {
async _oncontrolsession(type, state) {
switch (type) {
case 0:
this._onbatch(state);
@ -551,6 +535,8 @@ module.exports = class Protomux {
this._onclosesession(state);
break;
}
await this.syncState?.();
}
_bufferMessage(r, type, { buffer, start, end }) {
@ -605,15 +591,12 @@ module.exports = class Protomux {
const info = this._get(protocol, id);
if (this._slave) {
await this._mux.pullRemote();
await this._mux.syncState?.();
}
// allow the remote to grow the ids by one
if (this._remote.length === rid) {
this._remote.push(null);
if (this._slave) {
await this._mux.pushRemote(this._remote.length - 1);
}
}
if (rid >= this._remote.length || this._remote[rid] !== null) {
@ -625,14 +608,8 @@ module.exports = class Protomux {
const session = this._local[localId - 1];
if (session === null) {
if (this._slave) {
await this._mux.pullFree();
}
// we already closed the channel - ignore
this._free.push(localId - 1);
if (this._slave) {
await this._mux.pushFree(localId - 1);
}
return;
}
@ -657,14 +634,10 @@ module.exports = class Protomux {
info.pairing++;
info.incoming.push(remoteId);
if (this._slave) {
await this._mux.pushRemotes();
}
this._requestSession(protocol, id, info).catch(this._safeDestroyBound);
}
async _onrejectsession(state) {
_onrejectsession(state) {
const localId = c.uint.decode(state);
// TODO: can be done smarter...
@ -676,15 +649,7 @@ module.exports = class Protomux {
const session = this._local[localId - 1];
if (this._slave) {
await this._mux.pullFree(localId - 1);
}
this._free.push(localId - 1);
if (this._slave) {
await this._mux.pushFree(localId - 1);
}
if (session !== null) session._close(true);
this._gc(info);
@ -773,46 +738,6 @@ module.exports = class Protomux {
if (s !== null) s._close(true);
}
}
async popFree(id) {
await this.userData.syncProtomux("popFree", id);
}
async pushFree(id) {
await this.userData.syncProtomux("pushFree", id);
}
async pushLocal(id) {
await this.userData.syncProtomux("pushLocal", id);
}
async pushRemote(id) {
await this.userData.syncProtomux("pushRemote", id);
}
async pullLocal() {
const ids = await this.userData.syncProtomux("pullLocal");
ids.forEach((item) => {
item = parseInt(item);
if (typeof this._local[item] === "undefined") {
this._local[item] = null;
}
});
}
async pullFree() {
const ids = await this.userData.syncProtomux("pullFree");
this._free = Array.from(new Set([...this._free, ...ids]));
this._free = this._free.filter((item) => item !== null);
}
async pullRemote() {
const ids = await this.userData.syncProtomux("pullRemote");
ids.forEach((item) => {
item = parseInt(item);
if (typeof this._remote[item] === "undefined") {
this._remote[item] = null;
}
});
}
};
function noop() {}