diff options
| author | Fuwn <[email protected]> | 2021-06-02 20:18:12 +0000 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2021-06-02 20:18:12 +0000 |
| commit | 95fb0f956fd5cfacec3f7c69d6a10fcec3c95236 (patch) | |
| tree | b84950ef38d21b20058bd6e0d700f2da842f90fe | |
| parent | Merge pull request #38 from Whirlsplash/renovate/tokio-1.x (diff) | |
| download | whirl-95fb0f956fd5cfacec3f7c69d6a10fcec3c95236.tar.xz whirl-95fb0f956fd5cfacec3f7c69d6a10fcec3c95236.zip | |
refactor(server): use enum for command types
| -rw-r--r-- | crates/whirl_server/Cargo.toml | 1 | ||||
| -rw-r--r-- | crates/whirl_server/src/cmd/constants.rs | 33 | ||||
| -rw-r--r-- | crates/whirl_server/src/distributor.rs | 18 |
3 files changed, 43 insertions, 9 deletions
diff --git a/crates/whirl_server/Cargo.toml b/crates/whirl_server/Cargo.toml index bef599a..1697faf 100644 --- a/crates/whirl_server/Cargo.toml +++ b/crates/whirl_server/Cargo.toml @@ -20,6 +20,7 @@ log = "0.4.14" # Utility async-trait = "0.1.50" num-traits = "0.2.14" +num-derive = "0.3.3" # Byte Manipulation bytes = "1.0.1" diff --git a/crates/whirl_server/src/cmd/constants.rs b/crates/whirl_server/src/cmd/constants.rs index 1271a9a..ac17798 100644 --- a/crates/whirl_server/src/cmd/constants.rs +++ b/crates/whirl_server/src/cmd/constants.rs @@ -3,6 +3,39 @@ #![allow(dead_code)] +#[derive(num_derive::FromPrimitive)] +pub enum Command { + LongLoc = 1, + State, + Prop, + ShortLoc, + RoomChng, + SessInit, + SessExit, + AppInit, + PropReq = 10, + Disappr, + ApprActr, + RegObjId, + Text, + PropSet, + PropUpd, + Whisper, + Teleport, + RoomIdRq = 20, + RoomId, + Subscrib, + Unsubscr, + SubDist, + Redirect, + RedirId, + FingReq, + FingRep, + BuddyListUpdate, + BuddyListNotify, + Channel, +} + pub const LONGLOC: i32 = 1; pub const STATE: i32 = 2; pub const PROP: i32 = 3; diff --git a/crates/whirl_server/src/distributor.rs b/crates/whirl_server/src/distributor.rs index de87dcd..fc07662 100644 --- a/crates/whirl_server/src/distributor.rs +++ b/crates/whirl_server/src/distributor.rs @@ -32,7 +32,7 @@ use crate::{ room_id_request::RoomIdRequest, text::Text, }, - constants::{BUDDYLISTUPDATE, PROPREQ, PROPSET, ROOMIDRQ, SESSEXIT, SESSINIT}, + constants::Command, extendable::{Creatable, Parsable}, }, interaction::{peer::Peer, shared::Shared}, @@ -64,15 +64,15 @@ impl Server for Distributor { result = peer.bytes.next() => match result { Some(Ok(msg)) => { for msg in parse_commands_from_packet(msg) { - match msg.get(2).unwrap().to_owned().as_(): i32 { - PROPREQ => { + match num_traits::FromPrimitive::from_i32(msg.get(2).unwrap().to_owned().as_(): i32) { + Some(Command::PropReq) => { debug!("received property request from client"); peer.bytes.get_mut() .write_all(&property_update_as_distributor()).await?; trace!("sent property update to client"); } - SESSINIT => { + Some(Command::SessInit) => { username = (&*find_property_in_property_list( &parse_network_property(msg[3..].to_vec()), VAR_USERNAME, @@ -84,7 +84,7 @@ impl Server for Distributor { .write_all(&property_request_as_distributor()).await?; trace!("sent property request to {}", username); } - PROPSET => { + Some(Command::PropSet) => { debug!("received property set from {}", username); peer.bytes.get_mut() @@ -96,13 +96,13 @@ impl Server for Distributor { .write_all(&create()).await?; trace!("sent text to {}", username); } - BUDDYLISTUPDATE => { + Some(Command::BuddyListUpdate) => { let buddy = BuddyList::parse(msg.to_vec()); debug!("received buddy list update from {}: {}", username, buddy.buddy); peer.bytes.get_mut().write_all(&buddy.create()).await?; trace!("sent buddy list notify to {}: {}", username, buddy.buddy); } - ROOMIDRQ => { + Some(Command::RoomIdRq) => { let room = RoomIdRequest::parse(msg.to_vec()); debug!("received room id request from {}: {}", username, &room.room_name); @@ -123,10 +123,10 @@ impl Server for Distributor { }.create()).await?; trace!("sent redirect id to {}: {}", username, room.room_name); } - SESSEXIT => { + Some(Command::SessExit) => { debug!("received session exit from {}", username); break; } - _ => (), + _ => {}, } } } |