Client.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. #include "../CCallback.h"
  2. #include "../CConsoleHandler.h"
  3. #include "CGameInfo.h"
  4. #include "../lib/CGameState.h"
  5. #include "CPlayerInterface.h"
  6. #include "../StartInfo.h"
  7. #include "../hch/CArtHandler.h"
  8. #include "../hch/CDefObjInfoHandler.h"
  9. #include "../hch/CGeneralTextHandler.h"
  10. #include "../hch/CHeroHandler.h"
  11. #include "../hch/CTownHandler.h"
  12. #include "../hch/CObjectHandler.h"
  13. #include "../hch/CBuildingHandler.h"
  14. #include "../hch/CSpellHandler.h"
  15. #include "../lib/Connection.h"
  16. #include "../lib/Interprocess.h"
  17. #include "../lib/NetPacks.h"
  18. #include "../lib/VCMI_Lib.h"
  19. #include "../lib/map.h"
  20. #include "../mapHandler.h"
  21. #include "CConfigHandler.h"
  22. #include "Client.h"
  23. #include <boost/bind.hpp>
  24. #include <boost/foreach.hpp>
  25. #include <boost/thread.hpp>
  26. #include <boost/thread/shared_mutex.hpp>
  27. #include <sstream>
  28. #undef DLL_EXPORT
  29. #define DLL_EXPORT
  30. #include "../lib/RegisterTypes.cpp"
  31. extern std::string NAME;
  32. namespace intpr = boost::interprocess;
  33. /*
  34. * Client.cpp, part of VCMI engine
  35. *
  36. * Authors: listed in file AUTHORS in main folder
  37. *
  38. * License: GNU General Public License v2.0 or later
  39. * Full text of license available in license.txt file, in main folder
  40. *
  41. */
  42. class CBaseForCLApply
  43. {
  44. public:
  45. virtual void applyOnClAfter(CClient *cl, void *pack) const =0;
  46. virtual void applyOnClBefore(CClient *cl, void *pack) const =0;
  47. };
  48. template <typename T> class CApplyOnCL : public CBaseForCLApply
  49. {
  50. public:
  51. void applyOnClAfter(CClient *cl, void *pack) const
  52. {
  53. T *ptr = static_cast<T*>(pack);
  54. ptr->applyCl(cl);
  55. }
  56. void applyOnClBefore(CClient *cl, void *pack) const
  57. {
  58. T *ptr = static_cast<T*>(pack);
  59. ptr->applyFirstCl(cl);
  60. }
  61. };
  62. class CCLApplier
  63. {
  64. public:
  65. std::map<ui16,CBaseForCLApply*> apps;
  66. CCLApplier()
  67. {
  68. registerTypes2(*this);
  69. }
  70. template<typename T> void registerType(const T * t=NULL)
  71. {
  72. ui16 ID = typeList.registerType(t);
  73. apps[ID] = new CApplyOnCL<T>;
  74. }
  75. } *applier = NULL;
  76. void CClient::init()
  77. {
  78. pathInfo = NULL;
  79. applier = new CCLApplier;
  80. IObjectInterface::cb = this;
  81. serv = NULL;
  82. gs = NULL;
  83. cb = NULL;
  84. must_close = false;
  85. try
  86. {
  87. shared = new SharedMem();
  88. } HANDLE_EXCEPTION
  89. }
  90. CClient::CClient(void)
  91. :waitingRequest(false)
  92. {
  93. init();
  94. }
  95. CClient::CClient(CConnection *con, StartInfo *si)
  96. :waitingRequest(false)
  97. {
  98. init();
  99. newGame(con,si);
  100. }
  101. CClient::~CClient(void)
  102. {
  103. delete pathInfo;
  104. delete applier;
  105. delete shared;
  106. }
  107. void CClient::waitForMoveAndSend(int color)
  108. {
  109. try
  110. {
  111. BattleAction ba = playerint[color]->activeStack(gs->curB->activeStack);
  112. *serv << &MakeAction(ba);
  113. return;
  114. }HANDLE_EXCEPTION
  115. tlog1 << "We should not be here!" << std::endl;
  116. }
  117. void CClient::run()
  118. {
  119. try
  120. {
  121. CPack *pack;
  122. while(1)
  123. {
  124. if (must_close) {
  125. serv->close();
  126. tlog3 << "Our socket has been closed.\n";
  127. return;
  128. }
  129. //get the package from the server
  130. {
  131. boost::unique_lock<boost::mutex> lock(*serv->rmx);
  132. tlog5 << "Listening... ";
  133. *serv >> pack;
  134. tlog5 << "\treceived server message of type " << typeid(*pack).name() << std::endl;
  135. }
  136. CBaseForCLApply *apply = applier->apps[typeList.getTypeID(pack)]; //find the applier
  137. if(apply)
  138. {
  139. apply->applyOnClBefore(this,pack);
  140. tlog5 << "\tMade first apply on cl\n";
  141. gs->apply(pack);
  142. tlog5 << "\tApplied on gs\n";
  143. apply->applyOnClAfter(this,pack);
  144. tlog5 << "\tMade second apply on cl\n";
  145. }
  146. else
  147. {
  148. tlog1 << "Message cannot be applied, cannot find applier!\n";
  149. }
  150. delete pack;
  151. pack = NULL;
  152. }
  153. } HANDLE_EXCEPTION(tlog1 << "Lost connection to server, ending listening thread!\n");
  154. }
  155. void CClient::close()
  156. {
  157. if(!serv)
  158. return;
  159. tlog3 << "Connection has been requested to be closed.\n";
  160. boost::unique_lock<boost::mutex>(*serv->wmx);
  161. *serv << &CloseServer();
  162. tlog3 << "Sent closing signal to the server\n";
  163. must_close = true;
  164. }
  165. void CClient::save(const std::string & fname)
  166. {
  167. if(gs->curB)
  168. {
  169. tlog1 << "Game cannot be saved during battle!\n";
  170. return;
  171. }
  172. *serv << &SaveGame(fname);
  173. }
  174. void CClient::load( const std::string & fname )
  175. {
  176. tlog0 <<"\n\nLoading procedure started!\n\n";
  177. timeHandler tmh;
  178. close(); //kill server
  179. tlog0 <<"Sent kill signal to the server: "<<tmh.getDif()<<std::endl;
  180. delete CGI->mh;
  181. delete CGI->state;
  182. VLC->clear(); //delete old handlers
  183. for(std::map<ui8,CGameInterface *>::iterator i = playerint.begin(); i!=playerint.end(); i++)
  184. {
  185. delete i->second; //delete player interfaces
  186. }
  187. BOOST_FOREACH(CCallback *cb, callbacks)
  188. {
  189. delete cb;
  190. }
  191. tlog0 <<"Deleting old data: "<<tmh.getDif()<<std::endl;
  192. char portc[10];
  193. SDL_itoa(conf.cc.port,portc,10);
  194. runServer(portc); //create new server
  195. tlog0 <<"Restarting server: "<<tmh.getDif()<<std::endl;
  196. {
  197. ui32 ver;
  198. char sig[8];
  199. CMapHeader dum;
  200. CGI->mh = new CMapHandler();
  201. CLoadFile lf(fname + ".vlgm1");
  202. lf >> sig >> dum >> *sig;
  203. tlog0 <<"Reading save signature: "<<tmh.getDif()<<std::endl;
  204. lf >> *VLC;
  205. CGI->setFromLib();
  206. tlog0 <<"Reading handlers: "<<tmh.getDif()<<std::endl;
  207. lf >> gs;
  208. tlog0 <<"Reading gamestate: "<<tmh.getDif()<<std::endl;
  209. CGI->state = gs;
  210. CGI->mh->map = gs->map;
  211. pathInfo = new CPathsInfo(int3(gs->map->width, gs->map->height, gs->map->twoLevel+1));
  212. CGI->mh->init();
  213. tlog0 <<"Initing maphandler: "<<tmh.getDif()<<std::endl;
  214. }
  215. waitForServer();
  216. tlog0 <<"Waiting for server: "<<tmh.getDif()<<std::endl;
  217. serv = new CConnection(conf.cc.server,portc,NAME);
  218. tlog0 <<"Setting up connection: "<<tmh.getDif()<<std::endl;
  219. ui8 pom8;
  220. *serv << ui8(3) << ui8(1); //load game; one client
  221. *serv << fname;
  222. *serv >> pom8;
  223. if(pom8)
  224. throw "Server cannot open the savegame!";
  225. else
  226. tlog0 << "Server opened savegame properly.\n";
  227. *serv << ui8(gs->scenarioOps->playerInfos.size()+1); //number of players + neutral
  228. for(size_t i=0;i<gs->scenarioOps->playerInfos.size();i++)
  229. {
  230. *serv << ui8(gs->scenarioOps->playerInfos[i].color); //players
  231. }
  232. *serv << ui8(255); // neutrals
  233. tlog0 <<"Sent info to server: "<<tmh.getDif()<<std::endl;
  234. {
  235. CLoadFile lf(fname + ".vcgm1");
  236. lf >> *this;
  237. }
  238. //for (size_t i=0; i<gs->scenarioOps->playerInfos.size();++i) //initializing interfaces for players
  239. //{
  240. // ui8 color = gs->scenarioOps->playerInfos[i].color;
  241. // CCallback *cb = new CCallback(gs,color,this);
  242. // if(!gs->scenarioOps->playerInfos[i].human) {
  243. // playerint[color] = static_cast<CGameInterface*>(CAIHandler::getNewAI(cb,conf.cc.defaultAI));
  244. // }
  245. // else {
  246. // playerint[color] = new CPlayerInterface(color,i);
  247. // }
  248. // gs->currentPlayer = color;
  249. // playerint[color]->init(cb);
  250. // tlog0 <<"Setting up interface for player "<< (int)color <<": "<<tmh.getDif()<<std::endl;
  251. //}
  252. //playerint[255] = CAIHandler::getNewAI(cb,conf.cc.defaultAI);
  253. //playerint[255]->init(new CCallback(gs,255,this));
  254. //tlog0 <<"Setting up interface for neutral \"player\"" << tmh.getDif() << std::endl;
  255. }
  256. int CClient::getCurrentPlayer()
  257. {
  258. return gs->currentPlayer;
  259. }
  260. int CClient::getSelectedHero()
  261. {
  262. return IGameCallback::getSelectedHero(getCurrentPlayer())->id;
  263. }
  264. void CClient::newGame( CConnection *con, StartInfo *si )
  265. {
  266. timeHandler tmh;
  267. CGI->state = new CGameState();
  268. tlog0 <<"\tGamestate: "<<tmh.getDif()<<std::endl;
  269. serv = con;
  270. CConnection &c(*con);
  271. ////////////////////////////////////////////////////
  272. ui8 pom8;
  273. c << ui8(2) << ui8(1); //new game; one client
  274. c << *si;
  275. c >> pom8;
  276. if(pom8)
  277. throw "Server cannot open the map!";
  278. else
  279. tlog0 << "Server opened map properly.\n";
  280. c << ui8(si->playerInfos.size()+1); //number of players + neutral
  281. for(size_t i=0;i<si->playerInfos.size();i++)
  282. {
  283. c << ui8(si->playerInfos[i].color); //players
  284. }
  285. c << ui8(255); // neutrals
  286. ui32 seed, sum;
  287. std::string mapname;
  288. c >> mapname >> sum >> seed;
  289. tlog0 <<"\tSending/Getting info to/from the server: "<<tmh.getDif()<<std::endl;
  290. Mapa * mapa = new Mapa(mapname);
  291. tlog0 <<"Reading and detecting map file (together): "<<tmh.getDif()<<std::endl;
  292. tlog0 << "\tServer checksum for "<<mapname <<": "<<sum << std::endl;
  293. tlog0 << "\tOur checksum for the map: "<< mapa->checksum << std::endl;
  294. if(mapa->checksum != sum)
  295. {
  296. tlog1 << "Wrong map checksum!!!" << std::endl;
  297. throw std::string("Wrong checksum");
  298. }
  299. tlog0 << "\tUsing random seed: "<<seed << std::endl;
  300. gs = CGI->state;
  301. gs->scenarioOps = si;
  302. gs->init(si,mapa,seed);
  303. CGI->mh = new CMapHandler();
  304. tlog0 <<"Initializing GameState (together): "<<tmh.getDif()<<std::endl;
  305. CGI->mh->map = mapa;
  306. tlog0 <<"Creating mapHandler: "<<tmh.getDif()<<std::endl;
  307. CGI->mh->init();
  308. pathInfo = new CPathsInfo(int3(mapa->width, mapa->height, mapa->twoLevel+1));
  309. tlog0 <<"Initializing mapHandler (together): "<<tmh.getDif()<<std::endl;
  310. for (size_t i=0; i<gs->scenarioOps->playerInfos.size();++i) //initializing interfaces for players
  311. {
  312. ui8 color = gs->scenarioOps->playerInfos[i].color;
  313. CCallback *cb = new CCallback(gs,color,this);
  314. if(!gs->scenarioOps->playerInfos[i].human) {
  315. playerint[color] = static_cast<CGameInterface*>(CAIHandler::getNewAI(cb,conf.cc.defaultAI));
  316. }
  317. else {
  318. playerint[color] = new CPlayerInterface(color,i);
  319. }
  320. gs->currentPlayer = color;
  321. playerint[color]->init(cb);
  322. }
  323. playerint[255] = CAIHandler::getNewAI(cb,conf.cc.defaultAI);
  324. playerint[255]->init(new CCallback(gs,255,this));
  325. }
  326. void CClient::runServer(const char * portc)
  327. {
  328. static std::string comm = std::string(SERVER_NAME) + " " + portc + " > server_log.txt"; //needs to be static, if not - will be probably destroyed before new thread reads it
  329. boost::thread servthr(boost::bind(system,comm.c_str())); //runs server executable; //TODO: will it work on non-windows platforms?
  330. }
  331. void CClient::waitForServer()
  332. {
  333. intpr::scoped_lock<intpr::interprocess_mutex> slock(shared->sr->mutex);
  334. while(!shared->sr->ready)
  335. {
  336. shared->sr->cond.wait(slock);
  337. }
  338. }
  339. template <typename Handler>
  340. void CClient::serialize( Handler &h, const int version )
  341. {
  342. if(h.saving)
  343. {
  344. ui8 players = playerint.size();
  345. h & players;
  346. for(std::map<ui8,CGameInterface *>::iterator i = playerint.begin(); i != playerint.end(); i++)
  347. {
  348. h & i->first & i->second->dllName;
  349. i->second->serialize(h,version);
  350. }
  351. }
  352. else
  353. {
  354. ui8 players;
  355. h & players;
  356. for(int i=0; i < players; i++)
  357. {
  358. std::string dllname;
  359. ui8 pid;
  360. h & pid & dllname;
  361. CCallback *callback = new CCallback(gs,pid,this);
  362. callbacks.insert(callback);
  363. CGameInterface *nInt = NULL;
  364. if(dllname.length())
  365. nInt = CAIHandler::getNewAI(callback,dllname);
  366. else
  367. nInt = new CPlayerInterface(pid,i);
  368. playerint[pid] = nInt;
  369. nInt->init(callback);
  370. nInt->serialize(h, version);
  371. }
  372. }
  373. }
  374. //void CClient::sendRequest( const CPackForServer *request, bool waitForRealization )
  375. //{
  376. // if(waitForRealization)
  377. // waitingRequest.set(true);
  378. //
  379. // *serv << request;
  380. //
  381. // if(waitForRealization)
  382. // waitingRequest.waitWhileTrue();
  383. //}
  384. template void CClient::serialize( CISer<CLoadFile> &h, const int version );
  385. template void CClient::serialize( COSer<CSaveFile> &h, const int version );