Fixed bug that cause the program to crash when closing if a server had been started.
Removed memory leaks in Client.cpp.
This commit is contained in:
@@ -8,12 +8,14 @@
|
|||||||
#include <GLFW/glfw3.h> // For input event
|
#include <GLFW/glfw3.h> // For input event
|
||||||
|
|
||||||
#include "Network/MessageType.h"
|
#include "Network/MessageType.h"
|
||||||
#include "Network/NetworkDefines.h"
|
#include "Network/NetworkDefinitions.h"
|
||||||
#include "Network/PlayerDefinition.h"
|
#include "Network/PlayerDefinition.h"
|
||||||
|
#include "Network/SnapshotDefinitions.h"
|
||||||
#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/EKeyDown.h"
|
||||||
|
#include "Core/EKeyUp.h"
|
||||||
|
|
||||||
|
|
||||||
class Client
|
class Client
|
||||||
@@ -24,8 +26,8 @@ public:
|
|||||||
void Start(World* world, EventBroker* eventBroker);
|
void Start(World* world, EventBroker* eventBroker);
|
||||||
void Close();
|
void Close();
|
||||||
private:
|
private:
|
||||||
// Threaded
|
|
||||||
void ReadFromServer();
|
void ReadFromServer();
|
||||||
|
void SendToServer();
|
||||||
|
|
||||||
int Receive(char* data, size_t length);
|
int Receive(char* data, size_t length);
|
||||||
int CreateMessage(MessageType type, std::string message, char* data);
|
int CreateMessage(MessageType type, std::string message, char* data);
|
||||||
@@ -50,6 +52,7 @@ private:
|
|||||||
int m_PlayerID = -1;
|
int m_PlayerID = -1;
|
||||||
glm::vec2 m_PlayerPositions[MAXCONNECTIONS];
|
glm::vec2 m_PlayerPositions[MAXCONNECTIONS];
|
||||||
PlayerDefinition m_PlayerDefinitions[MAXCONNECTIONS];
|
PlayerDefinition m_PlayerDefinitions[MAXCONNECTIONS];
|
||||||
|
SnapshotDefinitions m_NextSnapshot;
|
||||||
std::clock_t m_StartPingTime;
|
std::clock_t m_StartPingTime;
|
||||||
double m_DurationOfPingTime;
|
double m_DurationOfPingTime;
|
||||||
std::string m_PlayerName;
|
std::string m_PlayerName;
|
||||||
@@ -61,7 +64,9 @@ private:
|
|||||||
// Events
|
// Events
|
||||||
EventBroker* m_EventBroker;
|
EventBroker* m_EventBroker;
|
||||||
EventRelay<Client, Events::KeyDown> m_EKeyDown;
|
EventRelay<Client, Events::KeyDown> m_EKeyDown;
|
||||||
bool OnKeyDown(const Events::KeyDown &e);
|
bool OnKeyDown(const Events::KeyDown &e);
|
||||||
|
EventRelay<Client, Events::KeyUp> m_EKeyUp;
|
||||||
|
bool OnKeyUp(const Events::KeyUp &e);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -1,15 +1,17 @@
|
|||||||
#ifndef Server_h__
|
#ifndef Server_h__
|
||||||
#define Server_h__
|
#define Server_h__
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
|
|
||||||
#include <boost/thread.hpp>
|
#include <boost/thread.hpp>
|
||||||
#include <boost/bind.hpp>
|
#include <boost/bind.hpp>
|
||||||
#include <glm/common.hpp>
|
#include <glm/common.hpp>
|
||||||
#include "NetworkDefines.h"
|
|
||||||
#include "MessageType.h"
|
#include "Network/MessageType.h"
|
||||||
#include "Core/World.h"
|
#include "Network/NetworkDefinitions.h"
|
||||||
#include "Network/PlayerDefinition.h"
|
#include "Network/PlayerDefinition.h"
|
||||||
|
#include "Core/World.h"
|
||||||
|
|
||||||
class Server
|
class Server
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -0,0 +1,12 @@
|
|||||||
|
#ifndef SnapshotDefinitions_h__
|
||||||
|
#define SnapshotDefinitions_h__
|
||||||
|
|
||||||
|
struct SnapshotDefinitions
|
||||||
|
{
|
||||||
|
// "+Forward" is 8 characters * sizeof(char) = 8
|
||||||
|
char* inputForward = new char[8];
|
||||||
|
// "+Right" is 6 characters * sizeof(char) = 6
|
||||||
|
char* inputRight = new char[6];
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -1,8 +1,16 @@
|
|||||||
[Debug]
|
[Debug]
|
||||||
|
|
||||||
LogLevel=1
|
LogLevel=1
|
||||||
|
|
||||||
|
|
||||||
[Video]
|
[Video]
|
||||||
|
|
||||||
Fullscreen=false
|
Fullscreen=false
|
||||||
|
|
||||||
VSYNC=false
|
VSYNC=false
|
||||||
|
|
||||||
Width=1280
|
Width=1280
|
||||||
Height=720
|
|
||||||
|
Height=720
|
||||||
|
[Networking]
|
||||||
|
StartNetwork=false
|
||||||
@@ -17,12 +17,16 @@ Client::~Client()
|
|||||||
|
|
||||||
void Client::Start(World* world, EventBroker* eventBroker)
|
void Client::Start(World* world, EventBroker* eventBroker)
|
||||||
{
|
{
|
||||||
// Subscribe to events
|
|
||||||
m_WasStarted = true;
|
m_WasStarted = true;
|
||||||
m_EventBroker = eventBroker;
|
m_EventBroker = eventBroker;
|
||||||
m_World = world;
|
m_World = world;
|
||||||
|
|
||||||
|
// Subscribe to events
|
||||||
m_EKeyDown = decltype(m_EKeyDown)(std::bind(&Client::OnKeyDown, this, std::placeholders::_1));
|
m_EKeyDown = decltype(m_EKeyDown)(std::bind(&Client::OnKeyDown, this, std::placeholders::_1));
|
||||||
m_EventBroker->Subscribe(m_EKeyDown);
|
m_EventBroker->Subscribe(m_EKeyDown);
|
||||||
|
m_EKeyUp = decltype(m_EKeyUp)(std::bind(&Client::OnKeyUp, this, std::placeholders::_1));
|
||||||
|
m_EventBroker->Subscribe(m_EKeyUp);
|
||||||
|
|
||||||
std::cout << "Please enter you name: ";
|
std::cout << "Please enter you name: ";
|
||||||
std::cin >> m_PlayerName;
|
std::cin >> m_PlayerName;
|
||||||
while (m_PlayerName.size() > 7) {
|
while (m_PlayerName.size() > 7) {
|
||||||
@@ -48,11 +52,40 @@ void Client::ReadFromServer()
|
|||||||
int bytesRead = -1;
|
int bytesRead = -1;
|
||||||
char readBuf[1024] = { 0 };
|
char readBuf[1024] = { 0 };
|
||||||
|
|
||||||
|
int snapshotInterval = 33;
|
||||||
|
std::clock_t previousSnapshotMessage = std::clock();
|
||||||
|
|
||||||
while (m_ThreadIsRunning) {
|
while (m_ThreadIsRunning) {
|
||||||
if (m_Socket.available()) {
|
if (m_Socket.available()) {
|
||||||
bytesRead = Receive(readBuf, INPUTSIZE);
|
bytesRead = Receive(readBuf, INPUTSIZE);
|
||||||
ParseMessageType(readBuf, bytesRead);
|
ParseMessageType(readBuf, bytesRead);
|
||||||
}
|
}
|
||||||
|
std::clock_t currentTime = std::clock();
|
||||||
|
if (snapshotInterval < (1000 * (currentTime - previousSnapshotMessage) / (double)CLOCKS_PER_SEC)) {
|
||||||
|
SendToServer();
|
||||||
|
previousSnapshotMessage = currentTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Client::SendToServer()
|
||||||
|
{
|
||||||
|
if (m_NextSnapshot.inputForward != "") {
|
||||||
|
char* dataPackage = new char[INPUTSIZE]; // The package that will be sent to the server, when filled
|
||||||
|
int len = CreateMessage(MessageType::Event, m_NextSnapshot.inputForward, dataPackage);
|
||||||
|
m_Socket.send_to(boost::asio::buffer(
|
||||||
|
dataPackage,
|
||||||
|
len),
|
||||||
|
m_ReceiverEndpoint, 0);
|
||||||
|
}
|
||||||
|
if (m_NextSnapshot.inputRight != "") {
|
||||||
|
char* dataPackage = new char[INPUTSIZE]; // The package that will be sent to the server, when filled
|
||||||
|
int len = CreateMessage(MessageType::Event, m_NextSnapshot.inputRight, dataPackage);
|
||||||
|
m_Socket.send_to(boost::asio::buffer(
|
||||||
|
dataPackage,
|
||||||
|
len),
|
||||||
|
m_ReceiverEndpoint, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -242,33 +275,18 @@ bool Client::OnKeyDown(const Events::KeyDown& event)
|
|||||||
{
|
{
|
||||||
char* dataPackage = new char[INPUTSIZE]; // The package that will be sent to the server, when filled
|
char* dataPackage = new char[INPUTSIZE]; // The package that will be sent to the server, when filled
|
||||||
if (event.KeyCode == GLFW_KEY_W) {
|
if (event.KeyCode == GLFW_KEY_W) {
|
||||||
int len = CreateMessage(MessageType::Event, "+Forward", dataPackage);
|
m_NextSnapshot.inputForward = "+Forward";
|
||||||
m_Socket.send_to(boost::asio::buffer(
|
|
||||||
dataPackage,
|
|
||||||
len),
|
|
||||||
m_ReceiverEndpoint, 0);
|
|
||||||
}
|
}
|
||||||
if (event.KeyCode == GLFW_KEY_A) {
|
if (event.KeyCode == GLFW_KEY_A) {
|
||||||
int len = CreateMessage(MessageType::Event, "-Right", dataPackage);
|
m_NextSnapshot.inputRight = "-Right";
|
||||||
m_Socket.send_to(boost::asio::buffer(
|
|
||||||
dataPackage,
|
|
||||||
len),
|
|
||||||
m_ReceiverEndpoint, 0);
|
|
||||||
}
|
}
|
||||||
if (event.KeyCode == GLFW_KEY_S) {
|
if (event.KeyCode == GLFW_KEY_S) {
|
||||||
int len = CreateMessage(MessageType::Event, "-Forward", dataPackage);
|
m_NextSnapshot.inputForward = "-Forward";
|
||||||
m_Socket.send_to(boost::asio::buffer(
|
|
||||||
dataPackage,
|
|
||||||
len),
|
|
||||||
m_ReceiverEndpoint, 0);
|
|
||||||
}
|
}
|
||||||
if (event.KeyCode == GLFW_KEY_D) {
|
if (event.KeyCode == GLFW_KEY_D) {
|
||||||
int len = CreateMessage(MessageType::Event, "+Right", dataPackage);
|
m_NextSnapshot.inputRight = "+Right";
|
||||||
m_Socket.send_to(boost::asio::buffer(
|
|
||||||
dataPackage,
|
|
||||||
len),
|
|
||||||
m_ReceiverEndpoint, 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (event.KeyCode == GLFW_KEY_V) {
|
if (event.KeyCode == GLFW_KEY_V) {
|
||||||
Disconnect();
|
Disconnect();
|
||||||
}
|
}
|
||||||
@@ -283,6 +301,19 @@ bool Client::OnKeyDown(const Events::KeyDown& event)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool Client::OnKeyUp(const Events::KeyUp & e)
|
||||||
|
{
|
||||||
|
if (e.KeyCode == GLFW_KEY_W || e.KeyCode == GLFW_KEY_S) {
|
||||||
|
m_NextSnapshot.inputForward = "";
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (e.KeyCode == GLFW_KEY_A || e.KeyCode == GLFW_KEY_D) {
|
||||||
|
m_NextSnapshot.inputRight = "";
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
void Client::CreateNewPlayer(int i)
|
void Client::CreateNewPlayer(int i)
|
||||||
{
|
{
|
||||||
m_PlayerDefinitions[i].EntityID = m_World->CreateEntity();
|
m_PlayerDefinitions[i].EntityID = m_World->CreateEntity();
|
||||||
|
|||||||
+4
-3
@@ -38,9 +38,10 @@ Game::Game(int argc, char* argv[])
|
|||||||
// Create a TEST WORLD
|
// Create a TEST WORLD
|
||||||
m_World = new HardcodedTestWorld();
|
m_World = new HardcodedTestWorld();
|
||||||
|
|
||||||
// TEMP: Invoke network
|
|
||||||
|
// Invoke network
|
||||||
boost::thread workerThread(&Game::NetworkFunction, this);
|
if(m_Config->Get<bool>("Networking.StartNetwork", false) == true)
|
||||||
|
boost::thread workerThread(&Game::NetworkFunction, this);
|
||||||
m_LastTime = glfwGetTime();
|
m_LastTime = glfwGetTime();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user