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();
|
||||
void Start(World* m_world);
|
||||
void Close();
|
||||
|
||||
private:
|
||||
// udp stuff
|
||||
@@ -30,15 +31,16 @@ private:
|
||||
std::clock_t m_StartPingTime;
|
||||
std::clock_t m_StopTimes[8];
|
||||
// Game logic
|
||||
|
||||
World* m_World;
|
||||
|
||||
// Close logic
|
||||
bool m_ThreadIsRunning = true;
|
||||
// Threaded
|
||||
void DisplayLoop();
|
||||
void ReadFromClients();
|
||||
void InputLoop();
|
||||
|
||||
|
||||
|
||||
int Receive(char* data, size_t length);
|
||||
int CreateMessage(MessageType type, std::string message, char * data);
|
||||
void MoveMessageHead(char*& data, size_t& length, size_t stepSize);
|
||||
|
||||
@@ -44,6 +44,7 @@ void Client::Close()
|
||||
Disconnect();
|
||||
m_ThreadIsRunning = false;
|
||||
m_EventBroker->Unsubscribe(m_EKeyDown);
|
||||
m_EventBroker->Unsubscribe(m_EKeyUp);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -78,6 +79,7 @@ void Client::SendToServer()
|
||||
dataPackage,
|
||||
len),
|
||||
m_ReceiverEndpoint, 0);
|
||||
delete[] dataPackage;
|
||||
}
|
||||
if (m_NextSnapshot.inputRight != "") {
|
||||
char* dataPackage = new char[INPUTSIZE]; // The package that will be sent to the server, when filled
|
||||
@@ -86,6 +88,7 @@ void Client::SendToServer()
|
||||
dataPackage,
|
||||
len),
|
||||
m_ReceiverEndpoint, 0);
|
||||
delete[] dataPackage;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -26,14 +26,14 @@ void Server::Start(World* world)
|
||||
threads.join_all();
|
||||
}
|
||||
|
||||
void Server::Close()
|
||||
{
|
||||
m_ThreadIsRunning = false;
|
||||
}
|
||||
|
||||
void Server::DisplayLoop()
|
||||
{
|
||||
|
||||
|
||||
for (;;) {
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
void Server::ReadFromClients()
|
||||
@@ -49,8 +49,11 @@ void Server::ReadFromClients()
|
||||
int snapshotInterval = 50;
|
||||
int timeToCheckTimeOutTime = 100;
|
||||
|
||||
for (;;) {
|
||||
if (m_Socket.available()) {
|
||||
while(m_ThreadIsRunning) {
|
||||
// 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 {
|
||||
bytesRead = Receive(readBuf, INPUTSIZE);
|
||||
ParseMessageType(readBuf, bytesRead);
|
||||
@@ -89,7 +92,7 @@ void Server::InputLoop()
|
||||
char inputBuffer[INPUTSIZE] = { 0 };
|
||||
std::string inputMessage;
|
||||
|
||||
for (;;) {
|
||||
while (m_ThreadIsRunning) {
|
||||
std::cin.getline(inputBuffer, INPUTSIZE);
|
||||
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].EntityID = -1;
|
||||
m_PlayerDefinitions[i].Name = "";
|
||||
|
||||
|
||||
}
|
||||
|
||||
void Server::ParseEvent(char * data, size_t length)
|
||||
|
||||
@@ -49,6 +49,7 @@ Game::~Game()
|
||||
{
|
||||
// Call before to ensure that thread closes correctly.
|
||||
m_Client.Close();
|
||||
m_Server.Close();
|
||||
|
||||
delete m_FrameStack;
|
||||
delete m_EventBroker;
|
||||
|
||||
Reference in New Issue
Block a user