diff options
| author | Fuwn <[email protected]> | 2021-06-06 00:09:18 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2021-06-06 00:09:18 -0700 |
| commit | 6ce28bb035b7080e449474af4ffb962878e7e534 (patch) | |
| tree | 86d95abd411b902ff0e1d26a82e1aa7d793017f4 | |
| parent | refactor(whirl_server): remove single imports (diff) | |
| download | whirl-6ce28bb035b7080e449474af4ffb962878e7e534.tar.xz whirl-6ce28bb035b7080e449474af4ffb962878e7e534.zip | |
refactor(whirl_server::net): move orphan functions to methods within property list struct
| -rw-r--r-- | crates/whirl_server/src/cmd/commands/property/create.rs | 248 | ||||
| -rw-r--r-- | crates/whirl_server/src/cmd/commands/property/mod.rs | 1 | ||||
| -rw-r--r-- | crates/whirl_server/src/cmd/commands/property/parse.rs | 14 | ||||
| -rw-r--r-- | crates/whirl_server/src/distributor.rs | 14 | ||||
| -rw-r--r-- | crates/whirl_server/src/hub.rs | 14 | ||||
| -rw-r--r-- | crates/whirl_server/src/net/converter.rs | 58 | ||||
| -rw-r--r-- | crates/whirl_server/src/net/mod.rs | 7 | ||||
| -rw-r--r-- | crates/whirl_server/src/net/network_property.rs (renamed from crates/whirl_server/src/net/structure.rs) | 6 | ||||
| -rw-r--r-- | crates/whirl_server/src/net/property_list.rs | 94 | ||||
| -rw-r--r-- | crates/whirl_server/src/net/property_parser.rs | 34 |
10 files changed, 227 insertions, 263 deletions
diff --git a/crates/whirl_server/src/cmd/commands/property/create.rs b/crates/whirl_server/src/cmd/commands/property/create.rs index d689857..c9f0e04 100644 --- a/crates/whirl_server/src/cmd/commands/property/create.rs +++ b/crates/whirl_server/src/cmd/commands/property/create.rs @@ -22,147 +22,135 @@ use crate::{ VAR_SMTP_SERVER, VAR_UPDATETIME, }, - converter::property_list_to_bytes, - structure::NetworkProperty, + network_property::NetworkProperty, + property_list::PropertyList, }, }; pub fn property_update_as_distributor() -> Vec<u8> { - property_list_to_bytes( - Command::PropUpd as i32, - 0xFF, - vec![ - NetworkProperty { - prop_id: VAR_MAIL_DOMAIN, - value: "worlds3d.com".to_string(), - }, - NetworkProperty { - prop_id: VAR_SMTP_SERVER, - value: "mail.worlds.net:25".to_string(), - }, - NetworkProperty { - prop_id: VAR_SCRIPT_SERVER, - value: "http://www-dynamic.us.worlds.net/cgi-bin".to_string(), - }, - NetworkProperty { - prop_id: VAR_EXTERNAL_HTTP_SERVER, - value: "http://www-static.us.worlds.net".to_string(), - }, - NetworkProperty { - prop_id: VAR_SERVERTYPE, - value: "1".to_string(), - }, - NetworkProperty { - prop_id: VAR_PROTOCOL, - value: "24".to_string(), - }, - NetworkProperty { - prop_id: VAR_APPNAME, - value: Config::get().whirlsplash.worldsmaster_username, - }, - ], - ) + PropertyList(vec![ + NetworkProperty { + prop_id: VAR_MAIL_DOMAIN, + value: "worlds3d.com".to_string(), + }, + NetworkProperty { + prop_id: VAR_SMTP_SERVER, + value: "mail.worlds.net:25".to_string(), + }, + NetworkProperty { + prop_id: VAR_SCRIPT_SERVER, + value: "http://www-dynamic.us.worlds.net/cgi-bin".to_string(), + }, + NetworkProperty { + prop_id: VAR_EXTERNAL_HTTP_SERVER, + value: "http://www-static.us.worlds.net".to_string(), + }, + NetworkProperty { + prop_id: VAR_SERVERTYPE, + value: "1".to_string(), + }, + NetworkProperty { + prop_id: VAR_PROTOCOL, + value: "24".to_string(), + }, + NetworkProperty { + prop_id: VAR_APPNAME, + value: Config::get().whirlsplash.worldsmaster_username, + }, + ]) + .as_bytes(Command::PropUpd as i32, 0xFF) } pub fn property_update_as_hub() -> Vec<u8> { - property_list_to_bytes( - Command::PropUpd as i32, - 0xFF, - vec![ - NetworkProperty { - prop_id: VAR_UPDATETIME, - value: "1000000".to_string(), - }, - NetworkProperty { - prop_id: VAR_MAIL_DOMAIN, - value: "worlds3d.com".to_string(), - }, - NetworkProperty { - prop_id: VAR_SMTP_SERVER, - value: "mail.worlds.net:25".to_string(), - }, - NetworkProperty { - prop_id: VAR_SCRIPT_SERVER, - value: "http://www-dynamic.us.worlds.net/cgi-bin".to_string(), - }, - NetworkProperty { - prop_id: VAR_EXTERNAL_HTTP_SERVER, - value: "http://www-static.us.worlds.net".to_string(), - }, - NetworkProperty { - prop_id: VAR_SERVERTYPE, - value: "3".to_string(), - }, - NetworkProperty { - prop_id: VAR_PROTOCOL, - value: "24".to_string(), - }, - NetworkProperty { - prop_id: VAR_APPNAME, - value: Config::get().whirlsplash.worldsmaster_username, - }, - ], - ) + PropertyList(vec![ + NetworkProperty { + prop_id: VAR_UPDATETIME, + value: "1000000".to_string(), + }, + NetworkProperty { + prop_id: VAR_MAIL_DOMAIN, + value: "worlds3d.com".to_string(), + }, + NetworkProperty { + prop_id: VAR_SMTP_SERVER, + value: "mail.worlds.net:25".to_string(), + }, + NetworkProperty { + prop_id: VAR_SCRIPT_SERVER, + value: "http://www-dynamic.us.worlds.net/cgi-bin".to_string(), + }, + NetworkProperty { + prop_id: VAR_EXTERNAL_HTTP_SERVER, + value: "http://www-static.us.worlds.net".to_string(), + }, + NetworkProperty { + prop_id: VAR_SERVERTYPE, + value: "3".to_string(), + }, + NetworkProperty { + prop_id: VAR_PROTOCOL, + value: "24".to_string(), + }, + NetworkProperty { + prop_id: VAR_APPNAME, + value: Config::get().whirlsplash.worldsmaster_username, + }, + ]) + .as_bytes(Command::PropUpd as i32, 0xFF) } pub fn property_request_as_distributor() -> Vec<u8> { - property_list_to_bytes( - Command::SessInit as i32, - 0x01, - vec![ - NetworkProperty { - prop_id: VAR_ERROR, - value: "0".to_string(), - }, - NetworkProperty { - prop_id: VAR_APPNAME, - value: Config::get().whirlsplash.worldsmaster_username, - }, - NetworkProperty { - prop_id: VAR_PROTOCOL, - value: "24".to_string(), - }, - NetworkProperty { - prop_id: VAR_SERVERTYPE, - value: "1".to_string(), - }, - NetworkProperty { - prop_id: VAR_SERIAL, - value: "DWLV000000000000".to_string(), - }, - NetworkProperty { - prop_id: VAR_PRIV, - value: "0".to_string(), - }, - NetworkProperty { - prop_id: VAR_CHANNEL, - value: "dimension-1".to_string(), - }, - ], - ) + PropertyList(vec![ + NetworkProperty { + prop_id: VAR_ERROR, + value: "0".to_string(), + }, + NetworkProperty { + prop_id: VAR_APPNAME, + value: Config::get().whirlsplash.worldsmaster_username, + }, + NetworkProperty { + prop_id: VAR_PROTOCOL, + value: "24".to_string(), + }, + NetworkProperty { + prop_id: VAR_SERVERTYPE, + value: "1".to_string(), + }, + NetworkProperty { + prop_id: VAR_SERIAL, + value: "DWLV000000000000".to_string(), + }, + NetworkProperty { + prop_id: VAR_PRIV, + value: "0".to_string(), + }, + NetworkProperty { + prop_id: VAR_CHANNEL, + value: "dimension-1".to_string(), + }, + ]) + .as_bytes(Command::SessInit as i32, 0x01) } pub fn property_request_as_hub() -> Vec<u8> { - property_list_to_bytes( - Command::SessInit as i32, - 0x01, - vec![ - NetworkProperty { - prop_id: VAR_ERROR, - value: "0".to_string(), - }, - NetworkProperty { - prop_id: VAR_SERVERTYPE, - value: "3".to_string(), - }, - NetworkProperty { - prop_id: VAR_UPDATETIME, - value: "1000000".to_string(), - }, - NetworkProperty { - prop_id: VAR_PROTOCOL, - value: "24".to_string(), - }, - ], - ) + PropertyList(vec![ + NetworkProperty { + prop_id: VAR_ERROR, + value: "0".to_string(), + }, + NetworkProperty { + prop_id: VAR_SERVERTYPE, + value: "3".to_string(), + }, + NetworkProperty { + prop_id: VAR_UPDATETIME, + value: "1000000".to_string(), + }, + NetworkProperty { + prop_id: VAR_PROTOCOL, + value: "24".to_string(), + }, + ]) + .as_bytes(Command::SessInit as i32, 0x01) } diff --git a/crates/whirl_server/src/cmd/commands/property/mod.rs b/crates/whirl_server/src/cmd/commands/property/mod.rs index 83b015b..8405101 100644 --- a/crates/whirl_server/src/cmd/commands/property/mod.rs +++ b/crates/whirl_server/src/cmd/commands/property/mod.rs @@ -2,4 +2,3 @@ // SPDX-License-Identifier: GPL-3.0-only pub mod create; -pub mod parse; diff --git a/crates/whirl_server/src/cmd/commands/property/parse.rs b/crates/whirl_server/src/cmd/commands/property/parse.rs deleted file mode 100644 index 415d19f..0000000 --- a/crates/whirl_server/src/cmd/commands/property/parse.rs +++ /dev/null @@ -1,14 +0,0 @@ -// Copyleft (ɔ) 2021-2021 The Whirlsplash Collective -// SPDX-License-Identifier: GPL-3.0-only - -use crate::net::structure::NetworkProperty; - -pub fn find_property_in_property_list( - property_list: &[NetworkProperty], - property: i32, -) -> &NetworkProperty { - property_list - .iter() - .find(|i| i.prop_id == property) - .unwrap() -} diff --git a/crates/whirl_server/src/distributor.rs b/crates/whirl_server/src/distributor.rs index fc07662..1e92089 100644 --- a/crates/whirl_server/src/distributor.rs +++ b/crates/whirl_server/src/distributor.rs @@ -24,10 +24,7 @@ use crate::{ commands::{ action::create, buddy_list::BuddyList, - property::{ - create::{property_request_as_distributor, property_update_as_distributor}, - parse::find_property_in_property_list, - }, + property::create::{property_request_as_distributor, property_update_as_distributor}, redirect_id::RedirectId, room_id_request::RoomIdRequest, text::Text, @@ -36,7 +33,7 @@ use crate::{ extendable::{Creatable, Parsable}, }, interaction::{peer::Peer, shared::Shared}, - net::{constants::VAR_USERNAME, property_parser::parse_network_property}, + net::constants::VAR_USERNAME, packet_parser::parse_commands_from_packet, Server, }; @@ -73,10 +70,9 @@ impl Server for Distributor { trace!("sent property update to client"); } Some(Command::SessInit) => { - username = (&*find_property_in_property_list( - &parse_network_property(msg[3..].to_vec()), - VAR_USERNAME, - ).value).to_string(); + username = (*crate::net::property_list::PropertyList::from_bytes(msg[3..] + .to_vec()) + .find(VAR_USERNAME)).value.to_string(); debug!("received session initialization from {}", username); diff --git a/crates/whirl_server/src/hub.rs b/crates/whirl_server/src/hub.rs index 7ccd247..2cb21d6 100644 --- a/crates/whirl_server/src/hub.rs +++ b/crates/whirl_server/src/hub.rs @@ -21,10 +21,7 @@ use crate::{ commands::{ action::create, buddy_list::BuddyList, - property::{ - create::{property_request_as_hub, property_update_as_hub}, - parse::find_property_in_property_list, - }, + property::create::{property_request_as_hub, property_update_as_hub}, subscribe_distance::SubscribeDistance, subscribe_room::SubscribeRoom, teleport::Teleport, @@ -34,7 +31,7 @@ use crate::{ extendable::{Creatable, Parsable, ParsableWithArguments}, }, interaction::{peer::Peer, shared::Shared}, - net::{constants::VAR_USERNAME, property_parser::parse_network_property}, + net::constants::VAR_USERNAME, packet_parser::parse_commands_from_packet, Server, }; @@ -73,10 +70,9 @@ impl Server for Hub { trace!("sent property update to client"); } Some(Command::SessInit) => { - username = (&*find_property_in_property_list( - &parse_network_property(msg[3..].to_vec()), - VAR_USERNAME, - ).value).to_string(); + username = (*crate::net::property_list::PropertyList::from_bytes(msg[3..] + .to_vec()) + .find(VAR_USERNAME)).value.to_string(); debug!("received session initialization from {}", username); diff --git a/crates/whirl_server/src/net/converter.rs b/crates/whirl_server/src/net/converter.rs deleted file mode 100644 index f8a5188..0000000 --- a/crates/whirl_server/src/net/converter.rs +++ /dev/null @@ -1,58 +0,0 @@ -// Copyleft (ɔ) 2021-2021 The Whirlsplash Collective -// SPDX-License-Identifier: GPL-3.0-only - -use bytes::{BufMut, BytesMut}; -use num_traits::AsPrimitive; - -use crate::{ - cmd::constants::Command, - net::{ - constants::{PROPACCESS_POSSESS, PROPFLAG_DBSTORE}, - PropertyList, - }, -}; - -pub fn property_list_to_bytes( - command_id: i32, - obj_id: i32, - mut property_list: PropertyList, -) -> Vec<u8> { - let mut command = BytesMut::new(); - - // Iterate over all network properties - loop { - // Check if there are any properties left - trace!("props left: {}", property_list.len()); - if property_list.is_empty() { - break; - } - - let property = &property_list[0]; // Property we are currently iterating over - trace!("current prop: {}:{}", property.prop_id, property.value); - - command.put_u8(property.prop_id.as_(): u8); // Property ID - - // NOTE: THIS IS SUPER BAD DO NOT DO THIS! But it works! - if command_id == Command::PropUpd as i32 { - command.put_u8(PROPFLAG_DBSTORE.as_(): u8); // Flag (s) - command.put_u8(PROPACCESS_POSSESS.as_(): u8); // Access - } - - command.put_u8(property.value.len().as_(): u8); // Property UTF-8 Length - command.put_slice(property.value.as_bytes()); // Property UTF-8 - - property_list.reverse(); - property_list.pop(); - property_list.reverse(); - } - - // Convert to vector and insert the header - let mut command_as_vec = command.to_vec(); - - command_as_vec.insert(0, command_id.as_(): u8); // Command ID - command_as_vec.insert(0, obj_id.as_(): u8); // ObjId - command_as_vec.insert(0, command.len().as_(): u8 + 3); // Data length - - // Return bytes - command_as_vec -} diff --git a/crates/whirl_server/src/net/mod.rs b/crates/whirl_server/src/net/mod.rs index 27e4cf5..32f9995 100644 --- a/crates/whirl_server/src/net/mod.rs +++ b/crates/whirl_server/src/net/mod.rs @@ -2,8 +2,5 @@ // SPDX-License-Identifier: GPL-3.0-only pub mod constants; -pub mod converter; -pub mod property_parser; -pub mod structure; - -type PropertyList = Vec<crate::net::structure::NetworkProperty>; +pub mod network_property; +pub mod property_list; diff --git a/crates/whirl_server/src/net/structure.rs b/crates/whirl_server/src/net/network_property.rs index 1549340..259c83b 100644 --- a/crates/whirl_server/src/net/structure.rs +++ b/crates/whirl_server/src/net/network_property.rs @@ -5,9 +5,9 @@ pub struct NetworkProperty { pub prop_id: i32, pub value: String, } -// impl NetworkProperty { -// pub fn new() -> Self { NetworkProperty::default() } -// } +impl NetworkProperty { + pub fn _new() -> Self { Self::default() } +} impl Default for NetworkProperty { fn default() -> Self { Self { diff --git a/crates/whirl_server/src/net/property_list.rs b/crates/whirl_server/src/net/property_list.rs new file mode 100644 index 0000000..4bba6c7 --- /dev/null +++ b/crates/whirl_server/src/net/property_list.rs @@ -0,0 +1,94 @@ +// Copyleft (ɔ) 2021-2021 The Whirlsplash Collective +// SPDX-License-Identifier: GPL-3.0-only + +use bytes::{BufMut, BytesMut}; +use num_traits::AsPrimitive; + +use crate::{ + cmd::constants::Command, + net::{ + constants::{PROPACCESS_POSSESS, PROPFLAG_DBSTORE}, + network_property::NetworkProperty, + }, +}; + +pub struct PropertyList(pub Vec<crate::net::network_property::NetworkProperty>); +impl PropertyList { + pub fn as_bytes(&mut self, command_id: i32, obj_id: i32) -> Vec<u8> { + let mut command = BytesMut::new(); + let property_list = &mut self.0; + + // Iterate over all network properties + loop { + // Check if there are any properties left + trace!("props left: {}", property_list.len()); + if property_list.is_empty() { + break; + } + + let property = &property_list[0]; // Property we are currently iterating over + trace!("current prop: {}:{}", property.prop_id, property.value); + + command.put_u8(property.prop_id.as_(): u8); // Property ID + + // NOTE: THIS IS SUPER BAD DO NOT DO THIS! But it works! + if command_id == Command::PropUpd as i32 { + command.put_u8(PROPFLAG_DBSTORE.as_(): u8); // Flag (s) + command.put_u8(PROPACCESS_POSSESS.as_(): u8); // Access + } + + command.put_u8(property.value.len().as_(): u8); // Property UTF-8 Length + command.put_slice(property.value.as_bytes()); // Property UTF-8 + + property_list.reverse(); + property_list.pop(); + property_list.reverse(); + } + + // Convert to vector and insert the header + let mut command_as_vec = command.to_vec(); + + command_as_vec.insert(0, command_id.as_(): u8); // Command ID + command_as_vec.insert(0, obj_id.as_(): u8); // ObjId + command_as_vec.insert(0, command.len().as_(): u8 + 3); // Data length + + // Return bytes + command_as_vec + } + + pub fn find(&self, property: i32) -> &NetworkProperty { + self.0.iter().find(|i| i.prop_id == property).unwrap() + } + + /// Iterate over a network property in the form of bytes (Vec<u8>) and return + /// a list of human-readable properties. + pub fn from_bytes(mut data: Vec<u8>) -> Self { + let mut property_list = vec![]; + + // Iterate over all network properties + loop { + // Check if any commands are present + if data.len() <= 2 { + break; + } + trace!("iteration: {:?}", data); + // if data[0] == 0 { + // break; + // } + + let property_length = data[1] + 2; + property_list.push(crate::net::network_property::NetworkProperty { + prop_id: i32::from(data[0]), + value: std::str::from_utf8(&data[2..data[1] as usize + 2]) + .unwrap() + .to_string(), + }); + + // Remove current property from the network property + data = data[property_length as usize..].to_vec(); + } + + // Return the human-readable network property + Self(property_list) + } +} diff --git a/crates/whirl_server/src/net/property_parser.rs b/crates/whirl_server/src/net/property_parser.rs deleted file mode 100644 index 9e04d1c..0000000 --- a/crates/whirl_server/src/net/property_parser.rs +++ /dev/null @@ -1,34 +0,0 @@ -// Copyleft (ɔ) 2021-2021 The Whirlsplash Collective -// SPDX-License-Identifier: GPL-3.0-only - -/// Iterate over a network property in the form of bytes (Vec<u8>) and return a -/// list of human-readable properties. -pub fn parse_network_property(mut data: Vec<u8>) -> crate::net::PropertyList { - let mut property_list = vec![]; - - // Iterate over all network properties - loop { - // Check if any commands are present - if data.len() <= 2 { - break; - } - trace!("iteration: {:?}", data); - // if data[0] == 0 { - // break; - // } - - let property_length = data[1] + 2; - property_list.push(crate::net::structure::NetworkProperty { - prop_id: i32::from(data[0]), - value: std::str::from_utf8(&data[2..data[1] as usize + 2]) - .unwrap() - .to_string(), - }); - - // Remove current property from the network property - data = data[property_length as usize..].to_vec(); - } - - // Return the human-readable network property - property_list -} |