Connection.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. VCMI_LIB_NAMESPACE_BEGIN
  16. #if defined(__hppa__) || \
  17. defined(__m68k__) || defined(mc68000) || defined(_M_M68K) || \
  18. (defined(__MIPS__) && defined(__MISPEB__)) || \
  19. defined(__ppc__) || defined(__POWERPC__) || defined(_M_PPC) || \
  20. defined(__sparc__)
  21. #define BIG_ENDIAN
  22. #else
  23. #define LIL_ENDIAN
  24. #endif
  25. void CConnection::init()
  26. {
  27. enableSmartPointerSerialization();
  28. disableStackSendingByID();
  29. registerTypes(iser);
  30. registerTypes(oser);
  31. #ifdef LIL_ENDIAN
  32. myEndianess = true;
  33. #else
  34. myEndianess = false;
  35. #endif
  36. std::string pom;
  37. //we got connection
  38. oser & std::string("Aiya!\n") & name & uuid & myEndianess; //identify ourselves
  39. iser & pom & pom & contactUuid & contactEndianess;
  40. logNetwork->info("Established connection with %s. UUID: %s", pom, contactUuid);
  41. connected = true;
  42. iser.fileVersion = SERIALIZATION_VERSION;
  43. }
  44. CConnection::CConnection(ENetHost * _client, ENetPeer * _peer, std::string Name, std::string UUID)
  45. : client(_client), peer(_peer), iser(this), oser(this), name(Name), uuid(UUID), connectionID(0), connected(false), channel(1)
  46. {
  47. //init();
  48. }
  49. CConnection::CConnection(ENetHost * _client, std::string host, ui16 port, std::string Name, std::string UUID)
  50. : client(_client), iser(this), oser(this), name(Name), uuid(UUID), connectionID(0), connected(false), channel(0)
  51. {
  52. ENetAddress address;
  53. enet_address_set_host(&address, host.c_str());
  54. address.port = port;
  55. peer = enet_host_connect(client, &address, 2, 0);
  56. if(peer == NULL)
  57. {
  58. throw std::runtime_error("Can't establish connection :(");
  59. }
  60. }
  61. void CConnection::dispatch(ENetPacket * packet)
  62. {
  63. boost::unique_lock<boost::mutex> lock(mutexRead);
  64. packets.push_back(packet);
  65. }
  66. const ENetPeer * CConnection::getPeer() const
  67. {
  68. return peer;
  69. }
  70. int CConnection::write(const void * data, unsigned size)
  71. {
  72. if(size == 0)
  73. return 0;
  74. boost::unique_lock<boost::mutex> lock(mutexWrite);
  75. ENetPacket * packet = enet_packet_create(data, size, ENET_PACKET_FLAG_RELIABLE);
  76. enet_peer_send(peer, channel, packet);
  77. }
  78. int CConnection::read(void * data, unsigned size)
  79. {
  80. const int timout = 1000;
  81. if(size == 0)
  82. return 0;
  83. for(int i = 0; packets.empty() && (i < timout || connected); ++i)
  84. {
  85. boost::this_thread::sleep(boost::posix_time::milliseconds(10));
  86. }
  87. boost::unique_lock<boost::mutex> lock(mutexRead);
  88. auto * packet = packets.front();
  89. packets.pop_front();
  90. if(packet->dataLength > 0)
  91. memcpy(data, packet->data, packet->dataLength);
  92. int ret = packet->dataLength;
  93. assert(ret == size);
  94. enet_packet_destroy(packet);
  95. return ret;
  96. }
  97. CConnection::~CConnection()
  98. {
  99. if(handler)
  100. handler->join();
  101. for(auto * packet : packets)
  102. enet_packet_destroy(packet);
  103. close();
  104. }
  105. template<class T>
  106. CConnection & CConnection::operator&(const T &t) {
  107. // throw std::exception();
  108. //XXX this is temporaly ? solution to fix gcc (4.3.3, other?) compilation
  109. // problem for more details contact [email protected] or [email protected]
  110. // do not remove this exception it shoudnt be called
  111. return *this;
  112. }
  113. void CConnection::close()
  114. {
  115. boost::unique_lock<boost::mutex> lock(mutexWrite);
  116. enet_peer_disconnect(peer, 0);
  117. enet_peer_reset(peer);
  118. }
  119. bool CConnection::isOpen() const
  120. {
  121. return connected;
  122. }
  123. void CConnection::reportState(vstd::CLoggerBase * out)
  124. {
  125. out->debug("CConnection");
  126. /*if(socket && socket->is_open())
  127. {
  128. out->debug("\tWe have an open and valid socket");
  129. out->debug("\t %d bytes awaiting", socket->available());
  130. }*/
  131. }
  132. CPack * CConnection::retrievePack()
  133. {
  134. CPack * pack = nullptr;
  135. iser & pack;
  136. logNetwork->trace("Received CPack of type %s", (pack ? typeid(*pack).name() : "nullptr"));
  137. if(pack == nullptr)
  138. {
  139. logNetwork->error("Received a nullptr CPack! You should check whether client and server ABI matches.");
  140. }
  141. else
  142. {
  143. pack->c = this->shared_from_this();
  144. }
  145. return pack;
  146. }
  147. void CConnection::sendPack(const CPack * pack)
  148. {
  149. logNetwork->trace("Sending a pack of type %s", typeid(*pack).name());
  150. oser & pack;
  151. }
  152. void CConnection::disableStackSendingByID()
  153. {
  154. CSerializer::sendStackInstanceByIds = false;
  155. }
  156. void CConnection::enableStackSendingByID()
  157. {
  158. CSerializer::sendStackInstanceByIds = true;
  159. }
  160. void CConnection::disableSmartPointerSerialization()
  161. {
  162. iser.smartPointerSerialization = oser.smartPointerSerialization = false;
  163. }
  164. void CConnection::enableSmartPointerSerialization()
  165. {
  166. iser.smartPointerSerialization = oser.smartPointerSerialization = true;
  167. }
  168. void CConnection::enterLobbyConnectionMode()
  169. {
  170. iser.loadedPointers.clear();
  171. oser.savedPointers.clear();
  172. disableSmartVectorMemberSerialization();
  173. disableSmartPointerSerialization();
  174. }
  175. void CConnection::enterGameplayConnectionMode(CGameState * gs)
  176. {
  177. enableStackSendingByID();
  178. disableSmartPointerSerialization();
  179. addStdVecItems(gs);
  180. }
  181. void CConnection::disableSmartVectorMemberSerialization()
  182. {
  183. CSerializer::smartVectorMembersSerialization = false;
  184. }
  185. void CConnection::enableSmartVectorMemberSerializatoin()
  186. {
  187. CSerializer::smartVectorMembersSerialization = true;
  188. }
  189. std::string CConnection::toString() const
  190. {
  191. boost::format fmt("Connection with %s (ID: %d UUID: %s)");
  192. fmt % name % connectionID % uuid;
  193. return fmt.str();
  194. }
  195. VCMI_LIB_NAMESPACE_END