Add simple utility trait for unpacking protobufs

This commit is contained in:
Nobody 2022-02-03 23:00:13 +05:00
parent 26f446616a
commit 02b9923f2a
3 changed files with 23 additions and 3 deletions

View File

@ -7,3 +7,4 @@ edition = "2018"
[dependencies]
proto = { path = "../../proto" }
prost = "0.8"

View File

@ -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<T: prost::Message + std::default::Default> EasilyUnpackable for T {
fn from(buf: &[u8]) -> Self {
Self::decode(&mut std::io::Cursor::new(buf)).unwrap()
}
}

View File

@ -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);
}