private void InitializeChannel(IChannel channel, ConnectionDirection connectionDirection, NodeId remoteId = null, string remoteHost = null, int?remotePort = null, INodeStats nodeStats = null) { if (connectionDirection == ConnectionDirection.In) { Metrics.IncomingConnections++; } else { Metrics.OutgoingConnections++; } P2PSession session = new P2PSession( LocalNodeId, remoteId, _localPort, connectionDirection, _serializationService, _synchronizationManager, _nodeStatsProvider, nodeStats, _logManager, channel, _perfService, _blockTree, _transactionPool, _timestamp); if (connectionDirection == ConnectionDirection.Out) { if (_logger.IsTrace) { _logger.Trace($"Initializing {connectionDirection.ToString().ToUpper()} channel{(connectionDirection == ConnectionDirection.Out ? $": {remoteId}@{remoteHost}:{remotePort}" : string.Empty)}"); } // this is the first moment we get confirmed publicKey of remote node in case of outgoing connections session.RemoteNodeId = remoteId; session.RemoteHost = remoteHost; session.RemotePort = remotePort; } SessionCreated?.Invoke(this, new SessionEventArgs(session)); HandshakeRole role = connectionDirection == ConnectionDirection.In ? HandshakeRole.Recipient : HandshakeRole.Initiator; var handshakeHandler = new NettyHandshakeHandler(_encryptionHandshakeService, session, role, remoteId, _logManager); IChannelPipeline pipeline = channel.Pipeline; pipeline.AddLast(new LoggingHandler(connectionDirection.ToString().ToUpper(), DotNetty.Handlers.Logging.LogLevel.TRACE)); pipeline.AddLast("enc-handshake-dec", new LengthFieldBasedFrameDecoder(ByteOrder.BigEndian, ushort.MaxValue, 0, 2, 0, 0, true)); pipeline.AddLast("enc-handshake-handler", handshakeHandler); channel.CloseCompletion.ContinueWith(async x => { if (_logger.IsTrace) { _logger.Trace($"Channel disconnected: {session.RemoteNodeId}"); } await session.DisconnectAsync(DisconnectReason.ClientQuitting, DisconnectType.Remote); SessionClosing?.Invoke(this, new SessionEventArgs(session)); }); }
/// <summary> /// This method calls the transport level call to close the connection to the listener. /// </summary> /// <param name="reasonForClose"> /// Message describing why the session is closing /// </param> /// <exception cref="PSRemotingTransportException"> /// If the transport call fails. /// </exception> internal override void CloseConnectionAsync(Exception reasonForClose) { // Raise the closing event SessionClosing.SafeInvoke(this, EventArgs.Empty); _transportManager.Close(reasonForClose); RemoteSessionStateMachineEventArgs closeCompletedArg = new RemoteSessionStateMachineEventArgs(RemoteSessionEvent.CloseCompleted); _stateMachine.RaiseEvent(closeCompletedArg); }
protected virtual void OnSessionClosing() { SessionStatus = SessionStatus.Closing; SessionClosing?.Invoke(this, new EventArgs()); }