CVCMIServer.cpp 6.4 KB

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