forked from lavina/lavina
1
0
Fork 0

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?)
}
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<Updates>,
promise: Promise<ConnectionId>,
},
TerminateConnection(ConnectionId),
Cmd(Cmd, ConnectionId),
/// Query - responds with a list of rooms the player is a member of.
GetRooms(Promise<Vec<RoomInfo>>),
@ -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 } => {

View File

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