mirror of
https://github.com/cerc-io/watcher-ts
synced 2025-02-09 03:22:50 +00:00
Refactor protocol change handler
This commit is contained in:
parent
582a8e10e3
commit
759c593b14
@ -239,13 +239,18 @@ export class Peer {
|
|||||||
async _handleChangeProtocols ({ peerId, protocols }: { peerId: PeerId, protocols: string[] }) {
|
async _handleChangeProtocols ({ peerId, protocols }: { peerId: PeerId, protocols: string[] }) {
|
||||||
assert(this._node);
|
assert(this._node);
|
||||||
|
|
||||||
// Handle protocol and open stream from only one peer
|
// Ignore self protocol changes
|
||||||
if (this._node.peerId.toString() > peerId.toString()) {
|
if (peerId.equals(this._node.peerId)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return if stream is self peer or chat protocol is not handled by remote peer
|
// Ignore if chat protocol is not handled by remote peer
|
||||||
if (peerId.equals(this._node.peerId) || !protocols.includes(CHAT_PROTOCOL)) {
|
if (!protocols.includes(CHAT_PROTOCOL)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle protocol and open stream from only one side
|
||||||
|
if (this._node.peerId.toString() > peerId.toString()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -308,14 +313,14 @@ export class Peer {
|
|||||||
if (remoteConnections.length > 1) {
|
if (remoteConnections.length > 1) {
|
||||||
// Close new connection if using relayed multiaddr
|
// Close new connection if using relayed multiaddr
|
||||||
if (connection.remoteAddr.protoNames().includes('p2p-circuit')) {
|
if (connection.remoteAddr.protoNames().includes('p2p-circuit')) {
|
||||||
console.log('Closing new connection for already connected peer');
|
console.log('Closing new relayed connection in favor of existing connection');
|
||||||
await connection.close();
|
await connection.close();
|
||||||
console.log('Closed');
|
console.log('Closed');
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('Closing exisiting connections for new webrtc connection');
|
console.log('Closing exisiting connections in favor of new webrtc connection');
|
||||||
// Close existing connections if new connection is not using relayed multiaddr (so it is a webrtc connection)
|
// Close existing connections if new connection is not using relayed multiaddr (so it is a webrtc connection)
|
||||||
const closeConnectionPromises = remoteConnections.filter(remoteConnection => remoteConnection.id !== connection.id)
|
const closeConnectionPromises = remoteConnections.filter(remoteConnection => remoteConnection.id !== connection.id)
|
||||||
.map(remoteConnection => remoteConnection.close());
|
.map(remoteConnection => remoteConnection.close());
|
||||||
@ -365,8 +370,9 @@ export class Peer {
|
|||||||
|
|
||||||
const peerConnections = this._node.getConnections(disconnectedPeerId);
|
const peerConnections = this._node.getConnections(disconnectedPeerId);
|
||||||
|
|
||||||
|
// If no connections left to the peer
|
||||||
if (!peerConnections.length) {
|
if (!peerConnections.length) {
|
||||||
// Stop connection check for disconnected peer
|
// Stop the heartbeat check
|
||||||
this._peerHeartbeatChecker?.stop(disconnectedPeerId);
|
this._peerHeartbeatChecker?.stop(disconnectedPeerId);
|
||||||
|
|
||||||
if (disconnectedPeerId.toString() === this._relayNodeMultiaddr?.getPeerId()) {
|
if (disconnectedPeerId.toString() === this._relayNodeMultiaddr?.getPeerId()) {
|
||||||
|
Loading…
Reference in New Issue
Block a user