diff --git a/client/scenes/in_world.cpp b/client/scenes/in_world.cpp index 3c20ad6..5cd2dc6 100644 --- a/client/scenes/in_world.cpp +++ b/client/scenes/in_world.cpp @@ -140,6 +140,20 @@ void InWorld::Update() { //update the camera camera.x = localCharacter->GetOrigin().x - camera.marginX; camera.y = localCharacter->GetOrigin().y - camera.marginY; + + //check the connection + if (Clock::now() - lastBeat > std::chrono::seconds(5)) { + if (attemptedBeats > 2) { + throw(std::runtime_error("Connection lost")); + } + + ServerPacket newPacket; + newPacket.type = SerialPacketType::PING; + network.SendTo(Channels::SERVER, &newPacket); + + attemptedBeats++; + lastBeat = Clock::now(); + } } void InWorld::FrameEnd() { @@ -305,9 +319,13 @@ void InWorld::HandlePing(ServerPacket* const argPacket) { } void InWorld::HandlePong(ServerPacket* const argPacket) { - //TODO: InWorld::HandlePong() -} + if (network.GetIPAddress(Channels::SERVER)->host != argPacket->srcAddress.host) { + throw(std::runtime_error("Heartbeat message received from unknown source")); + } + attemptedBeats = 0; + lastBeat = Clock::now(); +} void InWorld::HandleDisconnect(ClientPacket* const argPacket) { //TODO: More needed in the disconnection diff --git a/client/scenes/in_world.hpp b/client/scenes/in_world.hpp index a2479b7..15fcdce 100644 --- a/client/scenes/in_world.hpp +++ b/client/scenes/in_world.hpp @@ -46,6 +46,8 @@ //STL #include +#include + class InWorld : public BaseScene { public: //Public access members @@ -121,6 +123,11 @@ protected: //game Character* localCharacter = nullptr; + + //connections + typedef std::chrono::steady_clock Clock; + Clock::time_point lastBeat = Clock::now(); + int attemptedBeats = 0; }; #endif diff --git a/todo.txt b/todo.txt index 8e455b3..aec3faf 100644 --- a/todo.txt +++ b/todo.txt @@ -1,5 +1,6 @@ TODO: Heartbeat systems TODO: Rejection messages +TODO: The error handling is terrible TODO: Move the statistics into their own SQL table, instead of duplicating the structure a dozen times TODO: Get the rooms working, even if only via hotkeys TODO: Fix shoddy movement