From ca4fde0b509405b14892b1aa1340f1f44051f08d Mon Sep 17 00:00:00 2001 From: CodeSteak Date: Wed, 24 Apr 2019 18:45:01 +0200 Subject: [PATCH] add way to send Texture and Texture Arrays --- .../implementation/protocol_connection.rs | 44 ++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/server/texture-sync-server/src/protocol/implementation/protocol_connection.rs b/server/texture-sync-server/src/protocol/implementation/protocol_connection.rs index 7961d3c..502fec2 100644 --- a/server/texture-sync-server/src/protocol/implementation/protocol_connection.rs +++ b/server/texture-sync-server/src/protocol/implementation/protocol_connection.rs @@ -7,6 +7,8 @@ pub enum Package { JsonNull, JsonFalse, JsonTrue, + JsonTexture(Texture), + JsonTextureArray(Vec), Json(Command), Binary(Vec), Error(u16, String), @@ -156,7 +158,22 @@ impl Connection { Ok(None) => { return Ok(Package::JsonNull); } + _ => (), // else try other + } + // try single texture + match serde_json::from_slice::(&payload[..]) { + Ok(texture) => { + return Ok(Package::JsonTexture(texture)); + } + _ => (), // else try other + } + + // try texture vec + match serde_json::from_slice::>(&payload[..]) { + Ok(textures) => { + return Ok(Package::JsonTextureArray(textures)); + } _ => (), // else try other } @@ -182,6 +199,10 @@ impl Connection { Package::JsonNull => self.send_json(&Option::::None), Package::JsonTrue => self.send_json(&true), Package::JsonFalse => self.send_json(&false), + + Package::JsonTexture(texture) => self.send_json(texture), + Package::JsonTextureArray(textures) => self.send_json(textures), + Package::Json(cmd) => self.send_json(cmd), Package::Binary(bin) => self.send_binary(bin), @@ -189,7 +210,7 @@ impl Connection { } } - fn send_json(&mut self, msg: &J) -> Result<()> { + fn send_json(&mut self, msg: &J) -> Result<()> { let w = &mut self.writer; let payload = serde_json::to_vec(msg).unwrap(); @@ -234,6 +255,21 @@ mod test { use crate::model::*; // Utility fn + fn demo_texture() -> Texture { + const HASH_BYTES: [u8; 32] = [ + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, + 24, 25, 26, 27, 28, 29, 30, 31, + ]; + + Texture { + id: "d32e1f80-a17a-4dd7-8ed7-c3a2de1de1c9".to_string(), + name: "texture.png".to_string(), + tags: vec!["Wood".to_string(), "Hair".to_string()], + format: TextureFormat::PNG, + resolution: (512, 512), + texture_hash: Sha256(HASH_BYTES), + } + } fn test_read_back(pkg: &Package) { let dummy_read_data = [255u8]; @@ -377,6 +413,12 @@ mod test { test_read_back(&Package::JsonFalse); test_read_back(&Package::JsonTrue); + test_read_back(&Package::JsonTexture(demo_texture())); + test_read_back(&Package::JsonTextureArray(vec![ + demo_texture(), + demo_texture(), + ])); + test_read_back(&Package::Json(Command::Pong {})); test_read_back(&Package::Error(500, "I bims 1 Error".to_string()));