From 30186029e845448164e0777c0e65c614bd09ca51 Mon Sep 17 00:00:00 2001 From: Arisotura Date: Sat, 7 Oct 2023 15:25:11 +0200 Subject: [PATCH] handle disconnects more gracefully --- src/frontend/qt_sdl/LAN.cpp | 40 ++++++++++++++++++++++++++++++------- src/frontend/qt_sdl/LAN.h | 2 +- 2 files changed, 34 insertions(+), 8 deletions(-) diff --git a/src/frontend/qt_sdl/LAN.cpp b/src/frontend/qt_sdl/LAN.cpp index b8e36df1..58d6e972 100644 --- a/src/frontend/qt_sdl/LAN.cpp +++ b/src/frontend/qt_sdl/LAN.cpp @@ -357,9 +357,10 @@ void LANDialog::doUpdatePlayerList() QString status; switch (player->Status) { - case 1: status = "Ready"; break; - case 2: status = "Host"; break; + case 1: status = "Connected"; break; + case 2: status = "Game host"; break; case 3: status = "Connecting"; break; + case 4: status = "Connection lost"; break; } model->item(i, 2)->setText(status); @@ -370,8 +371,15 @@ void LANDialog::doUpdatePlayerList() } else { - QString ping = QString("%0 ms").arg(playerPing[i]); - model->item(i, 3)->setText(ping); + if (player->Status == 1 || player->Status == 2) + { + QString ping = QString("%0 ms").arg(playerPing[i]); + model->item(i, 3)->setText(ping); + } + else + { + model->item(i, 3)->setText("-"); + } // note on the player IP display // * we make an exception for the host -- the player list is issued by the host, so the host IP would be 127.0.0.1 @@ -493,6 +501,16 @@ void DeInit() enet_packet_destroy(packet); } + for (int i = 0; i < 16; i++) + { + if (i == MyPlayer.ID) continue; + + if (RemotePeers[i]) + enet_peer_disconnect(RemotePeers[i], 0); + + RemotePeers[i] = nullptr; + } + enet_host_destroy(Host); Host = nullptr; @@ -659,9 +677,6 @@ bool StartClient(const char* playername, const char* host) ENetPacket* pkt = enet_packet_create(cmd, 9+sizeof(Player), ENET_PACKET_FLAG_RELIABLE); enet_peer_send(event.peer, 0, pkt); - RemotePeers[0] = event.peer; - event.peer->data = &Players[0]; - conn = 2; break; } @@ -687,6 +702,7 @@ bool StartClient(const char* playername, const char* host) LastHostID = -1; LastHostPeer = nullptr; RemotePeers[0] = peer; + peer->data = &Players[0]; Active = true; IsHost = false; @@ -805,6 +821,12 @@ void HostUpdatePlayerList() lanDlg->updatePlayerList(); } +void ClientUpdatePlayerList() +{ + if (lanDlg) + lanDlg->updatePlayerList(); +} + void ProcessHostEvent(ENetEvent& event) { switch (event.type) @@ -989,6 +1011,10 @@ void ProcessClientEvent(ENetEvent& event) int id = player->ID; RemotePeers[id] = nullptr; + + player->Status = 4; + + ClientUpdatePlayerList(); } break; diff --git a/src/frontend/qt_sdl/LAN.h b/src/frontend/qt_sdl/LAN.h index 2ec21263..76cc95d1 100644 --- a/src/frontend/qt_sdl/LAN.h +++ b/src/frontend/qt_sdl/LAN.h @@ -41,7 +41,7 @@ struct Player { int ID; char Name[32]; - int Status; // 0=no player 1=normal 2=host 3=connecting + int Status; // 0=no player 1=normal 2=host 3=connecting 4=disconnected u32 Address; };