[Browser] Optimize PingRakNetServer() function

0.6.1
Koncord 8 years ago
parent 93bd3207db
commit 42eaa7a8fd

@ -7,7 +7,6 @@
#include <RakSleep.h> #include <RakSleep.h>
#include <GetTime.h> #include <GetTime.h>
#include <iostream>
#include <sstream> #include <sstream>
#include <components/openmw-mp/Version.hpp> #include <components/openmw-mp/Version.hpp>
@ -19,32 +18,42 @@ unsigned int PingRakNetServer(const char *addr, unsigned short port)
{ {
RakNet::Packet *packet; RakNet::Packet *packet;
bool done = false; bool done = false;
int attempts = 0; int attempt = 0;
static const int timeout = 5;
RakNet::TimeMS time = PING_UNREACHABLE; RakNet::TimeMS time = PING_UNREACHABLE;
RakNet::SocketDescriptor socketDescriptor {0, ""}; RakNet::SocketDescriptor socketDescriptor{0, ""};
RakNet::RakPeerInterface *peer = RakNet::RakPeerInterface::GetInstance(); RakNet::RakPeerInterface *peer = RakNet::RakPeerInterface::GetInstance();
peer->Startup(1,&socketDescriptor, 1); peer->Startup(1, &socketDescriptor, 1);
peer->Ping(addr, port, false); peer->Ping(addr, port, false);
while (!done) 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;
RakNet::BitStream bsIn(&packet->data[1], packet->length, false); attempt++;
bsIn.Read(time); RakSleep(timeout);
time = RakNet::GetTimeMS() - time - 5; continue;
done = true;
break;
}
} }
if (attempts >= 60) // wait 300 msec switch (packet->data[0])
done = true; {
attempts++; case ID_DISCONNECTION_NOTIFICATION:
RakSleep(5); 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 - timeout;
done = true;
break;
}
peer->DeallocatePacket(packet);
} }
peer->Shutdown(0); peer->Shutdown(0);
@ -110,7 +119,7 @@ ServerExtendedData getExtendedData(const char *addr, unsigned short port)
} }
puts(msg.c_str()); puts(msg.c_str());
if(queue == -1) // connection is failed if (queue == -1) // connection is failed
return data; return data;
{ {
@ -125,20 +134,20 @@ ServerExtendedData getExtendedData(const char *addr, unsigned short port)
{ {
for (packet = peer->Receive(); packet; peer->DeallocatePacket(packet), packet = peer->Receive()) for (packet = peer->Receive(); packet; peer->DeallocatePacket(packet), packet = peer->Receive())
{ {
if(packet->data[0] == (ID_USER_PACKET_ENUM+1)) if (packet->data[0] == (ID_USER_PACKET_ENUM + 1))
{ {
RakNet::BitStream bs(packet->data, packet->length, false); RakNet::BitStream bs(packet->data, packet->length, false);
bs.IgnoreBytes(1); bs.IgnoreBytes(1);
size_t length = 0; size_t length = 0;
bs.Read(length); bs.Read(length);
for(size_t i = 0; i < length; i++) for (size_t i = 0; i < length; i++)
{ {
RakNet::RakString str; RakNet::RakString str;
bs.Read(str); bs.Read(str);
data.players.push_back(str.C_String()); data.players.push_back(str.C_String());
} }
bs.Read(length); bs.Read(length);
for(size_t i = 0; i < length; i++) for (size_t i = 0; i < length; i++)
{ {
RakNet::RakString str; RakNet::RakString str;
bs.Read(str); bs.Read(str);

Loading…
Cancel
Save