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>, server: &Arc<Server>,
packet: &mut RawPacket, packet: &mut RawPacket,
) -> Result<(), DeserializerError> { ) -> Result<(), DeserializerError> {
// TODO: handle each packet's Error instead of calling .unwrap()
let bytebuf = &mut packet.bytebuf;
match self.connection_state.load() { 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 => { SHandShake::PACKET_ID => {
self.handle_handshake(server, SHandShake::read(bytebuf)?); self.handle_handshake(server, SHandShake::read(bytebuf)?);
Ok(()) Ok(())
@ -235,8 +258,16 @@ impl Client {
); );
Ok(()) 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 => { SStatusRequest::PACKET_ID => {
self.handle_status_request(server, SStatusRequest::read(bytebuf)?); self.handle_status_request(server, SStatusRequest::read(bytebuf)?);
Ok(()) Ok(())
@ -252,10 +283,16 @@ impl Client {
); );
Ok(()) 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 => { SLoginStart::PACKET_ID => {
self.handle_login_start(server, SLoginStart::read(bytebuf)?); self.handle_login_start(server, SLoginStart::read(bytebuf)?);
Ok(()) Ok(())
@ -280,8 +317,16 @@ impl Client {
); );
Ok(()) 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 => { SClientInformationConfig::PACKET_ID => {
self.handle_client_information_config( self.handle_client_information_config(
server, server,
@ -294,10 +339,7 @@ impl Client {
Ok(()) Ok(())
} }
SAcknowledgeFinishConfig::PACKET_ID => { SAcknowledgeFinishConfig::PACKET_ID => {
self.handle_config_acknowledged( self.handle_config_acknowledged(server, SAcknowledgeFinishConfig::read(bytebuf)?)
server,
SAcknowledgeFinishConfig::read(bytebuf)?,
)
.await; .await;
Ok(()) Ok(())
} }
@ -312,11 +354,6 @@ impl Client {
); );
Ok(()) Ok(())
} }
},
_ => {
log::error!("Invalid Connection state {:?}", self.connection_state);
Ok(())
}
} }
} }