diff --git a/packet-processor/Cargo.toml b/packet-processor/Cargo.toml index 9b6c8b7..ba62de7 100644 --- a/packet-processor/Cargo.toml +++ b/packet-processor/Cargo.toml @@ -7,3 +7,4 @@ edition = "2018" [dependencies] proto = { path = "../../proto" } +prost = "0.8" diff --git a/packet-processor/src/lib.rs b/packet-processor/src/lib.rs index e59a117..e7b234f 100644 --- a/packet-processor/src/lib.rs +++ b/packet-processor/src/lib.rs @@ -51,3 +51,22 @@ macro_rules! build { proto::$id { $($i: $e,)* ..proto::$id::default() } }}; } + +/* +#[macro_export] +macro_rules! unpack { + ($packet:ident, $buffer:ident) => {{ + proto::$packet::decode(&mut std::io::Cursor::new($buffer)).unwrap() + }}; +} +*/ + +pub trait EasilyUnpackable { + fn from(buf: &[u8]) -> Self; +} + +impl EasilyUnpackable for T { + fn from(buf: &[u8]) -> Self { + Self::decode(&mut std::io::Cursor::new(buf)).unwrap() + } +} diff --git a/src/server/network_server.rs b/src/server/network_server.rs index 0467fac..0a1dfbf 100644 --- a/src/server/network_server.rs +++ b/src/server/network_server.rs @@ -26,7 +26,7 @@ use proto::get_player_token_rsp; use prost::Message; -use packet_processor::PacketProcessor; +use packet_processor::{PacketProcessor, EasilyUnpackable}; extern crate kcp; @@ -105,7 +105,7 @@ impl NetworkServer { if packet_id == proto::PacketId::GetPlayerTokenRsp { // TODO: a bit hacky! - let token_rsp = GetPlayerTokenRsp::decode(&mut Cursor::new(data)).unwrap(); + let token_rsp: GetPlayerTokenRsp = EasilyUnpackable::from(&data); client.update_key(token_rsp.secret_key_seed); } }, @@ -211,7 +211,7 @@ impl NetworkServer { }; if packet_id == proto::PacketId::UnionCmdNotify { - let union = proto::UnionCmdNotify::decode(&mut Cursor::new(&data.data)).unwrap(); + let union: proto::UnionCmdNotify = EasilyUnpackable::from(&data.data); for u_cmd in union.cmd_list.into_iter() { self.send_packet_to_process(user_id, u_cmd.message_id as u16, &data.metadata, &u_cmd.body); }