소스 검색

Replace thread approach with callback based

nordsoft 2 년 전
부모
커밋
70d04ad957
4개의 변경된 파일21개의 추가작업 그리고 26개의 파일을 삭제
  1. 7 0
      client/CServerHandler.cpp
  2. 5 0
      client/CServerHandler.h
  3. 8 1
      client/NetPacksLobbyClient.cpp
  4. 1 25
      client/mainmenu/CMainMenu.cpp

+ 7 - 0
client/CServerHandler.cpp

@@ -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)

+ 5 - 0
client/CServerHandler.h

@@ -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 = {});

+ 8 - 1
client/NetPacksLobbyClient.cpp

@@ -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)

+ 1 - 25
client/mainmenu/CMainMenu.cpp

@@ -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()