예제 #1
0
        private void InitializeChannel(IChannel channel, EncryptionHandshakeRole role, NodeId remoteId = null, string remoteHost = null, int?remotePort = null)
        {
            var        connectionType = remoteId == null ? ClientConnectionType.In : ClientConnectionType.Out;
            P2PSession p2PSession     = new P2PSession(
                LocalNodeId,
                _localPort,
                _serializationService,
                _synchronizationManager,
                _logManager)
            {
                ClientConnectionType = connectionType
            };

            //This is the first moment we get confirmed publicKey of remote node in case of outgoing connections
            if (connectionType == ClientConnectionType.Out)
            {
                if (_logger.IsInfoEnabled)
                {
                    _logger.Info($"Initializing {connectionType.ToString().ToUpper()} channel{(connectionType == ClientConnectionType.Out ? $": {remoteId}@{remoteHost}:{remoteId}" : string.Empty)}");
                }

                p2PSession.RemoteNodeId = remoteId;
                p2PSession.RemoteHost   = remoteHost;
                p2PSession.RemotePort   = remotePort;
                ConnectionInitialized?.Invoke(this, new ConnectionInitializedEventArgs(p2PSession, connectionType));
            }

            var handshakeHandler = new NettyHandshakeHandler(_encryptionHandshakeService, p2PSession, role, remoteId, _logger);

            handshakeHandler.HandshakeInitialized += (s, e) =>
            {
                //This is the first moment we get confirmed publicKey of remote node in case of incoming connections
                if (connectionType == ClientConnectionType.In)
                {
                    if (_logger.IsInfoEnabled)
                    {
                        _logger.Info($"Initializing {connectionType.ToString().ToUpper()} channel {p2PSession.RemoteNodeId}@{p2PSession.RemoteHost}:{p2PSession.RemotePort}");
                    }

                    ConnectionInitialized?.Invoke(this, new ConnectionInitializedEventArgs(p2PSession, connectionType));
                }
            };

            IChannelPipeline pipeline = channel.Pipeline;

            pipeline.AddLast(new LoggingHandler(connectionType.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.IsInfoEnabled)
                {
                    _logger.Info($"Channel disconnection: {p2PSession.RemoteNodeId}");
                }
                await p2PSession.DisconnectAsync(DisconnectReason.ClientQuitting, DisconnectType.Remote);
            });
        }
예제 #2
0
 /// <summary>
 ///		Lanza el evento <see cref="ConnectionInitialized"/>
 /// </summary>
 internal void RaiseEventConnectionInitialized()
 {
     ConnectionInitialized?.Invoke(this, EventArgs.Empty);
 }
예제 #3
0
 public void TriggerConnectionInitialized(IP2PSession session, ClientConnectionType clientConnectionType = ClientConnectionType.Out)
 {
     ConnectionInitialized?.Invoke(this, new ConnectionInitializedEventArgs(session, clientConnectionType));
 }
예제 #4
0
 public void TriggerConnectionInitialized(IP2PSession session)
 {
     ConnectionInitialized?.Invoke(this, new ConnectionInitializedEventArgs(session));
 }