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") print("Lua script check")
mapMaker = require "map_maker" mapMaker = require "map_maker"
mapSaver = require "map_saver"
--BUG: #35 The server fails without at least one room --BUG: #35 The server fails without at least one room
--TODO: Create rooms with names? --TODO: Create rooms with names?
newRoom = RoomManager.CreateRoom() newRoom = RoomManager.CreateRoom("overworld", "overworld.bmp")
pager = Room.GetPager(newRoom) pager = Room.GetPager(newRoom)
RegionPager.SetOnCreate(pager, mapMaker.debugIsland) RegionPager.SetOnCreate(pager, mapMaker.debugIsland)
+6
View File
@@ -25,6 +25,8 @@
//map system //map system
#include "region_pager_lua.hpp" #include "region_pager_lua.hpp"
#include <string>
class RoomData { class RoomData {
public: public:
RoomData() = default; RoomData() = default;
@@ -33,11 +35,15 @@ public:
//accessors and mutators //accessors and mutators
RegionPagerLua* GetPager() { return &pager; } RegionPagerLua* GetPager() { return &pager; }
std::string SetTilesetName(std::string s) { return tilesetName = s; }
std::string GetTilesetName() { return tilesetName; }
private: private:
friend class RoomManager; friend class RoomManager;
//members //members
RegionPagerLua pager; RegionPagerLua pager;
std::string tilesetName;
}; };
#endif #endif
+1
View File
@@ -80,6 +80,7 @@ private:
void HandleSynchronize(ClientPacket* const); void HandleSynchronize(ClientPacket* const);
//utility methods //utility methods
void CheckClientConnections();
//TODO: a function that only sends to characters in a certain proximity //TODO: a function that only sends to characters in a certain proximity
void CleanupLostConnection(int index); void CleanupLostConnection(int index);
void PumpPacket(SerialPacket* const); void PumpPacket(SerialPacket* const);
+1 -16
View File
@@ -168,23 +168,8 @@ void ServerApplication::Proc() {
//update the internals //update the internals
//... //...
//TODO: This could be checked only every few seconds
//Check connections //Check connections
for (auto& it : clientMap) { CheckClientConnections();
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;
}
}
//give the computer a break //give the computer a break
SDL_Delay(10); SDL_Delay(10);
+19
View File
@@ -21,6 +21,7 @@
*/ */
#include "server_application.hpp" #include "server_application.hpp"
#include <chrono>
#include <iostream> #include <iostream>
//------------------------- //-------------------------
@@ -159,6 +160,7 @@ void ServerApplication::HandleRegionRequest(RegionPacket* const argPacket) {
newPacket.x = argPacket->x; newPacket.x = argPacket->x;
newPacket.y = argPacket->y; newPacket.y = argPacket->y;
//BUG: possibly related to #35
newPacket.region = roomMgr.GetRoom(argPacket->roomIndex)->GetPager()->GetRegion(argPacket->x, argPacket->y); newPacket.region = roomMgr.GetRoom(argPacket->roomIndex)->GetPager()->GetRegion(argPacket->x, argPacket->y);
//send the content //send the content
@@ -280,6 +282,23 @@ void ServerApplication::HandleSynchronize(ClientPacket* const argPacket) {
//utility methods //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) { void ServerApplication::CleanupLostConnection(int clientIndex) {
//NOTE: This assumes each player has only one account and character at a time //NOTE: This assumes each player has only one account and character at a time
//TODO: handle multiple characters (bots, etc.) //TODO: handle multiple characters (bots, etc.)