mirror of
				https://github.com/vcmi/vcmi.git
				synced 2025-10-31 00:07:39 +02:00 
			
		
		
		
	Clients report their PIDs so the memory usage can be monitored.
This commit is contained in:
		| @@ -19,6 +19,7 @@ | ||||
| #include "../lib/VCMI_Lib.h" | ||||
| #include "../lib/BattleState.h" | ||||
| #include "../lib/NetPacks.h" | ||||
| #include "../lib/CThreadHelper.h" | ||||
|  | ||||
| using namespace std; | ||||
| using namespace boost; | ||||
| @@ -29,13 +30,9 @@ std::string NAME = NAME_VER + std::string(" DLL runner"); | ||||
|  | ||||
| int main(int argc, char** argv) | ||||
| { | ||||
| 	int pid = -1; | ||||
| 	int pid = getMyPid(); | ||||
|  | ||||
|  | ||||
| #ifdef _WIN32 | ||||
| 	pid = GetCurrentProcessId(); | ||||
| #else | ||||
| 	pid = getpid(); | ||||
| #endif | ||||
| 	initDLL(console,logfile); | ||||
|  | ||||
| 	logfile = new std::ofstream(("VCMI_Runner_log_" + boost::lexical_cast<std::string>(pid) + ".txt").c_str()); | ||||
| @@ -67,6 +64,7 @@ int main(int argc, char** argv) | ||||
| 		ui8 color; | ||||
| 		StartInfo si; | ||||
| 		string battleAIName; | ||||
| 		*serv << getMyPid(); | ||||
| 		*serv >> si >> battleAIName >> color; | ||||
| 		assert(si.mode == StartInfo::DUEL); | ||||
| 		tlog0 << format("Server wants us to run %s in battle %s as side %d") % battleAIName % si.mapname % (int)color; | ||||
|   | ||||
| @@ -448,6 +448,7 @@ void CClient::newDuel(CConnection *con, StartInfo *si) | ||||
|  | ||||
| 		ui8 color; | ||||
| 		std::string battleAIName; | ||||
| 		*serv << getMyPid(); | ||||
| 		*serv >> *si >> battleAIName >> color; | ||||
| 		assert(si->mode == StartInfo::DUEL); | ||||
| 		assert(color > 1); //we are NOT participants | ||||
|   | ||||
| @@ -79,4 +79,14 @@ void setThreadName(long threadID, const std::string &name) | ||||
| #else | ||||
| 	//*nix counterpart? | ||||
| #endif | ||||
| } | ||||
|  | ||||
|  | ||||
| si32 getMyPid() | ||||
| { | ||||
| #ifdef _WIN32 | ||||
| 	return GetCurrentProcessId(); | ||||
| #else | ||||
| 	return getpid(); | ||||
| #endif | ||||
| } | ||||
| @@ -32,6 +32,9 @@ public: | ||||
| 	void run(); | ||||
| }; | ||||
|  | ||||
|  | ||||
| si32 DLL_EXPORT getMyPid(); | ||||
|  | ||||
| template <typename T> inline void setData(T * data, boost::function<T()> func) | ||||
| { | ||||
| 	*data = func(); | ||||
|   | ||||
| @@ -500,7 +500,8 @@ void CVCMIServer::loadGame() | ||||
|  | ||||
| void CVCMIServer::startDuel(const std::string &battle, const std::string &leftAI, const std::string &rightAI, int howManyClients) | ||||
| { | ||||
|  | ||||
| 	std::map<CConnection *, si32> pidsFromConns; | ||||
| 	si32 PIDs[3] = {0}; //[0] left [1] right; [2] reference | ||||
| 	//we need three connections | ||||
| 	std::vector<boost::thread*> threads(howManyClients, NULL); | ||||
| 	std::vector<CConnection*> conns(howManyClients, NULL); | ||||
| @@ -548,12 +549,14 @@ void CVCMIServer::startDuel(const std::string &battle, const std::string &leftAI | ||||
| 		gh->conns.insert(c); | ||||
| 		gh->states.addPlayer(player); | ||||
| 		*c << si; | ||||
| 		*c >> pidsFromConns[c]; | ||||
|  | ||||
| 		std::set<int> pom; | ||||
| 		pom.insert(player); | ||||
| 		threads[player] = new boost::thread(boost::bind(&CGameHandler::handleConnection, gh, pom, boost::ref(*c))); | ||||
| 	} | ||||
|  | ||||
| 	boost::this_thread::sleep(boost::posix_time::milliseconds(1000)); | ||||
| 	tlog0 << boost::format("Sending start info to connections!\n"); | ||||
| 	int aisSoFar = 0; | ||||
| 	for (int i = 0; i < howManyClients ; i++) | ||||
| @@ -572,22 +575,26 @@ void CVCMIServer::startDuel(const std::string &battle, const std::string &leftAI | ||||
| 			{ | ||||
| 				tlog0 << " will run " << (aisSoFar ? "right" : "left") << " AI " << std::endl; | ||||
| 				*c << gh->ais[aisSoFar] << ui8(aisSoFar); | ||||
| 				PIDs[aisSoFar] = pidsFromConns[c]; | ||||
| 				aisSoFar++; | ||||
| 			} | ||||
| 			else | ||||
| 			{ | ||||
| 				tlog0 << " will serve as a memory reference.\n"; | ||||
| 				*c << std::string() << ui8(254); | ||||
| 				PIDs[2] = pidsFromConns[c]; | ||||
| 			} | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	//TODO monitor memory of PIDs | ||||
|  | ||||
| 	std::string logFName = "duel_log.vdat"; | ||||
| 	tlog0 << "Logging battle activities (for replay possibility) in " << logFName << std::endl; | ||||
| 	gh->gameLog = new CSaveFile(logFName); | ||||
| 	gh->gameLog->smartPointerSerialization = false; | ||||
| 	*gh->gameLog << battle << leftAI << rightAI << ui8('$'); | ||||
|  | ||||
| 	 | ||||
| 	tlog0 << "Starting battle!\n"; | ||||
| 	gh->runBattle(); | ||||
| 	tlog0 << "Battle over!\n"; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user