Exemplo n.º 1
0
    public override Task HandleMessageAsync(byte[] incomingMessage)
    {
        int             packetId       = VariableStorageUtilities.ReadVarInt32(incomingMessage);
        int             packetIdLength = VariableStorageUtilities.GetVarInt32Length(packetId);
        MinecraftPacket packet         = new(packetId, incomingMessage.Skip(packetIdLength).ToArray());
        object          packetHeader   = GetMinecraftPacketType(packet.PacketId);

        try
        {
            _packetHandler.Invoke(Status, packetHeader, this, packet);
        }
        catch (HandlerActionNotFoundException)
        {
            if (!Socket.Connected)
            {
                return(Task.CompletedTask);
            }

            if (Enum.IsDefined(packetHeader.GetType(), packet.PacketId))
            {
                _logger.LogTrace($"[{Username}] Received an unimplemented {Status} packet {packetHeader} (0x{packet.PacketId:X2}) from {Socket.RemoteEndPoint}");
            }
            else
            {
                _logger.LogTrace($"[{Username}] Received an unknown {Status} packet: 0x{packet.PacketId:X2} from {Socket.RemoteEndPoint}");
            }
        }
        catch (Exception e)
        {
            _logger.LogError(e, $"An error occured while handling packet '{packetHeader}' during '{Status}' state.");

            if (Status != MinecraftUserStatus.Play)
            {
                Socket.Close();
            }

            throw;
        }

        return(Task.CompletedTask);
    }