Merge remote-tracking branch 'origin/Networking' into Networking
# Conflicts: # src/Engine/Network/Client.cpp
This commit is contained in:
@@ -5,10 +5,10 @@
|
||||
#include <ctime>
|
||||
|
||||
#include <glm/common.hpp>
|
||||
#include <boost/asio.hpp>
|
||||
|
||||
#include "Network/Network.h"
|
||||
#include "Network/MessageType.h"
|
||||
#include "Network/NetworkDefinitions.h"
|
||||
#include "Network/PlayerDefinition.h"
|
||||
#include "Network/SnapshotDefinitions.h"
|
||||
#include "Core/World.h"
|
||||
@@ -21,8 +21,8 @@ class Client : public Network
|
||||
public:
|
||||
Client(ConfigFile* config);
|
||||
~Client();
|
||||
void Start(World* world, EventBroker* eventBroker);
|
||||
void Update();
|
||||
void Start(World* world, EventBroker* eventBroker) override;
|
||||
void Update() override;
|
||||
void Close();
|
||||
private:
|
||||
// Assio UDP logic
|
||||
|
||||
@@ -5,15 +5,15 @@
|
||||
#include "Core/EventBroker.h"
|
||||
#include "Network/Packet.h"
|
||||
|
||||
#define MAXCONNECTIONS 8
|
||||
#define INPUTSIZE 128
|
||||
|
||||
class Network
|
||||
{
|
||||
public:
|
||||
Network();
|
||||
~Network();
|
||||
virtual void Start(World* m_world, EventBroker *eventBroker);
|
||||
virtual void Update();
|
||||
protected:
|
||||
|
||||
virtual ~Network() { };
|
||||
virtual void Start(World* m_world, EventBroker *eventBroker) = 0;
|
||||
virtual void Update() = 0;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,20 +0,0 @@
|
||||
#ifndef NetworkDefines_h__
|
||||
#define NetworkDefines_h__
|
||||
|
||||
#include <boost/asio/ip/udp.hpp>
|
||||
#include <queue>
|
||||
#include "Network/Packet.h"
|
||||
|
||||
|
||||
#define BOARDSIZE 16
|
||||
#define MAXCONNECTIONS 8
|
||||
#define INPUTSIZE 128
|
||||
#define PLAYERSPEED 0.2f;
|
||||
|
||||
typedef boost::shared_ptr<boost::asio::ip::udp::socket> socket_ptr;
|
||||
typedef boost::shared_ptr<std::string> string_ptr;
|
||||
typedef boost::shared_ptr<std::queue<string_ptr>> messageQueue_ptr;
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
@@ -20,6 +20,10 @@ public:
|
||||
template<typename T>
|
||||
void WritePrimitive(T val)
|
||||
{
|
||||
// Check if we are trying to add more than the package can fit.
|
||||
if (m_MaxPacketSize < m_Offset + sizeof(T)) {
|
||||
LOG_WARNING("Packet AddPrimitive(): You are trying to add more than we have allocated for!");
|
||||
}
|
||||
memcpy(m_Data + m_Offset, &val, sizeof(T));
|
||||
m_Offset += sizeof(T);
|
||||
}
|
||||
@@ -28,7 +32,7 @@ public:
|
||||
T ReadPrimitive()
|
||||
{
|
||||
if (m_Offset < m_ReturnDataOffset + sizeof(T)) {
|
||||
LOG_WARNING("Packaet PopFrontPrimitive(): You are trying to remove more than what exists in this packet!");
|
||||
LOG_WARNING("Packet PopFrontPrimitive(): You are trying to remove more than what exists in this packet!");
|
||||
return -1;
|
||||
}
|
||||
T returnValue;
|
||||
@@ -51,6 +55,7 @@ private:
|
||||
char* m_Data;
|
||||
unsigned int m_ReturnDataOffset = 0;
|
||||
int m_Offset = 0;
|
||||
unsigned int m_MaxPacketSize = 128;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -5,9 +5,9 @@
|
||||
#include <ctime>
|
||||
|
||||
#include <glm/common.hpp>
|
||||
#include <boost/asio/ip/udp.hpp>
|
||||
|
||||
#include "Network/MessageType.h"
|
||||
#include "Network/NetworkDefinitions.h"
|
||||
#include "Network/PlayerDefinition.h"
|
||||
#include "Core/World.h"
|
||||
#include "Core/EventBroker.h"
|
||||
@@ -18,8 +18,8 @@ class Server : public Network
|
||||
public:
|
||||
Server();
|
||||
~Server();
|
||||
void Start(World* m_world, EventBroker *eventBroker);
|
||||
void Update();
|
||||
void Start(World* m_world, EventBroker *eventBroker) override;
|
||||
void Update() override;
|
||||
void Close();
|
||||
|
||||
private:
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
#include "Network/Network.h"
|
||||
|
||||
Network::Network()
|
||||
{ }
|
||||
|
||||
Network::~Network()
|
||||
{ }
|
||||
|
||||
void Network::Start(World * m_world, EventBroker * eventBroker)
|
||||
{ }
|
||||
|
||||
void Network::Update()
|
||||
{ }
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
Packet::Packet(MessageType type, unsigned int& packetID)
|
||||
{
|
||||
m_Data = new char[128];
|
||||
m_Data = new char[m_MaxPacketSize];
|
||||
// Create message header
|
||||
// Add message type
|
||||
int messageType = static_cast<int>(type);
|
||||
@@ -12,10 +12,12 @@ Packet::Packet(MessageType type, unsigned int& packetID)
|
||||
packetID++;
|
||||
}
|
||||
|
||||
|
||||
// Create message
|
||||
Packet::Packet(char* data, const int sizeOfPacket)
|
||||
{
|
||||
// Create message
|
||||
// Resize message
|
||||
m_MaxPacketSize = sizeOfPacket;
|
||||
// Copy data newly allocated memory
|
||||
m_Data = new char[sizeOfPacket];
|
||||
memcpy(m_Data, data, sizeOfPacket);
|
||||
m_Offset = sizeOfPacket;
|
||||
@@ -29,14 +31,18 @@ Packet::~Packet()
|
||||
void Packet::WriteString(std::string str)
|
||||
{
|
||||
// Message, add one extra byte for null terminator
|
||||
memcpy(m_Data + m_Offset, str.data(), (str.size() + 1) * sizeof(char));
|
||||
m_Offset += (str.size() + 1) * sizeof(char);
|
||||
int sizeOfString = str.size() + 1;
|
||||
if (m_Offset + sizeOfString > m_MaxPacketSize) {
|
||||
LOG_WARNING("Package::WriteString(): Data size in packet exceeded maximum package size.\n");
|
||||
}
|
||||
memcpy(m_Data + m_Offset, str.data(), sizeOfString * sizeof(char));
|
||||
m_Offset += sizeOfString * sizeof(char);
|
||||
}
|
||||
|
||||
void Packet::WriteData(char * data, int sizeOfData)
|
||||
{
|
||||
if (m_Offset + sizeOfData > 128) {
|
||||
LOG_WARNING("Packet::AddData(): Data size in packet exceeded maximum packet size.\n");
|
||||
{
|
||||
if (m_Offset + sizeOfData > m_MaxPacketSize) {
|
||||
LOG_WARNING("Packet::WriteData(): Data size in packet exceeded maximum packet size.\n");
|
||||
}
|
||||
memcpy(m_Data + m_Offset, data, sizeOfData);
|
||||
m_Offset += sizeOfData;
|
||||
@@ -45,10 +51,10 @@ void Packet::WriteData(char * data, int sizeOfData)
|
||||
std::string Packet::ReadString()
|
||||
{
|
||||
std::string returnValue(m_Data + m_ReturnDataOffset);
|
||||
if (m_Offset < m_ReturnDataOffset + returnValue.size()){
|
||||
LOG_WARNING("packet PopFrontString(): Oh no! You are trying to remove things outside my memory kingdom");
|
||||
if (m_Offset < m_ReturnDataOffset + returnValue.size()) {
|
||||
LOG_WARNING("packet ReadString(): Oh no! You are trying to remove things outside my memory kingdom");
|
||||
return "PopFrontString Failed";
|
||||
}
|
||||
}
|
||||
// +1 for null terminator.
|
||||
m_ReturnDataOffset += returnValue.size() + 1;
|
||||
return returnValue;
|
||||
@@ -56,7 +62,11 @@ std::string Packet::ReadString()
|
||||
|
||||
char * Packet::ReadData(int SizeOfData)
|
||||
{
|
||||
if (m_Offset < m_ReturnDataOffset + SizeOfData) {
|
||||
LOG_WARNING("packet ReadData(): Oh no! You are trying to remove things outside my memory kingdom");
|
||||
return nullptr;
|
||||
}
|
||||
unsigned int oldReturnDataOffset = m_ReturnDataOffset;
|
||||
m_ReturnDataOffset += SizeOfData;
|
||||
return (m_Data + oldReturnDataOffset);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user