Fixed the bug where starting a server made the program crash on closing.
Fixed some memory leaks in Client.cpp.
This commit is contained in:
@@ -19,6 +19,7 @@ public:
|
|||||||
Server();
|
Server();
|
||||||
~Server();
|
~Server();
|
||||||
void Start(World* m_world);
|
void Start(World* m_world);
|
||||||
|
void Close();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// udp stuff
|
// udp stuff
|
||||||
@@ -30,15 +31,16 @@ private:
|
|||||||
std::clock_t m_StartPingTime;
|
std::clock_t m_StartPingTime;
|
||||||
std::clock_t m_StopTimes[8];
|
std::clock_t m_StopTimes[8];
|
||||||
// Game logic
|
// Game logic
|
||||||
|
|
||||||
World* m_World;
|
World* m_World;
|
||||||
|
// Close logic
|
||||||
|
bool m_ThreadIsRunning = true;
|
||||||
// Threaded
|
// Threaded
|
||||||
void DisplayLoop();
|
void DisplayLoop();
|
||||||
void ReadFromClients();
|
void ReadFromClients();
|
||||||
void InputLoop();
|
void InputLoop();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
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);
|
||||||
void MoveMessageHead(char*& data, size_t& length, size_t stepSize);
|
void MoveMessageHead(char*& data, size_t& length, size_t stepSize);
|
||||||
|
|||||||
@@ -44,6 +44,7 @@ void Client::Close()
|
|||||||
Disconnect();
|
Disconnect();
|
||||||
m_ThreadIsRunning = false;
|
m_ThreadIsRunning = false;
|
||||||
m_EventBroker->Unsubscribe(m_EKeyDown);
|
m_EventBroker->Unsubscribe(m_EKeyDown);
|
||||||
|
m_EventBroker->Unsubscribe(m_EKeyUp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -78,6 +79,7 @@ void Client::SendToServer()
|
|||||||
dataPackage,
|
dataPackage,
|
||||||
len),
|
len),
|
||||||
m_ReceiverEndpoint, 0);
|
m_ReceiverEndpoint, 0);
|
||||||
|
delete[] dataPackage;
|
||||||
}
|
}
|
||||||
if (m_NextSnapshot.inputRight != "") {
|
if (m_NextSnapshot.inputRight != "") {
|
||||||
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
|
||||||
@@ -86,6 +88,7 @@ void Client::SendToServer()
|
|||||||
dataPackage,
|
dataPackage,
|
||||||
len),
|
len),
|
||||||
m_ReceiverEndpoint, 0);
|
m_ReceiverEndpoint, 0);
|
||||||
|
delete[] dataPackage;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -26,14 +26,14 @@ void Server::Start(World* world)
|
|||||||
threads.join_all();
|
threads.join_all();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Server::Close()
|
||||||
|
{
|
||||||
|
m_ThreadIsRunning = false;
|
||||||
|
}
|
||||||
|
|
||||||
void Server::DisplayLoop()
|
void Server::DisplayLoop()
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
for (;;) {
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Server::ReadFromClients()
|
void Server::ReadFromClients()
|
||||||
@@ -49,8 +49,11 @@ void Server::ReadFromClients()
|
|||||||
int snapshotInterval = 50;
|
int snapshotInterval = 50;
|
||||||
int timeToCheckTimeOutTime = 100;
|
int timeToCheckTimeOutTime = 100;
|
||||||
|
|
||||||
for (;;) {
|
while(m_ThreadIsRunning) {
|
||||||
if (m_Socket.available()) {
|
// m_ThreadIsRunning might be unnecessary but the
|
||||||
|
// program crashed if it executed m_Socket.available()
|
||||||
|
// when closing the program.
|
||||||
|
if (m_ThreadIsRunning && m_Socket.available()) {
|
||||||
try {
|
try {
|
||||||
bytesRead = Receive(readBuf, INPUTSIZE);
|
bytesRead = Receive(readBuf, INPUTSIZE);
|
||||||
ParseMessageType(readBuf, bytesRead);
|
ParseMessageType(readBuf, bytesRead);
|
||||||
@@ -89,7 +92,7 @@ void Server::InputLoop()
|
|||||||
char inputBuffer[INPUTSIZE] = { 0 };
|
char inputBuffer[INPUTSIZE] = { 0 };
|
||||||
std::string inputMessage;
|
std::string inputMessage;
|
||||||
|
|
||||||
for (;;) {
|
while (m_ThreadIsRunning) {
|
||||||
std::cin.getline(inputBuffer, INPUTSIZE);
|
std::cin.getline(inputBuffer, INPUTSIZE);
|
||||||
inputMessage = (std::string)inputBuffer;
|
inputMessage = (std::string)inputBuffer;
|
||||||
|
|
||||||
@@ -283,8 +286,6 @@ void Server::Disconnect(int i)
|
|||||||
m_PlayerDefinitions[i].Endpoint = boost::asio::ip::udp::endpoint();
|
m_PlayerDefinitions[i].Endpoint = boost::asio::ip::udp::endpoint();
|
||||||
m_PlayerDefinitions[i].EntityID = -1;
|
m_PlayerDefinitions[i].EntityID = -1;
|
||||||
m_PlayerDefinitions[i].Name = "";
|
m_PlayerDefinitions[i].Name = "";
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Server::ParseEvent(char * data, size_t length)
|
void Server::ParseEvent(char * data, size_t length)
|
||||||
|
|||||||
@@ -49,6 +49,7 @@ Game::~Game()
|
|||||||
{
|
{
|
||||||
// Call before to ensure that thread closes correctly.
|
// Call before to ensure that thread closes correctly.
|
||||||
m_Client.Close();
|
m_Client.Close();
|
||||||
|
m_Server.Close();
|
||||||
|
|
||||||
delete m_FrameStack;
|
delete m_FrameStack;
|
||||||
delete m_EventBroker;
|
delete m_EventBroker;
|
||||||
|
|||||||
Reference in New Issue
Block a user