CVCMIServer.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  1. #include "stdafx.h"
  2. #include "../lib/CCampaignHandler.h"
  3. #include "../global.h"
  4. #include "../lib/Connection.h"
  5. #include "../lib/CArtHandler.h"
  6. #include "../lib/CDefObjInfoHandler.h"
  7. #include "../lib/CGeneralTextHandler.h"
  8. #include "../lib/CHeroHandler.h"
  9. #include "../lib/CTownHandler.h"
  10. #include "../lib/CObjectHandler.h"
  11. #include "../lib/CBuildingHandler.h"
  12. #include "../lib/CSpellHandler.h"
  13. #include "../lib/CCreatureHandler.h"
  14. #include "zlib.h"
  15. #ifndef __GNUC__
  16. #include <tchar.h>
  17. #endif
  18. #include "CVCMIServer.h"
  19. #include "../StartInfo.h"
  20. #include "../lib/map.h"
  21. #include "../lib/Interprocess.h"
  22. #include "../lib/VCMI_Lib.h"
  23. #include "../lib/VCMIDirs.h"
  24. #include "CGameHandler.h"
  25. #include "../lib/CMapInfo.h"
  26. #include "../lib/CondSh.h"
  27. std::string RESULTS_PATH = "./results.txt",
  28. LOGS_DIR = ".";
  29. std::string NAME_AFFIX = "server";
  30. std::string NAME = NAME_VER + std::string(" (") + NAME_AFFIX + ')'; //application name
  31. using namespace boost;
  32. using namespace boost::asio;
  33. using namespace boost::asio::ip;
  34. namespace intpr = boost::interprocess;
  35. bool end2 = false;
  36. int port = 3030;
  37. VCMIDirs GVCMIDirs;
  38. /*
  39. * CVCMIServer.cpp, part of VCMI engine
  40. *
  41. * Authors: listed in file AUTHORS in main folder
  42. *
  43. * License: GNU General Public License v2.0 or later
  44. * Full text of license available in license.txt file, in main folder
  45. *
  46. */
  47. static void vaccept(tcp::acceptor *ac, tcp::socket *s, boost::system::error_code *error)
  48. {
  49. ac->accept(*s,*error);
  50. }
  51. CPregameServer::CPregameServer(CConnection *Host, TAcceptor *Acceptor /*= NULL*/)
  52. : host(Host), listeningThreads(0), acceptor(Acceptor), upcomingConnection(NULL),
  53. curmap(NULL), curStartInfo(NULL), state(RUNNING)
  54. {
  55. initConnection(host);
  56. }
  57. void CPregameServer::handleConnection(CConnection *cpc)
  58. {
  59. try
  60. {
  61. while(!cpc->receivedStop)
  62. {
  63. CPackForSelectionScreen *cpfs = NULL;
  64. *cpc >> cpfs;
  65. tlog0 << "Got package to announce " << typeid(*cpfs).name() << " from " << *cpc << std::endl;
  66. boost::unique_lock<boost::recursive_mutex> queueLock(mx);
  67. bool quitting = dynamic_cast<QuitMenuWithoutStarting*>(cpfs),
  68. startingGame = dynamic_cast<StartWithCurrentSettings*>(cpfs);
  69. if(quitting || startingGame) //host leaves main menu or wants to start game -> we end
  70. {
  71. cpc->receivedStop = true;
  72. if(!cpc->sendStop)
  73. sendPack(cpc, *cpfs);
  74. if(cpc == host)
  75. toAnnounce.push_back(cpfs);
  76. }
  77. else
  78. toAnnounce.push_back(cpfs);
  79. if(startingGame)
  80. {
  81. //wait for sending thread to announce start
  82. mx.unlock();
  83. while(state == RUNNING) boost::this_thread::sleep(boost::posix_time::milliseconds(50));
  84. mx.lock();
  85. }
  86. }
  87. }
  88. catch (const std::exception& e)
  89. {
  90. boost::unique_lock<boost::recursive_mutex> queueLock(mx);
  91. tlog0 << *cpc << " dies... \nWhat happened: " << e.what() << std::endl;
  92. }
  93. boost::unique_lock<boost::recursive_mutex> queueLock(mx);
  94. if(state != ENDING_AND_STARTING_GAME)
  95. {
  96. connections -= cpc;
  97. //notify other players about leaving
  98. PlayerLeft *pl = new PlayerLeft();
  99. pl->playerID = cpc->connectionID;
  100. announceTxt(cpc->name + " left the game");
  101. toAnnounce.push_back(pl);
  102. if(!connections.size())
  103. {
  104. tlog0 << "Last connection lost, server will close itself...\n";
  105. boost::this_thread::sleep(boost::posix_time::seconds(2)); //we should never be hasty when networking
  106. state = ENDING_WITHOUT_START;
  107. }
  108. }
  109. tlog0 << "Thread listening for " << *cpc << " ended\n";
  110. listeningThreads--;
  111. delNull(cpc->handler);
  112. }
  113. void CPregameServer::run()
  114. {
  115. startListeningThread(host);
  116. start_async_accept();
  117. while(state == RUNNING)
  118. {
  119. {
  120. boost::unique_lock<boost::recursive_mutex> myLock(mx);
  121. while(toAnnounce.size())
  122. {
  123. processPack(toAnnounce.front());
  124. toAnnounce.pop_front();
  125. }
  126. // //we end sending thread if we ordered all our connections to stop
  127. // ending = true;
  128. // BOOST_FOREACH(CPregameConnection *pc, connections)
  129. // if(!pc->sendStop)
  130. // ending = false;
  131. if(state != RUNNING)
  132. {
  133. tlog0 << "Stopping listening for connections...\n";
  134. acceptor->close();
  135. }
  136. if(acceptor)
  137. {
  138. acceptor->get_io_service().reset();
  139. acceptor->get_io_service().poll();
  140. }
  141. } //frees lock
  142. boost::this_thread::sleep(boost::posix_time::milliseconds(50));
  143. }
  144. tlog0 << "Thread handling connections ended\n";
  145. if(state == ENDING_AND_STARTING_GAME)
  146. {
  147. tlog0 << "Waiting for listening thread to finish...\n";
  148. while(listeningThreads) boost::this_thread::sleep(boost::posix_time::milliseconds(50));
  149. tlog0 << "Preparing new game\n";
  150. }
  151. }
  152. CPregameServer::~CPregameServer()
  153. {
  154. delete acceptor;
  155. delete upcomingConnection;
  156. BOOST_FOREACH(CPackForSelectionScreen *pack, toAnnounce)
  157. delete pack;
  158. toAnnounce.clear();
  159. //TODO pregameconnections
  160. }
  161. void CPregameServer::connectionAccepted(const boost::system::error_code& ec)
  162. {
  163. if(ec)
  164. {
  165. tlog0 << "Something wrong during accepting: " << ec.message() << std::endl;
  166. return;
  167. }
  168. tlog0 << "We got a new connection! :)\n";
  169. CConnection *pc = new CConnection(upcomingConnection, NAME);
  170. initConnection(pc);
  171. upcomingConnection = NULL;
  172. *pc << (ui8)pc->connectionID << curmap;
  173. startListeningThread(pc);
  174. announceTxt(pc->name + " joins the game");
  175. PlayerJoined *pj = new PlayerJoined();
  176. pj->playerName = pc->name;
  177. pj->connectionID = pc->connectionID;
  178. toAnnounce.push_back(pj);
  179. start_async_accept();
  180. }
  181. void CPregameServer::start_async_accept()
  182. {
  183. assert(!upcomingConnection);
  184. assert(acceptor);
  185. upcomingConnection = new TSocket(acceptor->get_io_service());
  186. acceptor->async_accept(*upcomingConnection, boost::bind(&CPregameServer::connectionAccepted, this, boost::asio::placeholders::error));
  187. }
  188. void CPregameServer::announceTxt(const std::string &txt, const std::string &playerName /*= "system"*/)
  189. {
  190. tlog0 << playerName << " says: " << txt << std::endl;
  191. ChatMessage cm;
  192. cm.playerName = playerName;
  193. cm.message = txt;
  194. boost::unique_lock<boost::recursive_mutex> queueLock(mx);
  195. toAnnounce.push_front(new ChatMessage(cm));
  196. }
  197. void CPregameServer::announcePack(const CPackForSelectionScreen &pack)
  198. {
  199. BOOST_FOREACH(CConnection *pc, connections)
  200. sendPack(pc, pack);
  201. }
  202. void CPregameServer::sendPack(CConnection * pc, const CPackForSelectionScreen & pack)
  203. {
  204. if(!pc->sendStop)
  205. {
  206. tlog0 << "\tSending pack of type " << typeid(pack).name() << " to " << *pc << std::endl;
  207. *pc << &pack;
  208. }
  209. if(dynamic_cast<const QuitMenuWithoutStarting*>(&pack))
  210. {
  211. pc->sendStop = true;
  212. }
  213. else if(dynamic_cast<const StartWithCurrentSettings*>(&pack))
  214. {
  215. pc->sendStop = true;
  216. }
  217. }
  218. void CPregameServer::processPack(CPackForSelectionScreen * pack)
  219. {
  220. if(dynamic_cast<CPregamePackToHost*>(pack))
  221. {
  222. sendPack(host, *pack);
  223. }
  224. else if(SelectMap *sm = dynamic_cast<SelectMap*>(pack))
  225. {
  226. delNull(curmap);
  227. curmap = sm->mapInfo;
  228. sm->free = false;
  229. announcePack(*pack);
  230. }
  231. else if(UpdateStartOptions *uso = dynamic_cast<UpdateStartOptions*>(pack))
  232. {
  233. delNull(curStartInfo);
  234. curStartInfo = uso->options;
  235. uso->free = false;
  236. announcePack(*pack);
  237. }
  238. else if(dynamic_cast<const StartWithCurrentSettings*>(pack))
  239. {
  240. state = ENDING_AND_STARTING_GAME;
  241. announcePack(*pack);
  242. }
  243. else
  244. announcePack(*pack);
  245. delete pack;
  246. }
  247. void CPregameServer::initConnection(CConnection *c)
  248. {
  249. *c >> c->name;
  250. connections.insert(c);
  251. tlog0 << "Pregame connection with player " << c->name << " established!" << std::endl;
  252. }
  253. void CPregameServer::startListeningThread(CConnection * pc)
  254. {
  255. listeningThreads++;
  256. pc->handler = new boost::thread(&CPregameServer::handleConnection, this, pc);
  257. }
  258. CVCMIServer::CVCMIServer()
  259. : io(new io_service()), acceptor(new TAcceptor(*io, tcp::endpoint(tcp::v4(), port))), firstConnection(NULL)
  260. {
  261. tlog4 << "CVCMIServer created!" <<std::endl;
  262. }
  263. CVCMIServer::~CVCMIServer()
  264. {
  265. //delete io;
  266. //delete acceptor;
  267. }
  268. CGameHandler * CVCMIServer::initGhFromHostingConnection(CConnection &c)
  269. {
  270. CGameHandler *gh = new CGameHandler();
  271. StartInfo si;
  272. c >> si; //get start options
  273. int problem;
  274. #ifdef _MSC_VER
  275. FILE *f;
  276. problem = fopen_s(&f,si.mapname.c_str(),"r");
  277. #else
  278. FILE * f = fopen(si.mapname.c_str(),"r");
  279. problem = !f;
  280. #endif
  281. if(problem && si.mode == StartInfo::NEW_GAME) //TODO some checking for campaigns
  282. {
  283. c << ui8(problem); //WRONG!
  284. return NULL;
  285. }
  286. else
  287. {
  288. if(f)
  289. fclose(f);
  290. c << ui8(0); //OK!
  291. }
  292. c.addStdVecItems(gh->gs);
  293. gh->conns.insert(&c);
  294. return gh;
  295. }
  296. void CVCMIServer::newGame()
  297. {
  298. CConnection &c = *firstConnection;
  299. ui8 clients;
  300. c >> clients; //how many clients should be connected
  301. assert(clients == 1); //multi goes now by newPregame, TODO: custom lobbies
  302. CGameHandler *gh = initGhFromHostingConnection(c);
  303. gh->run(false);
  304. delNull(gh);
  305. }
  306. void CVCMIServer::newPregame()
  307. {
  308. CPregameServer *cps = new CPregameServer(firstConnection, acceptor);
  309. cps->run();
  310. if(cps->state == CPregameServer::ENDING_WITHOUT_START)
  311. {
  312. delete cps;
  313. return;
  314. }
  315. if(cps->state == CPregameServer::ENDING_AND_STARTING_GAME)
  316. {
  317. CGameHandler gh;
  318. gh.conns = cps->connections;
  319. gh.init(cps->curStartInfo,std::clock());
  320. BOOST_FOREACH(CConnection *c, gh.conns)
  321. c->addStdVecItems(gh.gs);
  322. gh.run(false);
  323. }
  324. }
  325. void CVCMIServer::start()
  326. {
  327. ServerReady *sr = NULL;
  328. intpr::mapped_region *mr;
  329. try
  330. {
  331. intpr::shared_memory_object smo(intpr::open_only,"vcmi_memory",intpr::read_write);
  332. smo.truncate(sizeof(ServerReady));
  333. mr = new intpr::mapped_region(smo,intpr::read_write);
  334. sr = reinterpret_cast<ServerReady*>(mr->get_address());
  335. }
  336. catch(...)
  337. {
  338. intpr::shared_memory_object smo(intpr::create_only,"vcmi_memory",intpr::read_write);
  339. smo.truncate(sizeof(ServerReady));
  340. mr = new intpr::mapped_region(smo,intpr::read_write);
  341. sr = new(mr->get_address())ServerReady();
  342. }
  343. boost::system::error_code error;
  344. tlog0<<"Listening for connections at port " << acceptor->local_endpoint().port() << std::endl;
  345. tcp::socket * s = new tcp::socket(acceptor->get_io_service());
  346. boost::thread acc(boost::bind(vaccept,acceptor,s,&error));
  347. sr->setToTrueAndNotify();
  348. delete mr;
  349. acc.join();
  350. if (error)
  351. {
  352. tlog2<<"Got connection but there is an error " << std::endl << error;
  353. return;
  354. }
  355. tlog0<<"We've accepted someone... " << std::endl;
  356. firstConnection = new CConnection(s,NAME);
  357. tlog0<<"Got connection!" << std::endl;
  358. while(!end2)
  359. {
  360. ui8 mode;
  361. *firstConnection >> mode;
  362. switch (mode)
  363. {
  364. case 0:
  365. firstConnection->close();
  366. exit(0);
  367. break;
  368. case 1:
  369. firstConnection->close();
  370. return;
  371. break;
  372. case 2:
  373. newGame();
  374. break;
  375. case 3:
  376. loadGame();
  377. break;
  378. case 4:
  379. newPregame();
  380. break;
  381. }
  382. }
  383. }
  384. void CVCMIServer::loadGame()
  385. {
  386. CConnection &c = *firstConnection;
  387. std::string fname;
  388. CGameHandler gh;
  389. boost::system::error_code error;
  390. ui8 clients;
  391. c >> clients >> fname; //how many clients should be connected - TODO: support more than one
  392. {
  393. char sig[8];
  394. CMapHeader dum;
  395. StartInfo *si;
  396. CLoadFile lf(fname + ".vlgm1");
  397. lf >> sig >> dum >> si;
  398. tlog0 <<"Reading save signature"<<std::endl;
  399. lf >> *VLC;
  400. tlog0 <<"Reading handlers"<<std::endl;
  401. lf >> (gh.gs);
  402. c.addStdVecItems(gh.gs);
  403. tlog0 <<"Reading gamestate"<<std::endl;
  404. }
  405. {
  406. CLoadFile lf(fname + ".vsgm1");
  407. lf >> gh;
  408. }
  409. c << ui8(0);
  410. CConnection* cc; //tcp::socket * ss;
  411. for(int i=0; i<clients; i++)
  412. {
  413. if(!i)
  414. {
  415. cc = &c;
  416. }
  417. else
  418. {
  419. tcp::socket * s = new tcp::socket(acceptor->get_io_service());
  420. acceptor->accept(*s,error);
  421. if(error) //retry
  422. {
  423. tlog3<<"Cannot establish connection - retrying..." << std::endl;
  424. i--;
  425. continue;
  426. }
  427. cc = new CConnection(s,NAME);
  428. cc->addStdVecItems(gh.gs);
  429. }
  430. gh.conns.insert(cc);
  431. }
  432. gh.run(true);
  433. }
  434. //memory monitoring
  435. CondSh<bool> testMem;
  436. volatile int monitringRes; //0 -- left AI violated mem limit; 1 -- right AI violated mem limit; 2 -- no mem limit violation;
  437. bool memViolated(const int pid, const int refpid, const int limit) {
  438. char call[1000], c2[1000];
  439. //sprintf(call, "if (( `cat /proc/%d/statm | cut -d' ' -f1` > `cat /proc/%d/statm | cut -d' ' -f1` + %d )); then cat /kle; else echo b; fi;", pid, refpid, limit);
  440. sprintf(call, "/proc/%d/statm", pid);
  441. sprintf(c2, "/proc/%d/statm", refpid);
  442. std::ifstream proc(call),
  443. ref(c2);
  444. int procUsage, refUsage;
  445. proc >> procUsage;
  446. ref >> refUsage;
  447. return procUsage > refUsage + limit;
  448. //return 0 != ::system(call);
  449. }
  450. void memoryMonitor(int lAIpid, int rAIpid, int refPid)
  451. {
  452. setThreadName(-1, "memoryMonitor");
  453. const int MAX_MEM = 20000; //in blocks (of, I hope, 4096 B)
  454. monitringRes = 2;
  455. tlog0 << "Monitor is activated\n";
  456. try {
  457. while(testMem.get()) {
  458. tlog0 << "Monitor is active 12\n";
  459. if(memViolated(lAIpid, refPid, MAX_MEM)) {
  460. monitringRes = 0;
  461. break;
  462. }
  463. if(memViolated(rAIpid, refPid, MAX_MEM)) {
  464. monitringRes = 1;
  465. break;
  466. }
  467. //sleep(3);
  468. boost::this_thread::sleep(boost::posix_time::seconds(3));
  469. tlog0 << "Monitor is active 34\n";
  470. }
  471. }
  472. catch(...) {
  473. tlog0 << "Monitor is throwing something...\n";
  474. }
  475. tlog0 << "Monitor is closing\n";
  476. }
  477. ////
  478. void CVCMIServer::startDuel(const std::string &battle, const std::string &leftAI, const std::string &rightAI, int howManyClients)
  479. {
  480. std::map<CConnection *, si32> pidsFromConns;
  481. si32 PIDs[3] = {0}; //[0] left [1] right; [2] reference
  482. //we need three connections
  483. std::vector<boost::thread*> threads(howManyClients, (boost::thread*)NULL);
  484. std::vector<CConnection*> conns(howManyClients, (CConnection*)NULL);
  485. for (int i = 0; i < howManyClients ; i++)
  486. {
  487. boost::system::error_code error;
  488. tlog0<<"Listening for connections at port " << acceptor->local_endpoint().port() << std::endl;
  489. tcp::socket * s = new tcp::socket(acceptor->get_io_service());
  490. acceptor->accept(*s, error);
  491. if (error)
  492. {
  493. tlog2<<"Got connection but there is an error " << std::endl << error;
  494. i--;
  495. delNull(s);
  496. }
  497. else
  498. {
  499. tlog0<<"We've accepted someone... " << std::endl;
  500. conns[i] = new CConnection(s, NAME);
  501. tlog0<<"Got connection!" << std::endl;
  502. }
  503. }
  504. StartInfo si;
  505. si.mode = StartInfo::DUEL;
  506. si.mapname = battle;
  507. tlog0 << "Preparing gh!\n";
  508. CGameHandler *gh = new CGameHandler();
  509. gh->init(&si,std::time(NULL));
  510. gh->ais[0] = leftAI;
  511. gh->ais[1] = rightAI;
  512. BOOST_FOREACH(CConnection *c, conns)
  513. {
  514. ui8 player = gh->conns.size();
  515. tlog0 << boost::format("Preparing connection %d!\n") % (int)player;
  516. c->connectionID = player;
  517. c->addStdVecItems(gh->gs, VLC);
  518. gh->connections[player] = c;
  519. gh->conns.insert(c);
  520. gh->states.addPlayer(player);
  521. *c << si;
  522. *c >> pidsFromConns[c];
  523. std::set<int> pom;
  524. pom.insert(player);
  525. threads[player] = new boost::thread(boost::bind(&CGameHandler::handleConnection, gh, pom, boost::ref(*c)));
  526. }
  527. boost::this_thread::sleep(boost::posix_time::milliseconds(1000));
  528. tlog0 << boost::format("Sending start info to connections!\n");
  529. int aisSoFar = 0;
  530. for (int i = 0; i < howManyClients ; i++)
  531. {
  532. tlog0 << "Connection nr " << i;
  533. CConnection *c = conns[i];
  534. if(c->contactName.find("client") != std::string::npos)
  535. {
  536. tlog0 << " is a visualization client!\n";
  537. *c << std::string() << ui8(254);
  538. }
  539. else
  540. {
  541. if(aisSoFar < 2)
  542. {
  543. tlog0 << " (PID=" << pidsFromConns[c] << ") will run " << (aisSoFar ? "right" : "left") << " AI: " << gh->ais[aisSoFar] << std::endl;
  544. *c << gh->ais[aisSoFar] << ui8(aisSoFar);
  545. PIDs[aisSoFar] = pidsFromConns[c];
  546. aisSoFar++;
  547. }
  548. else
  549. {
  550. tlog0 << " will serve as a memory reference.\n";
  551. *c << std::string() << ui8(254);
  552. PIDs[2] = pidsFromConns[c];
  553. }
  554. }
  555. }
  556. //TODO monitor memory of PIDs
  557. testMem.set(true);
  558. boost::thread* memMon = new boost::thread(boost::bind(memoryMonitor, PIDs[0], PIDs[1], PIDs[2]));
  559. std::string logFName = LOGS_DIR + "/" + leftAI + "_vs_"+rightAI+"_on_" + battle + ".vdat";
  560. tlog0 << "Logging battle activities (for replay possibility) in " << logFName << std::endl;
  561. gh->gameLog = new CSaveFile(logFName);
  562. gh->gameLog->smartPointerSerialization = false;
  563. *gh->gameLog << battle << leftAI << rightAI << ui8('$');
  564. tlog0 << "Starting battle!\n";
  565. gh->runBattle();
  566. tlog0 << "Battle over!\n";
  567. tlog0 << "Waiting for connections to close\n";
  568. testMem.set(false);
  569. memMon->join();
  570. delNull(memMon);
  571. tlog0 << "Memory violation checking result: " << monitringRes << std::endl;
  572. BOOST_FOREACH(boost::thread *t, threads)
  573. {
  574. t->join();
  575. delNull(t);
  576. }
  577. tlog0 << "Removing gh\n";
  578. delNull(gh);
  579. tlog0 << "Removed gh!\n";
  580. tlog0 << "Dying...\n";
  581. exit(0);
  582. }
  583. #ifndef __GNUC__
  584. int _tmain(int argc, _TCHAR* argv[])
  585. #else
  586. int main(int argc, char** argv)
  587. #endif
  588. {
  589. if(argc >= 6)
  590. LOGS_DIR = argv[5];
  591. std::string logName = LOGS_DIR + "/" + "VCMI_Server_log.txt";
  592. logfile = new std::ofstream(logName.c_str());
  593. console = new CConsoleHandler;
  594. //boost::thread t(boost::bind(&CConsoleHandler::run,::console));
  595. initDLL(console,logfile);
  596. srand ( (unsigned int)time(NULL) );
  597. try
  598. {
  599. io_service io_service;
  600. CVCMIServer server;
  601. if(argc == 6 || argc == 7)
  602. {
  603. RESULTS_PATH = argv[4];
  604. tlog1 << "Results path: " << RESULTS_PATH << std::endl;
  605. tlog1 << "Logs path: " << RESULTS_PATH << std::endl;
  606. server.startDuel(argv[1], argv[2], argv[3], argc-3);
  607. }
  608. else
  609. server.startDuel("b1.json", "StupidAI", "StupidAI", 2);
  610. while(!end2)
  611. {
  612. server.start();
  613. }
  614. io_service.run();
  615. }
  616. catch(boost::system::system_error &e) //for boost errors just log, not crash - probably client shut down connection
  617. {
  618. tlog1 << e.what() << std::endl;
  619. end2 = true;
  620. }HANDLE_EXCEPTION
  621. return 0;
  622. }