From 518bdca8ad61388be0e0a320833b0b80247ad974 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Sun, 16 Apr 2023 03:06:34 -0400 Subject: [PATCH] * Refactor Peer and MultiSocket classes to handle new peer channels more efficiently and cleanly. --- src/proxies/multiSocket.ts | 34 ++++++++++++++++----------------- src/proxies/multiSocket/peer.ts | 6 +++++- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/src/proxies/multiSocket.ts b/src/proxies/multiSocket.ts index 5ad7bc2..20b1086 100644 --- a/src/proxies/multiSocket.ts +++ b/src/proxies/multiSocket.ts @@ -113,14 +113,14 @@ export default class MultiSocketProxy extends Proxy { return this._sockets; } - handleNewPeerChannel(peer: Peer, channel: any) { - this.update(peer.socket.remotePublicKey, { peer }); + handleNewPeerChannel(peer: Peer) { + this.update(peer.stream.remotePublicKey, { peer }); - this._registerOpenSocketMessage(peer, channel); - this._registerWriteSocketMessage(peer, channel); - this._registerCloseSocketMessage(peer, channel); - this._registerTimeoutSocketMessage(peer, channel); - this._registerErrorSocketMessage(peer, channel); + this._registerOpenSocketMessage(peer); + this._registerWriteSocketMessage(peer); + this._registerCloseSocketMessage(peer); + this._registerTimeoutSocketMessage(peer); + this._registerErrorSocketMessage(peer); } async handleClosePeer(peer: Peer) { @@ -175,9 +175,9 @@ export default class MultiSocketProxy extends Proxy { return socket; } - private _registerOpenSocketMessage(peer: Peer, channel: any) { + private _registerOpenSocketMessage(peer: Peer) { const self = this; - const message = channel.addMessage({ + const message = peer.channel.addMessage({ encoding: { preencode: json.preencode, 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 message = channel.addMessage({ + const message = peer.channel.addMessage({ encoding: writeSocketEncoding, onmessage(m: WriteSocketRequest) { 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 message = channel.addMessage({ + const message = peer.channel.addMessage({ encoding: socketEncoding, onmessage(m: CloseSocketRequest) { 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 message = channel.addMessage({ + const message = peer.channel.addMessage({ encoding: socketEncoding, onmessage(m: SocketRequest) { // @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 message = channel.addMessage({ + const message = peer.channel.addMessage({ encoding: errorSocketEncoding, onmessage(m: ErrorSocketRequest) { // @ts-ignore diff --git a/src/proxies/multiSocket/peer.ts b/src/proxies/multiSocket/peer.ts index 1ba59ae..9fa08d3 100644 --- a/src/proxies/multiSocket/peer.ts +++ b/src/proxies/multiSocket/peer.ts @@ -6,11 +6,15 @@ export default class Peer extends BasePeer { protected declare _proxy: MultiSocketProxy; protected async initSocket() {} + get stream(): any { + return this._muxer.stream; + } + protected async handleChannelOnClose(socket: Socket): Promise { return this._proxy.handleClosePeer(this); } protected async handleChannelOnOpen(m: any): Promise { - await this._proxy.handleNewPeerChannel(this._peer, this._channel); + await this._proxy.handleNewPeerChannel(this); } }