Cleaned code

This commit is contained in:
stiffly
2015-12-17 10:47:08 +01:00
parent 97a0d97b0b
commit 4c29e071af
2 changed files with 43 additions and 47 deletions
+35 -38
View File
@@ -5,7 +5,6 @@
#include <ctime> #include <ctime>
#include <glm/common.hpp> #include <glm/common.hpp>
#include <GLFW/glfw3.h> // For input event
#include "Network/MessageType.h" #include "Network/MessageType.h"
#include "Network/NetworkDefinitions.h" #include "Network/NetworkDefinitions.h"
@@ -14,68 +13,66 @@
#include "Network/WinLeakCheck.h" #include "Network/WinLeakCheck.h"
#include "Core/World.h" #include "Core/World.h"
#include "Core/EventBroker.h" #include "Core/EventBroker.h"
#include "Core/EKeyDown.h"
#include "Core/EKeyUp.h"
#include "Input/EInputCommand.h" #include "Input/EInputCommand.h"
#include "Network/Network.h" #include "Network/Network.h"
class Client : public Network class Client : public Network
{ {
public: public:
Client(); Client();
~Client(); ~Client();
void Start(World* world, EventBroker* eventBroker); void Start(World* world, EventBroker* eventBroker);
void Update(); void Update();
void Close(); void Close();
private: private:
void ReadFromServer(); void ReadFromServer();
void SendSnapshotToServer(); void SendSnapshotToServer();
int Receive(char* data, size_t length); int Receive(char* data, size_t length);
void Send(Package& message); void Send(Package& message);
int CreateMessage(MessageType type, std::string message, char* data);
void Connect(); void Connect();
void Disconnect(); void Disconnect();
void Ping(); void Ping();
void MoveMessageHead(char*& data, size_t& length, size_t stepSize); void MoveMessageHead(char*& data, size_t& length, size_t stepSize);
void ParseMessageType(Package& package); void ParseMessageType(Package& package);
void ParseEventMessage(Package& package); void ParseEventMessage(Package& package);
void ParseConnect(Package& package); void ParseConnect(Package& package);
void ParsePing(); void ParsePing();
void ParseServerPing(); void ParseServerPing();
void ParseSnapshot(Package& package); void ParseSnapshot(Package& package);
void CreateNewPlayer(int i); void CreateNewPlayer(int i);
void IdentifyPacketLoss(); void IdentifyPacketLoss();
// udp stuff // UDP logic
boost::asio::ip::udp::endpoint m_ReceiverEndpoint; boost::asio::ip::udp::endpoint m_ReceiverEndpoint;
boost::asio::io_service m_IOService; boost::asio::io_service m_IOService;
boost::asio::ip::udp::socket m_Socket; boost::asio::ip::udp::socket m_Socket;
// Packet loss logic // Packet loss logic
unsigned int m_PacketID = 0; unsigned int m_PacketID = 0;
unsigned int m_PreviousPacketID = 0; unsigned int m_PreviousPacketID = 0;
unsigned int m_SendPacketID = 0; unsigned int m_SendPacketID = 0;
// Game Logic // Game logic
World* m_World;
std::vector<unsigned int> m_PlayersToCreate; std::vector<unsigned int> m_PlayersToCreate;
glm::vec2 m_PlayerPositions[MAXCONNECTIONS];
std::string m_PlayerName;
int m_PlayerID = -1;
IsWASDKeyDown m_IsWASDKeyDown;
World* m_World; // Network logic
int m_PlayerID = -1; PlayerDefinition m_PlayerDefinitions[MAXCONNECTIONS];
glm::vec2 m_PlayerPositions[MAXCONNECTIONS]; SnapshotDefinitions m_NextSnapshot;
PlayerDefinition m_PlayerDefinitions[MAXCONNECTIONS];
SnapshotDefinitions m_NextSnapshot;
std::clock_t m_StartPingTime;
double m_DurationOfPingTime;
std::string m_PlayerName;
bool m_ThreadIsRunning = true; bool m_ThreadIsRunning = true;
double m_DurationOfPingTime;
std::clock_t m_StartPingTime;
// Use to check if we should send disconnect message // Use to check if we should send disconnect message
// if game is turned of by closing window. // if game is turned of by closing window.
bool m_WasStarted = false; bool m_WasStarted = false;
IsWASDKeyDown m_IsWASDKeyDown;
// Events // Events
EventBroker* m_EventBroker; EventBroker* m_EventBroker;
EventRelay<Client, Events::InputCommand> m_EInputCommand; EventRelay<Client, Events::InputCommand> m_EInputCommand;
bool OnInputCommand(const Events::InputCommand &e); bool OnInputCommand(const Events::InputCommand &e);
}; };
+8 -9
View File
@@ -5,7 +5,7 @@ using namespace boost::asio::ip;
Client::Client() : m_Socket(m_IOService) Client::Client() : m_Socket(m_IOService)
{ {
m_ReceiverEndpoint = udp::endpoint(boost::asio::ip::address::from_string("192.168.1.2"), 13); m_ReceiverEndpoint = udp::endpoint(boost::asio::ip::address::from_string("192.168.1.6"), 13);
// Set up network stream // Set up network stream
m_NextSnapshot.InputForward = ""; m_NextSnapshot.InputForward = "";
m_NextSnapshot.InputRight = ""; m_NextSnapshot.InputRight = "";
@@ -38,7 +38,7 @@ void Client::Start(World* world, EventBroker* eventBroker)
} }
void Client::Update() void Client::Update()
{ {
while (m_PlayersToCreate.size() > 0) { while (m_PlayersToCreate.size() > 0) {
unsigned int i = m_PlayersToCreate.size() - 1; unsigned int i = m_PlayersToCreate.size() - 1;
unsigned int tempID = m_World->CreateEntity(); unsigned int tempID = m_World->CreateEntity();
@@ -114,7 +114,7 @@ void Client::SendSnapshotToServer()
package.AddString("0Forward"); package.AddString("0Forward");
Send(package); Send(package);
} }
if (m_NextSnapshot.InputRight != "") { if (m_NextSnapshot.InputRight != "") {
@@ -204,7 +204,7 @@ void Client::ParseSnapshot(Package& package)
// We're checking for empty name for now. This might not be the best way, // We're checking for empty name for now. This might not be the best way,
// but it is to avoid sending redundant data. // but it is to avoid sending redundant data.
tempName = package.PopFrontString(); tempName = package.PopFrontString();
// Apply the position data read to the player entity // Apply the position data read to the player entity
// New player connected on the server side // New player connected on the server side
@@ -225,7 +225,7 @@ void Client::ParseSnapshot(Package& package)
//playerPos.y = package.PopFrontPrimitive<float>(); //playerPos.y = package.PopFrontPrimitive<float>();
//playerPos.z = package.PopFrontPrimitive<float>(); //playerPos.z = package.PopFrontPrimitive<float>();
// Move player to server position // Move player to server position
if (m_PlayerDefinitions[i].EntityID != -1) { if (m_PlayerDefinitions[i].EntityID != -1) {
@@ -294,8 +294,7 @@ bool Client::OnInputCommand(const Events::InputCommand & e)
if (e.Command == "Forward") { if (e.Command == "Forward") {
if (e.Value > 0) { if (e.Value > 0) {
m_IsWASDKeyDown.W = true; m_IsWASDKeyDown.W = true;
} } else if (e.Value < 0) {
else if (e.Value < 0) {
m_IsWASDKeyDown.S = true; m_IsWASDKeyDown.S = true;
} else { } else {
m_IsWASDKeyDown.W = false; m_IsWASDKeyDown.W = false;
@@ -312,7 +311,7 @@ bool Client::OnInputCommand(const Events::InputCommand & e)
m_IsWASDKeyDown.D = false; m_IsWASDKeyDown.D = false;
} }
} }
if (e.Command == "Sprint") { // Temp connect if (e.Command == "Sprint") { // Connect for now
Connect(); Connect();
} }
return false; return false;
@@ -331,6 +330,6 @@ void Client::IdentifyPacketLoss()
// if no packets lost, difference should be equal to 1 // if no packets lost, difference should be equal to 1
int difference = m_PacketID - m_PreviousPacketID; int difference = m_PacketID - m_PreviousPacketID;
if (difference != 1) { if (difference != 1) {
LOG_INFO("%i Packet(s) were lost...", difference); LOG_INFO("%i Packet(s) were lost...", difference);
} }
} }