Fixed some crashes and added mutex around broadcast options on network server.

This commit is contained in:
Pontus Fransson 2014-02-23 22:07:33 +01:00
parent 203220f43b
commit 5fe89f1d8f
3 changed files with 35 additions and 23 deletions

View File

@ -194,7 +194,6 @@ const GameClientState::NetEvent& LanMenuState::DataRecieved( const NetEvent &mes
std::string ip = decoded.ip; std::string ip = decoded.ip;
std::string name = decoded.name; std::string name = decoded.name;
printf("Broadcast message: %d: %s: %s\n", port, ip.c_str(), name.c_str()); printf("Broadcast message: %d: %s: %s\n", port, ip.c_str(), name.c_str());
this->privData->connectIP->AppendText(L"Hej");
} }
break; break;

View File

@ -736,6 +736,8 @@ bool NetworkClient::StartListeningForBroadcasting(unsigned short port)
void NetworkClient::StopListeningForBroadcasting() void NetworkClient::StopListeningForBroadcasting()
{ {
if(this->privateData)
{
if(this->privateData->broadcastingStarted) if(this->privateData->broadcastingStarted)
{ {
//Tell the thread to shutdown //Tell the thread to shutdown
@ -751,4 +753,5 @@ void NetworkClient::StopListeningForBroadcasting()
this->privateData->broadcastingStarted = false; this->privateData->broadcastingStarted = false;
} }
}
} }

View File

@ -20,6 +20,7 @@
#include "Thread/OysterThread.h" #include "Thread/OysterThread.h"
#include "WinTimer.h" #include "WinTimer.h"
#include <mutex>
using namespace Oyster::Network; using namespace Oyster::Network;
using namespace Utility::DynamicMemory; using namespace Utility::DynamicMemory;
@ -83,6 +84,7 @@ public:
, port(-1) , port(-1)
, broadcast(0) , broadcast(0)
, broadcastTime(1.0f, 0.0f) , broadcastTime(1.0f, 0.0f)
, broadcastMutex(new std::mutex)
{ {
InitWinSock(); InitWinSock();
serverOptions.broadcastOptions.broadcastInterval = 1.0f; serverOptions.broadcastOptions.broadcastInterval = 1.0f;
@ -100,12 +102,6 @@ public:
} }
} }
void SendBroadcast()
{
broadcastConnection.Send(broadcastMessage);
}
bool DoWork(); bool DoWork();
public: public:
@ -123,6 +119,7 @@ public:
ServerOptions serverOptions; ServerOptions serverOptions;
SmartPointer<std::mutex> broadcastMutex;
ConnectionUDP broadcastConnection; ConnectionUDP broadcastConnection;
OysterByte broadcastMessage; OysterByte broadcastMessage;
@ -135,15 +132,16 @@ bool NetworkServer::PrivateData::DoWork()
{ {
if(serverOptions.broadcastOptions.broadcast) if(serverOptions.broadcastOptions.broadcast)
{ {
if( (this->serverTimer.getElapsedSeconds() - this->broadcastTime.previous) >= this->broadcastTime.length ) if( (this->serverTimer.getElapsedSeconds() - this->broadcastTime.previous) >= this->serverOptions.broadcastOptions.broadcastInterval )
{ {
broadcastMessage.Clear(); //broadcastMessage.Clear();
Translator t; //Translator t;
t.Pack(broadcastMessage, serverOptions.broadcastOptions.broadcastMessage); //t.Pack(broadcastMessage, serverOptions.broadcastOptions.broadcastMessage);
serverTimer.reset(); serverTimer.reset();
//Broadcast();
SendBroadcast();
broadcastMutex->lock();
broadcastConnection.Send(broadcastMessage);
broadcastMutex->unlock();
} }
} }
@ -353,29 +351,41 @@ int NetworkServer::GetPort()
//Set broadcast settings. //Set broadcast settings.
void NetworkServer::SetBroadcast(CustomNetProtocol& broadcastMessage, float interval, bool enable) void NetworkServer::SetBroadcast(CustomNetProtocol& broadcastMessage, float interval, bool enable)
{ {
this->privateData->broadcastMutex->lock();
this->privateData->serverOptions.broadcastOptions.broadcast = enable; this->privateData->serverOptions.broadcastOptions.broadcast = enable;
this->privateData->serverOptions.broadcastOptions.broadcastMessage = broadcastMessage; this->privateData->serverOptions.broadcastOptions.broadcastMessage = broadcastMessage;
this->privateData->serverOptions.broadcastOptions.broadcastInterval = interval; this->privateData->serverOptions.broadcastOptions.broadcastInterval = interval;
this->privateData->broadcastMessage.Clear();
Translator t; Translator t;
t.Pack(this->privateData->broadcastMessage, broadcastMessage); t.Pack(this->privateData->broadcastMessage, broadcastMessage);
this->privateData->broadcastMutex->unlock();
} }
//Set broadcast settings. //Set broadcast settings.
void NetworkServer::SetBroadcastMessage(CustomNetProtocol& broadcastMessage) void NetworkServer::SetBroadcastMessage(CustomNetProtocol& broadcastMessage)
{ {
this->privateData->broadcastMutex->lock();
this->privateData->serverOptions.broadcastOptions.broadcastMessage = broadcastMessage; this->privateData->serverOptions.broadcastOptions.broadcastMessage = broadcastMessage;
this->privateData->broadcastMessage.Clear();
Translator t; Translator t;
t.Pack(this->privateData->broadcastMessage, broadcastMessage); t.Pack(this->privateData->broadcastMessage, broadcastMessage);
this->privateData->broadcastMutex->unlock();
} }
//Enable/disable broadcast. //Enable/disable broadcast.
void NetworkServer::SetBroadcast(bool enable) void NetworkServer::SetBroadcast(bool enable)
{ {
this->privateData->broadcastMutex->lock();
this->privateData->serverOptions.broadcastOptions.broadcast = enable; this->privateData->serverOptions.broadcastOptions.broadcast = enable;
this->privateData->broadcastMutex->unlock();
} }
//Set interval between each broadcast message in seconds. //Set interval between each broadcast message in seconds.
void NetworkServer::SetBroadcastInterval(float interval) void NetworkServer::SetBroadcastInterval(float interval)
{ {
this->privateData->broadcastMutex->lock();
this->privateData->serverOptions.broadcastOptions.broadcastInterval = interval; this->privateData->serverOptions.broadcastOptions.broadcastInterval = interval;
this->privateData->broadcastMutex->unlock();
} }