protected override void ChannelRead0(IChannelHandlerContext context, IByteBuffer message)
        {
            if (_logger.IsTrace)
            {
                _logger.Trace($"Channel read {nameof(NettyHandshakeHandler)} from {context.Channel.RemoteAddress}");
            }
            if (message is IByteBuffer byteBuffer)
            {
                if (_role == HandshakeRole.Recipient)
                {
                    if (_logger.IsTrace)
                    {
                        _logger.Trace($"AUTH received from {context.Channel.RemoteAddress}");
                    }
                    byte[] authData = new byte[byteBuffer.ReadableBytes];
                    byteBuffer.ReadBytes(authData);
                    Packet ack = _service.Ack(_handshake, new Packet(authData));

                    //_p2PSession.RemoteNodeId = _remoteId;
                    if (_logger.IsTrace)
                    {
                        _logger.Trace($"Sending ACK to {RemoteId} @ {context.Channel.RemoteAddress}");
                    }
                    _buffer.WriteBytes(ack.Data);
                    context.WriteAndFlushAsync(_buffer);
                }
                else
                {
                    if (_logger.IsTrace)
                    {
                        _logger.Trace($"Received ACK from {RemoteId} @ {context.Channel.RemoteAddress}");
                    }
                    byte[] ackData = new byte[byteBuffer.ReadableBytes];
                    byteBuffer.ReadBytes(ackData);
                    _service.Agree(_handshake, new Packet(ackData));
                }

                _initCompletionSource?.SetResult(message);
                _session.Handshake(_handshake.RemoteNodeId);

                FrameCipher       frameCipher  = new FrameCipher(_handshake.Secrets.AesSecret);
                FrameMacProcessor macProcessor = new FrameMacProcessor(_session.RemoteNodeId, _handshake.Secrets);

                if (_logger.IsTrace)
                {
                    _logger.Trace($"Registering {nameof(NettyFrameDecoder)} for {RemoteId} @ {context.Channel.RemoteAddress}");
                }
                context.Channel.Pipeline.AddLast(new NettyFrameDecoder(frameCipher, macProcessor, _logger));
                if (_logger.IsTrace)
                {
                    _logger.Trace($"Registering {nameof(NettyFrameEncoder)} for {RemoteId} @ {context.Channel.RemoteAddress}");
                }
                context.Channel.Pipeline.AddLast(new NettyFrameEncoder(frameCipher, macProcessor, _logger));
                if (_logger.IsTrace)
                {
                    _logger.Trace($"Registering {nameof(NettyFrameMerger)} for {RemoteId} @ {context.Channel.RemoteAddress}");
                }
                context.Channel.Pipeline.AddLast(new NettyFrameMerger(_logger));
                if (_logger.IsTrace)
                {
                    _logger.Trace($"Registering {nameof(NettyPacketSplitter)} for {RemoteId} @ {context.Channel.RemoteAddress}");
                }
                context.Channel.Pipeline.AddLast(new NettyPacketSplitter());
                PacketSender packetSender = new PacketSender(_logManager);

                if (_logger.IsTrace)
                {
                    _logger.Trace($"Registering {nameof(PacketSender)} for {_session.RemoteNodeId} @ {context.Channel.RemoteAddress}");
                }
                context.Channel.Pipeline.AddLast(packetSender);

                if (_logger.IsTrace)
                {
                    _logger.Trace($"Registering {nameof(NettyP2PHandler)} for {RemoteId} @ {context.Channel.RemoteAddress}");
                }
                NettyP2PHandler handler = new NettyP2PHandler(_session, _logger);
                context.Channel.Pipeline.AddLast(_group, handler);

                handler.Init(packetSender, context);

                if (_logger.IsTrace)
                {
                    _logger.Trace($"Removing {nameof(NettyHandshakeHandler)}");
                }
                context.Channel.Pipeline.Remove(this);
                if (_logger.IsTrace)
                {
                    _logger.Trace($"Removing {nameof(LengthFieldBasedFrameDecoder)}");
                }
                context.Channel.Pipeline.Remove <LengthFieldBasedFrameDecoder>();
            }
            else
            {
                if (_logger.IsError)
                {
                    _logger.Error($"DIFFERENT TYPE OF DATA {message.GetType()}");
                }
            }
        }
