CVCMIServer.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. #include "../hch/CCampaignHandler.h"
  2. #include <iostream>
  3. #include <string>
  4. #include <boost/asio.hpp>
  5. #include "../global.h"
  6. #include "../lib/Connection.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 "zlib.h"
  16. #ifndef __GNUC__
  17. #include <tchar.h>
  18. #endif
  19. #include "CVCMIServer.h"
  20. #include <boost/crc.hpp>
  21. #include <boost/interprocess/mapped_region.hpp>
  22. #include <boost/interprocess/shared_memory_object.hpp>
  23. #include "../StartInfo.h"
  24. #include "../lib/map.h"
  25. #include "../hch/CLodHandler.h"
  26. #include "../lib/Interprocess.h"
  27. #include "../lib/VCMI_Lib.h"
  28. #include "../lib/VCMIDirs.h"
  29. #include "CGameHandler.h"
  30. std::string NAME_AFFIX = "server";
  31. std::string NAME = NAME_VER + std::string(" (") + NAME_AFFIX + ')'; //application name
  32. using namespace boost;
  33. using namespace boost::asio;
  34. using namespace boost::asio::ip;
  35. namespace intpr = boost::interprocess;
  36. bool end2 = false;
  37. int port = 3030;
  38. VCMIDirs GVCMIDirs;
  39. /*
  40. * CVCMIServer.cpp, part of VCMI engine
  41. *
  42. * Authors: listed in file AUTHORS in main folder
  43. *
  44. * License: GNU General Public License v2.0 or later
  45. * Full text of license available in license.txt file, in main folder
  46. *
  47. */
  48. static void vaccept(tcp::acceptor *ac, tcp::socket *s, boost::system::error_code *error)
  49. {
  50. ac->accept(*s,*error);
  51. }
  52. CVCMIServer::CVCMIServer()
  53. : io(new io_service()), acceptor(new tcp::acceptor(*io, tcp::endpoint(tcp::v4(), port)))
  54. {
  55. tlog4 << "CVCMIServer created!" <<std::endl;
  56. }
  57. CVCMIServer::~CVCMIServer()
  58. {
  59. //delete io;
  60. //delete acceptor;
  61. }
  62. void CVCMIServer::newGame(CConnection *c)
  63. {
  64. CGameHandler gh;
  65. boost::system::error_code error;
  66. StartInfo *si = new StartInfo;
  67. ui8 clients;
  68. *c >> clients; //how many clients should be connected - TODO: support more than one
  69. *c >> *si; //get start options
  70. int problem;
  71. #ifdef _MSC_VER
  72. FILE *f;
  73. problem = fopen_s(&f,si->mapname.c_str(),"r");
  74. #else
  75. FILE * f = fopen(si->mapname.c_str(),"r");
  76. problem = !f;
  77. #endif
  78. if(problem && si->mode == 0) //TODO some checking for campaigns
  79. {
  80. *c << ui8(problem); //WRONG!
  81. return;
  82. }
  83. else
  84. {
  85. if(f)
  86. fclose(f);
  87. *c << ui8(0); //OK!
  88. }
  89. StartInfo startInfoCpy = *si;
  90. gh.init(si,rand());
  91. c->addStdVecItems(gh.gs);
  92. CConnection* cc; //tcp::socket * ss;
  93. for(int i=0; i<clients; i++)
  94. {
  95. if(!i)
  96. {
  97. cc=c;
  98. }
  99. else
  100. {
  101. tcp::socket * s = new tcp::socket(acceptor->io_service());
  102. acceptor->accept(*s,error);
  103. if(error) //retry
  104. {
  105. tlog3<<"Cannot establish connection - retrying..." << std::endl;
  106. i--;
  107. continue;
  108. }
  109. cc = new CConnection(s,NAME);
  110. cc->addStdVecItems(gh.gs);
  111. }
  112. gh.conns.insert(cc);
  113. }
  114. gh.run(false, &startInfoCpy);
  115. }
  116. void CVCMIServer::start()
  117. {
  118. ServerReady *sr = NULL;
  119. intpr::mapped_region *mr;
  120. try
  121. {
  122. intpr::shared_memory_object smo(intpr::open_only,"vcmi_memory",intpr::read_write);
  123. smo.truncate(sizeof(ServerReady));
  124. mr = new intpr::mapped_region(smo,intpr::read_write);
  125. sr = reinterpret_cast<ServerReady*>(mr->get_address());
  126. }
  127. catch(...)
  128. {
  129. intpr::shared_memory_object smo(intpr::create_only,"vcmi_memory",intpr::read_write);
  130. smo.truncate(sizeof(ServerReady));
  131. mr = new intpr::mapped_region(smo,intpr::read_write);
  132. sr = new(mr->get_address())ServerReady();
  133. }
  134. boost::system::error_code error;
  135. tlog0<<"Listening for connections at port " << acceptor->local_endpoint().port() << std::endl;
  136. tcp::socket * s = new tcp::socket(acceptor->io_service());
  137. boost::thread acc(boost::bind(vaccept,acceptor,s,&error));
  138. sr->setToTrueAndNotify();
  139. delete mr;
  140. acc.join();
  141. if (error)
  142. {
  143. tlog2<<"Got connection but there is an error " << std::endl << error;
  144. return;
  145. }
  146. tlog0<<"We've accepted someone... " << std::endl;
  147. CConnection *connection = new CConnection(s,NAME);
  148. tlog0<<"Got connection!" << std::endl;
  149. while(!end2)
  150. {
  151. ui8 mode;
  152. *connection >> mode;
  153. switch (mode)
  154. {
  155. case 0:
  156. connection->socket->close();
  157. exit(0);
  158. break;
  159. case 1:
  160. connection->socket->close();
  161. return;
  162. break;
  163. case 2:
  164. newGame(connection);
  165. break;
  166. case 3:
  167. loadGame(connection);
  168. break;
  169. }
  170. }
  171. }
  172. void CVCMIServer::loadGame( CConnection *c )
  173. {
  174. std::string fname;
  175. CGameHandler gh;
  176. boost::system::error_code error;
  177. ui8 clients;
  178. *c >> clients >> fname; //how many clients should be connected - TODO: support more than one
  179. {
  180. ui32 ver;
  181. char sig[8];
  182. CMapHeader dum;
  183. StartInfo *si;
  184. CLoadFile lf(fname + ".vlgm1");
  185. lf >> sig >> dum >> si;
  186. tlog0 <<"Reading save signature"<<std::endl;
  187. lf >> *VLC;
  188. tlog0 <<"Reading handlers"<<std::endl;
  189. lf >> (gh.gs);
  190. c->addStdVecItems(gh.gs);
  191. tlog0 <<"Reading gamestate"<<std::endl;
  192. }
  193. {
  194. CLoadFile lf(fname + ".vsgm1");
  195. lf >> gh;
  196. }
  197. *c << ui8(0);
  198. CConnection* cc; //tcp::socket * ss;
  199. for(int i=0; i<clients; i++)
  200. {
  201. if(!i)
  202. {
  203. cc=c;
  204. }
  205. else
  206. {
  207. tcp::socket * s = new tcp::socket(acceptor->io_service());
  208. acceptor->accept(*s,error);
  209. if(error) //retry
  210. {
  211. tlog3<<"Cannot establish connection - retrying..." << std::endl;
  212. i--;
  213. continue;
  214. }
  215. cc = new CConnection(s,NAME);
  216. cc->addStdVecItems(gh.gs);
  217. }
  218. gh.conns.insert(cc);
  219. }
  220. gh.run(true);
  221. }
  222. #ifndef __GNUC__
  223. int _tmain(int argc, _TCHAR* argv[])
  224. #else
  225. int main(int argc, char** argv)
  226. #endif
  227. {
  228. logfile = new std::ofstream("VCMI_Server_log.txt");
  229. console = new CConsoleHandler;
  230. //boost::thread t(boost::bind(&CConsoleHandler::run,::console));
  231. if(argc > 1)
  232. {
  233. #ifdef _MSC_VER
  234. port = _tstoi(argv[1]);
  235. #else
  236. port = _ttoi(argv[1]);
  237. #endif
  238. }
  239. tlog0 << "Port " << port << " will be used." << std::endl;
  240. CLodHandler h3bmp;
  241. h3bmp.init(DATA_DIR "/Data/H3bitmap.lod", DATA_DIR "/Data");
  242. initDLL(console,logfile);
  243. srand ( (unsigned int)time(NULL) );
  244. try
  245. {
  246. io_service io_service;
  247. CVCMIServer server;
  248. while(!end2)
  249. {
  250. server.start();
  251. }
  252. io_service.run();
  253. }
  254. catch(boost::system::system_error &e) //for boost errors just log, not crash - probably client shut down connection
  255. {
  256. tlog1 << e.what() << std::endl;
  257. end2 = true;
  258. }HANDLE_EXCEPTION
  259. return 0;
  260. }