From a03a3a11a3ed572c658182d3ac65b52fa1229643 Mon Sep 17 00:00:00 2001 From: Nikita Vilunov Date: Wed, 15 Feb 2023 17:47:48 +0100 Subject: [PATCH] handle connection termination --- src/core/player.rs | 19 ++++++++++++++++++- src/projections/irc/mod.rs | 1 + 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/core/player.rs b/src/core/player.rs index 7d681af..fefd159 100644 --- a/src/core/player.rs +++ b/src/core/player.rs @@ -76,6 +76,10 @@ impl PlayerConnection { Ok(deferred.await?) } + pub async fn terminate(self) { + self.player_handle.send(PlayerCommand::TerminateConnection(self.connection_id)).await; + } + pub async fn send(&self, command: PlayerCommand) { self.player_handle.send(command).await; } @@ -146,6 +150,7 @@ pub enum PlayerCommand { sender: Sender, promise: Promise, }, + TerminateConnection(ConnectionId), Cmd(Cmd, ConnectionId), /// Query - responds with a list of rooms the player is a member of. GetRooms(Promise>), @@ -263,7 +268,13 @@ impl Player { match cmd { PlayerCommand::AddConnection { sender, promise } => { let connection_id = self.connections.insert(sender); - promise.send(ConnectionId(connection_id)); + if let Err(connection_id) = promise.send(ConnectionId(connection_id)) { + log::warn!("Connection {connection_id:?} terminated before finalization"); + self.terminate_connection(connection_id); + } + } + PlayerCommand::TerminateConnection(connection_id) => { + self.terminate_connection(connection_id); } PlayerCommand::GetRooms(promise) => { let mut response = vec![]; @@ -287,6 +298,12 @@ impl Player { self } + fn terminate_connection(&mut self, connection_id: ConnectionId) { + if let None = self.connections.pop(connection_id.0) { + log::warn!("Connection {connection_id:?} already terminated"); + } + } + async fn handle_cmd(&mut self, cmd: Cmd, connection_id: ConnectionId) { match cmd { Cmd::JoinRoom { room_id, promise } => { diff --git a/src/projections/irc/mod.rs b/src/projections/irc/mod.rs index 0417ee5..e343579 100644 --- a/src/projections/irc/mod.rs +++ b/src/projections/irc/mod.rs @@ -271,6 +271,7 @@ async fn handle_registered_socket<'a>( } } } + connection.terminate().await; Ok(()) }