Beispiel #1
0
        private void AddSession(ISession session, Peer peer)
        {
            bool newSessionIsIn     = session.Direction == ConnectionDirection.In;
            bool newSessionIsOut    = !newSessionIsIn;
            bool peerIsDisconnected = PeerIsDisconnected(peer);

            if (peerIsDisconnected)
            {
                if (newSessionIsIn)
                {
                    peer.InSession = session;
                }
                else
                {
                    peer.OutSession = session;
                }
            }
            else
            {
                bool peerHasAnOpenInSession  = !peer.InSession?.IsClosing ?? false;
                bool peerHasAnOpenOutSession = !peer.OutSession?.IsClosing ?? false;

                if (newSessionIsIn && peerHasAnOpenInSession || newSessionIsOut && peerHasAnOpenOutSession)
                {
                    if (_logger.IsDebug)
                    {
                        _logger.Debug($"Disconnecting a {session} - already connected");
                    }
                    session.InitiateDisconnect(DisconnectReason.AlreadyConnected, "same");
                }
                else if (newSessionIsIn && peerHasAnOpenOutSession || newSessionIsOut && peerHasAnOpenInSession)
                {
                    // disconnecting the new session as it lost to the existing one
                    ConnectionDirection directionToKeep = ChooseDirectionToKeep(session.RemoteNodeId);
                    if (session.Direction != directionToKeep)
                    {
                        if (_logger.IsDebug)
                        {
                            _logger.Debug($"Disconnecting a new {session} - {directionToKeep} session already connected");
                        }
                        session.InitiateDisconnect(DisconnectReason.AlreadyConnected, "same");
                    }
                    // replacing existing session with the new one as the new one won
                    else if (newSessionIsIn)
                    {
                        peer.InSession = session;
                        if (_logger.IsDebug)
                        {
                            _logger.Debug($"Disconnecting an existing {session} - {directionToKeep} session to replace");
                        }
                        peer.OutSession?.InitiateDisconnect(DisconnectReason.AlreadyConnected, "same");
                    }
                    else
                    {
                        peer.OutSession = session;
                        if (_logger.IsDebug)
                        {
                            _logger.Debug($"Disconnecting an existing {session} - {directionToKeep} session to replace");
                        }
                        peer.OutSession?.InitiateDisconnect(DisconnectReason.AlreadyConnected, "same");
                    }
                }
            }

            AddActivePeer(peer.Node.Id, peer, newSessionIsIn ? "new IN session" : "new OUT session");
        }
Beispiel #2
0
 private static bool PeerIsDisconnected(Peer peer)
 {
     return((peer.InSession?.IsClosing ?? true) && (peer.OutSession?.IsClosing ?? true));
 }
Beispiel #3
0
 private static bool IsConnected(Peer peer)
 {
     return(!(peer.InSession?.IsClosing ?? true) || !(peer.OutSession?.IsClosing ?? true));
 }