...
 
Commits (2)
......@@ -29,6 +29,11 @@ BroadcastClient::~BroadcastClient()
}
enet_socket_destroy(dataSocket);
discoverSocket = ENET_SOCKET_NULL;
dataSocket = ENET_SOCKET_NULL;
networkThread.join();
}
ENetSocket BroadcastClient::initSocket(enet_uint16 port)
......@@ -96,8 +101,7 @@ bool BroadcastClient::start()
return false;
}
networkThread.reset(new thread(&BroadcastClient::networkLoop, this));
networkThread->detach();
networkThread = thread(&BroadcastClient::networkLoop, this);
started = true;
return true;
......@@ -165,9 +169,12 @@ void BroadcastClient::networkLoop()
{
while(true)
{
if(discoverSocket == ENET_SOCKET_NULL || dataSocket == ENET_SOCKET_NULL)
return;
std::this_thread::sleep_for(std::chrono::milliseconds(BROADCAST_CLIENT_THREAD_SLEEP));
if(paused || discoverSocket == ENET_SOCKET_NULL || dataSocket == ENET_SOCKET_NULL)
if(paused)
continue;
ENetAddress address;
......
......@@ -67,7 +67,7 @@ public:
private:
bool started = false;
std::unique_ptr<std::thread> networkThread;
std::thread networkThread;
ENetSocket discoverSocket = ENET_SOCKET_NULL;
ENetSocket dataSocket = ENET_SOCKET_NULL;
......
......@@ -31,7 +31,12 @@ BroadcastServer::~BroadcastServer()
}
enet_socket_destroy(dataSocket);
discoverSocket = ENET_SOCKET_NULL;
dataSocket = ENET_SOCKET_NULL;
discoverThread.join();
dataThread.join();
}
bool BroadcastServer::start()
......@@ -76,13 +81,9 @@ bool BroadcastServer::start()
return false;
}
discoverThread.reset(new thread(&BroadcastServer::discoverLoop, this));
discoverThread->detach();
dataThread.reset(new thread(&BroadcastServer::dataLoop, this));
dataThread->detach();
discoverThread = thread(&BroadcastServer::discoverLoop, this);
dataThread = thread(&BroadcastServer::dataLoop, this);
started = true;
return true;
}
......@@ -122,9 +123,12 @@ void BroadcastServer::dataLoop()
{
while(true)
{
if(dataSocket == ENET_SOCKET_NULL)
return;
std::this_thread::sleep_for(std::chrono::milliseconds(BROADCAST_SERVER_THREAD_SLEEP));
if(dataSocket == ENET_SOCKET_NULL)
if(paused)
continue;
ENetSocketSet set;
......@@ -170,10 +174,18 @@ void BroadcastServer::discoverLoop()
{
while(true)
{
std::this_thread::sleep_for(std::chrono::milliseconds(discoverTimeout - DISCOVER_SELECT_TIMEOUT));
if(discoverSocket == ENET_SOCKET_NULL)
return;
if(paused || discoverSocket == ENET_SOCKET_NULL)
if(paused)
{
std::this_thread::sleep_for(std::chrono::milliseconds(BROADCAST_SERVER_THREAD_SLEEP));
continue;
}
else
{
std::this_thread::sleep_for(std::chrono::milliseconds(discoverTimeout - DISCOVER_SELECT_TIMEOUT));
}
cout << "BroadcastServer: starting discover..." << endl;
......
......@@ -106,8 +106,8 @@ private:
long long discoverTimeout = DISCOVER_DEFAULT_TIMEOUT;
DiscoverCallback discoverCallback;
std::unique_ptr<std::thread> discoverThread;
std::unique_ptr<std::thread> dataThread;
std::thread discoverThread;
std::thread dataThread;
std::mutex dicoverMutex;
ENetSocket discoverSocket = ENET_SOCKET_NULL;
......
......@@ -12,9 +12,6 @@ using namespace std;
GameClient::~GameClient()
{
if(networkThread)
networkThread.reset(nullptr);
peer = nullptr;
if(host != nullptr)
......@@ -23,6 +20,8 @@ GameClient::~GameClient()
host = nullptr;
connected = false;
networkThread.join();
}
bool GameClient::connect(enet_uint32 hostAddress, enet_uint16 port)
......@@ -66,8 +65,7 @@ bool GameClient::connect(enet_uint32 hostAddress, enet_uint16 port)
outgoingPacketQueue = std::queue<std::string>();
networkThread = unique_ptr<thread>(new thread(&GameClient::networkLoop, this));
networkThread->detach();
networkThread = thread(&GameClient::networkLoop, this);
return true;
}
......@@ -90,9 +88,7 @@ void GameClient::networkLoop()
while(true)
{
if(!host || !peer)
{
return;
}
outgoingPacketMutex.lock();
if(!outgoingPacketQueue.empty())
......
......@@ -61,7 +61,7 @@ public:
private:
std::unique_ptr<std::thread> networkThread;
std::thread networkThread;
std::mutex outgoingPacketMutex;
std::queue<std::string> outgoingPacketQueue;
......
......@@ -48,11 +48,8 @@ bool GameServer::start(enet_uint16 port)
return false;
}
eventThread = unique_ptr<thread>(new thread(&GameServer::networkLoop, this));
handleThread = unique_ptr<thread>(new thread(&GameServer::packetHandler, this));
eventThread->detach();
handleThread->detach();
eventThread = thread(&GameServer::networkLoop, this);
handleThread = thread(&GameServer::packetHandler, this);
cout << "GameServer: started" << endl;
......@@ -62,9 +59,6 @@ bool GameServer::start(enet_uint16 port)
void GameServer::stop()
{
eventThread.reset();
handleThread.reset();
incomingPacketQueue = queue<Packet>();
outgoingPacketQueue = queue<Packet>();
broadcastPacketQueue = queue<string>();
......@@ -75,6 +69,12 @@ void GameServer::stop()
host = nullptr;
started = false;
eventThread.join();
handleThread.join();
eventThread = thread();
handleThread = thread();
}
void GameServer::sendPacketToClient(unsigned int clientID, const std::string &packet)
......@@ -141,7 +141,7 @@ void GameServer::packetHandler()
this_thread::sleep_for(chrono::milliseconds(MESSAGE_THREAD_SLEEP));
if(host == nullptr)
continue;
return;
Packet packet;
bool hasPacket = false;
......@@ -170,7 +170,7 @@ void GameServer::networkLoop()
while(true)
{
if(host == nullptr)
continue;
return;
broadcastPacketMutex.lock();
if(!broadcastPacketQueue.empty())
......
......@@ -61,8 +61,8 @@ protected:
ENetHost* host = nullptr;
ENetAddress address;
std::unique_ptr<std::thread> eventThread;
std::unique_ptr<std::thread> handleThread;
std::thread eventThread;
std::thread handleThread;
std::map<unsigned int, std::shared_ptr<GameServerClient>> clients;
......
......@@ -33,7 +33,7 @@ public:
return ip;
}
const enet_uint32 &getHost() const{
const enet_uint32 getHost() const {
return peer == nullptr ? 0 : peer->address.host;
}
......