aboutsummaryrefslogtreecommitdiff
path: root/src/server
diff options
context:
space:
mode:
authorFuwn <[email protected]>2021-04-30 17:59:18 +0000
committerFuwn <[email protected]>2021-04-30 17:59:18 +0000
commit5ec0a47413d8623660156dc4049f98dffee7b35b (patch)
treeb1f5d5b860ffd48ad9c36fdc8b378bb1326d855e /src/server
parentfeat(hub): *implement* new commands; subscribe room, subscribe distance, tele... (diff)
downloadwhirl-5ec0a47413d8623660156dc4049f98dffee7b35b.tar.xz
whirl-5ec0a47413d8623660156dc4049f98dffee7b35b.zip
refactor(cmds): more orphan module functions to methods within struct
The rest of the current commands will also receive this refactor... when I have time.
Diffstat (limited to 'src/server')
-rw-r--r--src/server/cmd/commands/text.rs46
-rw-r--r--src/server/cmd/commands/text/create.rs31
-rw-r--r--src/server/cmd/commands/text/mod.rs6
-rw-r--r--src/server/cmd/commands/text/parse.rs13
-rw-r--r--src/server/cmd/commands/text/structure.rs7
-rw-r--r--src/server/distributor.rs6
-rw-r--r--src/server/hub.rs12
7 files changed, 55 insertions, 66 deletions
diff --git a/src/server/cmd/commands/text.rs b/src/server/cmd/commands/text.rs
new file mode 100644
index 0000000..3029eac
--- /dev/null
+++ b/src/server/cmd/commands/text.rs
@@ -0,0 +1,46 @@
+// Copyleft (ɔ) 2021-2021 The Whirlsplash Collective
+// SPDX-License-Identifier: GPL-3.0-only
+
+use std::str::from_utf8;
+
+use bytes::{BufMut, BytesMut};
+
+use crate::server::cmd::constants::TEXT;
+
+pub struct Text {
+ pub sender: String,
+ pub content: String,
+}
+impl Text {
+ pub fn parse(data: Vec<u8>, username: &str) -> Self {
+ Self {
+ sender: username.to_string(),
+ content: from_utf8(&data[6..]).unwrap().to_string(),
+ }
+ }
+
+ pub fn create(self) -> Vec<u8> {
+ let mut command = BytesMut::new();
+
+ // Header
+ command.put_u8(0x01);
+ command.put_i8(TEXT as i8);
+
+ // Content
+ // TODO: Find a way to parse ObjIds.
+ // The below byte is suspected to be the sender's short ObjId.
+ command.put_i8(0x00);
+
+ command.put_u8(self.sender.len() as u8);
+ command.put_slice(self.sender.as_bytes());
+ command.put_u8(self.content.len() as u8);
+ command.put_slice(self.content.as_bytes());
+
+ // Convert to vector and insert the length
+ let mut command_as_vec = command.to_vec();
+ command_as_vec.insert(0, command.len() as u8 + 1);
+
+ // Return bytes
+ command_as_vec
+ }
+}
diff --git a/src/server/cmd/commands/text/create.rs b/src/server/cmd/commands/text/create.rs
deleted file mode 100644
index 8fa8cbb..0000000
--- a/src/server/cmd/commands/text/create.rs
+++ /dev/null
@@ -1,31 +0,0 @@
-// Copyleft (ɔ) 2021-2021 The Whirlsplash Collective
-// SPDX-License-Identifier: GPL-3.0-only
-
-use bytes::{BufMut, BytesMut};
-
-use crate::server::cmd::{commands::text::structure::Text, constants::TEXT};
-
-pub fn create_text(text: Text) -> Vec<u8> {
- let mut command = BytesMut::new();
-
- // Header
- command.put_u8(0x01);
- command.put_u8(TEXT as u8);
-
- // Content
- // The fourth and fifth elements are presumed to be interpreted as a short by
- // the client, however, usernames aren't (?) allowed to be long enough that
- // they reach a number high enough to be converted to a short.
- command.put_u8(0x00);
- command.put_u8(text.sender.len() as u8);
- command.put_slice(text.sender.as_bytes());
- command.put_u8(text.content.len() as u8);
- command.put_slice(text.content.as_bytes());
-
- // Convert to vector and insert the length
- let mut command_as_vec = command.to_vec();
- command_as_vec.insert(0, command.len() as u8 + 1);
-
- // Return bytes
- command_as_vec
-}
diff --git a/src/server/cmd/commands/text/mod.rs b/src/server/cmd/commands/text/mod.rs
deleted file mode 100644
index b2820e2..0000000
--- a/src/server/cmd/commands/text/mod.rs
+++ /dev/null
@@ -1,6 +0,0 @@
-// Copyleft (ɔ) 2021-2021 The Whirlsplash Collective
-// SPDX-License-Identifier: GPL-3.0-only
-
-pub mod create;
-pub mod parse;
-pub mod structure;
diff --git a/src/server/cmd/commands/text/parse.rs b/src/server/cmd/commands/text/parse.rs
deleted file mode 100644
index 6505ace..0000000
--- a/src/server/cmd/commands/text/parse.rs
+++ /dev/null
@@ -1,13 +0,0 @@
-// Copyleft (ɔ) 2021-2021 The Whirlsplash Collective
-// SPDX-License-Identifier: GPL-3.0-only
-
-use std::str::from_utf8;
-
-use crate::server::cmd::commands::text::structure::Text;
-
-pub fn parse_text(data: Vec<u8>, username: &str) -> Text {
- Text {
- sender: username.to_string(),
- content: from_utf8(&data[6..]).unwrap().to_string(),
- }
-}
diff --git a/src/server/cmd/commands/text/structure.rs b/src/server/cmd/commands/text/structure.rs
deleted file mode 100644
index 945174a..0000000
--- a/src/server/cmd/commands/text/structure.rs
+++ /dev/null
@@ -1,7 +0,0 @@
-// Copyleft (ɔ) 2021-2021 The Whirlsplash Collective
-// SPDX-License-Identifier: GPL-3.0-only
-
-pub struct Text {
- pub sender: String,
- pub content: String,
-}
diff --git a/src/server/distributor.rs b/src/server/distributor.rs
index 28a4250..0f7952f 100644
--- a/src/server/distributor.rs
+++ b/src/server/distributor.rs
@@ -29,7 +29,7 @@ use crate::{
parse::find_property_in_property_list,
},
room::{create::create_room_id_request, parse::parse_room_id_request},
- text::{create::create_text, structure::Text},
+ text::Text,
},
constants::*,
},
@@ -86,10 +86,10 @@ impl Server for Distributor {
trace!("received property set from {}", username);
peer.bytes.get_mut()
- .write_all(&create_text(Text {
+ .write_all(&Text {
sender: Config::get()?.whirlsplash.worldsmaster_username,
content: Config::get()?.distributor.worldsmaster_greeting,
- })).await?;
+ }.create()).await?;
peer.bytes.get_mut()
.write_all(&create_action()).await?;
trace!("sent text to {}", username);
diff --git a/src/server/hub.rs b/src/server/hub.rs
index 8c982ad..7d0bb0a 100644
--- a/src/server/hub.rs
+++ b/src/server/hub.rs
@@ -28,7 +28,7 @@ use crate::{
subscribe_distance::parse::parse_subscribe_distance,
subscribe_room::parse::parse_subscribe_room,
teleport::parse::parse_teleport,
- text::{create::create_text, parse::parse_text, structure::Text},
+ text::Text,
},
constants::*,
},
@@ -87,10 +87,10 @@ impl Server for Hub {
trace!("received property set from {}", username);
peer.bytes.get_mut()
- .write_all(&create_text(Text {
+ .write_all(&Text {
sender: Config::get()?.whirlsplash.worldsmaster_username,
content: Config::get()?.distributor.worldsmaster_greeting,
- })).await?;
+ }.create()).await?;
peer.bytes.get_mut()
.write_all(&create_action()).await?;
trace!("sent text to {}", username);
@@ -111,14 +111,14 @@ impl Server for Hub {
trace!("received session exit from {}", username); break;
}
TEXT => {
- let text = parse_text(msg.to_vec(), &username);
+ let text = Text::parse(msg.to_vec(), &username);
trace!("received text from {}:{}", username, text.content);
{
- state.lock().await.broadcast(&create_text(Text {
+ state.lock().await.broadcast(&Text {
sender: username.clone(),
content: text.content,
- })).await;
+ }.create()).await;
}
trace!("broadcasted text to hub");
}