Example #1
0
        public void Wrong_pong_will_get_ignored()
        {
            Node node = new(TestItem.PublicKeyB, _host, _port);
            INodeLifecycleManager?manager = _discoveryManager.GetNodeLifecycleManager(node);

            Assert.AreEqual(NodeLifecycleState.New, manager?.State);

            PongMsg msgI = new (_nodeIds[0], GetExpirationTime(), new byte[32]);

            msgI.FarAddress = new IPEndPoint(IPAddress.Parse(_host), _port);
            _discoveryManager.OnIncomingMsg(msgI);

            Assert.AreEqual(NodeLifecycleState.New, manager?.State);
        }
    protected override void ChannelRead0(IChannelHandlerContext ctx, DatagramPacket packet)
    {
        IByteBuffer content = packet.Content;
        EndPoint    address = packet.Sender;

        byte[] msgBytes = new byte[content.ReadableBytes];
        content.ReadBytes(msgBytes);

        Interlocked.Add(ref Metrics.DiscoveryBytesReceived, msgBytes.Length);

        if (msgBytes.Length < 98)
        {
            if (_logger.IsDebug)
            {
                _logger.Debug($"Incorrect discovery message, length: {msgBytes.Length}, sender: {address}");
            }
            return;
        }

        byte typeRaw = msgBytes[97];

        if (!Enum.IsDefined(typeof(MsgType), (int)typeRaw))
        {
            if (_logger.IsDebug)
            {
                _logger.Debug($"Unsupported message type: {typeRaw}, sender: {address}, message {msgBytes.ToHexString()}");
            }
            return;
        }

        MsgType type = (MsgType)typeRaw;

        if (_logger.IsTrace)
        {
            _logger.Trace($"Received message: {type}");
        }

        DiscoveryMsg msg;

        try
        {
            msg            = Deserialize(type, msgBytes);
            msg.FarAddress = (IPEndPoint)address;
        }
        catch (Exception e)
        {
            if (_logger.IsDebug)
            {
                _logger.Debug($"Error during deserialization of the message, type: {type}, sender: {address}, msg: {msgBytes.ToHexString()}, {e.Message}");
            }
            return;
        }

        try
        {
            ReportMsgByType(msg);

            if (!ValidateMsg(msg, type, address, ctx, packet))
            {
                return;
            }

            _discoveryManager.OnIncomingMsg(msg);
        }
        catch (Exception e)
        {
            if (_logger.IsDebug)
            {
                _logger.Error($"DEBUG/ERROR Error while processing message, type: {type}, sender: {address}, message: {msg}", e);
            }
        }
    }