Client.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. #include "CMusicHandler.h"
  2. #include "../lib/CCampaignHandler.h"
  3. #include "../CCallback.h"
  4. #include "../CConsoleHandler.h"
  5. #include "CGameInfo.h"
  6. #include "../lib/CGameState.h"
  7. #include "CPlayerInterface.h"
  8. #include "../StartInfo.h"
  9. #include "../lib/BattleState.h"
  10. #include "../lib/CArtHandler.h"
  11. #include "../lib/CDefObjInfoHandler.h"
  12. #include "../lib/CGeneralTextHandler.h"
  13. #include "../lib/CHeroHandler.h"
  14. #include "../lib/CTownHandler.h"
  15. #include "../lib/CObjectHandler.h"
  16. #include "../lib/CBuildingHandler.h"
  17. #include "../lib/CSpellHandler.h"
  18. #include "../lib/Connection.h"
  19. #include "../lib/Interprocess.h"
  20. #include "../lib/NetPacks.h"
  21. #include "../lib/VCMI_Lib.h"
  22. #include "../lib/map.h"
  23. #include "mapHandler.h"
  24. #include "CConfigHandler.h"
  25. #include "Client.h"
  26. #include "GUIBase.h"
  27. #include <boost/bind.hpp>
  28. #include <boost/foreach.hpp>
  29. #include <boost/thread.hpp>
  30. #include <boost/thread/shared_mutex.hpp>
  31. #include <boost/lexical_cast.hpp>
  32. #include <sstream>
  33. #include "CPreGame.h"
  34. #include "CBattleInterface.h"
  35. #include "../CThreadHelper.h"
  36. #include "../lib/CScriptingModule.h"
  37. #include "../lib/CFileUtility.h"
  38. #define NOT_LIB
  39. #include "../lib/RegisterTypes.cpp"
  40. extern std::string NAME;
  41. namespace intpr = boost::interprocess;
  42. /*
  43. * Client.cpp, part of VCMI engine
  44. *
  45. * Authors: listed in file AUTHORS in main folder
  46. *
  47. * License: GNU General Public License v2.0 or later
  48. * Full text of license available in license.txt file, in main folder
  49. *
  50. */
  51. template <typename T> class CApplyOnCL;
  52. class CBaseForCLApply
  53. {
  54. public:
  55. virtual void applyOnClAfter(CClient *cl, void *pack) const =0;
  56. virtual void applyOnClBefore(CClient *cl, void *pack) const =0;
  57. virtual ~CBaseForCLApply(){}
  58. template<typename U> static CBaseForCLApply *getApplier(const U * t=NULL)
  59. {
  60. return new CApplyOnCL<U>;
  61. }
  62. };
  63. template <typename T> class CApplyOnCL : public CBaseForCLApply
  64. {
  65. public:
  66. void applyOnClAfter(CClient *cl, void *pack) const
  67. {
  68. T *ptr = static_cast<T*>(pack);
  69. ptr->applyCl(cl);
  70. }
  71. void applyOnClBefore(CClient *cl, void *pack) const
  72. {
  73. T *ptr = static_cast<T*>(pack);
  74. ptr->applyFirstCl(cl);
  75. }
  76. };
  77. static CApplier<CBaseForCLApply> *applier = NULL;
  78. void CClient::init()
  79. {
  80. hotSeat = false;
  81. connectionHandler = NULL;
  82. pathInfo = NULL;
  83. applier = new CApplier<CBaseForCLApply>;
  84. registerTypes2(*applier);
  85. IObjectInterface::cb = this;
  86. serv = NULL;
  87. gs = NULL;
  88. cb = NULL;
  89. erm = NULL;
  90. terminate = false;
  91. }
  92. CClient::CClient(void)
  93. :waitingRequest(false)
  94. {
  95. init();
  96. }
  97. CClient::CClient(CConnection *con, StartInfo *si)
  98. :waitingRequest(false)
  99. {
  100. init();
  101. newGame(con,si);
  102. }
  103. CClient::~CClient(void)
  104. {
  105. delete pathInfo;
  106. delete applier;
  107. }
  108. void CClient::waitForMoveAndSend(int color)
  109. {
  110. try
  111. {
  112. assert(vstd::contains(battleints, color));
  113. BattleAction ba = battleints[color]->activeStack(gs->curB->getStack(gs->curB->activeStack, false));
  114. MakeAction temp_action(ba);
  115. *serv << &temp_action;
  116. return;
  117. }HANDLE_EXCEPTION
  118. tlog1 << "We should not be here!" << std::endl;
  119. }
  120. void CClient::run()
  121. {
  122. setThreadName(-1, "CClient::run");
  123. try
  124. {
  125. CPack *pack = NULL;
  126. while(!terminate)
  127. {
  128. pack = serv->retreivePack(); //get the package from the server
  129. if (terminate)
  130. {
  131. delete pack;
  132. pack = NULL;
  133. break;
  134. }
  135. handlePack(pack);
  136. pack = NULL;
  137. }
  138. }
  139. catch (const std::exception& e)
  140. {
  141. tlog3 << "Lost connection to server, ending listening thread!\n";
  142. tlog1 << e.what() << std::endl;
  143. if(!terminate) //rethrow (-> boom!) only if closing connection was unexpected
  144. {
  145. tlog1 << "Something wrong, lost connection while game is still ongoing...\n";
  146. throw;
  147. }
  148. }
  149. }
  150. void CClient::save(const std::string & fname)
  151. {
  152. if(gs->curB)
  153. {
  154. tlog1 << "Game cannot be saved during battle!\n";
  155. return;
  156. }
  157. SaveGame save_game(fname);
  158. *serv << &save_game;
  159. }
  160. #include <fstream>
  161. void initVillagesCapitols(Mapa * map)
  162. {
  163. std::ifstream ifs(DATA_DIR "/config/townsDefs.txt");
  164. int ccc;
  165. ifs>>ccc;
  166. for(int i=0;i<ccc*2;i++)
  167. {
  168. const CGDefInfo *n;
  169. if(i<ccc)
  170. {
  171. n = CGI->state->villages[i];
  172. map->defy.push_back(CGI->state->forts[i]);
  173. }
  174. else
  175. n = CGI->state->capitols[i%ccc];
  176. ifs >> const_cast<CGDefInfo*>(n)->name;
  177. if(!n)
  178. tlog1 << "*HUGE* Warning - missing town def for " << i << std::endl;
  179. else
  180. map->defy.push_back(const_cast<CGDefInfo*>(n));
  181. }
  182. }
  183. void CClient::endGame( bool closeConnection /*= true*/ )
  184. {
  185. // Game is ending
  186. // Tell the network thread to reach a stable state
  187. if(closeConnection)
  188. stopConnection();
  189. tlog0 << "Closed connection." << std::endl;
  190. GH.curInt = NULL;
  191. LOCPLINT->terminate_cond.setn(true);
  192. LOCPLINT->pim->lock();
  193. tlog0 << "\n\nEnding current game!" << std::endl;
  194. if(GH.topInt())
  195. GH.topInt()->deactivate();
  196. GH.listInt.clear();
  197. GH.objsToBlit.clear();
  198. GH.statusbar = NULL;
  199. tlog0 << "Removed GUI." << std::endl;
  200. delete CGI->mh;
  201. const_cast<CGameInfo*>(CGI)->mh = NULL;
  202. const_cast<CGameInfo*>(CGI)->state.dellNull();
  203. tlog0 << "Deleted mapHandler and gameState." << std::endl;
  204. CPlayerInterface * oldInt = LOCPLINT;
  205. LOCPLINT = NULL;
  206. oldInt->pim->unlock();
  207. while (!playerint.empty())
  208. {
  209. CGameInterface *pint = playerint.begin()->second;
  210. playerint.erase(playerint.begin());
  211. delete pint;
  212. }
  213. BOOST_FOREACH(CCallback *cb, callbacks)
  214. {
  215. delete cb;
  216. }
  217. tlog0 << "Deleted playerInts." << std::endl;
  218. tlog0 << "Client stopped." << std::endl;
  219. }
  220. void CClient::loadGame( const std::string & fname )
  221. {
  222. tlog0 <<"\n\nLoading procedure started!\n\n";
  223. CServerHandler sh;
  224. sh.startServer();
  225. timeHandler tmh;
  226. {
  227. char sig[8];
  228. CMapHeader dum;
  229. const_cast<CGameInfo*>(CGI)->mh = new CMapHandler();
  230. StartInfo *si;
  231. CLoadFile lf(fname + ".vlgm1");
  232. lf >> sig >> dum >> si;
  233. tlog0 <<"Reading save signature: "<<tmh.getDif()<<std::endl;
  234. lf >> *VLC;
  235. const_cast<CGameInfo*>(CGI)->setFromLib();
  236. tlog0 <<"Reading handlers: "<<tmh.getDif()<<std::endl;
  237. lf >> gs;
  238. tlog0 <<"Reading gamestate: "<<tmh.getDif()<<std::endl;
  239. const_cast<CGameInfo*>(CGI)->state = gs;
  240. const_cast<CGameInfo*>(CGI)->mh->map = gs->map;
  241. pathInfo = new CPathsInfo(int3(gs->map->width, gs->map->height, gs->map->twoLevel+1));
  242. CGI->mh->init();
  243. initVillagesCapitols(gs->map);
  244. tlog0 <<"Initing maphandler: "<<tmh.getDif()<<std::endl;
  245. }
  246. serv = sh.connectToServer();
  247. serv->addStdVecItems(gs);
  248. tmh.update();
  249. ui8 pom8;
  250. *serv << ui8(3) << ui8(1); //load game; one client
  251. *serv << fname;
  252. *serv >> pom8;
  253. if(pom8)
  254. throw "Server cannot open the savegame!";
  255. else
  256. tlog0 << "Server opened savegame properly.\n";
  257. *serv << ui32(gs->scenarioOps->playerInfos.size()+1); //number of players + neutral
  258. for(std::map<int, PlayerSettings>::iterator it = gs->scenarioOps->playerInfos.begin();
  259. it != gs->scenarioOps->playerInfos.end(); ++it)
  260. {
  261. *serv << ui8(it->first); //players
  262. }
  263. *serv << ui8(255); // neutrals
  264. tlog0 <<"Sent info to server: "<<tmh.getDif()<<std::endl;
  265. {
  266. CLoadFile lf(fname + ".vcgm1");
  267. lf >> *this;
  268. }
  269. }
  270. void CClient::newGame( CConnection *con, StartInfo *si )
  271. {
  272. enum {SINGLE, HOST, GUEST} networkMode = SINGLE;
  273. std::set<ui8> myPlayers;
  274. if (con == NULL)
  275. {
  276. CServerHandler sh;
  277. serv = sh.connectToServer();
  278. }
  279. else
  280. {
  281. serv = con;
  282. networkMode = (con->connectionID == 1) ? HOST : GUEST;
  283. }
  284. for(std::map<int, PlayerSettings>::iterator it =si->playerInfos.begin();
  285. it != si->playerInfos.end(); ++it)
  286. {
  287. if((networkMode == SINGLE) //single - one client has all player
  288. || (networkMode != SINGLE && serv->connectionID == it->second.human) //multi - client has only "its players"
  289. || (networkMode == HOST && it->second.human == false)) //multi - host has all AI players
  290. {
  291. myPlayers.insert(ui8(it->first)); //add player
  292. }
  293. }
  294. if(networkMode != GUEST)
  295. myPlayers.insert(255); //neutral
  296. timeHandler tmh;
  297. const_cast<CGameInfo*>(CGI)->state = new CGameState();
  298. tlog0 <<"\tGamestate: "<<tmh.getDif()<<std::endl;
  299. CConnection &c(*serv);
  300. ////////////////////////////////////////////////////
  301. if(networkMode == SINGLE)
  302. {
  303. ui8 pom8;
  304. c << ui8(2) << ui8(1); //new game; one client
  305. c << *si;
  306. c >> pom8;
  307. if(pom8)
  308. throw "Server cannot open the map!";
  309. else
  310. tlog0 << "Server opened map properly.\n";
  311. }
  312. c << myPlayers;
  313. ui32 seed, sum;
  314. c >> si >> sum >> seed;
  315. tlog0 <<"\tSending/Getting info to/from the server: "<<tmh.getDif()<<std::endl;
  316. tlog0 << "\tUsing random seed: "<<seed << std::endl;
  317. gs = const_cast<CGameInfo*>(CGI)->state;
  318. gs->scenarioOps = si;
  319. gs->init(si, sum, seed);
  320. tlog0 <<"Initializing GameState (together): "<<tmh.getDif()<<std::endl;
  321. if(gs->map)
  322. {
  323. const_cast<CGameInfo*>(CGI)->mh = new CMapHandler();
  324. CGI->mh->map = gs->map;
  325. tlog0 <<"Creating mapHandler: "<<tmh.getDif()<<std::endl;
  326. CGI->mh->init();
  327. initVillagesCapitols(gs->map);
  328. pathInfo = new CPathsInfo(int3(gs->map->width, gs->map->height, gs->map->twoLevel+1));
  329. tlog0 <<"Initializing mapHandler (together): "<<tmh.getDif()<<std::endl;
  330. }
  331. int humanPlayers = 0;
  332. for(std::map<int, PlayerSettings>::iterator it = gs->scenarioOps->playerInfos.begin();
  333. it != gs->scenarioOps->playerInfos.end(); ++it)//initializing interfaces for players
  334. {
  335. ui8 color = it->first;
  336. gs->currentPlayer = color;
  337. if(!vstd::contains(myPlayers, color))
  338. continue;
  339. if(si->mode != StartInfo::DUEL)
  340. {
  341. CCallback *cb = new CCallback(gs,color,this);
  342. if(!it->second.human)
  343. {
  344. playerint[color] = static_cast<CGameInterface*>(CDynLibHandler::getNewAI(conf.cc.defaultPlayerAI));
  345. }
  346. else
  347. {
  348. playerint[color] = new CPlayerInterface(color);
  349. humanPlayers++;
  350. }
  351. battleints[color] = playerint[color];
  352. playerint[color]->init(cb);
  353. }
  354. else
  355. {
  356. CBattleCallback * cbc = new CBattleCallback(gs, color, this);
  357. battleints[color] = CDynLibHandler::getNewBattleAI("StupidAI");
  358. battleints[color]->init(cbc);
  359. }
  360. }
  361. if(si->mode == StartInfo::DUEL)
  362. {
  363. CPlayerInterface *p = new CPlayerInterface(-1);
  364. p->observerInDuelMode = true;
  365. battleints[254] = playerint[254] = p;
  366. GH.curInt = p;
  367. p->init(new CCallback(gs, -1, this));
  368. battleStarted(gs->curB);
  369. }
  370. else
  371. {
  372. loadNeutralBattleAI();
  373. }
  374. serv->addStdVecItems(const_cast<CGameInfo*>(CGI)->state);
  375. hotSeat = (humanPlayers > 1);
  376. std::vector<FileInfo> scriptModules;
  377. CFileUtility::getFilesWithExt(scriptModules, LIB_DIR "/Scripting", "." LIB_EXT);
  378. BOOST_FOREACH(FileInfo &m, scriptModules)
  379. {
  380. CScriptingModule * nm = CDynLibHandler::getNewScriptingModule(m.name);
  381. privilagedGameEventReceivers.push_back(nm);
  382. privilagedBattleEventReceivers.push_back(nm);
  383. nm->giveActionCB(this);
  384. nm->giveInfoCB(this);
  385. nm->init();
  386. erm = nm; //something tells me that there'll at most one module and it'll be ERM
  387. }
  388. }
  389. template <typename Handler>
  390. void CClient::serialize( Handler &h, const int version )
  391. {
  392. h & hotSeat;
  393. if(h.saving)
  394. {
  395. ui8 players = playerint.size();
  396. h & players;
  397. for(std::map<ui8,CGameInterface *>::iterator i = playerint.begin(); i != playerint.end(); i++)
  398. {
  399. h & i->first & i->second->dllName;
  400. i->second->serialize(h,version);
  401. }
  402. }
  403. else
  404. {
  405. ui8 players;
  406. h & players;
  407. for(int i=0; i < players; i++)
  408. {
  409. std::string dllname;
  410. ui8 pid;
  411. h & pid & dllname;
  412. CGameInterface *nInt = NULL;
  413. if(dllname.length())
  414. {
  415. if(pid == 255)
  416. {
  417. CBattleCallback * cbc = new CBattleCallback(gs, pid, this);
  418. CBattleGameInterface *cbgi = CDynLibHandler::getNewBattleAI(dllname);
  419. battleints[pid] = cbgi;
  420. cbgi->init(cb);
  421. //TODO? consider serialization
  422. continue;
  423. }
  424. else
  425. nInt = CDynLibHandler::getNewAI(dllname);
  426. }
  427. else
  428. nInt = new CPlayerInterface(pid);
  429. CCallback *callback = new CCallback(gs,pid,this);
  430. callbacks.insert(callback);
  431. battleints[pid] = playerint[pid] = nInt;
  432. nInt->init(callback);
  433. nInt->serialize(h, version);
  434. }
  435. if(!vstd::contains(battleints, NEUTRAL_PLAYER))
  436. loadNeutralBattleAI();
  437. }
  438. }
  439. void CClient::handlePack( CPack * pack )
  440. {
  441. CBaseForCLApply *apply = applier->apps[typeList.getTypeID(pack)]; //find the applier
  442. if(apply)
  443. {
  444. apply->applyOnClBefore(this,pack);
  445. tlog5 << "\tMade first apply on cl\n";
  446. gs->apply(pack);
  447. tlog5 << "\tApplied on gs\n";
  448. apply->applyOnClAfter(this,pack);
  449. tlog5 << "\tMade second apply on cl\n";
  450. }
  451. else
  452. {
  453. tlog1 << "Message cannot be applied, cannot find applier! TypeID " << typeList.getTypeID(pack) << std::endl;
  454. }
  455. delete pack;
  456. }
  457. void CClient::updatePaths()
  458. {
  459. const CGHeroInstance *h = getSelectedHero();
  460. if (h)//if we have selected hero...
  461. calculatePaths(h);
  462. }
  463. void CClient::finishCampaign( CCampaignState * camp )
  464. {
  465. }
  466. void CClient::proposeNextMission( CCampaignState * camp )
  467. {
  468. GH.pushInt(new CBonusSelection(camp));
  469. GH.curInt = CGP;
  470. }
  471. void CClient::stopConnection()
  472. {
  473. terminate = true;
  474. if (serv) //request closing connection
  475. {
  476. tlog0 << "Connection has been requested to be closed.\n";
  477. boost::unique_lock<boost::mutex>(*serv->wmx);
  478. CloseServer close_server;
  479. *serv << &close_server;
  480. tlog0 << "Sent closing signal to the server\n";
  481. }
  482. if(connectionHandler)//end connection handler
  483. {
  484. if(connectionHandler->get_id() != boost::this_thread::get_id())
  485. connectionHandler->join();
  486. tlog0 << "Connection handler thread joined" << std::endl;
  487. delete connectionHandler;
  488. connectionHandler = NULL;
  489. }
  490. if (serv) //and delete connection
  491. {
  492. serv->close();
  493. delete serv;
  494. serv = NULL;
  495. tlog3 << "Our socket has been closed." << std::endl;
  496. }
  497. }
  498. void CClient::battleStarted(const BattleInfo * info)
  499. {
  500. CPlayerInterface * att, * def;
  501. if(vstd::contains(playerint, info->sides[0]) && playerint[info->sides[0]]->human)
  502. att = static_cast<CPlayerInterface*>( playerint[info->sides[0]] );
  503. else
  504. att = NULL;
  505. if(vstd::contains(playerint, info->sides[1]) && playerint[info->sides[1]]->human)
  506. def = static_cast<CPlayerInterface*>( playerint[info->sides[1]] );
  507. else
  508. def = NULL;
  509. new CBattleInterface(info->belligerents[0], info->belligerents[1], info->heroes[0], info->heroes[1], Rect((conf.cc.resx - 800)/2, (conf.cc.resy - 600)/2, 800, 600), att, def);
  510. if(vstd::contains(battleints,info->sides[0]))
  511. battleints[info->sides[0]]->battleStart(info->belligerents[0], info->belligerents[1], info->tile, info->heroes[0], info->heroes[1], 0);
  512. if(vstd::contains(battleints,info->sides[1]))
  513. battleints[info->sides[1]]->battleStart(info->belligerents[0], info->belligerents[1], info->tile, info->heroes[0], info->heroes[1], 1);
  514. if(vstd::contains(battleints,254))
  515. battleints[254]->battleStart(info->belligerents[0], info->belligerents[1], info->tile, info->heroes[0], info->heroes[1], 1);
  516. }
  517. void CClient::loadNeutralBattleAI()
  518. {
  519. battleints[255] = CDynLibHandler::getNewBattleAI(conf.cc.defaultBattleAI);
  520. battleints[255]->init(new CBattleCallback(gs, 255, this));
  521. }
  522. void CClient::commitPackage( CPackForClient *pack )
  523. {
  524. CommitPackage cp;
  525. cp.freePack = false;
  526. cp.packToCommit = pack;
  527. *serv << &cp;
  528. }
  529. int CClient::getLocalPlayer() const
  530. {
  531. if(LOCPLINT)
  532. return LOCPLINT->playerID;
  533. return getCurrentPlayer();
  534. }
  535. void CClient::calculatePaths(const CGHeroInstance *h)
  536. {
  537. assert(h);
  538. boost::unique_lock<boost::mutex> pathLock(pathMx);
  539. gs->calculatePaths(h, *pathInfo);
  540. }
  541. template void CClient::serialize( CISer<CLoadFile> &h, const int version );
  542. template void CClient::serialize( COSer<CSaveFile> &h, const int version );
  543. void CServerHandler::startServer()
  544. {
  545. th.update();
  546. serverThread = new boost::thread(&CServerHandler::callServer, this); //runs server executable;
  547. if(verbose)
  548. tlog0 << "Setting up thread calling server: " << th.getDif() << std::endl;
  549. }
  550. void CServerHandler::waitForServer()
  551. {
  552. if(!serverThread)
  553. startServer();
  554. th.update();
  555. intpr::scoped_lock<intpr::interprocess_mutex> slock(shared->sr->mutex);
  556. while(!shared->sr->ready)
  557. {
  558. shared->sr->cond.wait(slock);
  559. }
  560. if(verbose)
  561. tlog0 << "Waiting for server: " << th.getDif() << std::endl;
  562. }
  563. CConnection * CServerHandler::connectToServer()
  564. {
  565. if(!shared->sr->ready)
  566. waitForServer();
  567. th.update();
  568. CConnection *ret = justConnectToServer(conf.cc.server, port);
  569. if(verbose)
  570. tlog0<<"\tConnecting to the server: "<<th.getDif()<<std::endl;
  571. return ret;
  572. }
  573. CServerHandler::CServerHandler(bool runServer /*= false*/)
  574. {
  575. serverThread = NULL;
  576. shared = NULL;
  577. port = boost::lexical_cast<std::string>(conf.cc.port);
  578. verbose = false;
  579. boost::interprocess::shared_memory_object::remove("vcmi_memory"); //if the application has previously crashed, the memory may not have been removed. to avoid problems - try to destroy it
  580. try
  581. {
  582. shared = new SharedMem();
  583. } HANDLE_EXCEPTIONC(tlog1 << "Cannot open interprocess memory: ";)
  584. }
  585. CServerHandler::~CServerHandler()
  586. {
  587. delete shared;
  588. delete serverThread; //detaches, not kills thread
  589. }
  590. void CServerHandler::callServer()
  591. {
  592. setThreadName(-1, "CServerHandler::callServer");
  593. std::string comm = std::string(BIN_DIR PATH_SEPARATOR SERVER_NAME " ") + port + " > server_log.txt";
  594. std::system(comm.c_str());
  595. tlog0 << "Server finished\n";
  596. }
  597. CConnection * CServerHandler::justConnectToServer(const std::string &host, const std::string &port)
  598. {
  599. CConnection *ret = NULL;
  600. while(!ret)
  601. {
  602. try
  603. {
  604. tlog0 << "Establishing connection...\n";
  605. ret = new CConnection( host.size() ? host : conf.cc.server,
  606. port.size() ? port : boost::lexical_cast<std::string>(conf.cc.port),
  607. NAME);
  608. }
  609. catch(...)
  610. {
  611. tlog1 << "\nCannot establish connection! Retrying within 2 seconds" << std::endl;
  612. SDL_Delay(2000);
  613. }
  614. }
  615. return ret;
  616. }