Added server has timed out logic to client. (>'')>

This commit is contained in:
stiffly
2016-01-20 16:27:53 +01:00
parent 4d01cdf895
commit 3376f4a16e
5 changed files with 32 additions and 19 deletions
+3 -2
View File
@@ -48,6 +48,7 @@ private:
std::string m_PlayerName; std::string m_PlayerName;
int m_PlayerID = -1; int m_PlayerID = -1;
EntityID m_ServerEntityID = std::numeric_limits<EntityID>::max(); EntityID m_ServerEntityID = std::numeric_limits<EntityID>::max();
bool m_IsConnected = false;
// Server Client Lookup map // Server Client Lookup map
// Assumes that root node for client and server is EntityID 0. // Assumes that root node for client and server is EntityID 0.
@@ -71,14 +72,14 @@ private:
void ping(); void ping();
void parseMessageType(Packet& packet); void parseMessageType(Packet& packet);
void updateFields(Packet& packet, const ComponentInfo& componentInfo, const EntityID& entityID, const std::string& componentType); void updateFields(Packet& packet, const ComponentInfo& componentInfo, const EntityID& entityID, const std::string& componentType);
void parseConnect(Packet& packet); void parseConnect(Packet& packet);
void parsePlayerConnected(Packet& packet); void parsePlayerConnected(Packet& packet);
void parsePing(); void parsePing();
void parseServerPing(); void parseServerPing();
void InterpolateFields(Packet & packet, const ComponentInfo & componentInfo, const EntityID & entityID, const std::string & componentType); void InterpolateFields(Packet & packet, const ComponentInfo & componentInfo, const EntityID & entityID, const std::string & componentType);
void parseSnapshot(Packet& packet); void parseSnapshot(Packet& packet);
void identifyPacketLoss(); void identifyPacketLoss();
bool isConnected(); bool hasServerTimedOut();
EntityID createPlayer(); EntityID createPlayer();
void sendInputCommands(); void sendInputCommands();
// Mapping Logic // Mapping Logic
+1
View File
@@ -7,6 +7,7 @@
#define MAXCONNECTIONS 8 #define MAXCONNECTIONS 8
#define INPUTSIZE 4097 #define INPUTSIZE 4097
#define TIMEOUTMS 15000
class Network class Network
{ {
+1 -1
View File
@@ -11,7 +11,7 @@
#include "Network/PlayerDefinition.h" #include "Network/PlayerDefinition.h"
#include "Core/World.h" #include "Core/World.h"
#include "Core/EventBroker.h" #include "Core/EventBroker.h"
#include "Network/Network.h" #include "../Network/Network.h"
#include "Input/EInputCommand.h" #include "Input/EInputCommand.h"
#include "Core/EPlayerDamage.h" #include "Core/EPlayerDamage.h"
+26 -14
View File
@@ -35,6 +35,9 @@ void Client::Update()
{ {
m_EventBroker->Process<Client>(); m_EventBroker->Process<Client>();
readFromServer(); readFromServer();
if (m_IsConnected) {
hasServerTimedOut();
}
} }
void Client::readFromServer() void Client::readFromServer()
@@ -86,10 +89,8 @@ void Client::parseMessageType(Packet& packet)
void Client::parseConnect(Packet& packet) void Client::parseConnect(Packet& packet)
{ {
// Set your own player id // Set your own player id
m_PlayerID = packet.ReadPrimitive<int>();
m_ServerEntityID = packet.ReadPrimitive<EntityID>();
// Map ServerEntityID and your PlayerID // Map ServerEntityID and your PlayerID
LOG_INFO("%i: I am player: %i", m_PacketID, m_PlayerID); LOG_INFO("I are connected PogChamp");
} }
void Client::parsePlayerConnected(Packet & packet) void Client::parsePlayerConnected(Packet & packet)
@@ -100,12 +101,18 @@ void Client::parsePlayerConnected(Packet & packet)
void Client::parsePing() void Client::parsePing()
{ {
m_DurationOfPingTime = 1000 * (std::clock() - m_StartPingTime) / static_cast<double>(CLOCKS_PER_SEC);
LOG_INFO("%i: response time with ctime(ms): %f", m_PacketID, m_DurationOfPingTime);
} }
void Client::parseServerPing() void Client::parseServerPing()
{ {
// Might miss connect message so set it here instead.
m_IsConnected = true;
// Time since last ping was received
m_DurationOfPingTime = 1000 * (std::clock() - m_StartPingTime) / static_cast<double>(CLOCKS_PER_SEC);
LOG_INFO("%i: response time with ctime(ms): %f", m_PacketID, m_DurationOfPingTime);
m_StartPingTime = std::clock();
Packet packet(MessageType::ServerPing, m_SendPacketID); Packet packet(MessageType::ServerPing, m_SendPacketID);
packet.WriteString("Ping recieved"); packet.WriteString("Ping recieved");
send(packet); send(packet);
@@ -238,16 +245,18 @@ void Client::connect()
void Client::disconnect() void Client::disconnect()
{ {
m_PreviousPacketID = 0;
m_PacketID = 0;
Packet packet(MessageType::Disconnect, m_SendPacketID); Packet packet(MessageType::Disconnect, m_SendPacketID);
send(packet); send(packet);
} }
void Client::ping() void Client::ping()
{ {
Packet packet(MessageType::Connect, m_SendPacketID); //Packet packet(MessageType::Connect, m_SendPacketID);
packet.WriteString("Ping"); //packet.WriteString("Ping");
m_StartPingTime = std::clock(); //m_StartPingTime = std::clock();
send(packet); //send(packet);
} }
bool Client::OnInputCommand(const Events::InputCommand & e) bool Client::OnInputCommand(const Events::InputCommand & e)
@@ -290,12 +299,15 @@ void Client::identifyPacketLoss()
} }
} }
bool Client::isConnected() bool Client::hasServerTimedOut()
{ {
if (m_PlayerID != -1) { // Time in ms
if (m_PlayerDefinitions[m_PlayerID].EntityID != -1) { float timeSincePing = 1000 * (std::clock() - m_StartPingTime) / static_cast<double>(CLOCKS_PER_SEC);
return true; if (timeSincePing > TIMEOUTMS) {
} // Clear everything and go to menu.
LOG_INFO("Server has timed out, returning to menu, Beep Boop.");
m_IsConnected = false;
return true;
} }
return false; return false;
} }
+1 -2
View File
@@ -182,7 +182,6 @@ void Server::sendPing()
void Server::checkForTimeOuts() void Server::checkForTimeOuts()
{ {
int timeOutTimeMs = 5000;
int startPing = 1000 * m_StartPingTime int startPing = 1000 * m_StartPingTime
/ static_cast<double>(CLOCKS_PER_SEC); / static_cast<double>(CLOCKS_PER_SEC);
@@ -190,7 +189,7 @@ void Server::checkForTimeOuts()
if (m_PlayerDefinitions[i].Endpoint.address() != boost::asio::ip::address()) { if (m_PlayerDefinitions[i].Endpoint.address() != boost::asio::ip::address()) {
int stopPing = 1000 * m_PlayerDefinitions[i].StopTime / int stopPing = 1000 * m_PlayerDefinitions[i].StopTime /
static_cast<double>(CLOCKS_PER_SEC); static_cast<double>(CLOCKS_PER_SEC);
if (startPing > stopPing + timeOutTimeMs) { if (startPing > stopPing + TIMEOUTMS) {
LOG_INFO("Player %i timed out!", i); LOG_INFO("Player %i timed out!", i);
disconnect(i); disconnect(i);
} }