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;
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;

View File

@ -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;
};