Client.cpp 10.0 KB

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