Use std atomic for server state

This commit is contained in:
Benau 2018-02-20 11:14:57 +08:00
parent 04a6f6d08b
commit 14b401b54e
2 changed files with 11 additions and 7 deletions

View File

@ -198,7 +198,7 @@ bool ServerLobby::notifyEventAsynchronous(Event* event)
*/
void ServerLobby::update(float dt)
{
switch (m_state)
switch (m_state.load())
{
case INIT_WAN:
// Start the protocol to find the public ip address.
@ -382,7 +382,8 @@ void ServerLobby::startSelection(const Event *event)
if (m_state != ACCEPTING_CLIENTS)
{
Log::warn("ServerLobby",
"Received startSelection while being in state %d", m_state);
"Received startSelection while being in state %d",
m_state.load());
return;
}
if(event && !event->getPeer()->isAuthorised())
@ -730,7 +731,7 @@ void ServerLobby::kartSelectionRequested(Event* event)
if(m_state!=SELECTING)
{
Log::warn("Server", "Received kart selection while in state %d.",
m_state);
m_state.load());
return;
}

View File

@ -5,14 +5,15 @@
#include "utils/cpp2011.hpp"
#include "utils/synchronised.hpp"
#include <atomic>
#include <set>
class ServerLobby : public LobbyProtocol
, public CallbackObject
{
private:
public:
/* The state for a small finite state machine. */
enum
enum ServerState : unsigned int
{
INIT_WAN, // Start state for WAN game
GETTING_PUBLIC_ADDRESS, // Waiting to receive its public ip address
@ -27,7 +28,9 @@ private:
RESULT_DISPLAY, // Show result screen
DONE, // shutting down server
EXITING
} m_state;
};
private:
std::atomic<ServerState> m_state;
/** Available karts and tracks for all clients, this will be initialized
* with data in server first. */
@ -94,7 +97,7 @@ public:
void checkIncomingConnectionRequests();
void checkRaceFinished();
void finishedLoadingWorld();
ServerState getCurrentState() const { return m_state.load(); }
virtual void callback(Protocol *protocol) OVERRIDE;
}; // class ServerLobby