handle connection termination

This commit is contained in:
Nikita Vilunov 2023-02-15 17:47:48 +01:00
parent 23898038e1
commit a03a3a11a3
2 changed files with 19 additions and 1 deletions

View File

@ -76,6 +76,10 @@ impl PlayerConnection {
Ok(deferred.await?) 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) { pub async fn send(&self, command: PlayerCommand) {
self.player_handle.send(command).await; self.player_handle.send(command).await;
} }
@ -146,6 +150,7 @@ pub enum PlayerCommand {
sender: Sender<Updates>, sender: Sender<Updates>,
promise: Promise<ConnectionId>, promise: Promise<ConnectionId>,
}, },
TerminateConnection(ConnectionId),
Cmd(Cmd, ConnectionId), Cmd(Cmd, ConnectionId),
/// Query - responds with a list of rooms the player is a member of. /// Query - responds with a list of rooms the player is a member of.
GetRooms(Promise<Vec<RoomInfo>>), GetRooms(Promise<Vec<RoomInfo>>),
@ -263,7 +268,13 @@ impl Player {
match cmd { match cmd {
PlayerCommand::AddConnection { sender, promise } => { PlayerCommand::AddConnection { sender, promise } => {
let connection_id = self.connections.insert(sender); 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) => { PlayerCommand::GetRooms(promise) => {
let mut response = vec![]; let mut response = vec![];
@ -287,6 +298,12 @@ impl Player {
self 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) { async fn handle_cmd(&mut self, cmd: Cmd, connection_id: ConnectionId) {
match cmd { match cmd {
Cmd::JoinRoom { room_id, promise } => { Cmd::JoinRoom { room_id, promise } => {

View File

@ -271,6 +271,7 @@ async fn handle_registered_socket<'a>(
} }
} }
} }
connection.terminate().await;
Ok(()) Ok(())
} }