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
|
pub mod game;
pub mod handshake;
pub mod login;
pub mod status;
use crate::{
connect::PacketFlow,
mc_buf::{McBufReadable, McBufWritable, Readable, Writable},
};
use async_trait::async_trait;
use num_derive::FromPrimitive;
use num_traits::FromPrimitive;
use tokio::io::AsyncRead;
pub const PROTOCOL_VERSION: u32 = 757;
#[derive(Debug, Clone, PartialEq, Eq, Hash, FromPrimitive)]
pub enum ConnectionProtocol {
Handshake = -1,
Game = 0,
Status = 1,
Login = 2,
}
#[derive(Clone, Debug)]
pub enum Packet {
Game(game::GamePacket),
Handshake(handshake::HandshakePacket),
Login(login::LoginPacket),
Status(Box<status::StatusPacket>),
}
/// An enum of packets for a certain protocol
#[async_trait]
pub trait ProtocolPacket
where
Self: Sized,
{
fn id(&self) -> u32;
/// Read a packet by its id, ConnectionProtocol, and flow
async fn read<T: tokio::io::AsyncRead + std::marker::Unpin + std::marker::Send>(
id: u32,
flow: &PacketFlow,
buf: &mut T,
) -> Result<Self, String>
where
Self: Sized;
fn write(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error>;
}
#[async_trait]
impl McBufReadable for ConnectionProtocol {
async fn read_into<R>(buf: &mut R) -> Result<Self, String>
where
R: AsyncRead + std::marker::Unpin + std::marker::Send,
{
ConnectionProtocol::from_i32(buf.read_varint().await?)
.ok_or_else(|| "Invalid intention".to_string())
}
}
impl McBufWritable for ConnectionProtocol {
fn write_into(&self, buf: &mut Vec<u8>) -> Result<(), std::io::Error> {
buf.write_varint(self.clone() as i32)
}
}
|