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 <ctime>
|
||||
|
||||
#include <boost/thread.hpp>
|
||||
#include <boost/bind.hpp>
|
||||
#include <glm/common.hpp>
|
||||
#include <GLFW/glfw3.h> // For input event
|
||||
|
||||
@@ -34,6 +32,7 @@ private:
|
||||
int CreateMessage(MessageType type, std::string message, char* data);
|
||||
void Connect();
|
||||
void Disconnect();
|
||||
void Ping();
|
||||
void MoveMessageHead(char*& data, size_t& length, size_t stepSize);
|
||||
void ParseMessageType(char* data, size_t length);
|
||||
void ParseEventMessage(char* data, size_t length);
|
||||
@@ -50,13 +49,11 @@ private:
|
||||
|
||||
World* m_World;
|
||||
int m_PlayerID = -1;
|
||||
char m_GameBoard[BOARDSIZE][BOARDSIZE];
|
||||
glm::vec2 m_PlayerPositions[MAXCONNECTIONS];
|
||||
//std::string m_PlayerNames[MAXCONNECTIONS];
|
||||
PlayerDefinition m_PlayerDefinitions[MAXCONNECTIONS];
|
||||
std::clock_t m_StartPingTime;
|
||||
double m_DurationOfPingTime;
|
||||
bool m_ShouldDrawGameBoard = true;
|
||||
std::string m_PlayerName;
|
||||
bool m_ThreadIsRunning = true;
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
#include <string>
|
||||
|
||||
struct PlayerDefinition {
|
||||
unsigned int EntityID = -1;
|
||||
int EntityID = -1;
|
||||
std::string Name = "";
|
||||
boost::asio::ip::udp::endpoint Endpoint;
|
||||
};
|
||||
|
||||
@@ -5,12 +5,8 @@ using namespace boost::asio::ip;
|
||||
|
||||
Client::Client() : m_Socket(m_IOService)
|
||||
{
|
||||
//m_EventBroker = eventBroker;
|
||||
// Set up network stream
|
||||
m_ReceiverEndpoint = udp::endpoint(boost::asio::ip::address::from_string("192.168.1.6"), 13);
|
||||
|
||||
|
||||
//EVENT_SUBSCRIBE_MEMBER(m_EKeyDown, &Client::OnKeyDown);
|
||||
}
|
||||
|
||||
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::cin >> m_PlayerName;
|
||||
}
|
||||
|
||||
boost::thread_group threads;
|
||||
|
||||
m_Socket.connect(m_ReceiverEndpoint);
|
||||
std::cout << "I am client. BIP BOP\n";
|
||||
|
||||
threads.create_thread(boost::bind(&Client::ReadFromServer, this));
|
||||
|
||||
threads.join_all();
|
||||
ReadFromServer();
|
||||
}
|
||||
|
||||
void Client::Close()
|
||||
@@ -157,7 +148,6 @@ void Client::ParseSnapshot(char* data, size_t length)
|
||||
tempName = std::string(data);
|
||||
// +1 for null terminator
|
||||
MoveMessageHead(data, length, tempName.size() + 1);
|
||||
|
||||
// Apply the position data read to the player entity
|
||||
// New player connected on the server side
|
||||
if (m_PlayerDefinitions[i].Name == "" && tempName != "") {
|
||||
@@ -225,6 +215,21 @@ void Client::Disconnect()
|
||||
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)
|
||||
{
|
||||
data += stepSize;
|
||||
@@ -268,7 +273,9 @@ bool Client::OnKeyDown(const Events::KeyDown& event)
|
||||
if (event.KeyCode == GLFW_KEY_C) {
|
||||
Connect();
|
||||
}
|
||||
|
||||
if (event.KeyCode == GLFW_KEY_P) {
|
||||
Ping();
|
||||
}
|
||||
memset(dataPackage, 0, INPUTSIZE);
|
||||
delete[] dataPackage;
|
||||
return true;
|
||||
|
||||
@@ -205,7 +205,10 @@ void Server::SendSnapshot()
|
||||
{
|
||||
char* data = new char[INPUTSIZE];
|
||||
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
|
||||
glm::vec3 playerPos = m_World->GetComponent(m_PlayerDefinitions[i].EntityID, "Transform")["Position"];
|
||||
memcpy(data + offset, &playerPos.x, sizeof(float));
|
||||
|
||||
Reference in New Issue
Block a user