Moved heartbeat code to ServerApplication::CheckClientConnections()
This commit is contained in:
@@ -25,6 +25,8 @@
|
||||
//map system
|
||||
#include "region_pager_lua.hpp"
|
||||
|
||||
#include <string>
|
||||
|
||||
class RoomData {
|
||||
public:
|
||||
RoomData() = default;
|
||||
@@ -33,11 +35,15 @@ public:
|
||||
//accessors and mutators
|
||||
RegionPagerLua* GetPager() { return &pager; }
|
||||
|
||||
std::string SetTilesetName(std::string s) { return tilesetName = s; }
|
||||
std::string GetTilesetName() { return tilesetName; }
|
||||
|
||||
private:
|
||||
friend class RoomManager;
|
||||
|
||||
//members
|
||||
RegionPagerLua pager;
|
||||
std::string tilesetName;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -80,6 +80,7 @@ private:
|
||||
void HandleSynchronize(ClientPacket* const);
|
||||
|
||||
//utility methods
|
||||
void CheckClientConnections();
|
||||
//TODO: a function that only sends to characters in a certain proximity
|
||||
void CleanupLostConnection(int index);
|
||||
void PumpPacket(SerialPacket* const);
|
||||
|
||||
+1
-16
@@ -168,23 +168,8 @@ void ServerApplication::Proc() {
|
||||
//update the internals
|
||||
//...
|
||||
|
||||
//TODO: This could be checked only every few seconds
|
||||
//Check connections
|
||||
for (auto& it : clientMap) {
|
||||
if (std::chrono::steady_clock::now() - it.second.GetLastBeat() > std::chrono::seconds(3)) {
|
||||
ServerPacket newPacket;
|
||||
newPacket.type = SerialPacketType::PING;
|
||||
network.SendTo(it.second.GetAddress(), &newPacket);
|
||||
it.second.IncrementAttempts();
|
||||
}
|
||||
|
||||
if (it.second.GetAttempts() > 2) {
|
||||
CleanupLostConnection(it.first);
|
||||
|
||||
//all iterators are invalid, so we can't continue
|
||||
break;
|
||||
}
|
||||
}
|
||||
CheckClientConnections();
|
||||
|
||||
//give the computer a break
|
||||
SDL_Delay(10);
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
*/
|
||||
#include "server_application.hpp"
|
||||
|
||||
#include <chrono>
|
||||
#include <iostream>
|
||||
|
||||
//-------------------------
|
||||
@@ -159,6 +160,7 @@ void ServerApplication::HandleRegionRequest(RegionPacket* const argPacket) {
|
||||
newPacket.x = argPacket->x;
|
||||
newPacket.y = argPacket->y;
|
||||
|
||||
//BUG: possibly related to #35
|
||||
newPacket.region = roomMgr.GetRoom(argPacket->roomIndex)->GetPager()->GetRegion(argPacket->x, argPacket->y);
|
||||
|
||||
//send the content
|
||||
@@ -280,6 +282,23 @@ void ServerApplication::HandleSynchronize(ClientPacket* const argPacket) {
|
||||
//utility methods
|
||||
//-------------------------
|
||||
|
||||
void ServerApplication::CheckClientConnections() {
|
||||
for (auto& it : clientMap) {
|
||||
if (std::chrono::steady_clock::now() - it.second.GetLastBeat() > std::chrono::seconds(3)) {
|
||||
ServerPacket newPacket;
|
||||
newPacket.type = SerialPacketType::PING;
|
||||
network.SendTo(it.second.GetAddress(), &newPacket);
|
||||
it.second.IncrementAttempts();
|
||||
}
|
||||
|
||||
if (it.second.GetAttempts() > 2) {
|
||||
CleanupLostConnection(it.first);
|
||||
//all iterators are invalid, so we can't continue
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ServerApplication::CleanupLostConnection(int clientIndex) {
|
||||
//NOTE: This assumes each player has only one account and character at a time
|
||||
//TODO: handle multiple characters (bots, etc.)
|
||||
|
||||
Reference in New Issue
Block a user