handle disconnects more gracefully

This commit is contained in:
Arisotura
2023-10-07 15:25:11 +02:00
parent a5fb871e64
commit 30186029e8
2 changed files with 34 additions and 8 deletions

View File

@ -357,9 +357,10 @@ void LANDialog::doUpdatePlayerList()
QString status; QString status;
switch (player->Status) switch (player->Status)
{ {
case 1: status = "Ready"; break; case 1: status = "Connected"; break;
case 2: status = "Host"; break; case 2: status = "Game host"; break;
case 3: status = "Connecting"; break; case 3: status = "Connecting"; break;
case 4: status = "Connection lost"; break;
} }
model->item(i, 2)->setText(status); model->item(i, 2)->setText(status);
@ -369,9 +370,16 @@ void LANDialog::doUpdatePlayerList()
model->item(i, 4)->setText("(local)"); model->item(i, 4)->setText("(local)");
} }
else else
{
if (player->Status == 1 || player->Status == 2)
{ {
QString ping = QString("%0 ms").arg(playerPing[i]); QString ping = QString("%0 ms").arg(playerPing[i]);
model->item(i, 3)->setText(ping); model->item(i, 3)->setText(ping);
}
else
{
model->item(i, 3)->setText("-");
}
// note on the player IP display // 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 // * 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); 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); enet_host_destroy(Host);
Host = nullptr; 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); ENetPacket* pkt = enet_packet_create(cmd, 9+sizeof(Player), ENET_PACKET_FLAG_RELIABLE);
enet_peer_send(event.peer, 0, pkt); enet_peer_send(event.peer, 0, pkt);
RemotePeers[0] = event.peer;
event.peer->data = &Players[0];
conn = 2; conn = 2;
break; break;
} }
@ -687,6 +702,7 @@ bool StartClient(const char* playername, const char* host)
LastHostID = -1; LastHostID = -1;
LastHostPeer = nullptr; LastHostPeer = nullptr;
RemotePeers[0] = peer; RemotePeers[0] = peer;
peer->data = &Players[0];
Active = true; Active = true;
IsHost = false; IsHost = false;
@ -805,6 +821,12 @@ void HostUpdatePlayerList()
lanDlg->updatePlayerList(); lanDlg->updatePlayerList();
} }
void ClientUpdatePlayerList()
{
if (lanDlg)
lanDlg->updatePlayerList();
}
void ProcessHostEvent(ENetEvent& event) void ProcessHostEvent(ENetEvent& event)
{ {
switch (event.type) switch (event.type)
@ -989,6 +1011,10 @@ void ProcessClientEvent(ENetEvent& event)
int id = player->ID; int id = player->ID;
RemotePeers[id] = nullptr; RemotePeers[id] = nullptr;
player->Status = 4;
ClientUpdatePlayerList();
} }
break; break;

View File

@ -41,7 +41,7 @@ struct Player
{ {
int ID; int ID;
char Name[32]; 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; u32 Address;
}; };