* Refactor Peer and MultiSocket classes to handle new peer channels more efficiently and cleanly.
This commit is contained in:
parent
84bc6ce1cb
commit
518bdca8ad
|
@ -113,14 +113,14 @@ export default class MultiSocketProxy extends Proxy {
|
||||||
return this._sockets;
|
return this._sockets;
|
||||||
}
|
}
|
||||||
|
|
||||||
handleNewPeerChannel(peer: Peer, channel: any) {
|
handleNewPeerChannel(peer: Peer) {
|
||||||
this.update(peer.socket.remotePublicKey, { peer });
|
this.update(peer.stream.remotePublicKey, { peer });
|
||||||
|
|
||||||
this._registerOpenSocketMessage(peer, channel);
|
this._registerOpenSocketMessage(peer);
|
||||||
this._registerWriteSocketMessage(peer, channel);
|
this._registerWriteSocketMessage(peer);
|
||||||
this._registerCloseSocketMessage(peer, channel);
|
this._registerCloseSocketMessage(peer);
|
||||||
this._registerTimeoutSocketMessage(peer, channel);
|
this._registerTimeoutSocketMessage(peer);
|
||||||
this._registerErrorSocketMessage(peer, channel);
|
this._registerErrorSocketMessage(peer);
|
||||||
}
|
}
|
||||||
|
|
||||||
async handleClosePeer(peer: Peer) {
|
async handleClosePeer(peer: Peer) {
|
||||||
|
@ -175,9 +175,9 @@ export default class MultiSocketProxy extends Proxy {
|
||||||
return socket;
|
return socket;
|
||||||
}
|
}
|
||||||
|
|
||||||
private _registerOpenSocketMessage(peer: Peer, channel: any) {
|
private _registerOpenSocketMessage(peer: Peer) {
|
||||||
const self = this;
|
const self = this;
|
||||||
const message = channel.addMessage({
|
const message = peer.channel.addMessage({
|
||||||
encoding: {
|
encoding: {
|
||||||
preencode: json.preencode,
|
preencode: json.preencode,
|
||||||
encode: json.encode,
|
encode: json.encode,
|
||||||
|
@ -223,9 +223,9 @@ export default class MultiSocketProxy extends Proxy {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private _registerWriteSocketMessage(peer: Peer, channel: any) {
|
private _registerWriteSocketMessage(peer: Peer) {
|
||||||
const self = this;
|
const self = this;
|
||||||
const message = channel.addMessage({
|
const message = peer.channel.addMessage({
|
||||||
encoding: writeSocketEncoding,
|
encoding: writeSocketEncoding,
|
||||||
onmessage(m: WriteSocketRequest) {
|
onmessage(m: WriteSocketRequest) {
|
||||||
self._sockets.get(m.id)?.push(m.data);
|
self._sockets.get(m.id)?.push(m.data);
|
||||||
|
@ -236,9 +236,9 @@ export default class MultiSocketProxy extends Proxy {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private _registerCloseSocketMessage(peer: Peer, channel: any) {
|
private _registerCloseSocketMessage(peer: Peer) {
|
||||||
const self = this;
|
const self = this;
|
||||||
const message = channel.addMessage({
|
const message = peer.channel.addMessage({
|
||||||
encoding: socketEncoding,
|
encoding: socketEncoding,
|
||||||
onmessage(m: CloseSocketRequest) {
|
onmessage(m: CloseSocketRequest) {
|
||||||
self._sockets.get(m.id)?.end();
|
self._sockets.get(m.id)?.end();
|
||||||
|
@ -249,9 +249,9 @@ export default class MultiSocketProxy extends Proxy {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private _registerTimeoutSocketMessage(peer: Peer, channel: any) {
|
private _registerTimeoutSocketMessage(peer: Peer) {
|
||||||
const self = this;
|
const self = this;
|
||||||
const message = channel.addMessage({
|
const message = peer.channel.addMessage({
|
||||||
encoding: socketEncoding,
|
encoding: socketEncoding,
|
||||||
onmessage(m: SocketRequest) {
|
onmessage(m: SocketRequest) {
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
|
@ -263,9 +263,9 @@ export default class MultiSocketProxy extends Proxy {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private _registerErrorSocketMessage(peer: Peer, channel: any) {
|
private _registerErrorSocketMessage(peer: Peer) {
|
||||||
const self = this;
|
const self = this;
|
||||||
const message = channel.addMessage({
|
const message = peer.channel.addMessage({
|
||||||
encoding: errorSocketEncoding,
|
encoding: errorSocketEncoding,
|
||||||
onmessage(m: ErrorSocketRequest) {
|
onmessage(m: ErrorSocketRequest) {
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
|
|
|
@ -6,11 +6,15 @@ export default class Peer extends BasePeer {
|
||||||
protected declare _proxy: MultiSocketProxy;
|
protected declare _proxy: MultiSocketProxy;
|
||||||
protected async initSocket() {}
|
protected async initSocket() {}
|
||||||
|
|
||||||
|
get stream(): any {
|
||||||
|
return this._muxer.stream;
|
||||||
|
}
|
||||||
|
|
||||||
protected async handleChannelOnClose(socket: Socket): Promise<void> {
|
protected async handleChannelOnClose(socket: Socket): Promise<void> {
|
||||||
return this._proxy.handleClosePeer(this);
|
return this._proxy.handleClosePeer(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected async handleChannelOnOpen(m: any): Promise<void> {
|
protected async handleChannelOnOpen(m: any): Promise<void> {
|
||||||
await this._proxy.handleNewPeerChannel(this._peer, this._channel);
|
await this._proxy.handleNewPeerChannel(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue