Merge branch 'Networking' of github.com:teamfisk/TacticalZ into Networking
# Conflicts: # include/Engine/Network/Client.h # src/Engine/Network/Client.cpp
This commit is contained in:
@@ -4,8 +4,6 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
|
|
||||||
#include <boost/thread.hpp>
|
|
||||||
#include <boost/bind.hpp>
|
|
||||||
#include <glm/common.hpp>
|
#include <glm/common.hpp>
|
||||||
#include <GLFW/glfw3.h> // For input event
|
#include <GLFW/glfw3.h> // For input event
|
||||||
|
|
||||||
@@ -34,6 +32,7 @@ private:
|
|||||||
int CreateMessage(MessageType type, std::string message, char* data);
|
int CreateMessage(MessageType type, std::string message, char* data);
|
||||||
void Connect();
|
void Connect();
|
||||||
void Disconnect();
|
void Disconnect();
|
||||||
|
void Ping();
|
||||||
void MoveMessageHead(char*& data, size_t& length, size_t stepSize);
|
void MoveMessageHead(char*& data, size_t& length, size_t stepSize);
|
||||||
void ParseMessageType(char* data, size_t length);
|
void ParseMessageType(char* data, size_t length);
|
||||||
void ParseEventMessage(char* data, size_t length);
|
void ParseEventMessage(char* data, size_t length);
|
||||||
@@ -50,13 +49,11 @@ private:
|
|||||||
|
|
||||||
World* m_World;
|
World* m_World;
|
||||||
int m_PlayerID = -1;
|
int m_PlayerID = -1;
|
||||||
char m_GameBoard[BOARDSIZE][BOARDSIZE];
|
|
||||||
glm::vec2 m_PlayerPositions[MAXCONNECTIONS];
|
glm::vec2 m_PlayerPositions[MAXCONNECTIONS];
|
||||||
//std::string m_PlayerNames[MAXCONNECTIONS];
|
//std::string m_PlayerNames[MAXCONNECTIONS];
|
||||||
PlayerDefinition m_PlayerDefinitions[MAXCONNECTIONS];
|
PlayerDefinition m_PlayerDefinitions[MAXCONNECTIONS];
|
||||||
std::clock_t m_StartPingTime;
|
std::clock_t m_StartPingTime;
|
||||||
double m_DurationOfPingTime;
|
double m_DurationOfPingTime;
|
||||||
bool m_ShouldDrawGameBoard = true;
|
|
||||||
std::string m_PlayerName;
|
std::string m_PlayerName;
|
||||||
bool m_ThreadIsRunning = true;
|
bool m_ThreadIsRunning = true;
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
struct PlayerDefinition {
|
struct PlayerDefinition {
|
||||||
unsigned int EntityID = -1;
|
int EntityID = -1;
|
||||||
std::string Name = "";
|
std::string Name = "";
|
||||||
boost::asio::ip::udp::endpoint Endpoint;
|
boost::asio::ip::udp::endpoint Endpoint;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -5,12 +5,8 @@ using namespace boost::asio::ip;
|
|||||||
|
|
||||||
Client::Client() : m_Socket(m_IOService)
|
Client::Client() : m_Socket(m_IOService)
|
||||||
{
|
{
|
||||||
//m_EventBroker = eventBroker;
|
|
||||||
// Set up network stream
|
// Set up network stream
|
||||||
m_ReceiverEndpoint = udp::endpoint(boost::asio::ip::address::from_string("192.168.1.6"), 13);
|
m_ReceiverEndpoint = udp::endpoint(boost::asio::ip::address::from_string("192.168.1.6"), 13);
|
||||||
|
|
||||||
|
|
||||||
//EVENT_SUBSCRIBE_MEMBER(m_EKeyDown, &Client::OnKeyDown);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Client::~Client()
|
Client::~Client()
|
||||||
@@ -32,15 +28,10 @@ void Client::Start(World* world, EventBroker* eventBroker)
|
|||||||
std::cout << "Please enter you name(No longer than 7 characters): ";
|
std::cout << "Please enter you name(No longer than 7 characters): ";
|
||||||
std::cin >> m_PlayerName;
|
std::cin >> m_PlayerName;
|
||||||
}
|
}
|
||||||
|
|
||||||
boost::thread_group threads;
|
|
||||||
|
|
||||||
m_Socket.connect(m_ReceiverEndpoint);
|
m_Socket.connect(m_ReceiverEndpoint);
|
||||||
std::cout << "I am client. BIP BOP\n";
|
std::cout << "I am client. BIP BOP\n";
|
||||||
|
|
||||||
threads.create_thread(boost::bind(&Client::ReadFromServer, this));
|
ReadFromServer();
|
||||||
|
|
||||||
threads.join_all();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Client::Close()
|
void Client::Close()
|
||||||
@@ -157,7 +148,6 @@ void Client::ParseSnapshot(char* data, size_t length)
|
|||||||
tempName = std::string(data);
|
tempName = std::string(data);
|
||||||
// +1 for null terminator
|
// +1 for null terminator
|
||||||
MoveMessageHead(data, length, tempName.size() + 1);
|
MoveMessageHead(data, length, tempName.size() + 1);
|
||||||
|
|
||||||
// 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
|
||||||
if (m_PlayerDefinitions[i].Name == "" && tempName != "") {
|
if (m_PlayerDefinitions[i].Name == "" && tempName != "") {
|
||||||
@@ -225,6 +215,21 @@ void Client::Disconnect()
|
|||||||
delete[] dataPackage;
|
delete[] dataPackage;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Client::Ping()
|
||||||
|
{
|
||||||
|
char* dataPackage = new char[INPUTSIZE];
|
||||||
|
if (GetAsyncKeyState('P')) { // Maybe use previous key here
|
||||||
|
int length = CreateMessage(MessageType::ClientPing, "Ping", dataPackage);
|
||||||
|
m_StartPingTime = std::clock();
|
||||||
|
m_Socket.send_to(boost::asio::buffer(
|
||||||
|
dataPackage,
|
||||||
|
length),
|
||||||
|
m_ReceiverEndpoint, 0);
|
||||||
|
}
|
||||||
|
memset(dataPackage, 0, INPUTSIZE);
|
||||||
|
delete[] dataPackage;
|
||||||
|
}
|
||||||
|
|
||||||
void Client::MoveMessageHead(char*& data, size_t& length, size_t stepSize)
|
void Client::MoveMessageHead(char*& data, size_t& length, size_t stepSize)
|
||||||
{
|
{
|
||||||
data += stepSize;
|
data += stepSize;
|
||||||
@@ -268,7 +273,9 @@ bool Client::OnKeyDown(const Events::KeyDown& event)
|
|||||||
if (event.KeyCode == GLFW_KEY_C) {
|
if (event.KeyCode == GLFW_KEY_C) {
|
||||||
Connect();
|
Connect();
|
||||||
}
|
}
|
||||||
|
if (event.KeyCode == GLFW_KEY_P) {
|
||||||
|
Ping();
|
||||||
|
}
|
||||||
memset(dataPackage, 0, INPUTSIZE);
|
memset(dataPackage, 0, INPUTSIZE);
|
||||||
delete[] dataPackage;
|
delete[] dataPackage;
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@@ -205,7 +205,10 @@ void Server::SendSnapshot()
|
|||||||
{
|
{
|
||||||
char* data = new char[INPUTSIZE];
|
char* data = new char[INPUTSIZE];
|
||||||
int offset = CreateHeader(MessageType::Snapshot, data);
|
int offset = CreateHeader(MessageType::Snapshot, data);
|
||||||
for (size_t i = 0; i < MAXCONNECTIONS; i++) {
|
for (size_t i = 0; i < MAXCONNECTIONS; i++) {
|
||||||
|
if (m_PlayerDefinitions[i].EntityID == -1) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
// Pack player pos into data package
|
// Pack player pos into data package
|
||||||
glm::vec3 playerPos = m_World->GetComponent(m_PlayerDefinitions[i].EntityID, "Transform")["Position"];
|
glm::vec3 playerPos = m_World->GetComponent(m_PlayerDefinitions[i].EntityID, "Transform")["Position"];
|
||||||
memcpy(data + offset, &playerPos.x, sizeof(float));
|
memcpy(data + offset, &playerPos.x, sizeof(float));
|
||||||
|
|||||||
Reference in New Issue
Block a user