Example #1
0
        private void OnDisconnected(object sender, DisconnectEventArgs e)
        {
            var session = (ISession)sender;

            ToggleSessionEventListeners(session, false);
            if (_logger.IsTrace)
            {
                _logger.Trace($"|NetworkTrace| {session} closing");
            }

            if (session.State != SessionState.Disconnected)
            {
                throw new InvalidAsynchronousStateException($"Invalid session state in {nameof(OnDisconnected)} - {session.State}");
            }

            if (_logger.IsTrace)
            {
                _logger.Trace($"|NetworkTrace| peer disconnected event in PeerManager - {session} {e.DisconnectReason} {e.DisconnectType}");
            }

            if (session.RemoteNodeId == null)
            {
                // this happens when we have a disconnect on incoming connection before handshake
                if (_logger.IsTrace)
                {
                    _logger.Trace($"Disconnect on session with no RemoteNodeId - {session}");
                }
                return;
            }

            if (_activePeers.TryGetValue(session.RemoteNodeId, out var activePeer))
            {
                //we want to update reputation always
                _stats.ReportDisconnect(session.Node, e.DisconnectType, e.DisconnectReason);

                if (activePeer.InSession?.SessionId != session.SessionId && activePeer.OutSession?.SessionId != session.SessionId)
                {
                    if (_logger.IsTrace)
                    {
                        _logger.Trace($"Received disconnect on a different session than the active peer runs. Ignoring. Id: {activePeer.Node.Id}");
                    }
                    return;
                }

                DeactivatePeerIfDisconnected(activePeer, "session disconnected");

                if (_logger.IsTrace)
                {
                    _stats.DumpNodeStats(activePeer.Node);
                }
                _peerUpdateRequested.Set();
            }
        }