Moved heartbeat code to ServerApplication::CheckClientConnections()

This commit is contained in:
Kayne Ruse
2014-09-30 02:01:56 +10:00
parent 87ef03d512
commit db40f198be
7 changed files with 29 additions and 17 deletions
View File
View File
+2 -1
View File
@@ -1,10 +1,11 @@
print("Lua script check")
mapMaker = require "map_maker"
mapSaver = require "map_saver"
--BUG: #35 The server fails without at least one room
--TODO: Create rooms with names?
newRoom = RoomManager.CreateRoom()
newRoom = RoomManager.CreateRoom("overworld", "overworld.bmp")
pager = Room.GetPager(newRoom)
RegionPager.SetOnCreate(pager, mapMaker.debugIsland)
+6
View File
@@ -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
+1
View File
@@ -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
View File
@@ -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);
+19
View File
@@ -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.)