1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-08-13 19:54:17 +02:00

Client threading tweak

This commit is contained in:
AlexVinS
2017-11-16 14:15:43 +03:00
parent d7366f04a9
commit 805b5215de
4 changed files with 49 additions and 3 deletions

View File

@@ -103,6 +103,10 @@ BattleAction CBattleAI::activeStack( const CStack * stack )
} }
} }
} }
catch(boost::thread_interrupted &)
{
throw;
}
catch(std::exception &e) catch(std::exception &e)
{ {
logAi->error("Exception occurred in %s %s",__FUNCTION__, e.what()); logAi->error("Exception occurred in %s %s",__FUNCTION__, e.what());

View File

@@ -816,6 +816,7 @@ void CClient::battleStarted(const BattleInfo * info)
void CClient::battleFinished() void CClient::battleFinished()
{ {
stopAllBattleActions();
for(auto & side : gs->curB->sides) for(auto & side : gs->curB->sides)
if(battleCallbacks.count(side.color)) if(battleCallbacks.count(side.color))
battleCallbacks[side.color]->setBattle(nullptr); battleCallbacks[side.color]->setBattle(nullptr);
@@ -982,6 +983,38 @@ std::string CClient::aiNameForPlayer(bool battleAI)
return goodAI; return goodAI;
} }
void CClient::startPlayerBattleAction(PlayerColor color)
{
stopPlayerBattleAction(color);
if(vstd::contains(battleints, color))
{
auto thread = std::make_shared<boost::thread>(std::bind(&CClient::waitForMoveAndSend, this, color));
playerActionThreads[color] = thread;
}
}
void CClient::stopPlayerBattleAction(PlayerColor color)
{
if(vstd::contains(playerActionThreads, color))
{
auto thread = playerActionThreads.at(color);
if(thread->joinable())
{
thread->interrupt();
thread->join();
}
playerActionThreads.erase(color);
}
}
void CClient::stopAllBattleActions()
{
while(!playerActionThreads.empty())
stopPlayerBattleAction(playerActionThreads.begin()->first);
}
void CServerHandler::startServer() void CServerHandler::startServer()
{ {
if(settings["session"]["donotstartserver"].Bool()) if(settings["session"]["donotstartserver"].Bool())

View File

@@ -123,6 +123,8 @@ public:
class CClient : public IGameCallback class CClient : public IGameCallback
{ {
std::unique_ptr<CPathsInfo> pathInfo; std::unique_ptr<CPathsInfo> pathInfo;
std::map<PlayerColor, std::shared_ptr<boost::thread>> playerActionThreads;
public: public:
std::map<PlayerColor,std::shared_ptr<CCallback> > callbacks; //callbacks given to player interfaces std::map<PlayerColor,std::shared_ptr<CCallback> > callbacks; //callbacks given to player interfaces
std::map<PlayerColor,std::shared_ptr<CBattleCallback> > battleCallbacks; //callbacks given to player interfaces std::map<PlayerColor,std::shared_ptr<CBattleCallback> > battleCallbacks; //callbacks given to player interfaces
@@ -143,7 +145,7 @@ public:
static ThreadSafeVector<int> waitingRequest;//FIXME: make this normal field (need to join all threads before client destruction) static ThreadSafeVector<int> waitingRequest;//FIXME: make this normal field (need to join all threads before client destruction)
void waitForMoveAndSend(PlayerColor color);
//void sendRequest(const CPackForServer *request, bool waitForRealization); //void sendRequest(const CPackForServer *request, bool waitForRealization);
CClient(void); CClient(void);
CClient(CConnection *con, StartInfo *si); CClient(CConnection *con, StartInfo *si);
@@ -252,4 +254,11 @@ public:
void serialize(BinarySerializer & h, const int version, const std::set<PlayerColor>& playerIDs); void serialize(BinarySerializer & h, const int version, const std::set<PlayerColor>& playerIDs);
void serialize(BinaryDeserializer & h, const int version, const std::set<PlayerColor>& playerIDs); void serialize(BinaryDeserializer & h, const int version, const std::set<PlayerColor>& playerIDs);
void battleFinished(); void battleFinished();
void startPlayerBattleAction(PlayerColor color);
void stopPlayerBattleAction(PlayerColor color);
void stopAllBattleActions();
private:
void waitForMoveAndSend(PlayerColor color);
}; };

View File

@@ -647,8 +647,8 @@ void BattleSetActiveStack::applyCl(CClient *cl)
{ {
playerToCall = activated->owner; playerToCall = activated->owner;
} }
if (vstd::contains(cl->battleints, playerToCall))
boost::thread(std::bind(&CClient::waitForMoveAndSend, cl, playerToCall)); cl->startPlayerBattleAction(playerToCall);
} }
void BattleTriggerEffect::applyCl(CClient * cl) void BattleTriggerEffect::applyCl(CClient * cl)