From 91a1321f0ae1a02df9c3d1a8f890d84a94953a61 Mon Sep 17 00:00:00 2001 From: natrixaeria Date: Sun, 19 May 2019 17:09:56 +0200 Subject: Validate tokens and then push client --- game_server/src/gameserver.rs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) (limited to 'game_server/src/gameserver.rs') diff --git a/game_server/src/gameserver.rs b/game_server/src/gameserver.rs index 6f6dead..a4b1ed5 100644 --- a/game_server/src/gameserver.rs +++ b/game_server/src/gameserver.rs @@ -13,7 +13,7 @@ use super::backend_connection::BackendConnection; const PROTOCOL: &str = "tuesday"; -type Token = u32; +pub type Token = u32; #[derive(Debug)] pub enum GameServerError { @@ -26,7 +26,7 @@ pub enum GameServerError { pub struct GameServer { addr: SocketAddr, lobby: Lobby, - backend: BackendConnection, + backend: Arc>, clients: Arc>>, } @@ -70,7 +70,7 @@ impl GameServer { GameServer { addr, lobby, - backend, + backend: Arc::new(Mutex::new(backend)), clients: Arc::new(Mutex::new(HashMap::new())), } } @@ -86,13 +86,20 @@ impl GameServer { fn add_client(&self, mut client: GameClient) { let clients = Arc::clone(&self.clients); + let backend = Arc::clone(&self.backend); std::thread::spawn(move || { let token = client.require_token(); if let Some(token) = token { println!("Token: {}", token); - clients.lock().unwrap().insert(token, client); + let locked_backend = backend.lock().unwrap(); + let result = locked_backend.validate_token(&token); + if let Err(err) = result { + warn!("token {} is invalid: '{:?}'", token, err); + } else { + clients.lock().unwrap().insert(token, client); + } } else { - warn!("Client sent invalid token"); + warn!("client sent invalid token"); } }); } -- cgit v1.2.3-54-g00ecf