Client.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. *serv >> pack;
  109. tlog5 << "Received server message of type " << typeid(*pack).name() << std::endl;
  110. CBaseForCLApply *apply = applier->apps[typeList.getTypeID(pack)];
  111. if(apply)
  112. {
  113. apply->applyOnClBefore(this,pack);
  114. gs->apply(pack);
  115. apply->applyOnClAfter(this,pack);
  116. tlog5 << "Message successfully applied!\n";
  117. }
  118. else
  119. {
  120. tlog5 << "Message cannot be applied, cannot find applier!\n";
  121. }
  122. delete pack;
  123. pack = NULL;
  124. }
  125. }
  126. void CClient::close()
  127. {
  128. if(!serv)
  129. return;
  130. tlog3 << "Connection has been requested to be closed.\n";
  131. boost::unique_lock<boost::mutex>(*serv->wmx);
  132. *serv << &CloseServer();
  133. tlog3 << "Sent closing signal to the server\n";
  134. serv->close();
  135. tlog3 << "Our socket has been closed.\n";
  136. }
  137. void CClient::save(const std::string & fname)
  138. {
  139. if(gs->curB)
  140. {
  141. tlog1 << "Game cannot be saved during battle!\n";
  142. return;
  143. }
  144. *serv << &SaveGame(fname);
  145. }
  146. void CClient::load( const std::string & fname )
  147. {
  148. tlog0 <<"\n\nLoading procedure started!\n\n";
  149. timeHandler tmh;
  150. close(); //kill server
  151. tlog0 <<"Sent kill signal to the server: "<<tmh.getDif()<<std::endl;
  152. VLC->clear(); //delete old handlers
  153. delete CGI->mh;
  154. delete CGI->state;
  155. //TODO: del callbacks
  156. for(std::map<ui8,CGameInterface *>::iterator i = playerint.begin(); i!=playerint.end(); i++)
  157. {
  158. delete i->second; //delete player interfaces
  159. }
  160. tlog0 <<"Deleting old data: "<<tmh.getDif()<<std::endl;
  161. char portc[10];
  162. SDL_itoa(conf.cc.port,portc,10);
  163. runServer(portc); //create new server
  164. tlog0 <<"Restarting server: "<<tmh.getDif()<<std::endl;
  165. {
  166. ui32 ver;
  167. char sig[8];
  168. CMapHeader dum;
  169. CGI->mh = new CMapHandler();
  170. CLoadFile lf(fname + ".vlgm1");
  171. lf >> sig >> ver >> dum >> *sig;
  172. tlog0 <<"Reading save signature: "<<tmh.getDif()<<std::endl;
  173. lf >> *VLC;
  174. CGI->setFromLib();
  175. tlog0 <<"Reading handlers: "<<tmh.getDif()<<std::endl;
  176. lf >> gs;
  177. tlog0 <<"Reading gamestate: "<<tmh.getDif()<<std::endl;
  178. CGI->state = gs;
  179. CGI->mh->map = gs->map;
  180. CGI->mh->init();
  181. tlog0 <<"Initing maphandler: "<<tmh.getDif()<<std::endl;
  182. }
  183. waitForServer();
  184. tlog0 <<"Waiting for server: "<<tmh.getDif()<<std::endl;
  185. serv = new CConnection(conf.cc.server,portc,NAME);
  186. tlog0 <<"Setting up connection: "<<tmh.getDif()<<std::endl;
  187. ui8 pom8;
  188. *serv << ui8(3) << ui8(1); //load game; one client
  189. *serv << fname;
  190. *serv >> pom8;
  191. if(pom8)
  192. throw "Server cannot open the savegame!";
  193. else
  194. tlog0 << "Server opened savegame properly.\n";
  195. *serv << ui8(gs->scenarioOps->playerInfos.size()+1); //number of players + neutral
  196. for(size_t i=0;i<gs->scenarioOps->playerInfos.size();i++)
  197. {
  198. *serv << ui8(gs->scenarioOps->playerInfos[i].color); //players
  199. }
  200. *serv << ui8(255); // neutrals
  201. tlog0 <<"Sent info to server: "<<tmh.getDif()<<std::endl;
  202. for (size_t i=0; i<gs->scenarioOps->playerInfos.size();++i) //initializing interfaces for players
  203. {
  204. ui8 color = gs->scenarioOps->playerInfos[i].color;
  205. CCallback *cb = new CCallback(gs,color,this);
  206. if(!gs->scenarioOps->playerInfos[i].human) {
  207. playerint[color] = static_cast<CGameInterface*>(CAIHandler::getNewAI(cb,conf.cc.defaultAI));
  208. }
  209. else {
  210. playerint[color] = new CPlayerInterface(color,i);
  211. }
  212. gs->currentPlayer = color;
  213. playerint[color]->init(cb);
  214. tlog0 <<"Setting up interface for player "<< (int)color <<": "<<tmh.getDif()<<std::endl;
  215. }
  216. playerint[255] = CAIHandler::getNewAI(cb,conf.cc.defaultAI);
  217. playerint[255]->init(new CCallback(gs,255,this));
  218. tlog0 <<"Setting up interface for neutral \"player\"" << tmh.getDif() << std::endl;
  219. }
  220. int CClient::getCurrentPlayer()
  221. {
  222. return gs->currentPlayer;
  223. }
  224. int CClient::getSelectedHero()
  225. {
  226. return IGameCallback::getSelectedHero(getCurrentPlayer())->id;
  227. }
  228. void CClient::newGame( CConnection *con, StartInfo *si )
  229. {
  230. timeHandler tmh;
  231. CGI->state = new CGameState();
  232. tlog0 <<"\tGamestate: "<<tmh.getDif()<<std::endl;
  233. serv = con;
  234. CConnection &c(*con);
  235. ////////////////////////////////////////////////////
  236. ui8 pom8;
  237. c << ui8(2) << ui8(1); //new game; one client
  238. c << *si;
  239. c >> pom8;
  240. if(pom8)
  241. throw "Server cannot open the map!";
  242. else
  243. tlog0 << "Server opened map properly.\n";
  244. c << ui8(si->playerInfos.size()+1); //number of players + neutral
  245. for(size_t i=0;i<si->playerInfos.size();i++)
  246. {
  247. c << ui8(si->playerInfos[i].color); //players
  248. }
  249. c << ui8(255); // neutrals
  250. ui32 seed, sum;
  251. std::string mapname;
  252. c >> mapname >> sum >> seed;
  253. tlog0 <<"\tSending/Getting info to/from the server: "<<tmh.getDif()<<std::endl;
  254. Mapa * mapa = new Mapa(mapname);
  255. tlog0 <<"Reading and detecting map file (together): "<<tmh.getDif()<<std::endl;
  256. tlog0 << "\tServer checksum for "<<mapname <<": "<<sum << std::endl;
  257. tlog0 << "\tOur checksum for the map: "<< mapa->checksum << std::endl;
  258. if(mapa->checksum != sum)
  259. {
  260. tlog1 << "Wrong map checksum!!!" << std::endl;
  261. throw std::string("Wrong checksum");
  262. }
  263. tlog0 << "\tUsing random seed: "<<seed << std::endl;
  264. gs = CGI->state;
  265. gs->scenarioOps = si;
  266. gs->init(si,mapa,seed);
  267. CGI->mh = new CMapHandler();
  268. tlog0 <<"Initializing GameState (together): "<<tmh.getDif()<<std::endl;
  269. CGI->mh->map = mapa;
  270. tlog0 <<"Creating mapHandler: "<<tmh.getDif()<<std::endl;
  271. CGI->mh->init();
  272. tlog0 <<"Initializing mapHandler (together): "<<tmh.getDif()<<std::endl;
  273. for (size_t i=0; i<CGI->state->scenarioOps->playerInfos.size();++i) //initializing interfaces for players
  274. {
  275. ui8 color = gs->scenarioOps->playerInfos[i].color;
  276. CCallback *cb = new CCallback(gs,color,this);
  277. if(!gs->scenarioOps->playerInfos[i].human) {
  278. playerint[color] = static_cast<CGameInterface*>(CAIHandler::getNewAI(cb,conf.cc.defaultAI));
  279. }
  280. else {
  281. playerint[color] = new CPlayerInterface(color,i);
  282. }
  283. gs->currentPlayer = color;
  284. playerint[color]->init(cb);
  285. }
  286. playerint[255] = CAIHandler::getNewAI(cb,conf.cc.defaultAI);
  287. playerint[255]->init(new CCallback(gs,255,this));
  288. }
  289. void CClient::runServer(const char * portc)
  290. {
  291. 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
  292. boost::thread servthr(boost::bind(system,comm.c_str())); //runs server executable; //TODO: will it work on non-windows platforms?
  293. }
  294. void CClient::waitForServer()
  295. {
  296. intpr::scoped_lock<intpr::interprocess_mutex> slock(shared->sr->mutex);
  297. while(!shared->sr->ready)
  298. {
  299. shared->sr->cond.wait(slock);
  300. }
  301. }