Don't lock in async update in protocol manager

It allows GUI interacts with protocol more actively

Also don't handle lan connection request if we are not waiting for
players
This commit is contained in:
Benau 2018-02-25 02:07:24 +08:00
parent b26b784b6a
commit 2a117d8e44
7 changed files with 39 additions and 13 deletions

View File

@ -505,9 +505,12 @@ void ProtocolManager::asynchronousUpdate()
// The lock is likely not necessary, since this function is only
// called from the ProtocolManager thread, and this thread is also
// the only one who changes the number of protocols.
opt.lock();
// Edit: remove this lock can avoid hanging the GUI when connecting
// to or creating server, but you need to make sure async and non-async
// update in each protocol will have atomic or mutex write
//opt.lock();
opt.update(0, /*async*/true); // dt does not matter, so set it to 0
opt.unlock();
//opt.unlock();
}
PROFILER_POP_CPU_MARKER();

View File

@ -81,6 +81,8 @@ public:
virtual void finishedLoadingWorld() OVERRIDE;
virtual void setup() OVERRIDE;
virtual void update(float dt) OVERRIDE;
virtual bool waitingForPlayers() const OVERRIDE
{ return m_state == LINKED; }
virtual void asynchronousUpdate() OVERRIDE {}
};

View File

@ -84,7 +84,7 @@ void ConnectToServer::setup()
// ----------------------------------------------------------------------------
void ConnectToServer::asynchronousUpdate()
{
switch(m_state)
switch(m_state.load())
{
case SET_PUBLIC_ADDRESS:
{
@ -210,19 +210,31 @@ void ConnectToServer::asynchronousUpdate()
return;
}
m_state = DONE;
break;
case DONE:
case EXITING:
break;
}
} // asynchronousUpdate
// ----------------------------------------------------------------------------
void ConnectToServer::update(float dt)
{
switch(m_state.load())
{
case DONE:
{
// lobby room protocol if we're connected only
if (STKHost::get()->getPeerCount() > 0 &&
STKHost::get()->getPeers()[0]->isConnected() &&
!m_server_address.isUnset())
{
// Let main thread create ClientLobby for better
// synchronization with GUI
auto cl = LobbyProtocol::create<ClientLobby>();
cl->setAddress(m_server_address);
cl->requestStart();
}
break;
case DONE:
requestTerminate();
m_state = EXITING;
if (STKHost::get()->getPeerCount() == 0)
{
// Shutdown STKHost (go back to online menu too)
@ -231,11 +243,14 @@ void ConnectToServer::asynchronousUpdate()
m_server_address.toString().c_str()));
STKHost::get()->requestShutdown();
}
break;
case EXITING:
requestTerminate();
m_state = EXITING;
break;
}
} // asynchronousUpdate
default:
break;
}
} // update
// ----------------------------------------------------------------------------
/** Register this client with the STK server.

View File

@ -22,6 +22,7 @@
#include "network/protocol.hpp"
#include "network/transport_address.hpp"
#include "utils/cpp2011.hpp"
#include <atomic>
#include <string>
class ConnectToServer : public Protocol
@ -38,7 +39,7 @@ private:
bool m_quick_join;
/** State for finite state machine. */
enum
enum ConnectState : unsigned int
{
SET_PUBLIC_ADDRESS,
REGISTER_SELF_ADDRESS,
@ -49,7 +50,8 @@ private:
HIDING_ADDRESS,
DONE,
EXITING
} m_state;
};
std::atomic<ConnectState> m_state;
void registerWithSTKServer();
void handleQuickConnect();
@ -63,7 +65,7 @@ public:
virtual bool notifyEventAsynchronous(Event* event) OVERRIDE;
virtual void setup() OVERRIDE;
virtual void asynchronousUpdate() OVERRIDE;
virtual void update(float dt) OVERRIDE {}
virtual void update(float dt) OVERRIDE;
}; // class ConnectToServer
#endif // CONNECT_TO_SERVER_HPP

View File

@ -100,6 +100,7 @@ public:
virtual void update(float dt) = 0;
virtual void finishedLoadingWorld() = 0;
virtual void loadWorld();
virtual bool waitingForPlayers() const = 0;
void terminateLatencyProtocol();
virtual void requestKartSelection(uint8_t player_id,
const std::string &kart_name)

View File

@ -99,6 +99,8 @@ public:
void checkRaceFinished();
void finishedLoadingWorld();
ServerState getCurrentState() const { return m_state.load(); }
virtual bool waitingForPlayers() const OVERRIDE
{ return m_state.load() == ACCEPTING_CLIENTS; }
}; // class ServerLobby

View File

@ -727,7 +727,8 @@ void STKHost::mainLoop()
while (m_exit_flag.test_and_set())
{
if (lan_network)
auto sl = LobbyProtocol::get<ServerLobby>();
if (lan_network && sl && sl->waitingForPlayers())
{
handleDirectSocketRequest(lan_network);
} // if discovery host