1
0
Fork 1
mirror of https://github.com/TES3MP/openmw-tes3mp.git synced 2025-01-16 21:19:55 +00:00

[Browser] Optimize PingRakNetServer() function

This commit is contained in:
Koncord 2017-05-07 00:35:47 +08:00
parent 93bd3207db
commit 42eaa7a8fd

View file

@ -7,7 +7,6 @@
#include <RakSleep.h>
#include <GetTime.h>
#include <iostream>
#include <sstream>
#include <components/openmw-mp/Version.hpp>
@ -19,7 +18,8 @@ unsigned int PingRakNetServer(const char *addr, unsigned short port)
{
RakNet::Packet *packet;
bool done = false;
int attempts = 0;
int attempt = 0;
static const int timeout = 5;
RakNet::TimeMS time = PING_UNREACHABLE;
RakNet::SocketDescriptor socketDescriptor{0, ""};
@ -29,22 +29,31 @@ unsigned int PingRakNetServer(const char *addr, unsigned short port)
peer->Ping(addr, port, false);
while (!done)
{
for (packet=peer->Receive(); packet; peer->DeallocatePacket(packet), packet=peer->Receive())
packet = peer->Receive();
if (!packet)
{
if(packet->data[0] == ID_UNCONNECTED_PONG)
if (attempt > 5)
done = true;
attempt++;
RakSleep(timeout);
continue;
}
switch (packet->data[0])
{
case ID_DISCONNECTION_NOTIFICATION:
case ID_CONNECTION_LOST:
done = true;
break;
case ID_CONNECTED_PING:
case ID_UNCONNECTED_PONG:
RakNet::BitStream bsIn(&packet->data[1], packet->length, false);
bsIn.Read(time);
time = RakNet::GetTimeMS() - time - 5;
time = RakNet::GetTimeMS() - time - timeout;
done = true;
break;
}
}
if (attempts >= 60) // wait 300 msec
done = true;
attempts++;
RakSleep(5);
peer->DeallocatePacket(packet);
}
peer->Shutdown(0);