protected override void ChannelRead0(IChannelHandlerContext context, IByteBuffer input)
        {
            if (_logger.IsTrace)
            {
                _logger.Trace($"Channel read {nameof(NettyHandshakeHandler)} from {context.Channel.RemoteAddress}");
            }

            if (_role == HandshakeRole.Recipient)
            {
                if (_logger.IsTrace)
                {
                    _logger.Trace($"AUTH received from {context.Channel.RemoteAddress}");
                }
                byte[] authData = new byte[input.ReadableBytes];
                input.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}");
                }
                IByteBuffer buffer = PooledByteBufferAllocator.Default.Buffer();
                buffer.WriteBytes(ack.Data);
                context.WriteAndFlushAsync(buffer);
            }
            else
            {
                if (_logger.IsTrace)
                {
                    _logger.Trace($"Received ACK from {RemoteId} @ {context.Channel.RemoteAddress}");
                }
                byte[] ackData = new byte[input.ReadableBytes];
                input.ReadBytes(ackData);
                _service.Agree(_handshake, new Packet(ackData));
            }

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

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

            if (_role == HandshakeRole.Recipient)
            {
                if (_logger.IsTrace)
                {
                    _logger.Trace($"Registering {nameof(LengthFieldBasedFrameDecoder)}  for {RemoteId} @ {context.Channel.RemoteAddress}");
                }
                context.Channel.Pipeline.AddLast("enc-handshake-dec", new LengthFieldBasedFrameDecoder(ByteOrder.BigEndian, ushort.MaxValue, 0, 2, 0, 0, true));
            }
            if (_logger.IsTrace)
            {
                _logger.Trace($"Registering {nameof(ReadTimeoutHandler)} for {RemoteId} @ {context.Channel.RemoteAddress}");
            }
            context.Channel.Pipeline.AddLast(new ReadTimeoutHandler(TimeSpan.FromSeconds(30))); // read timeout instead of session monitoring
            if (_logger.IsTrace)
            {
                _logger.Trace($"Registering {nameof(ZeroFrameDecoder)} for {RemoteId} @ {context.Channel.RemoteAddress}");
            }
            context.Channel.Pipeline.AddLast(new ZeroFrameDecoder(frameCipher, macProcessor, _logManager));
            if (_logger.IsTrace)
            {
                _logger.Trace($"Registering {nameof(ZeroFrameEncoder)} for {RemoteId} @ {context.Channel.RemoteAddress}");
            }
            context.Channel.Pipeline.AddLast(new ZeroFrameEncoder(frameCipher, macProcessor, _logManager));
            if (_logger.IsTrace)
            {
                _logger.Trace($"Registering {nameof(ZeroFrameMerger)} for {RemoteId} @ {context.Channel.RemoteAddress}");
            }
            context.Channel.Pipeline.AddLast(new ZeroFrameMerger(_logManager));
            if (_logger.IsTrace)
            {
                _logger.Trace($"Registering {nameof(ZeroPacketSplitter)} for {RemoteId} @ {context.Channel.RemoteAddress}");
            }
            context.Channel.Pipeline.AddLast(new ZeroPacketSplitter(_logManager));

            PacketSender packetSender = new PacketSender(_serializationService, _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(ZeroNettyP2PHandler)} for {RemoteId} @ {context.Channel.RemoteAddress}");
            }
            ZeroNettyP2PHandler handler = new ZeroNettyP2PHandler(_session, _logManager);

            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>();
        }
        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()}");
                }
            }
        }
示例#3
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()}");
                }
            }
        }