1use crate::{
2 buffer::Buffer,
3 packets::{
4 link_udp::LinkUDP, notify_connection_info::NotifyConnectionInfo, null::Null, player_join_packet::PlayerJoinPacket, player_leave_packet::PlayerLeavePacket, player_list_packet::PlayerListPacket, update_clients_positions::UpdateClientsPositions, update_server_position::UpdateServerPositions
5 }
6};
7
8#[derive(Debug)]
9pub enum PacketTypes{
10 NotifyConnectionInfo(NotifyConnectionInfo),
11 LinkUDP(LinkUDP),
12
13 UpdateServerPositions(UpdateServerPositions),
14 UpdateClientsPositions(UpdateClientsPositions),
15
16 PlayerJoinPacket(PlayerJoinPacket),
17 PlayerListPacket(PlayerListPacket),
18 PlayerLeavePacket(PlayerLeavePacket),
19
20 Null(Null)
21}
22
23pub trait Packet{
24 fn from_buf(buf: Buffer) -> Self;
25 fn to_buf(&self) -> Buffer;
26}
27
28pub fn parse(mut buf: Buffer) -> PacketTypes{
29 let id = buf.get_u16();
30
31 match id{
32 0 => PacketTypes::NotifyConnectionInfo(NotifyConnectionInfo::from_buf(buf)),
33 1 => PacketTypes::LinkUDP(LinkUDP::from_buf(buf)),
34
35 2 => PacketTypes::UpdateServerPositions(UpdateServerPositions::from_buf(buf)),
36 6 => PacketTypes::UpdateClientsPositions(UpdateClientsPositions::from_buf(buf)),
37
38 3 => PacketTypes::PlayerJoinPacket(PlayerJoinPacket::from_buf(buf)),
39 4 => PacketTypes::PlayerListPacket(PlayerListPacket::from_buf(buf)),
40 5 => PacketTypes::PlayerLeavePacket(PlayerLeavePacket::from_buf(buf)),
41
42 _ => PacketTypes::Null(Null::from_buf(buf))
43 }
44}