Skip to content

Commit d65d5ac

Browse files
committed
slightly better handling i think?
1 parent 43a836b commit d65d5ac

File tree

2 files changed

+8
-13
lines changed

2 files changed

+8
-13
lines changed

server/src/network/client.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ impl ClientHandler {
3838
}
3939

4040
// this does not inform the network thread about the read tasks shutdown.
41-
pub async fn disconnect(&mut self) -> Result<(), io::Error> {
42-
self.writer.shutdown().await?;
41+
pub async fn disconnect(mut self) -> Result<(), io::Error> {
4342
self.handle.abort();
43+
self.writer.shutdown().await?;
4444
Ok(())
4545
}
4646

server/src/network/network.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,34 +61,29 @@ async fn run_network_thread(
6161
if let Some(handler) = clients.get_mut(&client_id) {
6262
if let Err(e) = handler.send(&buffer).await {
6363
eprintln!("Client {} handler failed to send: {}", client_id, e);
64-
disconnect_client(client_id, &main_tx, &mut clients);
64+
clients.remove(&client_id);
65+
main_tx.send(MainThreadMessage::ClientDisconnected { client_id }).expect("Main thread should never drop its network reciever.");
6566
}
6667
}
6768
}
6869

6970
NetworkThreadMessage::DisconnectClient { client_id } => {
70-
if let Some(handler) = clients.get_mut(&client_id) {
71+
if let Some(handler) = clients.remove(&client_id) {
7172
if let Err(e) = handler.disconnect().await {
7273
eprintln!("Client {} writer failed to shutdown: {}", client_id, e);
7374
}
74-
disconnect_client(client_id, &main_tx, &mut clients);
75+
main_tx.send(MainThreadMessage::ClientDisconnected { client_id }).expect("Main thread should never drop its network reciever.");
7576
}
7677
}
7778

7879
NetworkThreadMessage::ConnectionClosed { client_id, connection_state } => {
79-
if connection_state == ConnectionState::Play {
80-
// we probably shouldnt tell the main thread a client it never added got disconnected?
80+
// we probably shouldnt tell the main thread a client it never added got disconnected?
81+
if clients.remove(&client_id).is_some() && connection_state == ConnectionState::Play {
8182
main_tx.send(MainThreadMessage::ClientDisconnected { client_id }).expect("Main thread should never drop its network reciever.");
8283
}
83-
clients.remove(&client_id);
8484
}
8585
}
8686
}
8787
}
8888
}
89-
}
90-
91-
fn disconnect_client(client_id: ClientId, main_tx: &UnboundedSender<MainThreadMessage>, clients: &mut ClientMap) {
92-
main_tx.send(MainThreadMessage::ClientDisconnected { client_id }).expect("Main thread should never drop its network reciever.");
93-
clients.remove(&client_id);
9489
}

0 commit comments

Comments
 (0)