From 36a618cadff2b95e59d78bf68342decb42548afd Mon Sep 17 00:00:00 2001 From: Wiktor Mazur Date: Wed, 13 Nov 2019 19:25:03 +0100 Subject: [PATCH] Photon_protocol -> photon_decode --- backend/Cargo.lock | 6 +- backend/Cargo.toml | 2 +- backend/lib/photon-protocol/Cargo.lock | 48 -- backend/lib/photon-protocol/Cargo.toml | 10 - backend/lib/photon-protocol/src/decode.rs | 442 ------------------ backend/lib/photon-protocol/src/layout.rs | 94 ---- backend/lib/photon-protocol/src/lib.rs | 101 ---- .../photon-protocol/tests/decoding_tests.rs | 361 -------------- .../tests/integration_tests.rs | 277 ----------- backend/src/core.rs | 14 +- backend/src/game_protocol/message.rs | 4 +- backend/src/game_protocol/packet.rs | 2 +- 12 files changed, 15 insertions(+), 1346 deletions(-) delete mode 100644 backend/lib/photon-protocol/Cargo.lock delete mode 100644 backend/lib/photon-protocol/Cargo.toml delete mode 100644 backend/lib/photon-protocol/src/decode.rs delete mode 100644 backend/lib/photon-protocol/src/layout.rs delete mode 100644 backend/lib/photon-protocol/src/lib.rs delete mode 100644 backend/lib/photon-protocol/tests/decoding_tests.rs delete mode 100644 backend/lib/photon-protocol/tests/integration_tests.rs diff --git a/backend/Cargo.lock b/backend/Cargo.lock index 2de89da..661106a 100644 --- a/backend/Cargo.lock +++ b/backend/Cargo.lock @@ -303,7 +303,7 @@ dependencies = [ "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", "packet_sniffer 0.1.0", - "photon_protocol 0.1.0", + "photon_decode 0.1.0 (git+https://github.com/mazurwiktor/photon_decode)", "simplelog 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", "timer 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -444,8 +444,9 @@ dependencies = [ ] [[package]] -name = "photon_protocol" +name = "photon_decode" version = "0.1.0" +source = "git+https://github.com/mazurwiktor/photon_decode#d0864258129cfe6da862793f7c5a8916b96744fd" dependencies = [ "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -952,6 +953,7 @@ dependencies = [ "checksum owning_ref 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "49a4b8ea2179e6a2e27411d3bca09ca6dd630821cf6894c6c7c8467a8ee7ef13" "checksum parking_lot 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)" = "f0802bff09003b291ba756dc7e79313e51cc31667e94afbe847def490424cde5" "checksum parking_lot_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ad7f7e6ebdc79edff6fdcb87a55b620174f7a989e3eb31b65231f4af57f00b8c" +"checksum photon_decode 0.1.0 (git+https://github.com/mazurwiktor/photon_decode)" = "" "checksum pnet 0.22.0 (registry+https://github.com/rust-lang/crates.io-index)" = "63d693c84430248366146e3181ff9d330243464fa9e6146c372b2f3eb2e2d8e7" "checksum pnet_base 0.22.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4df28acf2fcc77436dd2b91a9a0c2bb617f9ca5f2acefee1a4135058b9f9801f" "checksum pnet_datalink 0.22.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7b34f8ca857599d05b6b082e9baff8d27c54cb9c26568cf3c0993a5755816966" diff --git a/backend/Cargo.toml b/backend/Cargo.toml index 7fde997..cf2608e 100644 --- a/backend/Cargo.toml +++ b/backend/Cargo.toml @@ -15,7 +15,7 @@ version = "0.3" [dependencies] packet_sniffer = { path = "./lib/packet-sniffer" } -photon_protocol = { path = "./lib/photon-protocol" } +photon_decode = { git = "https://github.com/mazurwiktor/photon_decode" } bytes = "0.4" simplelog = "0.6" timer = "0.1" diff --git a/backend/lib/photon-protocol/Cargo.lock b/backend/lib/photon-protocol/Cargo.lock deleted file mode 100644 index e6bc75a..0000000 --- a/backend/lib/photon-protocol/Cargo.lock +++ /dev/null @@ -1,48 +0,0 @@ -# This file is automatically @generated by Cargo. -# It is not intended for manual editing. -[[package]] -name = "byteorder" -version = "1.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" - -[[package]] -name = "bytes" -version = "0.4.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)", - "iovec 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "iovec" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "libc 0.2.60 (registry+https://github.com/rust-lang/crates.io-index)", - "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "libc" -version = "0.2.60" -source = "registry+https://github.com/rust-lang/crates.io-index" - -[[package]] -name = "photon_protocol" -version = "0.1.0" -dependencies = [ - "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "winapi" -version = "0.2.8" -source = "registry+https://github.com/rust-lang/crates.io-index" - -[metadata] -"checksum byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a7c3dd8985a7111efc5c80b44e23ecdd8c007de8ade3b96595387e812b957cf5" -"checksum bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)" = "206fdffcfa2df7cbe15601ef46c813fce0965eb3286db6b56c583b814b51c81c" -"checksum iovec 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "dbe6e417e7d0975db6512b90796e8ce223145ac4e33c377e4a42882a0e88bb08" -"checksum libc 0.2.60 (registry+https://github.com/rust-lang/crates.io-index)" = "d44e80633f007889c7eff624b709ab43c92d708caad982295768a7b13ca3b5eb" -"checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a" diff --git a/backend/lib/photon-protocol/Cargo.toml b/backend/lib/photon-protocol/Cargo.toml deleted file mode 100644 index de2ba60..0000000 --- a/backend/lib/photon-protocol/Cargo.toml +++ /dev/null @@ -1,10 +0,0 @@ -[package] -name = "photon_protocol" -version = "0.1.0" -authors = ["Wiktor Mazur "] -edition = "2018" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] -bytes = "0.4" \ No newline at end of file diff --git a/backend/lib/photon-protocol/src/decode.rs b/backend/lib/photon-protocol/src/decode.rs deleted file mode 100644 index 6432229..0000000 --- a/backend/lib/photon-protocol/src/decode.rs +++ /dev/null @@ -1,442 +0,0 @@ -use std::collections::HashMap; -use std::io::{Cursor, Read}; -use std::mem::size_of; - -use bytes::Buf; - -use crate::layout::*; - -pub type PhotonDecodeError = &'static str; -pub type PhotonDecodeResult = std::result::Result; -pub type PhotonCursor<'a> = Cursor<&'a [u8]>; - -pub trait Decode { - fn decode(&mut self) -> PhotonDecodeResult; -} - -trait TypedDecode { - fn typed_decode(&mut self, type_code: u8) -> PhotonDecodeResult; -} - -macro_rules! impl_decode { - ($type:ty, $decode_func:ident, $bytes_to_consume:expr) => { - impl Decode<$type> for PhotonCursor<'_> { - fn decode(&mut self) -> PhotonDecodeResult<$type> { - let v = if self.remaining() >= $bytes_to_consume { - self.$decode_func() - } else { - return Err(concat!( - "Failed to decode", - stringify!($type), - ", not enough bytes" - )); - }; - Ok(v) - } - } - }; -} - -impl_decode!(u8, get_u8, 1); -impl_decode!(f32, get_f32_be, 4); -impl_decode!(u32, get_u32_be, 4); -impl_decode!(i64, get_i64_be, 8); -impl_decode!(i16, get_i16_be, 2); -impl_decode!(f64, get_f64_be, 8); - -impl Decode for PhotonCursor<'_> { - fn decode(&mut self) -> PhotonDecodeResult { - let v = if self.remaining() >= 1 { - self.get_u8() - } else { - return Err("Failed to decode bool, not enough bytes"); - }; - Ok(v != 0) - } -} - -impl Decode for PhotonCursor<'_> { - fn decode(&mut self) -> PhotonDecodeResult { - let size: i16 = self.decode()?; - if size < 0 { - return Err("Failed to decode String, unreasonable size"); - } - - let mut local_buffer = vec![0; size as usize]; - if let Ok(_) = self.read_exact(&mut local_buffer[..]) { - if let Ok(s) = String::from_utf8(local_buffer) { - return Ok(s); - } - } - - Err("Failed to decode String, not enough bytes") - } -} - -impl Decode> for PhotonCursor<'_> { - fn decode(&mut self) -> PhotonDecodeResult> { - let size: i16 = self.decode()?; - if size < 0 { - return Err("Failed to decode String, unreasonable size"); - } - let mut value: Vec = vec![]; - for _ in 0..size { - value.push(self.decode()?); - } - - Ok(value) - } -} - -impl Decode> for PhotonCursor<'_> { - fn decode(&mut self) -> PhotonDecodeResult> { - let size: u32 = self.decode()?; - let mut value: Vec = vec![]; - for _ in 0..size { - value.push(self.decode()?); - } - - Ok(value) - } -} - -impl Decode> for PhotonCursor<'_> { - fn decode(&mut self) -> PhotonDecodeResult> { - let size: i16 = self.decode()?; - if size <= 0 { - return Err("Failed to decode Vec, unreasonable size"); - } - let type_code: u8 = self.decode()?; - - let mut value: Vec = vec![]; - for _ in 0..size { - if let Ok(v) = self.typed_decode(type_code) { - value.push(v); - } else { - break; - } - } - - Ok(value) - } -} - -impl Decode> for PhotonCursor<'_> { - fn decode(&mut self) -> PhotonDecodeResult> { - let key_type_code: u8 = self.decode()?; - let value_type_code: u8 = self.decode()?; - let size: i16 = self.decode()?; - if size <= 0 { - return Err("Failed to decode HashMap, unreasonable size"); - } - - let mut value: HashMap = HashMap::new(); - for _ in 0..size { - let key_code: u8 = if key_type_code == 0 || key_type_code == 42 { - self.decode()? - } else { - key_type_code - }; - - let key = self.typed_decode(key_code); - - let value_code: u8 = if value_type_code == 0 || value_type_code == 42 { - self.decode()? - } else { - value_type_code - }; - let val = self.typed_decode(value_code); - value.insert(format!("{}", key.unwrap()), val.unwrap()); - } - - Ok(value) - } -} - -impl Decode> for PhotonCursor<'_> { - fn decode(&mut self) -> PhotonDecodeResult> { - let size: i16 = self.decode()?; - if size <= 0 { - return Err("Failed to decode HashMap, unreasonable size"); - } - - let mut value: HashMap = HashMap::new(); - for _ in 0..size { - let key_type_code: u8 = if let Ok(v) = self.decode() { - v - } else { - break; - }; - let val: Value = if let Ok(v) = self.decode() { - v - } else { - break; - }; - value.insert(key_type_code, val); - } - - Ok(value) - } -} - -impl Decode for PhotonCursor<'_> { - fn decode(&mut self) -> PhotonDecodeResult { - let code: u8 = self.decode()?; - let parameters: HashMap = self.decode()?; - Ok(EventData { code, parameters }) - } -} - -impl Decode for PhotonCursor<'_> { - fn decode(&mut self) -> PhotonDecodeResult { - let code: u8 = self.decode()?; - let return_code: i16 = self.decode()?; - let maybe_debug_message: Value = self.decode()?; - let debug_message = if let Value::String(s) = maybe_debug_message { - s - } else { - "None".to_owned() - }; - let parameters: HashMap = self.decode()?; - - Ok(OperationResponse { - code, - return_code, - debug_message, - parameters, - }) - } -} - -impl Decode for PhotonCursor<'_> { - fn decode(&mut self) -> PhotonDecodeResult { - let code: u8 = self.decode()?; - let parameters: HashMap = self.decode()?; - Ok(OperationRequest { code, parameters }) - } -} - -impl Decode>> for PhotonCursor<'_> { - fn decode(&mut self) -> PhotonDecodeResult>> { - let size: i16 = self.decode()?; - if size <= 0 { - return Err("Failed to decode Vec>, unreasonable size"); - } - let mut value = vec![]; - for _ in 0..size { - let type_code: u8 = if let Ok(v) = self.decode() { - v - } else { - break; - }; - value.push(Box::new(self.typed_decode(type_code).unwrap())); - } - Ok(value) - } -} - -impl Decode> for PhotonCursor<'_> { - fn decode(&mut self) -> PhotonDecodeResult> { - let size: i16 = self.decode()?; - if size <= 0 { - return Err("Failed to decode Vec, unreasonable size"); - } - let mut value = vec![]; - for _ in 0..size { - value.push(self.decode()?); - } - Ok(value) - } -} - -impl TypedDecode for PhotonCursor<'_> { - fn typed_decode(&mut self, type_code: u8) -> PhotonDecodeResult { - match TypeCode::from(type_code) { - TypeCode::Null => Ok(Value::None), - TypeCode::Boolean => Ok(Value::Boolean(self.decode()?)), - TypeCode::Byte => Ok(Value::Byte(self.decode()?)), - TypeCode::Double => Ok(Value::Double(self.decode()?)), - TypeCode::Float => Ok(Value::Float(self.decode()?)), - TypeCode::Integer => Ok(Value::Integer(self.decode()?)), - TypeCode::Long => Ok(Value::Long(self.decode()?)), - TypeCode::Short => Ok(Value::Short(self.decode()?)), - TypeCode::String => Ok(Value::String(self.decode()?)), - TypeCode::StringArray => Ok(Value::StringArray(self.decode()?)), - TypeCode::ByteArray => Ok(Value::ByteArray(self.decode()?)), - TypeCode::Dictionary => Ok(Value::Dictionary(self.decode()?)), - TypeCode::EventData => Ok(Value::EventData(self.decode()?)), - TypeCode::OperationRequest => Ok(Value::OperationRequest(self.decode()?)), - TypeCode::OperationResponse => Ok(Value::OperationResponse(self.decode()?)), - TypeCode::BooleanArray => Ok(Value::BooleanArray(self.decode()?)), - TypeCode::Array => Ok(Value::Array(self.decode()?)), - TypeCode::ObjectArray => Ok(Value::ObjectArray(self.decode()?)), - _ => Err("Failed to decode Value, unknown type code"), - } - } -} - -impl Decode for PhotonCursor<'_> { - fn decode(&mut self) -> PhotonDecodeResult { - let type_code: u8 = self.decode()?; - self.typed_decode(type_code) - } -} - -impl Decode for PhotonCursor<'_> { - fn decode(&mut self) -> PhotonDecodeResult { - let peer_id = self.decode()?; - let crc_enabled = self.decode()?; - let command_count = self.decode()?; - let timestamp = self.decode()?; - let challenge = self.decode()?; - - Ok(PhotonHeader { - peer_id, - crc_enabled, - command_count, - timestamp, - challenge, - }) - } -} - -impl Decode for PhotonCursor<'_> { - fn decode(&mut self) -> PhotonDecodeResult { - let channel_id = self.decode()?; - let flags = self.decode()?; - let reserved_byte = self.decode()?; - let length: u32 = self.decode()?; - let reliable_sequence_number = self.decode()?; - let msg_len = length - size_of::() as u32; - Ok(ReliableCommand { - channel_id, - flags, - reserved_byte, - msg_len, - reliable_sequence_number, - }) - } -} - -impl Decode for PhotonCursor<'_> { - fn decode(&mut self) -> PhotonDecodeResult { - let mut reliable_command: ReliableCommand = self.decode()?; - let unknown = self.decode()?; - reliable_command.msg_len = reliable_command.msg_len - size_of::() as u32; - Ok(UnreliableCommand { - reliable_command, - unknown, - }) - } -} - -impl Decode for PhotonCursor<'_> { - fn decode(&mut self) -> PhotonDecodeResult { - let mut reliable_command: ReliableCommand = self.decode()?; - let sequence_number = self.decode()?; - let fragment_count = self.decode()?; - let fragment_number = self.decode()?; - let total_length = self.decode()?; - let operation_length = self.decode()?; - - reliable_command.msg_len = reliable_command.msg_len - (size_of::() * 5) as u32; - let mut payload = vec![0u8; reliable_command.msg_len as usize]; - self.read_exact(&mut payload).unwrap(); - - Ok(ReliableFragment { - reliable_command, - sequence_number, - fragment_count, - fragment_number, - total_length, - operation_length, - payload, - }) - } -} - -impl Decode for PhotonCursor<'_> { - fn decode(&mut self) -> PhotonDecodeResult { - let cmd_type_id: u8 = self.decode()?; - match cmd_type_id { - 4 => Ok(Command::LogOut), - 6 => Ok(Command::SendReliable(self.decode()?)), - 7 => Ok(Command::SendUnreliable( - Decode::::decode(self)?.reliable_command, - )), - 8 => Ok(Command::SendReliableFragment(self.decode()?)), - _ => Ok(Command::SendReliable(self.decode()?)), - } - } -} - -pub enum TypeCode { - Unknown = 0, - Null = 42, - Dictionary = 68, - StringArray = 97, - Byte = 98, - Double = 100, - EventData = 101, - Float = 102, - Integer = 105, - Short = 107, - Long = 108, - BooleanArray = 110, - Boolean = 111, - OperationResponse = 112, - OperationRequest = 113, - String = 115, - ByteArray = 120, - Array = 121, - ObjectArray = 122, -} - -impl From for TypeCode { - fn from(v: u8) -> Self { - match v { - 0 => TypeCode::Unknown, - 42 => TypeCode::Null, - 68 => TypeCode::Dictionary, - 97 => TypeCode::StringArray, - 98 => TypeCode::Byte, - 100 => TypeCode::Double, - 101 => TypeCode::EventData, - 102 => TypeCode::Float, - 105 => TypeCode::Integer, - 107 => TypeCode::Short, - 108 => TypeCode::Long, - 110 => TypeCode::BooleanArray, - 111 => TypeCode::Boolean, - 112 => TypeCode::OperationResponse, - 113 => TypeCode::OperationRequest, - 115 => TypeCode::String, - 120 => TypeCode::ByteArray, - 121 => TypeCode::Array, - 122 => TypeCode::ObjectArray, - _ => TypeCode::Unknown, - } - } -} - -impl std::ops::Index for Value { - type Output = Value; - - fn index(&self, index: usize) -> &Self::Output { - match self { - Value::Array(v) => &v[index], - _ => panic!("Non indexable type"), - } - } -} - -impl std::fmt::Display for Value { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - Value::String(v) => write!(f, "{}", v), - Value::Byte(v) => write!(f, "{}", v), - Value::Integer(v) => write!(f, "{}", v), - v => write!(f, "{:?}", v), - } - } -} diff --git a/backend/lib/photon-protocol/src/layout.rs b/backend/lib/photon-protocol/src/layout.rs deleted file mode 100644 index 955aca1..0000000 --- a/backend/lib/photon-protocol/src/layout.rs +++ /dev/null @@ -1,94 +0,0 @@ -use std::collections::HashMap; - -#[derive(Clone, Debug, PartialEq)] -pub enum Value { - None, - Dictionary(HashMap), - StringArray(Vec), - Byte(u8), - Double(f64), - EventData(EventData), - Float(f32), - Integer(u32), - Short(i16), - Long(i64), - BooleanArray(Vec), - Boolean(bool), - OperationResponse(OperationResponse), - OperationRequest(OperationRequest), - String(String), - ByteArray(Vec), - Array(Vec), - Object(Box), - ObjectArray(Vec>), -} - -pub type Parameters = HashMap; - -#[derive(Clone, Debug, PartialEq)] -pub struct EventData { - pub code: u8, - pub parameters: Parameters, -} - -#[derive(Clone, Debug, PartialEq)] -pub struct OperationResponse { - pub code: u8, - pub return_code: i16, - pub debug_message: String, - pub parameters: Parameters, -} - -#[derive(Clone, Debug, PartialEq)] -pub struct OperationRequest { - pub code: u8, - pub parameters: Parameters, -} - -#[derive(Debug, PartialEq)] -pub enum Message { - Request(OperationRequest), - Response(OperationResponse), - Event(EventData), -} - -#[derive(Debug)] -pub struct PhotonHeader { - pub peer_id: i16, - pub crc_enabled: bool, - pub command_count: u8, - pub timestamp: u32, - pub challenge: u32, -} - -#[derive(Debug, Clone)] -pub struct ReliableCommand { - pub channel_id: u8, - pub flags: u8, - pub reserved_byte: u8, - pub msg_len: u32, - pub reliable_sequence_number: u32, -} - -pub struct UnreliableCommand { - pub reliable_command: ReliableCommand, - pub unknown: u32, -} - -#[derive(Debug, Clone)] -pub struct ReliableFragment { - pub reliable_command: ReliableCommand, - pub sequence_number: u32, - pub fragment_count: u32, - pub fragment_number: u32, - pub total_length: u32, - pub operation_length: u32, - pub payload: Vec, -} - -pub enum Command { - LogOut, - SendUnreliable(ReliableCommand), - SendReliable(ReliableCommand), - SendReliableFragment(ReliableFragment), -} diff --git a/backend/lib/photon-protocol/src/lib.rs b/backend/lib/photon-protocol/src/lib.rs deleted file mode 100644 index 5e8a231..0000000 --- a/backend/lib/photon-protocol/src/lib.rs +++ /dev/null @@ -1,101 +0,0 @@ -mod decode; -mod layout; - -pub use decode::*; -pub use layout::*; - -use std::collections::HashMap; -use std::convert::TryFrom; -use std::io::{Cursor}; - -impl TryFrom<(&ReliableCommand, &mut Cursor<&[u8]>)> for Message { - type Error = &'static str; - - fn try_from(message: (&ReliableCommand, &mut Cursor<&[u8]>)) -> Result { - let (command, cursor) = message; - let _: u8 = cursor.decode()?; - let msg_type: u8 = cursor.decode()?; - match msg_type { - 2 => { - let v = cursor.decode()?; - Ok(Message::Request(v)) - } - 3 => { - let v = cursor.decode()?; - Ok(Message::Response(v)) - } - 4 => { - let v = cursor.decode()?; - Ok(Message::Event(v)) - } - _ => { - cursor.set_position(cursor.position() + command.msg_len as u64 - 2); - Err("Unknown message") - } - } - } -} - -pub struct Photon { - fragments: HashMap>, -} - -impl Photon { - pub fn new() -> Self { - Self { - fragments: HashMap::new(), - } - } - - pub fn try_decode(&mut self, payload: &[u8]) -> PhotonDecodeResult> { - let mut result: Vec = vec![]; - let mut cursor = Cursor::new(payload); - for _ in 0..Decode::::decode(&mut cursor)?.command_count { - let command = Decode::::decode(&mut cursor)?; - match command { - Command::SendReliable(c) | Command::SendUnreliable(c) => { - if let Ok(message) = Message::try_from((&c, &mut cursor)) { - result.push(message); - } - } - Command::SendReliableFragment(fragment) => { - self.fragments - .entry(fragment.sequence_number) - .or_insert(vec![]) - .push(fragment.clone()); - if let Ok(message) = self.decode_reliable_fragment(&fragment) { - result.push(message); - self.fragments.remove(&fragment.sequence_number).unwrap(); - } - } - _ => {} - } - } - - Ok(result) - } - - pub fn decode(&mut self, payload: &[u8]) -> Vec { - if let Ok(messages) = self.try_decode(payload) { - return messages; - } - vec![] - } - - fn decode_reliable_fragment( - &mut self, - fragment: &ReliableFragment, - ) -> PhotonDecodeResult { - if let Some(fragments) = self.fragments.get(&fragment.sequence_number) { - if fragments.len() == fragment.fragment_count as usize { - let mut buf = Vec::::new(); - for fragment in fragments { - buf.extend(fragment.payload.iter()); - } - let mut c = Cursor::new(&buf[..]); - return Message::try_from((&fragment.reliable_command, &mut c)); - } - } - Err("Not enough information to decode the fragment") - } -} diff --git a/backend/lib/photon-protocol/tests/decoding_tests.rs b/backend/lib/photon-protocol/tests/decoding_tests.rs deleted file mode 100644 index f8b9ab1..0000000 --- a/backend/lib/photon-protocol/tests/decoding_tests.rs +++ /dev/null @@ -1,361 +0,0 @@ -use photon_protocol::*; - -use std::io::{Read, Cursor}; - -#[test] -fn deserialize_dictionary() { - let value = vec![ - 68, 115, 115, 0, 2, 0, 8, 116, 101, 115, 116, 75, 101, 121, 49, 0, 10, 116, 101, 115, - 116, 86, 97, 108, 117, 101, 49, 0, 8, 116, 101, 115, 116, 75, 101, 121, 50, 0, 10, 116, - 101, 115, 116, 86, 97, 108, 117, 101, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - ]; - let mut buf = Cursor::new(&value[..]); - let result = buf.decode(); - assert!(result.is_ok(), "Unimplemented!"); - let value = result.unwrap(); - match value { - Value::Dictionary(v) => { - return assert_eq!( - v.get("testKey1"), - Some(&Value::String("testValue1".to_owned())) - ) - } - _ => assert!(false), - } -} - -#[test] -fn deserialize_string_array() { - let value = vec![ - 121, 0, 2, 115, 0, 5, 116, 101, 115, 116, 49, 0, 5, 116, 101, 115, 116, 50, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - ]; - let mut buf = Cursor::new(&value[..]); - let result = buf.decode(); - assert!(result.is_ok(), "Unimplemented!"); - let value = result.unwrap(); - - match value { - Value::Array(v) => { - if let Value::String(val) = &v[0] { - assert_eq!(val, &"test1".to_owned()); - } - if let Value::String(val) = &v[1] { - assert_eq!(val, &"test2".to_owned()); - } - return; - } - Value::StringArray(v) => { - return assert_eq!(vec!["test1".to_owned(), "test2".to_owned()], v) - } - _ => assert!(false), - } -} - -#[test] -fn test_deserialize_byte() { - let value = vec![98, 6]; - let mut buf = Cursor::new(&value[..]); - let result = buf.decode(); - assert!(result.is_ok(), "Unimplemented!"); - let value = result.unwrap(); - match value { - Value::Byte(v) => return assert_eq!(v, 6), - _ => assert!(false), - } -} - -#[test] -fn deserialize_double() { - let value = vec![100, 64, 147, 74, 51, 51, 51, 51, 51, 0, 0, 0, 0, 0, 0, 0]; - let mut buf = Cursor::new(&value[..]); - let result = buf.decode(); - assert!(result.is_ok(), "Unimplemented!"); - let value = result.unwrap(); - match value { - Value::Double(v) => return assert_eq!(v, 1234.55), - _ => assert!(false), - } -} - -#[test] -fn deserialize_event_data() { - let value = vec![ - 101, 100, 0, 2, 0, 115, 0, 5, 116, 101, 115, 116, 49, 1, 115, 0, 5, 116, 101, 115, 116, - 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - ]; - let mut buf = Cursor::new(&value[..]); - let result = buf.decode(); - assert!(result.is_ok(), "Unimplemented!"); - let value = result.unwrap(); - match value { - Value::EventData(v) => { - assert_eq!(v.code, 100); - assert_eq!( - v.parameters.get(&0).unwrap(), - &Value::String("test1".to_owned()) - ); - return; - } - _ => assert!(false), - } -} - -#[test] -fn deserialize_float() { - let value = vec![102, 68, 154, 81, 154, 0, 0, 0]; - let mut buf = Cursor::new(&value[..]); - let result = buf.decode(); - assert!(result.is_ok(), "Unimplemented!"); - let value = result.unwrap(); - match value { - Value::Float(v) => return assert_eq!(v, 1234.55), - _ => assert!(false), - } -} - -#[test] -fn deserialize_integer() { - let value = vec![105, 0, 0, 4, 210, 0, 0, 0]; - let mut buf = Cursor::new(&value[..]); - let result = buf.decode(); - assert!(result.is_ok(), "Unimplemented!"); - let value = result.unwrap(); - match value { - Value::Integer(v) => return assert_eq!(v, 1234), - _ => assert!(false), - } -} - -#[test] -fn deserialize_short() { - let value = vec![107, 4, 210, 0]; - let mut buf = Cursor::new(&value[..]); - let result = buf.decode(); - assert!(result.is_ok(), "Unimplemented!"); - let value = result.unwrap(); - match value { - Value::Short(v) => return assert_eq!(v, 1234), - _ => assert!(false), - } -} - -#[test] -fn deserialize_long() { - let value = vec![108, 0, 0, 0, 0, 0, 0, 4, 210, 0, 0, 0, 0, 0, 0, 0]; - let mut buf = Cursor::new(&value[..]); - let result = buf.decode(); - assert!(result.is_ok(), "Unimplemented!"); - let value = result.unwrap(); - match value { - Value::Long(v) => return assert_eq!(v, 1234), - _ => assert!(false), - } -} - -#[test] -fn deserialize_integer_array() { - let value = vec![121, 0, 2, 105, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0]; - let mut buf = Cursor::new(&value[..]); - let result = buf.decode(); - assert!(result.is_ok(), "Unimplemented!"); - let value = result.unwrap(); - match value { - Value::Array(v) => { - if let Value::Integer(val) = v[0] { - assert_eq!(val, 0); - } - if let Value::Integer(val) = v[1] { - assert_eq!(val, 1); - } - return; - } - _ => assert!(false), - } -} - -#[test] -fn deserialize_boolean() { - let value = vec![111, 1]; - let mut buf = Cursor::new(&value[..]); - let result = buf.decode(); - assert!(result.is_ok(), "Unimplemented!"); - let value = result.unwrap(); - match value { - Value::Boolean(v) => return assert_eq!(v, true), - _ => assert!(false), - } -} - -#[test] -fn deserialize_operation_response() { - let value = vec![ - 112, 100, 0, 100, 42, 0, 2, 0, 115, 0, 5, 116, 101, 115, 116, 49, 1, 115, 0, 5, 116, - 101, 115, 116, 50, 0, 0, 0, 0, 0, 0, 0, - ]; - let mut buf = Cursor::new(&value[..]); - let result = buf.decode(); - assert!(result.is_ok(), "Unimplemented!"); - let value = result.unwrap(); - match value { - Value::OperationResponse(v) => { - assert_eq!(v.code, 100); - assert_eq!(v.return_code, 100); - assert_eq!( - v.parameters.get(&1).unwrap(), - &Value::String("test2".to_owned()) - ); - return; - } - _ => assert!(false), - } -} - -#[test] -fn deserialize_operation_request() { - let value = vec![ - 113, 100, 0, 2, 0, 115, 0, 5, 116, 101, 115, 116, 49, 1, 115, 0, 5, 116, 101, 115, 116, - 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - ]; - let mut buf = Cursor::new(&value[..]); - let result = buf.decode(); - assert!(result.is_ok(), "Unimplemented!"); - let value = result.unwrap(); - match value { - Value::OperationRequest(v) => { - assert_eq!(v.code, 100); - assert_eq!( - v.parameters.get(&1).unwrap(), - &Value::String("test2".to_owned()) - ); - return; - } - _ => assert!(false), - } -} - -#[test] -fn deserialize_string() { - let value = vec![ - 115, 0, 12, 116, 101, 115, 116, 95, 109, 101, 115, 115, 97, 103, 101, 0, - ]; - let mut buf = Cursor::new(&value[..]); - let result = buf.decode(); - assert!(result.is_ok(), "Unimplemented!"); - let value = result.unwrap(); - match value { - Value::String(v) => return assert_eq!(&v, "test_message"), - _ => assert!(false), - } -} - -#[test] -fn deserialize_byte_array() { - let value = vec![120, 0, 0, 0, 2, 6, 7, 0]; - let mut buf = Cursor::new(&value[..]); - let result = buf.decode(); - assert!(result.is_ok(), "Unimplemented!"); - let value = result.unwrap(); - match value { - Value::ByteArray(v) => { - assert_eq!(&v[0], &6); - assert_eq!(&v[1], &7); - return; - } - _ => assert!(false), - } -} - -#[test] -fn deserialize_array_dictionary() { - let value = vec![ - 121, 0, 1, 68, 105, 115, 0, 1, 0, 0, 0, 0, 0, 5, 116, 101, 115, 116, 49, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, - ]; - let mut buf = Cursor::new(&value[..]); - let result = buf.decode(); - assert!(result.is_ok(), "Unimplemented!"); - let value = result.unwrap(); - match value { - Value::Array(v) => { - if let Value::Dictionary(val) = &v[0] { - assert_eq!(val.get("0").unwrap(), &Value::String("test1".to_owned())); - } - return; - } - _ => assert!(false), - } -} - -#[test] -fn deserialize_array_byte_array() { - let value = vec![121, 0, 1, 120, 0, 0, 0, 4, 0, 2, 4, 8, 0, 0, 0, 0]; - let mut buf = Cursor::new(&value[..]); - let result = buf.decode(); - assert!(result.is_ok(), "Unimplemented!"); - let value = result.unwrap(); - - match value { - Value::Array(v) => { - if let Value::ByteArray(val) = &v[0] { - assert_eq!(val[0], 0); - assert_eq!(val[1], 2); - assert_eq!(val[2], 4); - assert_eq!(val[3], 8); - return; - } - } - _ => assert!(false), - } -} - -#[test] -fn deserialize_array_array() { - let value = vec![ - 121, 0, 1, 121, 0, 3, 105, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, - ]; - let mut buf = Cursor::new(&value[..]); - let result = buf.decode(); - assert!(result.is_ok(), "Unimplemented!"); - let value = result.unwrap(); - match value { - Value::Array(v) => { - if let Value::Integer(val) = &v[0][0] { - assert_eq!(val, &1); - } - if let Value::Integer(val) = &v[0][1] { - assert_eq!(val, &2); - } - if let Value::Integer(val) = &v[0][2] { - assert_eq!(val, &3); - } - return; - } - _ => assert!(false), - } -} - -#[test] -fn deserialize_object_array() { - let value = vec![ - 122, 0, 2, 115, 0, 5, 116, 101, 115, 116, 49, 115, 0, 5, 116, 101, 115, 116, 50, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - ]; - let mut buf = Cursor::new(&value[..]); - let result = buf.decode(); - assert!(result.is_ok(), "Unimplemented!"); - let value = result.unwrap(); - - match value { - Value::ObjectArray(v) => { - if let Value::Object(val) = &*v[0] { - assert_eq!(**val, Value::String("test1".to_owned())); - } - if let Value::Object(val) = &*v[1] { - assert_eq!(**val, Value::String("test2".to_owned())); - } - } - _ => assert!(false), - }; -} \ No newline at end of file diff --git a/backend/lib/photon-protocol/tests/integration_tests.rs b/backend/lib/photon-protocol/tests/integration_tests.rs deleted file mode 100644 index e0694a6..0000000 --- a/backend/lib/photon-protocol/tests/integration_tests.rs +++ /dev/null @@ -1,277 +0,0 @@ -use photon_protocol; - -use photon_protocol::Message; -use photon_protocol::EventData; -use photon_protocol::OperationRequest; -use photon_protocol::OperationResponse; -use photon_protocol::Value; -use photon_protocol::Parameters; - -#[test] -fn test_single_event_message() -{ - let header = vec![ - 0x00, 0x01, // PeerID - 0x01, // CrcEnabled - 0x01, // CommandCount - 0x00, 0x00, 0x00, 0x01, // Timestamp - 0x00, 0x00, 0x00, 0x01, // Challenge - ]; - - let photon_command = vec![ - 0x06, // Type - 0x01, // ChannelID - 0x01, // Flags - 0x04, // ReservedByte - 0x00, 0x00, 0x00, 0x1a, // Length - 0x00, 0x00, 0x00, 0x01, // ReliableSequenceNumber - ]; - - let message = vec![ - 0x01, // ?? - 0x04, // MsgType (Event) - // Payload - 0x64, 0x00, 0x02, // Key - 0x00, 0x73, // String type - 0x00, 0x05, // String length - 0x74, 0x65, 0x73, 0x74, 0x31 //test1 - ]; - - let mut photon = photon_protocol::Photon::new(); - - let decoded = photon.decode( - &[&header[..], &photon_command[..], &message[..]].concat()); - - assert!(decoded.len() == 1); - let parameters: Parameters = - [(0, Value::String("test1".to_owned()))] - .iter().cloned().collect(); - assert_eq!(decoded[0], Message::Event(EventData{ code: 100, parameters})); -} - -#[test] -fn test_fragmented_event_message() -{ - let header = vec![ - 0x00, 0x01, // PeerID - 0x01, // CrcEnabled - 0x01, // CommandCount - 0x00, 0x00, 0x00, 0x01, // Timestamp - 0x00, 0x00, 0x00, 0x01, // Challenge - ]; - - let mut photon = photon_protocol::Photon::new(); - - let photon_command = vec![ - 0x08, // Type - 0x01, // ChannelID - 0x01, // Flags - 0x04, // ReservedByte - 0x00, 0x00, 0x00, 0x22, // Length - 0x00, 0x00, 0x00, 0x01, // ReliableSequenceNumber - 0x00, 0x00, 0x00, 0x01, // SequenceNumber - 0x00, 0x00, 0x00, 0x03, // FragmentCount - 0x00, 0x00, 0x00, 0x01, // FragmentNumber - 0x00, 0x00, 0x00, 0x1a, // TotalLength - 0x00, 0x00, 0x00, 0x01, // OperationLength - ]; - - let message = vec![ - 0x01, // ?? - 0x04, // MsgType (Event) - // Payload - ]; - - let decoded = photon.decode( - &[&header[..], &photon_command[..], &message[..]].concat()); - assert!(decoded.len() == 0); - - let photon_command = vec![ - 0x08, // Type - 0x01, // ChannelID - 0x01, // Flags - 0x04, // ReservedByte - 0x00, 0x00, 0x00, 0x27, // Length - 0x00, 0x00, 0x00, 0x01, // ReliableSequenceNumber - 0x00, 0x00, 0x00, 0x01, // SequenceNumber - 0x00, 0x00, 0x00, 0x03, // FragmentCount - 0x00, 0x00, 0x00, 0x02, // FragmentNumber - 0x00, 0x00, 0x00, 0x1a, // TotalLength - 0x00, 0x00, 0x00, 0x01, // OperationLength - ]; - - let message = vec![ - // Payload - 0x64, 0x00, 0x02, // Key - 0x00, 0x73, // String type - 0x00, 0x05, // String length - ]; - - let decoded = photon.decode( - &[&header[..], &photon_command[..], &message[..]].concat()); - assert!(decoded.len() == 0); - - let photon_command = vec![ - 0x08, // Type - 0x01, // ChannelID - 0x01, // Flags - 0x04, // ReservedByte - 0x00, 0x00, 0x00, 0x25, // Length - 0x00, 0x00, 0x00, 0x01, // ReliableSequenceNumber - 0x00, 0x00, 0x00, 0x01, // SequenceNumber - 0x00, 0x00, 0x00, 0x03, // FragmentCount - 0x00, 0x00, 0x00, 0x03, // FragmentNumber - 0x00, 0x00, 0x00, 0x1a, // TotalLength - 0x00, 0x00, 0x00, 0x01, // OperationLength - ]; - - let message = vec![ - 0x74, 0x65, 0x73, 0x74, 0x31 //test1 - ]; - - let decoded = photon.decode( - &[&header[..], &photon_command[..], &message[..]].concat()); - assert_eq!(decoded.len(), 1); - - let parameters: Parameters = - [(0, Value::String("test1".to_owned()))] - .iter().cloned().collect(); - assert_eq!(decoded[0], Message::Event(EventData{ code: 100, parameters})); -} - - -#[test] -fn test_two_event_messages() -{ - let payload = vec![ - 0x00, 0x00, - 0x00, - 0x02, - 0x72, 0xe9, 0xe2, 0x84, - 0x11, 0x63, 0x88, 0x88, - - 0x07, - 0x00, - 0x00, - 0x00, - 0x00, 0x00, 0x00, 0x45, - 0x00, 0x00, 0x16, 0x8e, - 0x00, 0x00, 0x45, 0x2b, - 0xf3, 0x04, 0x02, 0x00, 0x02, 0x00, 0x6c, 0x00, 0x00, 0x00, 0x00, 0x00, 0xa, 0xb7, 0x36, 0x01, 0x78, 0x00, 0x00, 0x00, 0x20, 0x03, 0x6b, 0xc9, 0x04, 0xc1, 0xe2, 0x62, 0xd7, 0x08, 0x6b, 0xb0, 0x91, 0xc1, 0x2a, 0x80, 0x1b, 0xc2, 0x26, 0x00, 0x00, 0xb0, 0x40, 0xd8, 0xe6, 0x53, 0xc1, 0x8f, 0xcd, 0xc, 0xc2, 0x00, 0x00, - - 0x06, - 0x00, - 0x01, - 0x00, - 0x00, 0x00, 0x00, 0x1b, - 0x00, 0x00, 0x16, 0x8f, - 0xf3, 0x04, 0x01, 0x00, 0x02, 0x00, 0x69, 0x00, 0xa, 0xf6, 0xdf, 0xfc, 0x6b, 0x00, 0x01 - ]; - - let mut photon = photon_protocol::Photon::new(); - - let decoded = photon.decode(&payload); - - assert!(decoded.len() == 2); - let parameters: Parameters = - [ - (0x01, Value::ByteArray(vec![ - 3, 107, 201, 4, 193, 226, 98, 215, 8, 107, 176, 145, 193, - 42, 128, 27, 194, 38, 0, 0, 176, 64, 216, 230, 83, 193, - 143, 205, 12, 194, 0, 0])), - (0x00, Value::Long(702262)) - ] - .iter().cloned().collect(); - assert_eq!(decoded[0], Message::Event(EventData{ code: 2, parameters})); - - let parameters: Parameters = - [ - (0x00, Value::Integer(718559)), - (0xfc, Value::Short(1)) - ] - .iter().cloned().collect(); - assert_eq!(decoded[1], Message::Event(EventData{ code: 1, parameters})); - -} - -#[test] -fn test_request_message() -{ - let payload = vec![ - 0x7d, 0xd3, - 0x00, - 0x01, - 0x00, 0x04, 0xcc, 0xb6, - 0x11, 0x63, 0x88, 0x88, - - 0x06, - 0x00, - 0x01, - 0x04, - 0x00, 0x00, 0x00, 0x2a, - 0x00, 0x00, 0x00, 0x19, - 0xf3, 0x02, 0x01, 0x00, 0x03, 0x00, - 0x79, 0x00, 0x02, 0x66, 0xc1, 0xd3, 0x61, 0x20, 0xc2, 0x27, 0xc, 0xcd, 0x01, 0x62, - 0x00, 0xfd, // key - 0x73, // String type - 0x00, 0x05, // String length - 0x74, 0x65, 0x73, 0x74, 0x31 // test1 - ]; - - let mut photon = photon_protocol::Photon::new(); - let decoded = photon.decode(&payload); - - assert!(decoded.len() == 1); - let parameters: Parameters = - [ - (0x00, Value::Array(vec![Value::Float(-26.422424), Value::Float(-41.7625)])), - (0x01, Value::Byte(0)), - (0xfd, Value::String("test1".to_owned())) - - ] - .iter().cloned().collect(); - assert_eq!(decoded[0], Message::Request(OperationRequest { code: 1, parameters})); -} - -#[test] -fn test_response_message() -{ - let payload = vec![ - 0x00, 0x00, - 0x00, - 0x02, - 0x72, 0xea, 0x1d, 0x6a, - 0x11, 0x63, 0x88, 0x88, - - 0x01, - 0x00, - 0x00, - 0x00, - 0x00, 0x00, 0x00, 0x14, - 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x24, 0x00, 0x04, 0xd7, 0x1a, - - 0x06, - 0x00, - 0x01, - 0x00, - 0x00, 0x00, 0x00, 0xb2, - 0x00, 0x00, 0x17, 0x71, - 0xf3, 0x03, 0x01, 0x00, 0x00, 0x2a, 0x00, 0xa, 0x00, 0x69, 0x18, 0xf2, 0x79, 0x68, 0x01, 0x73, 0x00, 0x5d, 0x40, 0x49, 0x53, 0x4c, 0x41, 0x4e, 0x44, 0x40, 0x65, 0x66, 0x33, 0x37, 0x39, 0x34, 0x64, 0x61, 0x2d, 0x38, 0x66, 0x38, 0x33, 0x2d, 0x34, 0x33, 0x34, 0x30, 0x2d, 0x62, 0x66, 0x35, 0x32, 0x2d, 0x36, 0x38, 0x31, 0x31, 0x37, 0x61, 0x30, 0x36, 0x37, 0x32, 0x65, 0x34, 0x7c, 0x57, 0x65, 0x64, 0x72, 0x6f, 0x77, 0x79, 0x63, 0x7a, 0x4a, 0x61, 0x6b, 0x75, 0x62, 0x7c, 0x36, 0x33, 0x37, 0x30, 0x37, 0x35, 0x33, 0x37, 0x38, 0x38, 0x30, 0x33, 0x31, 0x38, 0x33, 0x39, 0x33, 0x35, 0x7c, 0x57, 0x65, 0x64, 0x72, 0x6f, 0x77, 0x79, 0x63, 0x7a, 0x4a, 0x61, 0x6b, 0x75, 0x62, 0x02, 0x79, 0x00, 0x00, 0x78, 0x03, 0x79, 0x00, 0x00, 0x73, 0x04, 0x79, 0x00, 0x00, 0x73, 0x05, 0x78, 0x00, 0x00, 0x00, 0x00, 0x06, 0x79, 0x00, 0x01, 0x79, 0x00, 0x01, 0x69, 0xff, 0xff, 0xff, 0xff, 0x07, 0x79, 0x00, 0x01, 0x79, 0x00, 0x01, 0x69, 0xff, 0xff, 0xff, 0xff, 0x08, 0x78, 0x00, 0x00, 0x00, 0x00, 0xfd, 0x6b, 0x00, 0xa6 - ]; - - let mut photon = photon_protocol::Photon::new(); - - let decoded = photon.decode(&payload); - - assert_eq!(decoded.len(), 1); - let parameters: Parameters = - [ - (0x00, Value::Integer(418543976)), - (0x01, Value::String("@ISLAND@ef3794da-8f83-4340-bf52-68117a0672e4|WedrowyczJakub|637075378803183935|WedrowyczJakub".to_owned())) - - ] - .iter().cloned().collect(); - assert_eq!(decoded[0], Message::Response(OperationResponse { code: 1, return_code: 0, debug_message: "None".to_owned(), parameters})); -} - diff --git a/backend/src/core.rs b/backend/src/core.rs index 54f90a7..528d669 100644 --- a/backend/src/core.rs +++ b/backend/src/core.rs @@ -8,8 +8,8 @@ use simplelog::*; use packet_sniffer::UdpPacket; -use photon_protocol; -use photon_protocol::Photon; +use photon_decode; +use photon_decode::Photon; use crate::game_protocol; use crate::meter; @@ -151,25 +151,25 @@ fn register_message(events: &mut meter::Meter, message: &game_protocol::Message) } } -fn into_game_message(photon_message : photon_protocol::Message) -> Option +fn into_game_message(photon_message : photon_decode::Message) -> Option { static REQUEST_CONSTANT: usize = 10000; static RESPONSE_CONSTANT: usize = 1000; match photon_message { - photon_protocol::Message::Event(e) => { + photon_decode::Message::Event(e) => { if e.code != 2 && e.parameters.get(&252u8).is_some() { - if let photon_protocol::Value::Short(event_code) = e.parameters.get(&252u8).unwrap() { + if let photon_decode::Value::Short(event_code) = e.parameters.get(&252u8).unwrap() { return game_protocol::Packet{code: *event_code as usize, parameters: e.parameters}.decode() } } game_protocol::Packet{code: 0, parameters: e.parameters} }, - photon_protocol::Message::Request(r) => { + photon_decode::Message::Request(r) => { let code = r.code as usize + REQUEST_CONSTANT; game_protocol::Packet{code, parameters: r.parameters} }, - photon_protocol::Message::Response(r) => { + photon_decode::Message::Response(r) => { let code = r.code as usize + RESPONSE_CONSTANT; game_protocol::Packet{code, parameters: r.parameters} } diff --git a/backend/src/game_protocol/message.rs b/backend/src/game_protocol/message.rs index c36e348..4ced101 100644 --- a/backend/src/game_protocol/message.rs +++ b/backend/src/game_protocol/message.rs @@ -1,8 +1,8 @@ use log::*; use super::packet::Packet; -use photon_protocol::Parameters; -use photon_protocol::Value; +use photon_decode::Parameters; +use photon_decode::Value; macro_rules! deserialize_number { ($val:expr, $index:expr, $name:expr) => { diff --git a/backend/src/game_protocol/packet.rs b/backend/src/game_protocol/packet.rs index b8f4bd4..68b57e1 100644 --- a/backend/src/game_protocol/packet.rs +++ b/backend/src/game_protocol/packet.rs @@ -1,4 +1,4 @@ -use photon_protocol::Parameters; +use photon_decode::Parameters; #[derive(Debug)] pub struct Packet {