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

View File

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