コード例 #1
0
        public void Can_enable_snappy()
        {
            Session             session = new Session(30312, LimboLogs.Instance, _channel, new Node("127.0.0.1", 8545));
            ZeroNettyP2PHandler handler = new ZeroNettyP2PHandler(session, LimboLogs.Instance);

            _pipeline.Get <ZeroNettyP2PHandler>().Returns(handler);
            Assert.False(handler.SnappyEnabled);
            session.Handshake(TestItem.PublicKeyA);
            session.Init(5, _channelHandlerContext, _packetSender);
            session.EnableSnappy();
            Assert.True(handler.SnappyEnabled);
            _pipeline.Received().Get <ZeroPacketSplitter>();
            _pipeline.Received().AddBefore(Arg.Any <string>(), Arg.Any <string>(), Arg.Any <ZeroSnappyEncoder>());
        }
コード例 #2
0
        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>();
        }