Client-side heartbeat is working

This commit is contained in:
Kayne Ruse
2014-09-03 01:21:17 +10:00
parent 5577387d61
commit 06922dc820
3 changed files with 28 additions and 2 deletions
+20 -2
View File
@@ -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
+7
View File
@@ -46,6 +46,8 @@
//STL
#include <map>
#include <chrono>
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