aboutsummaryrefslogtreecommitdiff
path: root/minecraft-protocol/src/packets/status/mod.rs
blob: 7e327ca74318f5d8d8ebf022e3f282eb237e8b92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
pub mod clientbound_status_response_packet;
pub mod serverbound_status_request_packet;

use async_trait::async_trait;
use tokio::io::BufReader;

use crate::connect::PacketFlow;

use super::{ConnectionProtocol, PacketTrait, ProtocolPacket};

#[derive(Clone, Debug)]
pub enum StatusPacket {
    ServerboundStatusRequestPacket(
        serverbound_status_request_packet::ServerboundStatusRequestPacket,
    ),
    ClientboundStatusResponsePacket(
        clientbound_status_response_packet::ClientboundStatusResponsePacket,
    ),
}

// #[async_trait]
// impl ProtocolPacket for StatusPacket {
impl StatusPacket {
    fn get_inner(self) -> impl PacketTrait {
        match self {
            StatusPacket::ServerboundStatusRequestPacket(packet) => packet,
            StatusPacket::ClientboundStatusResponsePacket(packet) => packet,
        }
    }
    // fn get_inner(&self) -> StatusPacket {
    //     match self {
    //         StatusPacket::ServerboundStatusRequestPacket(packet) => packet,
    //         StatusPacket::ClientboundStatusResponsePacket(packet) => packet,
    //     }
    // }

    fn id(&self) -> u32 {
        match self {
            StatusPacket::ServerboundStatusRequestPacket(_packet) => 0x00,
            StatusPacket::ClientboundStatusResponsePacket(_packet) => 0x00,
        }
    }

    fn write(&self, buf: &mut Vec<u8>) {
        match self {
            StatusPacket::ServerboundStatusRequestPacket(packet) => packet.write(buf),
            StatusPacket::ClientboundStatusResponsePacket(packet) => packet.write(buf),
        }
    }

    /// Read a packet by its id, ConnectionProtocol, and flow
    async fn read<
        T: tokio::io::AsyncRead + std::marker::Unpin + std::marker::Send,
        P: ProtocolPacket,
    >(
        id: u32,
        flow: &PacketFlow,
        buf: &mut BufReader<T>,
    ) -> Result<P, String>
    where
        Self: Sized,
    {
        match flow {
            PacketFlow::ServerToClient => match id {
                0x00 => Ok(
                    clientbound_status_response_packet::ClientboundStatusResponsePacket::read(buf)
                        .await?,
                ),
                _ => Err(format!("Unknown ServerToClient status packet id: {}", id)),
            },
            PacketFlow::ClientToServer => match id {
                0x00 => Ok(
                    serverbound_status_request_packet::ServerboundStatusRequestPacket::read(buf)
                        .await?,
                ),
                _ => Err(format!("Unknown ClientToServer status packet id: {}", id)),
            },
        }
    }
}