Connection.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. * Connection.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "Connection.h"
  12. #include "../registerTypes/RegisterTypes.h"
  13. #include "../mapping/CMap.h"
  14. #include "../CGameState.h"
  15. #if BOOST_VERSION >= 106600
  16. #define BOOST_ASIO_ENABLE_OLD_SERVICES
  17. #endif
  18. #include <boost/asio.hpp>
  19. using namespace boost;
  20. using namespace boost::asio::ip;
  21. #if defined(__hppa__) || \
  22. defined(__m68k__) || defined(mc68000) || defined(_M_M68K) || \
  23. (defined(__MIPS__) && defined(__MISPEB__)) || \
  24. defined(__ppc__) || defined(__POWERPC__) || defined(_M_PPC) || \
  25. defined(__sparc__)
  26. #define BIG_ENDIAN
  27. #else
  28. #define LIL_ENDIAN
  29. #endif
  30. void CConnection::init()
  31. {
  32. socket->set_option(boost::asio::ip::tcp::no_delay(true));
  33. socket->set_option(boost::asio::socket_base::send_buffer_size(4194304));
  34. socket->set_option(boost::asio::socket_base::receive_buffer_size(4194304));
  35. enableSmartPointerSerialization();
  36. disableStackSendingByID();
  37. registerTypes(iser);
  38. registerTypes(oser);
  39. #ifdef LIL_ENDIAN
  40. myEndianess = true;
  41. #else
  42. myEndianess = false;
  43. #endif
  44. connected = true;
  45. std::string pom;
  46. //we got connection
  47. oser & std::string("Aiya!\n") & name & uuid & myEndianess; //identify ourselves
  48. iser & pom & pom & contactUuid & contactEndianess;
  49. logNetwork->info("Established connection with %s. UUID: %s", pom, contactUuid);
  50. mutexRead = std::make_shared<boost::mutex>();
  51. mutexWrite = std::make_shared<boost::mutex>();
  52. iser.fileVersion = SERIALIZATION_VERSION;
  53. }
  54. CConnection::CConnection(std::string host, ui16 port, std::string Name, std::string UUID)
  55. : iser(this), oser(this), io_service(std::make_shared<asio::io_service>()), connectionID(0), name(Name), uuid(UUID)
  56. {
  57. int i;
  58. boost::system::error_code error = asio::error::host_not_found;
  59. socket = std::make_shared<tcp::socket>(*io_service);
  60. tcp::resolver resolver(*io_service);
  61. tcp::resolver::iterator end, pom, endpoint_iterator = resolver.resolve(tcp::resolver::query(host, std::to_string(port)),error);
  62. if(error)
  63. {
  64. logNetwork->error("Problem with resolving: \n%s", error.message());
  65. goto connerror1;
  66. }
  67. pom = endpoint_iterator;
  68. if(pom != end)
  69. logNetwork->info("Found endpoints:");
  70. else
  71. {
  72. logNetwork->error("Critical problem: No endpoints found!");
  73. goto connerror1;
  74. }
  75. i=0;
  76. while(pom != end)
  77. {
  78. logNetwork->info("\t%d:%s", i, (boost::asio::ip::tcp::endpoint&)*pom);
  79. pom++;
  80. }
  81. i=0;
  82. while(endpoint_iterator != end)
  83. {
  84. logNetwork->info("Trying connection to %s(%d)", (boost::asio::ip::tcp::endpoint&)*endpoint_iterator, i++);
  85. socket->connect(*endpoint_iterator, error);
  86. if(!error)
  87. {
  88. init();
  89. return;
  90. }
  91. else
  92. {
  93. logNetwork->error("Problem with connecting: %s", error.message());
  94. }
  95. endpoint_iterator++;
  96. }
  97. //we shouldn't be here - error handling
  98. connerror1:
  99. logNetwork->error("Something went wrong... checking for error info");
  100. if(error)
  101. logNetwork->error(error.message());
  102. else
  103. logNetwork->error("No error info. ");
  104. throw std::runtime_error("Can't establish connection :(");
  105. }
  106. CConnection::CConnection(std::shared_ptr<TSocket> Socket, std::string Name, std::string UUID)
  107. : iser(this), oser(this), socket(Socket), io_service(&Socket->get_io_service()), connectionID(0), name(Name), uuid(UUID)
  108. {
  109. init();
  110. }
  111. CConnection::CConnection(std::shared_ptr<TAcceptor> acceptor, std::shared_ptr<boost::asio::io_service> Io_service, std::string Name, std::string UUID)
  112. : iser(this), oser(this), connectionID(0), name(Name), uuid(UUID)
  113. {
  114. boost::system::error_code error = asio::error::host_not_found;
  115. socket = std::make_shared<tcp::socket>(*io_service);
  116. acceptor->accept(*socket,error);
  117. if (error)
  118. {
  119. logNetwork->error("Error on accepting: %s", error.message());
  120. socket.reset();
  121. throw std::runtime_error("Can't establish connection :(");
  122. }
  123. init();
  124. }
  125. int CConnection::write(const void * data, unsigned size)
  126. {
  127. try
  128. {
  129. int ret;
  130. ret = asio::write(*socket,asio::const_buffers_1(asio::const_buffer(data,size)));
  131. return ret;
  132. }
  133. catch(...)
  134. {
  135. //connection has been lost
  136. connected = false;
  137. throw;
  138. }
  139. }
  140. int CConnection::read(void * data, unsigned size)
  141. {
  142. try
  143. {
  144. int ret = asio::read(*socket,asio::mutable_buffers_1(asio::mutable_buffer(data,size)));
  145. return ret;
  146. }
  147. catch(...)
  148. {
  149. //connection has been lost
  150. connected = false;
  151. throw;
  152. }
  153. }
  154. CConnection::~CConnection()
  155. {
  156. if(handler)
  157. handler->join();
  158. close();
  159. }
  160. template<class T>
  161. CConnection & CConnection::operator&(const T &t) {
  162. // throw std::exception();
  163. //XXX this is temporaly ? solution to fix gcc (4.3.3, other?) compilation
  164. // problem for more details contact [email protected] or [email protected]
  165. // do not remove this exception it shoudnt be called
  166. return *this;
  167. }
  168. void CConnection::close()
  169. {
  170. if(socket)
  171. {
  172. socket->close();
  173. socket.reset();
  174. }
  175. }
  176. bool CConnection::isOpen() const
  177. {
  178. return socket && connected;
  179. }
  180. void CConnection::reportState(vstd::CLoggerBase * out)
  181. {
  182. out->debug("CConnection");
  183. if(socket && socket->is_open())
  184. {
  185. out->debug("\tWe have an open and valid socket");
  186. out->debug("\t %d bytes awaiting", socket->available());
  187. }
  188. }
  189. CPack * CConnection::retrievePack()
  190. {
  191. CPack * pack = nullptr;
  192. boost::unique_lock<boost::mutex> lock(*mutexRead);
  193. iser & pack;
  194. logNetwork->trace("Received CPack of type %s", (pack ? typeid(*pack).name() : "nullptr"));
  195. if(pack == nullptr)
  196. {
  197. logNetwork->error("Received a nullptr CPack! You should check whether client and server ABI matches.");
  198. }
  199. else
  200. {
  201. pack->c = this->shared_from_this();
  202. }
  203. return pack;
  204. }
  205. void CConnection::sendPack(const CPack * pack)
  206. {
  207. boost::unique_lock<boost::mutex> lock(*mutexWrite);
  208. logNetwork->trace("Sending a pack of type %s", typeid(*pack).name());
  209. oser & pack;
  210. }
  211. void CConnection::disableStackSendingByID()
  212. {
  213. CSerializer::sendStackInstanceByIds = false;
  214. }
  215. void CConnection::enableStackSendingByID()
  216. {
  217. CSerializer::sendStackInstanceByIds = true;
  218. }
  219. void CConnection::disableSmartPointerSerialization()
  220. {
  221. iser.smartPointerSerialization = oser.smartPointerSerialization = false;
  222. }
  223. void CConnection::enableSmartPointerSerialization()
  224. {
  225. iser.smartPointerSerialization = oser.smartPointerSerialization = true;
  226. }
  227. void CConnection::enterLobbyConnectionMode()
  228. {
  229. iser.loadedPointers.clear();
  230. oser.savedPointers.clear();
  231. disableSmartVectorMemberSerialization();
  232. disableSmartPointerSerialization();
  233. }
  234. void CConnection::enterGameplayConnectionMode(CGameState * gs)
  235. {
  236. enableStackSendingByID();
  237. disableSmartPointerSerialization();
  238. addStdVecItems(gs);
  239. }
  240. void CConnection::disableSmartVectorMemberSerialization()
  241. {
  242. CSerializer::smartVectorMembersSerialization = false;
  243. }
  244. void CConnection::enableSmartVectorMemberSerializatoin()
  245. {
  246. CSerializer::smartVectorMembersSerialization = true;
  247. }
  248. std::string CConnection::toString() const
  249. {
  250. boost::format fmt("Connection with %s (ID: %d UUID: %s)");
  251. fmt % name % connectionID % uuid;
  252. return fmt.str();
  253. }