Client.cpp 9.8 KB

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