* Refactor Peer and MultiSocket classes to handle new peer channels more efficiently and cleanly.

This commit is contained in:
Derrick Hammer 2023-04-16 03:06:34 -04:00
parent 84bc6ce1cb
commit 518bdca8ad
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
2 changed files with 22 additions and 18 deletions

View File

@ -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

View File

@ -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<void> {
return this._proxy.handleClosePeer(this);
}
protected async handleChannelOnOpen(m: any): Promise<void> {
await this._proxy.handleNewPeerChannel(this._peer, this._channel);
await this._proxy.handleNewPeerChannel(this);
}
}