Fixed some crashes and added mutex around broadcast options on network server.
This commit is contained in:
parent
203220f43b
commit
5fe89f1d8f
|
@ -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;
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
Loading…
Reference in New Issue