Client.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. #include "../CCallback.h"
  2. #include "../CConsoleHandler.h"
  3. #include "../CGameInfo.h"
  4. #include "../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 "../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. applier = new CCLApplier;
  79. IObjectInterface::cb = this;
  80. serv = NULL;
  81. gs = NULL;
  82. cb = NULL;
  83. try
  84. {
  85. shared = new SharedMem();
  86. } HANDLE_EXCEPTION
  87. }
  88. CClient::CClient(void)
  89. {
  90. init();
  91. }
  92. CClient::CClient(CConnection *con, StartInfo *si)
  93. {
  94. init();
  95. newGame(con,si);
  96. }
  97. CClient::~CClient(void)
  98. {
  99. delete applier;
  100. delete shared;
  101. }
  102. void CClient::waitForMoveAndSend(int color)
  103. {
  104. try
  105. {
  106. BattleAction ba = playerint[color]->activeStack(gs->curB->activeStack);
  107. *serv << &MakeAction(ba);
  108. return;
  109. }HANDLE_EXCEPTION
  110. tlog1 << "We should not be here!" << std::endl;
  111. }
  112. void CClient::run()
  113. {
  114. try
  115. {
  116. CPack *pack;
  117. while(1)
  118. {
  119. //get the package from the server
  120. {
  121. boost::unique_lock<boost::mutex> lock(*serv->rmx);
  122. tlog5 << "Listening... ";
  123. *serv >> pack;
  124. tlog5 << "\treceived server message of type " << typeid(*pack).name() << std::endl;
  125. }
  126. CBaseForCLApply *apply = applier->apps[typeList.getTypeID(pack)]; //find the applier
  127. if(apply)
  128. {
  129. apply->applyOnClBefore(this,pack);
  130. tlog5 << "\tMade first apply on cl\n";
  131. gs->apply(pack);
  132. tlog5 << "\tApplied on gs\n";
  133. apply->applyOnClAfter(this,pack);
  134. tlog5 << "\tMade second apply on cl\n";
  135. }
  136. else
  137. {
  138. tlog1 << "Message cannot be applied, cannot find applier!\n";
  139. }
  140. delete pack;
  141. pack = NULL;
  142. }
  143. } HANDLE_EXCEPTION(tlog1 << "Lost connection to server, ending listening thread!\n");
  144. }
  145. void CClient::close()
  146. {
  147. if(!serv)
  148. return;
  149. tlog3 << "Connection has been requested to be closed.\n";
  150. boost::unique_lock<boost::mutex>(*serv->wmx);
  151. *serv << &CloseServer();
  152. tlog3 << "Sent closing signal to the server\n";
  153. serv->close();
  154. tlog3 << "Our socket has been closed.\n";
  155. }
  156. void CClient::save(const std::string & fname)
  157. {
  158. if(gs->curB)
  159. {
  160. tlog1 << "Game cannot be saved during battle!\n";
  161. return;
  162. }
  163. *serv << &SaveGame(fname);
  164. }
  165. void CClient::load( const std::string & fname )
  166. {
  167. tlog0 <<"\n\nLoading procedure started!\n\n";
  168. timeHandler tmh;
  169. close(); //kill server
  170. tlog0 <<"Sent kill signal to the server: "<<tmh.getDif()<<std::endl;
  171. VLC->clear(); //delete old handlers
  172. delete CGI->mh;
  173. delete CGI->state;
  174. //TODO: del callbacks
  175. for(std::map<ui8,CGameInterface *>::iterator i = playerint.begin(); i!=playerint.end(); i++)
  176. {
  177. delete i->second; //delete player interfaces
  178. }
  179. tlog0 <<"Deleting old data: "<<tmh.getDif()<<std::endl;
  180. char portc[10];
  181. SDL_itoa(conf.cc.port,portc,10);
  182. runServer(portc); //create new server
  183. tlog0 <<"Restarting server: "<<tmh.getDif()<<std::endl;
  184. {
  185. ui32 ver;
  186. char sig[8];
  187. CMapHeader dum;
  188. CGI->mh = new CMapHandler();
  189. CLoadFile lf(fname + ".vlgm1");
  190. lf >> sig >> ver >> dum >> *sig;
  191. tlog0 <<"Reading save signature: "<<tmh.getDif()<<std::endl;
  192. lf >> *VLC;
  193. CGI->setFromLib();
  194. tlog0 <<"Reading handlers: "<<tmh.getDif()<<std::endl;
  195. lf >> gs;
  196. tlog0 <<"Reading gamestate: "<<tmh.getDif()<<std::endl;
  197. CGI->state = gs;
  198. CGI->mh->map = gs->map;
  199. CGI->mh->init();
  200. tlog0 <<"Initing maphandler: "<<tmh.getDif()<<std::endl;
  201. }
  202. waitForServer();
  203. tlog0 <<"Waiting for server: "<<tmh.getDif()<<std::endl;
  204. serv = new CConnection(conf.cc.server,portc,NAME);
  205. tlog0 <<"Setting up connection: "<<tmh.getDif()<<std::endl;
  206. ui8 pom8;
  207. *serv << ui8(3) << ui8(1); //load game; one client
  208. *serv << fname;
  209. *serv >> pom8;
  210. if(pom8)
  211. throw "Server cannot open the savegame!";
  212. else
  213. tlog0 << "Server opened savegame properly.\n";
  214. *serv << ui8(gs->scenarioOps->playerInfos.size()+1); //number of players + neutral
  215. for(size_t i=0;i<gs->scenarioOps->playerInfos.size();i++)
  216. {
  217. *serv << ui8(gs->scenarioOps->playerInfos[i].color); //players
  218. }
  219. *serv << ui8(255); // neutrals
  220. tlog0 <<"Sent info to server: "<<tmh.getDif()<<std::endl;
  221. for (size_t i=0; i<gs->scenarioOps->playerInfos.size();++i) //initializing interfaces for players
  222. {
  223. ui8 color = gs->scenarioOps->playerInfos[i].color;
  224. CCallback *cb = new CCallback(gs,color,this);
  225. if(!gs->scenarioOps->playerInfos[i].human) {
  226. playerint[color] = static_cast<CGameInterface*>(CAIHandler::getNewAI(cb,conf.cc.defaultAI));
  227. }
  228. else {
  229. playerint[color] = new CPlayerInterface(color,i);
  230. }
  231. gs->currentPlayer = color;
  232. playerint[color]->init(cb);
  233. tlog0 <<"Setting up interface for player "<< (int)color <<": "<<tmh.getDif()<<std::endl;
  234. }
  235. playerint[255] = CAIHandler::getNewAI(cb,conf.cc.defaultAI);
  236. playerint[255]->init(new CCallback(gs,255,this));
  237. tlog0 <<"Setting up interface for neutral \"player\"" << tmh.getDif() << std::endl;
  238. }
  239. int CClient::getCurrentPlayer()
  240. {
  241. return gs->currentPlayer;
  242. }
  243. int CClient::getSelectedHero()
  244. {
  245. return IGameCallback::getSelectedHero(getCurrentPlayer())->id;
  246. }
  247. void CClient::newGame( CConnection *con, StartInfo *si )
  248. {
  249. timeHandler tmh;
  250. CGI->state = new CGameState();
  251. tlog0 <<"\tGamestate: "<<tmh.getDif()<<std::endl;
  252. serv = con;
  253. CConnection &c(*con);
  254. ////////////////////////////////////////////////////
  255. ui8 pom8;
  256. c << ui8(2) << ui8(1); //new game; one client
  257. c << *si;
  258. c >> pom8;
  259. if(pom8)
  260. throw "Server cannot open the map!";
  261. else
  262. tlog0 << "Server opened map properly.\n";
  263. c << ui8(si->playerInfos.size()+1); //number of players + neutral
  264. for(size_t i=0;i<si->playerInfos.size();i++)
  265. {
  266. c << ui8(si->playerInfos[i].color); //players
  267. }
  268. c << ui8(255); // neutrals
  269. ui32 seed, sum;
  270. std::string mapname;
  271. c >> mapname >> sum >> seed;
  272. tlog0 <<"\tSending/Getting info to/from the server: "<<tmh.getDif()<<std::endl;
  273. Mapa * mapa = new Mapa(mapname);
  274. tlog0 <<"Reading and detecting map file (together): "<<tmh.getDif()<<std::endl;
  275. tlog0 << "\tServer checksum for "<<mapname <<": "<<sum << std::endl;
  276. tlog0 << "\tOur checksum for the map: "<< mapa->checksum << std::endl;
  277. if(mapa->checksum != sum)
  278. {
  279. tlog1 << "Wrong map checksum!!!" << std::endl;
  280. throw std::string("Wrong checksum");
  281. }
  282. tlog0 << "\tUsing random seed: "<<seed << std::endl;
  283. gs = CGI->state;
  284. gs->scenarioOps = si;
  285. gs->init(si,mapa,seed);
  286. CGI->mh = new CMapHandler();
  287. tlog0 <<"Initializing GameState (together): "<<tmh.getDif()<<std::endl;
  288. CGI->mh->map = mapa;
  289. tlog0 <<"Creating mapHandler: "<<tmh.getDif()<<std::endl;
  290. CGI->mh->init();
  291. tlog0 <<"Initializing mapHandler (together): "<<tmh.getDif()<<std::endl;
  292. for (size_t i=0; i<CGI->state->scenarioOps->playerInfos.size();++i) //initializing interfaces for players
  293. {
  294. ui8 color = gs->scenarioOps->playerInfos[i].color;
  295. CCallback *cb = new CCallback(gs,color,this);
  296. if(!gs->scenarioOps->playerInfos[i].human) {
  297. playerint[color] = static_cast<CGameInterface*>(CAIHandler::getNewAI(cb,conf.cc.defaultAI));
  298. }
  299. else {
  300. playerint[color] = new CPlayerInterface(color,i);
  301. }
  302. gs->currentPlayer = color;
  303. playerint[color]->init(cb);
  304. }
  305. playerint[255] = CAIHandler::getNewAI(cb,conf.cc.defaultAI);
  306. playerint[255]->init(new CCallback(gs,255,this));
  307. }
  308. void CClient::runServer(const char * portc)
  309. {
  310. 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
  311. boost::thread servthr(boost::bind(system,comm.c_str())); //runs server executable; //TODO: will it work on non-windows platforms?
  312. }
  313. void CClient::waitForServer()
  314. {
  315. intpr::scoped_lock<intpr::interprocess_mutex> slock(shared->sr->mutex);
  316. while(!shared->sr->ready)
  317. {
  318. shared->sr->cond.wait(slock);
  319. }
  320. }
  321. template <typename Handler>
  322. void CClient::serialize( Handler &h, const int version )
  323. {
  324. if(h.saving)
  325. {
  326. ui8 players = playerint.size();
  327. h & players;
  328. for(std::map<ui8,CGameInterface *>::iterator i = playerint.begin(); i != playerint.end(); i++)
  329. {
  330. h & i->first & i->second->dllName;
  331. i->second->serialize(h,version);
  332. }
  333. }
  334. else
  335. {
  336. ui8 players;
  337. h & players;
  338. for(int i=0; i < players; i++)
  339. {
  340. std::string dllname;
  341. ui8 pid;
  342. h & pid & dllname;
  343. if(dllname.length())
  344. {
  345. CCallback *callback = new CCallback(gs,pid,this);
  346. callbacks.insert(callback);
  347. playerint[pid] = CAIHandler::getNewAI(callback,dllname);
  348. playerint[pid]->init(callback);
  349. }
  350. }
  351. }
  352. }
  353. template void CClient::serialize( CISer<CLoadFile> &h, const int version );
  354. template void CClient::serialize( COSer<CSaveFile> &h, const int version );