Network is now working as udp or tcp. Next step is making the hybrid.
This commit is contained in:
@@ -1,20 +1,32 @@
|
||||
#ifndef NetworkServer_h__
|
||||
#define NetworkServer_h__
|
||||
|
||||
#include <boost/asio.hpp>
|
||||
#include "Network/Packet.h"
|
||||
#include "Network/PlayerDefinition.h"
|
||||
#define BUFFERSIZE 32000
|
||||
typedef unsigned int PlayerID;
|
||||
typedef unsigned int PacketID;
|
||||
|
||||
class NetworkServer
|
||||
{
|
||||
//public:
|
||||
// virtual void Connect(std::string playerName, std::string address, int port) = 0;
|
||||
// virtual void Disconnect() = 0;
|
||||
// virtual Packet Receive() = 0;
|
||||
// virtual void Send(Packet & packet) = 0;
|
||||
//protected:
|
||||
// char m_ReadBuffer[BUFFERSIZE] = { 0 };
|
||||
public:
|
||||
virtual void AcceptNewConnections(int& nextPlayerID, std::map<PlayerID, PlayerDefinition>& connectedPlayers) = 0;
|
||||
virtual void Receive(Packet & packet, PlayerDefinition & playerDefinition) = 0;
|
||||
virtual void Send(Packet & packet, PlayerDefinition & playerDefinition) = 0;
|
||||
virtual void Send(Packet & packet) = 0;
|
||||
protected:
|
||||
char m_ReadBuffer[BUFFERSIZE] = { 0 };
|
||||
//void handle_accept(boost::shared_ptr<boost::asio::ip::tcp::socket> socket, const boost::system::error_code & error);
|
||||
//void parseConnect(Packet & packet);
|
||||
//void readFromClients();
|
||||
|
||||
//public:
|
||||
// virtual void Connect(std::string playerName, std::string address, int port) = 0;
|
||||
// virtual void Disconnect() = 0;
|
||||
// virtual Packet Receive() = 0;
|
||||
// virtual void Send(Packet & packet) = 0;
|
||||
//protected:
|
||||
// char m_ReadBuffer[BUFFERSIZE] = { 0 };
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -57,7 +57,7 @@ public:
|
||||
void ChangePacketID(unsigned int& packetID);
|
||||
int Size() { return m_Offset; };
|
||||
char* Data() { return m_Data; };
|
||||
MessageType GetMessageType() { return m_MessageType; };
|
||||
MessageType GetMessageType();
|
||||
unsigned int DataReadSize() { return m_ReturnDataOffset; }
|
||||
unsigned int MaxSize() { return m_MaxPacketSize; }
|
||||
unsigned int HeaderSize() { return m_HeaderSize; }
|
||||
@@ -68,7 +68,6 @@ private:
|
||||
int m_Offset = 0;
|
||||
unsigned int m_MaxPacketSize = 512;
|
||||
unsigned int m_HeaderSize = 0;
|
||||
MessageType m_MessageType = MessageType::Invalid;
|
||||
void resizeData();
|
||||
void resizeData(int size);
|
||||
};
|
||||
|
||||
@@ -6,6 +6,8 @@
|
||||
|
||||
#include <glm/common.hpp>
|
||||
|
||||
#include "Network/TCPServer.h"
|
||||
#include "Network/UDPServer.h"
|
||||
#include "Network/MessageType.h"
|
||||
#include "Network/PlayerDefinition.h"
|
||||
#include "Core/World.h"
|
||||
@@ -17,6 +19,7 @@
|
||||
#include "Core/EPlayerSpawned.h"
|
||||
#include "Core/EEntityDeleted.h"
|
||||
#include "Core/EComponentDeleted.h"
|
||||
|
||||
class Server : public Network
|
||||
{
|
||||
public:
|
||||
@@ -49,7 +52,7 @@ protected:
|
||||
// Game logic
|
||||
World* m_World;
|
||||
EventBroker* m_EventBroker;
|
||||
|
||||
|
||||
// Packet loss logic
|
||||
PacketID m_PacketID = 0;
|
||||
PacketID m_PreviousPacketID = 0;
|
||||
@@ -70,13 +73,15 @@ protected:
|
||||
void parsePlayerTransform(Packet& packet);
|
||||
void parseOnInputCommand(Packet& packet);
|
||||
void parseClientPing();
|
||||
void parsePing();
|
||||
void parsePing();
|
||||
void parseConnect(Packet & packet, PlayerDefinition & pd);
|
||||
void parseTCPConnect(Packet & packet);
|
||||
void parseDisconnect();
|
||||
// Pure virtual functions
|
||||
virtual void readFromClients() = 0;
|
||||
virtual void send(Packet& packet, PlayerDefinition & playerDefinition) = 0;
|
||||
virtual void send(Packet& packet) = 0;
|
||||
virtual void parseConnect(Packet& packet) = 0;
|
||||
//// Pure virtual functions
|
||||
//virtual void readFromClients() = 0;
|
||||
//virtual void send(Packet& packet, PlayerDefinition & playerDefinition) = 0;
|
||||
//virtual void send(Packet& packet) = 0;
|
||||
|
||||
// Debug event
|
||||
EventRelay<Server, Events::InputCommand> m_EInputCommand;
|
||||
bool OnInputCommand(const Events::InputCommand& e);
|
||||
@@ -86,6 +91,9 @@ protected:
|
||||
bool OnEntityDeleted(const Events::EntityDeleted& e);
|
||||
EventRelay<Server, Events::ComponentDeleted> m_EComponentDeleted;
|
||||
bool OnComponentDeleted(const Events::ComponentDeleted& e);
|
||||
private:
|
||||
TCPServer m_TCPServer;
|
||||
//UDPServer m_UDPServer;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,26 +1,30 @@
|
||||
#ifndef TCPServer_h__
|
||||
#define TCPServer_h__
|
||||
|
||||
#include "Server.h"
|
||||
#include <boost/asio.hpp>
|
||||
#include <boost/bind.hpp>
|
||||
#include <map>
|
||||
#include "NetworkServer.h"
|
||||
|
||||
class TCPServer : public Server
|
||||
class TCPServer : public NetworkServer
|
||||
{
|
||||
public:
|
||||
TCPServer();
|
||||
~TCPServer();
|
||||
void AcceptNewConnections(int& nextPlayerID, std::map<PlayerID, PlayerDefinition>& connectedPlayers);
|
||||
void Receive(Packet & packet, PlayerDefinition & playerDefinition);
|
||||
void Send(Packet & packet, PlayerDefinition & playerDefinition);
|
||||
void Send(Packet & packet);
|
||||
private:
|
||||
// TCP logic
|
||||
boost::asio::io_service m_IOService;
|
||||
std::unique_ptr<boost::asio::ip::tcp::acceptor> acceptor;
|
||||
boost::shared_ptr<boost::asio::ip::tcp::socket> lastReceivedSocket;
|
||||
|
||||
void acceptNewConnections();
|
||||
void handle_accept(boost::shared_ptr<boost::asio::ip::tcp::socket> socket, const boost::system::error_code & error);
|
||||
void readFromClients();
|
||||
int receive(char * data, boost::asio::ip::tcp::socket& socket);
|
||||
void parseConnect(Packet & packet);
|
||||
void send(Packet & packet, PlayerDefinition & playerDefinition);
|
||||
void send(Packet & packet);
|
||||
|
||||
void handle_accept(boost::shared_ptr<boost::asio::ip::tcp::socket> socket,
|
||||
int& nextPlayerID, std::map<PlayerID, PlayerDefinition>& connectedPlayers,
|
||||
const boost::system::error_code& error);
|
||||
int readBuffer(char* data, PlayerDefinition& playerDefinition);
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,25 +1,31 @@
|
||||
#ifndef UDPServer_h__
|
||||
#define UDPServer_h__
|
||||
|
||||
#include "Server.h"
|
||||
#include "NetworkServer.h"
|
||||
#include <boost/asio/ip/udp.hpp>
|
||||
//
|
||||
//virtual void AcceptNewConnections() = 0;
|
||||
//virtual void Receive(Packet & packet, PlayerDefinition & playerDefinition) = 0;
|
||||
//virtual void Send(Packet & packet, PlayerDefinition & playerDefinition) = 0;
|
||||
//virtual void Send(Packet & packet) = 0;
|
||||
|
||||
class UDPServer : public Server
|
||||
class UDPServer : public NetworkServer
|
||||
{
|
||||
public:
|
||||
UDPServer();
|
||||
~UDPServer();
|
||||
void AcceptNewConnections(int& nextPlayerID, std::map<PlayerID, PlayerDefinition>& connectedPlayers);
|
||||
void Receive(Packet & packet, PlayerDefinition & playerDefinition);
|
||||
//void parseConnect(Packet & packet, PlayerDefinition & playerDefinition);
|
||||
void Send(Packet & packet, PlayerDefinition & playerDefinition);
|
||||
void Send(Packet & packet);
|
||||
bool IsSocketAvailable();
|
||||
private:
|
||||
// UDP logic
|
||||
boost::asio::io_service m_IOService;
|
||||
boost::asio::ip::udp::endpoint m_ReceiverEndpoint;
|
||||
std::unique_ptr<boost::asio::ip::udp::socket> m_Socket;
|
||||
|
||||
void readFromClients();
|
||||
int receive(char * data);
|
||||
void parseConnect(Packet & packet);
|
||||
void send(Packet & packet, PlayerDefinition & playerDefinition);
|
||||
void send(Packet & packet);
|
||||
int readBuffer(char* data);
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user