Exemple #2
0
        public override void ChannelRead(IChannelHandlerContext context, object message)
        {
            _logger.Trace($"Channel read {nameof(NettyHandshakeHandler)} from {context.Channel.RemoteAddress}");
            if (message is IByteBuffer byteBuffer)
            {
                if (_role == EncryptionHandshakeRole.Recipient)
                {
                    if (_logger.IsDebugEnabled)
                    {
                        _logger.Debug($"AUTH received from {context.Channel.RemoteAddress}");
                    }
                    byte[] authData = new byte[byteBuffer.ReadableBytes];
                    byteBuffer.ReadBytes(authData);
                    Packet ack = _service.Ack(_handshake, new Packet(authData));
                    _remoteId = _handshake.RemoteNodeId;

                    if (_logger.IsDebugEnabled)
                    {
                        _logger.Debug($"Sending ACK to {_remoteId} @ {context.Channel.RemoteAddress}");
                    }
                    _buffer.WriteBytes(ack.Data);
                    context.WriteAndFlushAsync(_buffer);
                }
                else
                {
                    if (_logger.IsDebugEnabled)
                    {
                        _logger.Debug($"Received ACK from {_remoteId} @ {context.Channel.RemoteAddress}");
                    }
                    byte[] ackData = new byte[byteBuffer.ReadableBytes];
                    byteBuffer.ReadBytes(ackData);
                    _service.Agree(_handshake, new Packet(ackData));
                }

                _p2PSession.RemoteNodeId = _handshake.RemoteNodeId;
                //_p2PSession.RemoteHost = ((IPEndPoint)context.Channel.RemoteAddress).Address.ToString();
                //_p2PSession.RemotePort = ((IPEndPoint)context.Channel.RemoteAddress).Port;

                _initCompletionSource?.SetResult(message);
                HandshakeInitialized?.Invoke(this, new HandshakeInitializedEventArgs());

                FrameCipher       frameCipher  = new FrameCipher(_handshake.Secrets.AesSecret);
                FrameMacProcessor macProcessor = new FrameMacProcessor(_handshake.Secrets);

                if (_logger.IsTraceEnabled)
                {
                    _logger.Trace($"Registering {nameof(NettyFrameDecoder)} for {_remoteId} @ {context.Channel.RemoteAddress}");
                }
                context.Channel.Pipeline.AddLast(new NettyFrameDecoder(frameCipher, macProcessor, _logger));
                if (_logger.IsTraceEnabled)
                {
                    _logger.Trace($"Registering {nameof(NettyFrameEncoder)} for {_remoteId} @ {context.Channel.RemoteAddress}");
                }
                context.Channel.Pipeline.AddLast(new NettyFrameEncoder(frameCipher, macProcessor, _logger));
                if (_logger.IsTraceEnabled)
                {
                    _logger.Trace($"Registering {nameof(NettyFrameMerger)} for {_remoteId} @ {context.Channel.RemoteAddress}");
                }
                context.Channel.Pipeline.AddLast(new NettyFrameMerger(_logger));
                if (_logger.IsTraceEnabled)
                {
                    _logger.Trace($"Registering {nameof(NettyPacketSplitter)} for {_remoteId} @ {context.Channel.RemoteAddress}");
                }
                context.Channel.Pipeline.AddLast(new NettyPacketSplitter());

                Multiplexor multiplexor = new Multiplexor(_logger);
                if (_logger.IsTraceEnabled)
                {
                    _logger.Trace($"Registering {nameof(Multiplexor)} for {_p2PSession.RemoteNodeId} @ {context.Channel.RemoteAddress}");
                }
                context.Channel.Pipeline.AddLast(multiplexor);

                if (_logger.IsTraceEnabled)
                {
                    _logger.Trace($"Registering {nameof(NettyP2PHandler)} for {_remoteId} @ {context.Channel.RemoteAddress}");
                }
                NettyP2PHandler handler = new NettyP2PHandler(_p2PSession, _logger);
                context.Channel.Pipeline.AddLast(handler);

                handler.Init(multiplexor, context);

                if (_logger.IsTraceEnabled)
                {
                    _logger.Trace($"Removing {nameof(NettyHandshakeHandler)}");
                }
                context.Channel.Pipeline.Remove(this);
                if (_logger.IsTraceEnabled)
                {
                    _logger.Trace($"Removing {nameof(LengthFieldBasedFrameDecoder)}");
                }
                context.Channel.Pipeline.Remove <LengthFieldBasedFrameDecoder>();
            }
            else
            {
                if (_logger.IsErrorEnabled)
                {
                    _logger.Error($"DIFFERENT TYPE OF DATA {message.GetType()}");
                }
            }
        }