Merge branch 'Networking' of github.com:teamfisk/TacticalZ into Networking

# Conflicts:
#	src/Engine/Network/Client.cpp
#	src/Engine/Network/Server.cpp
This commit is contained in:
Jocke
2016-01-20 16:30:12 +01:00
5 changed files with 31 additions and 16 deletions
+3 -2
View File
@@ -48,6 +48,7 @@ private:
std::string m_PlayerName;
int m_PlayerID = -1;
EntityID m_ServerEntityID = std::numeric_limits<EntityID>::max();
bool m_IsConnected = false;
// Server Client Lookup map
// Assumes that root node for client and server is EntityID 0.
@@ -71,14 +72,14 @@ private:
void ping();
void parseMessageType(Packet& packet);
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 parsePing();
void parseServerPing();
void InterpolateFields(Packet & packet, const ComponentInfo & componentInfo, const EntityID & entityID, const std::string & componentType);
void parseSnapshot(Packet& packet);
void identifyPacketLoss();
bool isConnected();
bool hasServerTimedOut();
EntityID createPlayer();
void sendInputCommands();
// Mapping Logic
+1
View File
@@ -7,6 +7,7 @@
#define MAXCONNECTIONS 8
#define INPUTSIZE 4097
#define TIMEOUTMS 15000
class Network
{
+1 -1
View File
@@ -11,7 +11,7 @@
#include "Network/PlayerDefinition.h"
#include "Core/World.h"
#include "Core/EventBroker.h"
#include "Network/Network.h"
#include "../Network/Network.h"
#include "Input/EInputCommand.h"
#include "Core/EPlayerDamage.h"
+25 -11
View File
@@ -35,6 +35,9 @@ void Client::Update()
{
m_EventBroker->Process<Client>();
readFromServer();
if (m_IsConnected) {
hasServerTimedOut();
}
}
void Client::readFromServer()
@@ -97,12 +100,18 @@ void Client::parsePlayerConnected(Packet & packet)
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()
{
// 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.WriteString("Ping recieved");
send(packet);
@@ -235,16 +244,18 @@ void Client::connect()
void Client::disconnect()
{
m_PreviousPacketID = 0;
m_PacketID = 0;
Packet packet(MessageType::Disconnect, m_SendPacketID);
send(packet);
}
void Client::ping()
{
Packet packet(MessageType::Connect, m_SendPacketID);
packet.WriteString("Ping");
m_StartPingTime = std::clock();
send(packet);
//Packet packet(MessageType::Connect, m_SendPacketID);
//packet.WriteString("Ping");
//m_StartPingTime = std::clock();
//send(packet);
}
bool Client::OnInputCommand(const Events::InputCommand & e)
@@ -287,12 +298,15 @@ void Client::identifyPacketLoss()
}
}
bool Client::isConnected()
bool Client::hasServerTimedOut()
{
if (m_PlayerID != -1) {
if (m_PlayerDefinitions[m_PlayerID].EntityID != -1) {
return true;
}
// Time in ms
float timeSincePing = 1000 * (std::clock() - m_StartPingTime) / static_cast<double>(CLOCKS_PER_SEC);
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;
}
+1 -2
View File
@@ -181,7 +181,6 @@ void Server::sendPing()
void Server::checkForTimeOuts()
{
int timeOutTimeMs = 5000;
int startPing = 1000 * m_StartPingTime
/ static_cast<double>(CLOCKS_PER_SEC);
@@ -189,7 +188,7 @@ void Server::checkForTimeOuts()
if (m_ConnectedUsers[i].Endpoint.address() != boost::asio::ip::address()) {
int stopPing = 1000 * m_ConnectedUsers[i].StopTime /
static_cast<double>(CLOCKS_PER_SEC);
if (startPing > stopPing + timeOutTimeMs) {
if (startPing > stopPing + TIMEOUTMS) {
LOG_INFO("User %i timed out!", i);
disconnect(i);
}