mirror of
https://github.com/vcmi/vcmi.git
synced 2024-11-28 08:48:48 +02:00
Replace thread approach with callback based
This commit is contained in:
parent
ef7008a753
commit
70d04ad957
@ -140,6 +140,7 @@ void CServerHandler::resetStateForLobby(const StartInfo::EMode mode, const std::
|
||||
{
|
||||
hostClientId = -1;
|
||||
state = EClientState::NONE;
|
||||
mapToStart = nullptr;
|
||||
th = std::make_unique<CStopWatch>();
|
||||
packsForLobbyScreen.clear();
|
||||
c.reset();
|
||||
@ -396,6 +397,7 @@ void CServerHandler::sendClientDisconnecting()
|
||||
return;
|
||||
|
||||
state = EClientState::DISCONNECTING;
|
||||
mapToStart = nullptr;
|
||||
LobbyClientDisconnected lcd;
|
||||
lcd.clientId = c->connectionID;
|
||||
logNetwork->info("Connection has been requested to be closed.");
|
||||
@ -553,6 +555,11 @@ void CServerHandler::sendStartGame(bool allowOnlyAI) const
|
||||
c->disableStackSendingByID();
|
||||
}
|
||||
|
||||
void CServerHandler::startMapAfterConnection(std::shared_ptr<CMapInfo> to)
|
||||
{
|
||||
mapToStart = to;
|
||||
}
|
||||
|
||||
void CServerHandler::startGameplay(VCMI_LIB_WRAP_NAMESPACE(CGameState) * gameState)
|
||||
{
|
||||
if(CMM)
|
||||
|
@ -74,10 +74,14 @@ public:
|
||||
/// structure to handle running server and connecting to it
|
||||
class CServerHandler : public IServerAPI, public LobbyInfo
|
||||
{
|
||||
friend class ApplyOnLobbyHandlerNetPackVisitor;
|
||||
|
||||
std::shared_ptr<CApplier<CBaseForLobbyApply>> applier;
|
||||
|
||||
std::shared_ptr<boost::recursive_mutex> mx;
|
||||
std::list<CPackForLobby *> packsForLobbyScreen; //protected by mx
|
||||
|
||||
std::shared_ptr<CMapInfo> mapToStart;
|
||||
|
||||
std::vector<std::string> myNames;
|
||||
|
||||
@ -148,6 +152,7 @@ public:
|
||||
void sendRestartGame() const override;
|
||||
void sendStartGame(bool allowOnlyAI = false) const override;
|
||||
|
||||
void startMapAfterConnection(std::shared_ptr<CMapInfo> to);
|
||||
void startGameplay(VCMI_LIB_WRAP_NAMESPACE(CGameState) * gameState = nullptr);
|
||||
void endGameplay(bool closeConnection = true, bool restart = false);
|
||||
void startCampaignScenario(std::shared_ptr<CampaignState> cs = {});
|
||||
|
@ -38,7 +38,9 @@ void ApplyOnLobbyHandlerNetPackVisitor::visitLobbyClientConnected(LobbyClientCon
|
||||
if(pack.uuid == handler.c->uuid)
|
||||
{
|
||||
handler.c->connectionID = pack.clientId;
|
||||
if(!settings["session"]["headless"].Bool())
|
||||
if(handler.mapToStart)
|
||||
handler.setMapInfo(handler.mapToStart);
|
||||
else if(!settings["session"]["headless"].Bool())
|
||||
GH.windows().createAndPushWindow<CLobbyScreen>(static_cast<ESelectionScreen>(handler.screenType));
|
||||
handler.state = EClientState::LOBBY;
|
||||
}
|
||||
@ -136,6 +138,11 @@ void ApplyOnLobbyHandlerNetPackVisitor::visitLobbyUpdateState(LobbyUpdateState &
|
||||
{
|
||||
pack.hostChanged = pack.state.hostClientId != handler.hostClientId;
|
||||
static_cast<LobbyState &>(handler) = pack.state;
|
||||
if(handler.mapToStart && handler.mi)
|
||||
{
|
||||
handler.startMapAfterConnection(nullptr);
|
||||
handler.sendStartGame();
|
||||
}
|
||||
}
|
||||
|
||||
void ApplyOnLobbyScreenNetPackVisitor::visitLobbyUpdateState(LobbyUpdateState & pack)
|
||||
|
@ -383,32 +383,8 @@ void CMainMenu::startTutorial()
|
||||
|
||||
auto mapInfo = std::make_shared<CMapInfo>();
|
||||
mapInfo->mapInit(tutorialMap.getName());
|
||||
|
||||
CSH->startMapAfterConnection(mapInfo);
|
||||
CMainMenu::openLobby(ESelectionScreen::newGame, true, nullptr, ELoadMode::NONE);
|
||||
|
||||
std::thread waitForConnectionThread([mapInfo](){
|
||||
boost::this_thread::sleep(boost::posix_time::milliseconds(100)); //delay this thread
|
||||
|
||||
//connecting to server
|
||||
while(CSH->state != EClientState::LOBBY)
|
||||
{
|
||||
if(CSH->state == EClientState::CONNECTION_CANCELLED || CSH->state == EClientState::NONE)
|
||||
return;
|
||||
boost::this_thread::sleep(boost::posix_time::milliseconds(50));
|
||||
}
|
||||
|
||||
//start game from main thread
|
||||
GH.dispatchMainThread([mapInfo]()
|
||||
{
|
||||
while(!CSH->si || mapInfo->fileURI != CSH->si->mapname)
|
||||
{
|
||||
CSH->setMapInfo(mapInfo);
|
||||
boost::this_thread::sleep(boost::posix_time::milliseconds(50));
|
||||
}
|
||||
CSH->sendStartGame();
|
||||
});
|
||||
});
|
||||
waitForConnectionThread.detach();
|
||||
}
|
||||
|
||||
std::shared_ptr<CMainMenu> CMainMenu::create()
|
||||
|
Loading…
Reference in New Issue
Block a user