Convert packets matches into their functions

This commit is contained in:
Snowiiii 2024-10-11 11:43:58 +02:00
parent 4967e9aa37
commit b5452245d8

View File

@ -220,10 +220,33 @@ impl Client {
server: &Arc<Server>,
packet: &mut RawPacket,
) -> Result<(), DeserializerError> {
// TODO: handle each packet's Error instead of calling .unwrap()
let bytebuf = &mut packet.bytebuf;
match self.connection_state.load() {
pumpkin_protocol::ConnectionState::HandShake => match packet.id.0 {
pumpkin_protocol::ConnectionState::HandShake => {
self.handle_handshake_packet(server, packet)
}
pumpkin_protocol::ConnectionState::Status => self.handle_status_packet(server, packet),
// TODO: Check config if transfer is enabled
pumpkin_protocol::ConnectionState::Login
| pumpkin_protocol::ConnectionState::Transfer => {
self.handle_login_packet(server, packet).await
}
pumpkin_protocol::ConnectionState::Config => {
self.handle_config_packet(server, packet).await
}
_ => {
log::error!("Invalid Connection state {:?}", self.connection_state);
Ok(())
}
}
}
fn handle_handshake_packet(
&self,
server: &Arc<Server>,
packet: &mut RawPacket,
) -> Result<(), DeserializerError> {
let bytebuf = &mut packet.bytebuf;
match packet.id.0 {
SHandShake::PACKET_ID => {
self.handle_handshake(server, SHandShake::read(bytebuf)?);
Ok(())
@ -235,8 +258,16 @@ impl Client {
);
Ok(())
}
},
pumpkin_protocol::ConnectionState::Status => match packet.id.0 {
}
}
fn handle_status_packet(
&self,
server: &Arc<Server>,
packet: &mut RawPacket,
) -> Result<(), DeserializerError> {
let bytebuf = &mut packet.bytebuf;
match packet.id.0 {
SStatusRequest::PACKET_ID => {
self.handle_status_request(server, SStatusRequest::read(bytebuf)?);
Ok(())
@ -252,10 +283,16 @@ impl Client {
);
Ok(())
}
},
// TODO: Check config if transfer is enabled
pumpkin_protocol::ConnectionState::Login
| pumpkin_protocol::ConnectionState::Transfer => match packet.id.0 {
}
}
async fn handle_login_packet(
&self,
server: &Arc<Server>,
packet: &mut RawPacket,
) -> Result<(), DeserializerError> {
let bytebuf = &mut packet.bytebuf;
match packet.id.0 {
SLoginStart::PACKET_ID => {
self.handle_login_start(server, SLoginStart::read(bytebuf)?);
Ok(())
@ -280,8 +317,16 @@ impl Client {
);
Ok(())
}
},
pumpkin_protocol::ConnectionState::Config => match packet.id.0 {
}
}
async fn handle_config_packet(
&self,
server: &Arc<Server>,
packet: &mut RawPacket,
) -> Result<(), DeserializerError> {
let bytebuf = &mut packet.bytebuf;
match packet.id.0 {
SClientInformationConfig::PACKET_ID => {
self.handle_client_information_config(
server,
@ -294,10 +339,7 @@ impl Client {
Ok(())
}
SAcknowledgeFinishConfig::PACKET_ID => {
self.handle_config_acknowledged(
server,
SAcknowledgeFinishConfig::read(bytebuf)?,
)
self.handle_config_acknowledged(server, SAcknowledgeFinishConfig::read(bytebuf)?)
.await;
Ok(())
}
@ -312,11 +354,6 @@ impl Client {
);
Ok(())
}
},
_ => {
log::error!("Invalid Connection state {:?}", self.connection_state);
Ok(())
}
}
}