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 name = decoded.name;
printf("Broadcast message: %d: %s: %s\n", port, ip.c_str(), name.c_str());
this->privData->connectIP->AppendText(L"Hej");
}
break;

View File

@ -735,6 +735,8 @@ bool NetworkClient::StartListeningForBroadcasting(unsigned short port)
}
void NetworkClient::StopListeningForBroadcasting()
{
if(this->privateData)
{
if(this->privateData->broadcastingStarted)
{
@ -752,3 +754,4 @@ void NetworkClient::StopListeningForBroadcasting()
this->privateData->broadcastingStarted = false;
}
}
}

View File

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