diff options
| author | Fuwn <[email protected]> | 2021-05-01 12:03:40 +0000 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2021-05-01 12:03:40 +0000 |
| commit | b83db32c503c2c818ce513966351020ce7308ce3 (patch) | |
| tree | ed8194a42a0a86faa0bc7568f11fdeb0b1e1596a /src/server | |
| parent | chore(git): add prepare-commit-hook for commitizen (diff) | |
| download | whirl-b83db32c503c2c818ce513966351020ce7308ce3.tar.xz whirl-b83db32c503c2c818ce513966351020ce7308ce3.zip | |
refactor(cmds): even more of2m-ifying
Diffstat (limited to 'src/server')
| -rw-r--r-- | src/server/cmd/commands/mod.rs | 3 | ||||
| -rw-r--r-- | src/server/cmd/commands/property/create.rs | 2 | ||||
| -rw-r--r-- | src/server/cmd/commands/redirect_id.rs | 41 | ||||
| -rw-r--r-- | src/server/cmd/commands/room/create.rs | 34 | ||||
| -rw-r--r-- | src/server/cmd/commands/room/mod.rs | 5 | ||||
| -rw-r--r-- | src/server/cmd/commands/room/parse.rs | 8 | ||||
| -rw-r--r-- | src/server/cmd/commands/room_id_request.rs | 16 | ||||
| -rw-r--r-- | src/server/distributor.rs | 27 | ||||
| -rw-r--r-- | src/server/hub.rs | 12 |
9 files changed, 82 insertions, 66 deletions
diff --git a/src/server/cmd/commands/mod.rs b/src/server/cmd/commands/mod.rs index d06c061..0d5fc1c 100644 --- a/src/server/cmd/commands/mod.rs +++ b/src/server/cmd/commands/mod.rs @@ -4,7 +4,8 @@ pub mod action; pub mod buddy_list; pub mod property; -pub mod room; +pub mod redirect_id; +pub mod room_id_request; pub mod subscribe_distance; pub mod subscribe_room; pub mod teleport; diff --git a/src/server/cmd/commands/property/create.rs b/src/server/cmd/commands/property/create.rs index 6cfe540..ec4de1e 100644 --- a/src/server/cmd/commands/property/create.rs +++ b/src/server/cmd/commands/property/create.rs @@ -1,6 +1,8 @@ // Copyleft (ɔ) 2021-2021 The Whirlsplash Collective // SPDX-License-Identifier: GPL-3.0-only +// TODO: of2m-ify + use crate::{ config::Config, server::{ diff --git a/src/server/cmd/commands/redirect_id.rs b/src/server/cmd/commands/redirect_id.rs new file mode 100644 index 0000000..5153c38 --- /dev/null +++ b/src/server/cmd/commands/redirect_id.rs @@ -0,0 +1,41 @@ +// Copyleft (ɔ) 2021-2021 The Whirlsplash Collective +// SPDX-License-Identifier: GPL-3.0-only + +use bytes::{BufMut, BytesMut}; + +use crate::{config::Config, server::cmd::constants::REDIRID}; + +#[derive(Debug)] +pub struct RedirectId { + pub room_name: String, + pub room_number: i8, +} +impl RedirectId { + pub fn create(self) -> Vec<u8> { + let mut command = BytesMut::new(); + + // Header + command.put_u8(0x01); // ObjId + command.put_u8(REDIRID as u8); // Type + + // Content + command.put_u8(self.room_name.len() as u8); // Room name length + command.put_slice(self.room_name.as_bytes()); // Room name + // command.put_u8(0x00); // Unimplemented byte (?) + // command.put_u8(room_id); // Room ID + command.put_u16(self.room_number as u16); // Room ID + + // IP + for byte in "0.0.0.0".split('.') { + command.put_u8(byte.parse::<u8>().unwrap()); + } + command.put_u16(Config::get().unwrap().hub.port as u16); // Port + + // Length + let mut command_as_vec = command.to_vec(); + command_as_vec.insert(0, command.len() as u8 + 1); + + // Return + command_as_vec + } +} diff --git a/src/server/cmd/commands/room/create.rs b/src/server/cmd/commands/room/create.rs deleted file mode 100644 index 943b7c9..0000000 --- a/src/server/cmd/commands/room/create.rs +++ /dev/null @@ -1,34 +0,0 @@ -// Copyleft (ɔ) 2021-2021 The Whirlsplash Collective -// SPDX-License-Identifier: GPL-3.0-only - -use bytes::{BufMut, BytesMut}; - -use crate::{config::Config, server::cmd::constants::REDIRID}; - -pub fn create_room_id_request(room: &str, room_id: u8) -> Vec<u8> { - let mut command = BytesMut::new(); - - // Header - command.put_u8(0x01); // ObjId - command.put_u8(REDIRID as u8); // Type - - // Content - command.put_u8(room.len() as u8); // Room name length - command.put_slice(room.as_bytes()); // Room name - // command.put_u8(0x00); // Unimplemented byte (?) - // command.put_u8(room_id); // Room ID - command.put_u16(room_id as u16); // Room ID - - // IP - for byte in "0.0.0.0".split('.') { - command.put_u8(byte.parse::<u8>().unwrap()); - } - command.put_u16(Config::get().unwrap().hub.port as u16); // Port - - // Length - let mut command_as_vec = command.to_vec(); - command_as_vec.insert(0, command.len() as u8 + 1); - - // Return - command_as_vec -} diff --git a/src/server/cmd/commands/room/mod.rs b/src/server/cmd/commands/room/mod.rs deleted file mode 100644 index 83b015b..0000000 --- a/src/server/cmd/commands/room/mod.rs +++ /dev/null @@ -1,5 +0,0 @@ -// Copyleft (ɔ) 2021-2021 The Whirlsplash Collective -// SPDX-License-Identifier: GPL-3.0-only - -pub mod create; -pub mod parse; diff --git a/src/server/cmd/commands/room/parse.rs b/src/server/cmd/commands/room/parse.rs deleted file mode 100644 index 79ec8af..0000000 --- a/src/server/cmd/commands/room/parse.rs +++ /dev/null @@ -1,8 +0,0 @@ -// Copyleft (ɔ) 2021-2021 The Whirlsplash Collective -// SPDX-License-Identifier: GPL-3.0-only - -use std::str::from_utf8; - -pub fn parse_room_id_request(data: Vec<u8>) -> String { - from_utf8(&data[4..data[0] as usize]).unwrap().to_string() -} diff --git a/src/server/cmd/commands/room_id_request.rs b/src/server/cmd/commands/room_id_request.rs new file mode 100644 index 0000000..812374c --- /dev/null +++ b/src/server/cmd/commands/room_id_request.rs @@ -0,0 +1,16 @@ +// Copyleft (ɔ) 2021-2021 The Whirlsplash Collective +// SPDX-License-Identifier: GPL-3.0-only + +use std::str::from_utf8; + +#[derive(Debug)] +pub struct RoomIdRequest { + pub room_name: String, +} +impl RoomIdRequest { + pub fn parse(data: Vec<u8>) -> Self { + Self { + room_name: from_utf8(&data[4..data[0] as usize]).unwrap().to_string(), + } + } +} diff --git a/src/server/distributor.rs b/src/server/distributor.rs index 0b9c24d..8023514 100644 --- a/src/server/distributor.rs +++ b/src/server/distributor.rs @@ -28,7 +28,8 @@ use crate::{ create::{create_property_request_as_distributor, create_property_update_as_distributor}, parse::find_property_in_property_list, }, - room::{create::create_room_id_request, parse::parse_room_id_request}, + redirect_id::RedirectId, + room_id_request::RoomIdRequest, text::Text, }, constants::*, @@ -103,23 +104,25 @@ impl Server for Distributor { trace!("sent buddy list notify to {}: {}", username, buddy.buddy); } ROOMIDRQ => { - let room = parse_room_id_request(msg.to_vec()); - trace!("received room id request from {}: {}", username, &room); + let room = RoomIdRequest::parse(msg.to_vec()); + trace!("received room id request from {}: {}", username, &room.room_name); let room_id; - if !room_ids.contains(&room) { - room_ids.push(room.clone()); - room_id = room_ids.iter().position(|r| r == &room).unwrap(); - debug!("inserted room: {}", room); + if !room_ids.contains(&room.room_name) { + room_ids.push(room.room_name.clone()); + room_id = room_ids.iter().position(|r| r == &room.room_name).unwrap(); + debug!("inserted room: {}", room.room_name); } else { - let position = room_ids.iter().position(|r| r == &room).unwrap(); - debug!("found room: {}", room); + let position = room_ids.iter().position(|r| r == &room.room_name).unwrap(); + debug!("found room: {}", room.room_name); room_id = position; } - peer.bytes.get_mut() - .write_all(&create_room_id_request(&room, room_id as u8)).await?; - trace!("sent room id redirect to {}: {}", username, room); + peer.bytes.get_mut().write_all(&RedirectId { + room_name: room.room_name.clone(), + room_number: room_id as i8, + }.create()).await?; + trace!("sent redirect id to {}: {}", username, room.room_name); } SESSEXIT => { trace!("received session exit from {}", username); break; diff --git a/src/server/hub.rs b/src/server/hub.rs index 1676aab..58e5989 100644 --- a/src/server/hub.rs +++ b/src/server/hub.rs @@ -24,7 +24,6 @@ use crate::{ create::{create_property_request_as_hub, create_property_update_as_hub}, parse::find_property_in_property_list, }, - room::{create::create_room_id_request, parse::parse_room_id_request}, subscribe_distance::SubscribeDistance, subscribe_room::SubscribeRoom, teleport::Teleport, @@ -103,11 +102,12 @@ impl Server for Hub { }.create()).await?; trace!("sent buddy list notify to {}: {}", username, buddy.buddy); } - ROOMIDRQ => { - let room = parse_room_id_request(msg.to_vec()); - trace!("received room id request from {}: {}", username, room); - debug!("{:?}", create_room_id_request(&room, 0x04)); - } + // ROOMIDRQ => { + // // TODO: Figure out if this is actually even needed. + // let room = RoomIdRequest::parse(msg.to_vec()); + // trace!("received room id request from {}: {}", username, room.room_name); + // debug!("{:?}", create_room_id_request(&room.room_name, 0x00)); + // } SESSEXIT => { trace!("received session exit from {}", username); break; } |