[Server] Use "else if" instead "if" + "return"

sol2-server-rewrite
Koncord 7 years ago
parent 23da0b16ea
commit 7deff7a42a

@ -251,25 +251,23 @@ bool Networking::update(RakNet::Packet *packet)
{ {
playerPacketController->SetStream(&bsIn, nullptr); playerPacketController->SetStream(&bsIn, nullptr);
processPlayerPacket(packet); processPlayerPacket(packet);
return true;
} }
else if (actorPacketController->ContainsPacket(packet->data[0]))
if (actorPacketController->ContainsPacket(packet->data[0]))
{ {
actorPacketController->SetStream(&bsIn, nullptr); actorPacketController->SetStream(&bsIn, nullptr);
processActorPacket(packet); processActorPacket(packet);
return true;
} }
else if (worldPacketController->ContainsPacket(packet->data[0]))
if (worldPacketController->ContainsPacket(packet->data[0]))
{ {
worldPacketController->SetStream(&bsIn, nullptr); worldPacketController->SetStream(&bsIn, nullptr);
processWorldPacket(packet); processWorldPacket(packet);
return true;
} }
else
LOG_MESSAGE_SIMPLE(Log::LOG_WARN, "Unhandled RakNet packet with identifier %i has arrived", (int) packet->data[0]); {
return false; LOG_MESSAGE_SIMPLE(Log::LOG_WARN, "Unhandled RakNet packet with identifier %i has arrived", (int) packet->data[0]);
return false;
}
return true;
} }
void Networking::newPlayer(RakNet::RakNetGUID guid) void Networking::newPlayer(RakNet::RakNetGUID guid)
@ -425,12 +423,43 @@ void Networking::stopServer(int code)
exitCode = code; exitCode = code;
} }
using hrclock = chrono::high_resolution_clock;
template<typename _Rep, typename _Period>
hrclock::time_point limitTPS(const chrono::duration<_Rep, _Period> &limit)
{
static hrclock::time_point now = hrclock::now();
static auto last = now;
now = hrclock::now();
hrclock::duration delta = now - last;
auto tmp = last;
last = now;
if (delta < limit)
this_thread::sleep_for(limit - delta);
return tmp;
}
int Networking::mainLoop() int Networking::mainLoop()
{ {
RakNet::Packet *packet; RakNet::Packet *packet;
auto &timerCtrl = luaState.getTimerCtrl(); auto &timerCtrl = luaState.getTimerCtrl();
auto OneSecTimer = []() {
static hrclock::time_point time;
time = hrclock::now();
static auto last2 = time;
hrclock::duration delta = time - last2;
if (delta >= 1s)
{
last2 = time;
return true;
}
return false;
};
while (running) while (running)
{ {
bool updated = false; bool updated = false;
@ -483,6 +512,13 @@ int Networking::mainLoop()
timerCtrl.tick(); timerCtrl.tick();
if (updated) if (updated)
Players::processUpdated(); Players::processUpdated();
const int limit = 60;
auto t = limitTPS(1.0s / limit); // 60 ticks per 1 second
if (OneSecTimer())
printf("TPS %.1Lf / %d \n", 1.0s / (hrclock::now() - t), limit);
} }
timerCtrl.terminate(); timerCtrl.terminate();

Loading…
Cancel
Save