diff --git a/lib/src/models.rs b/lib/src/models.rs index 10eb4eb..b4ef8c9 100644 --- a/lib/src/models.rs +++ b/lib/src/models.rs @@ -1,4 +1,5 @@ use serde::{Deserialize, Serialize}; +use std::sync::{Arc, Mutex}; /// Games update #[derive(Serialize, Deserialize, Debug)] @@ -58,13 +59,12 @@ pub struct NewGameRequest { } /// New game request structure -use std::pin::Pin; #[derive(Debug)] -pub struct NewGameManifest<'user> { +pub struct NewGameManifest { /// Game name pub name: String, /// Game host - pub host: &'user Pin>, + pub host: Arc>, } /// Game join request structure diff --git a/server/src/api.rs b/server/src/api.rs index 8539882..2aa681f 100644 --- a/server/src/api.rs +++ b/server/src/api.rs @@ -12,6 +12,7 @@ use futures::{SinkExt, StreamExt}; use lib::models::*; use rand::seq::SliceRandom; use serde_json::to_string; +use std::sync::Mutex; use std::{net::SocketAddr, sync::Arc}; pub mod message_handler; use crate::message_handler::*; @@ -72,7 +73,7 @@ async fn handle_new_user( addr: &SocketAddr, ) -> Result<()> { // Create - let new_user = Box::pin(generate_new_user(&state)); + let new_user = Arc::new(Mutex::new(generate_new_user(&state))); tracing::debug!("User created at ptr: {:p}", new_user); tracing::debug!("User borrowed ptr: {:p}", *&new_user); @@ -122,9 +123,9 @@ fn generate_new_user(state: &Arc) -> User { } /// Generate message to notify client of user changes -fn client_self_user_update(new_user: &User) -> String { +fn client_self_user_update(new_user: &Arc>) -> String { to_string::(&UserUpdate { - username: new_user.name.clone(), + username: new_user.lock().unwrap().name.clone(), }) .unwrap() } @@ -135,7 +136,7 @@ fn chat_meta_update(state: &Arc) -> String { let mut names = vec![]; for user in state.online_users.lock().unwrap().iter() { - names.push(user.1.name.clone()); + names.push(user.1.lock().unwrap().name.clone()); } to_string::(&ChatUpdate { @@ -178,7 +179,7 @@ fn games_update(state: &Arc) -> String { fn announce_join(state: &Arc, addr: &SocketAddr) -> String { let msg = format!( "{} joined.", - state.online_users.lock().unwrap().get(addr).unwrap().name + state.online_users.lock().unwrap().get(addr).unwrap().lock().unwrap().name ); tracing::debug!("{}", &msg); diff --git a/server/src/api/message_handler.rs b/server/src/api/message_handler.rs index 05b1dab..aa787c0 100644 --- a/server/src/api/message_handler.rs +++ b/server/src/api/message_handler.rs @@ -57,10 +57,10 @@ fn handle_new_game( let binding = state.online_users.lock().unwrap(); let user = binding.get(&addr).unwrap(); - tracing::debug!("{:#?} from {}", new_game, user.name); + tracing::debug!("{:#?} from {}", new_game, user.lock().unwrap().name); let manifest = NewGameManifest { name: new_game.name, - host: user, + host: user.clone(), }; // create game @@ -90,7 +90,7 @@ fn handle_chat_message( tx: &Sender, addr: SocketAddr, ) -> Result<()> { - let msg = format! {"{0}: {1}", state.online_users.lock().unwrap().get(&addr).unwrap().name, chat_message.text}; + let msg = format! {"{0}: {1}", state.online_users.lock().unwrap().get(&addr).unwrap().lock().unwrap().name, chat_message.text}; tracing::debug!("{msg}"); tx.send(to_string::(&ChatMessage { text: msg })?)?; @@ -110,6 +110,8 @@ fn handle_user_log_in( .unwrap() .get(&addr) .unwrap() + .lock() + .unwrap() .name .clone(); let new_name = user_log_in.username.clone(); @@ -144,6 +146,8 @@ fn handle_user_log_in( .unwrap() .get_mut(&addr) .unwrap() + .lock() + .unwrap() .change_name(user_log_in.username); let msg = format! { @@ -183,7 +187,15 @@ fn handle_close( if let Some(cf) = close_frame { tracing::debug!( "Close received from {0} with code: {1} and reason: {2}", - state.online_users.lock().unwrap().get(&addr).unwrap().name, + state + .online_users + .lock() + .unwrap() + .get(&addr) + .unwrap() + .lock() + .unwrap() + .name, cf.code, cf.reason ) @@ -194,7 +206,15 @@ fn handle_close( let msg = ChatMessage { text: format!( "{0} left.", - state.online_users.lock().unwrap().get(&addr).unwrap().name + state + .online_users + .lock() + .unwrap() + .get(&addr) + .unwrap() + .lock() + .unwrap() + .name ), }; @@ -206,6 +226,8 @@ fn handle_close( .unwrap() .get(&addr) .unwrap() + .lock() + .unwrap() .name .clone(); diff --git a/server/src/main.rs b/server/src/main.rs index 511a91d..1be48b4 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -8,7 +8,6 @@ use std::{ fs::{read_to_string, File}, io::{BufRead, BufReader}, net::SocketAddr, - pin::Pin, sync::{Arc, Mutex}, }; use tokio::sync::broadcast; @@ -44,8 +43,8 @@ fn load_names(path: &str) -> Result> { // Our shared state pub struct AppState { // We require unique usernames. This tracks which usernames have been taken. - online_users: Mutex>>>, - offline_users: Mutex>>>, + online_users: Mutex>>>, + offline_users: Mutex>>>, // Channel used to send messages to all connected clients. tx: broadcast::Sender, // Master card decks @@ -71,8 +70,8 @@ async fn main() -> Result<()> { // Set up application state for use with with_state(). // Main Broadcast Channel let (tx, _rx) = broadcast::channel(100); - let online_users = Mutex::new(HashMap::>>::new()); - let offline_users = Mutex::new(HashMap::>>::new()); + let online_users = Mutex::new(HashMap::>>::new()); + let offline_users = Mutex::new(HashMap::>>::new()); let all_cards = load_cards_from_json("data/cah-cards-full.json")?; let games = Mutex::new(vec![]); let first_names = load_names("data/first.txt")?;