CVCMIServer.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #include <iostream>
  2. #include <string>
  3. #include <boost/asio.hpp>
  4. #include "../global.h"
  5. #include "../lib/Connection.h"
  6. #include "zlib.h"
  7. #ifndef __GNUC__
  8. #include <tchar.h>
  9. #endif
  10. #include "CVCMIServer.h"
  11. #include <boost/crc.hpp>
  12. #include <boost/serialization/split_member.hpp>
  13. #include "../StartInfo.h"
  14. #include "../map.h"
  15. #include "../hch/CLodHandler.h"
  16. #include "../lib/VCMI_Lib.h"
  17. #include "CGameHandler.h"
  18. std::string NAME = NAME_VER + std::string(" (server)");
  19. using boost::asio::ip::tcp;
  20. using namespace boost;
  21. using namespace boost::asio;
  22. using namespace boost::asio::ip;
  23. bool end2 = false;
  24. int port = 3030;
  25. CVCMIServer::CVCMIServer()
  26. : io(new io_service()), acceptor(new tcp::acceptor(*io, tcp::endpoint(tcp::v4(), port)))
  27. {
  28. std::cout << "CVCMIServer created!" <<std::endl;
  29. }
  30. CVCMIServer::~CVCMIServer()
  31. {
  32. //delete io;
  33. //delete acceptor;
  34. }
  35. void CVCMIServer::newGame(CConnection *c)
  36. {
  37. CGameHandler gh;
  38. boost::system::error_code error;
  39. StartInfo *si = new StartInfo;
  40. ui8 clients;
  41. *c >> clients; //how many clients should be connected - TODO: support more than one
  42. *c >> *si; //get start options
  43. int problem;
  44. #ifdef _MSC_VER
  45. FILE *f;
  46. problem = fopen_s(&f,si->mapname.c_str(),"r");
  47. #else
  48. FILE * f = fopen(si->mapname.c_str(),"r");
  49. problem = !f;
  50. #endif
  51. if(problem)
  52. {
  53. *c << ui8(problem); //WRONG!
  54. return;
  55. }
  56. else
  57. {
  58. fclose(f);
  59. *c << ui8(0); //OK!
  60. }
  61. gh.init(si,rand());
  62. CConnection* cc; //tcp::socket * ss;
  63. for(int i=0; i<clients; i++)
  64. {
  65. if(!i)
  66. {
  67. cc=c;
  68. }
  69. else
  70. {
  71. tcp::socket * s = new tcp::socket(acceptor->io_service());
  72. acceptor->accept(*s,error);
  73. if(error) //retry
  74. {
  75. std::cout<<"Cannot establish connection - retrying..." << std::endl;
  76. i--;
  77. continue;
  78. }
  79. cc = new CConnection(s,NAME,std::cout);
  80. }
  81. gh.conns.insert(cc);
  82. }
  83. gh.run();
  84. }
  85. void CVCMIServer::start()
  86. {
  87. boost::system::error_code error;
  88. std::cout<<"Listening for connections at port " << acceptor->local_endpoint().port() << std::endl;
  89. tcp::socket * s = new tcp::socket(acceptor->io_service());
  90. acceptor->accept(*s,error);
  91. if (error)
  92. {
  93. std::cout<<"Got connection but there is an error " << std::endl << error;
  94. return;
  95. }
  96. std::cout<<"We've accepted someone... " << std::endl;
  97. CConnection *connection = new CConnection(s,NAME,std::cout);
  98. std::cout<<"Got connection!" << std::endl;
  99. while(!end2)
  100. {
  101. uint8_t mode;
  102. *connection >> mode;
  103. switch (mode)
  104. {
  105. case 0:
  106. connection->socket->close();
  107. exit(0);
  108. break;
  109. case 1:
  110. connection->socket->close();
  111. return;
  112. break;
  113. case 2:
  114. newGame(connection);
  115. break;
  116. }
  117. }
  118. }
  119. #ifndef __GNUC__
  120. int _tmain(int argc, _TCHAR* argv[])
  121. #else
  122. int main(int argc, char** argv)
  123. #endif
  124. {
  125. if(argc > 1)
  126. {
  127. #ifdef _MSC_VER
  128. port = _tstoi(argv[1]);
  129. #else
  130. port = _ttoi(argv[1]);
  131. #endif
  132. }
  133. std::cout << "Port " << port << " will be used." << std::endl;
  134. CLodHandler h3bmp;
  135. h3bmp.init("Data" PATHSEPARATOR "H3bitmap.lod","Data");
  136. initDLL(&h3bmp);
  137. srand ( (unsigned int)time(NULL) );
  138. try
  139. {
  140. io_service io_service;
  141. CVCMIServer server;
  142. while(!end2)
  143. server.start();
  144. io_service.run();
  145. } HANDLE_EXCEPTION
  146. return 0;
  147. }