Connection.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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(std::shared_ptr<EnetConnection> _c, std::string Name, std::string UUID)
  45. : enetConnection(_c), iser(this), oser(this), name(Name), uuid(UUID), connectionID(0), connected(false)
  46. {
  47. init();
  48. }
  49. int CConnection::write(const void * data, unsigned size)
  50. {
  51. if(connected)
  52. enetConnection->write(data, size);
  53. return size;
  54. }
  55. int CConnection::read(void * data, unsigned size)
  56. {
  57. if(connected)
  58. enetConnection->read(data, size);
  59. return size;
  60. }
  61. CConnection::~CConnection()
  62. {
  63. if(handler)
  64. handler->join();
  65. close();
  66. }
  67. template<class T>
  68. CConnection & CConnection::operator&(const T &t) {
  69. // throw std::exception();
  70. //XXX this is temporaly ? solution to fix gcc (4.3.3, other?) compilation
  71. // problem for more details contact [email protected] or [email protected]
  72. // do not remove this exception it shoudnt be called
  73. return *this;
  74. }
  75. const std::shared_ptr<EnetConnection> CConnection::getEnetConnection() const
  76. {
  77. return enetConnection;
  78. }
  79. void CConnection::close()
  80. {
  81. connected = false;
  82. if(enetConnection->isOpen())
  83. enetConnection->close();
  84. else
  85. enetConnection->kill();
  86. }
  87. bool CConnection::isOpen() const
  88. {
  89. return connected;
  90. }
  91. void CConnection::reportState(vstd::CLoggerBase * out)
  92. {
  93. out->debug("CConnection");
  94. /*if(socket && socket->is_open())
  95. {
  96. out->debug("\tWe have an open and valid socket");
  97. out->debug("\t %d bytes awaiting", socket->available());
  98. }*/
  99. }
  100. CPack * CConnection::retrievePack()
  101. {
  102. CPack * pack = nullptr;
  103. iser & pack;
  104. logNetwork->trace("Received CPack of type %s", (pack ? typeid(*pack).name() : "nullptr"));
  105. if(pack == nullptr)
  106. {
  107. logNetwork->error("Received a nullptr CPack! You should check whether client and server ABI matches.");
  108. }
  109. else
  110. {
  111. pack->c = this->shared_from_this();
  112. }
  113. return pack;
  114. }
  115. void CConnection::sendPack(const CPack * pack)
  116. {
  117. logNetwork->trace("Sending a pack of type %s", typeid(*pack).name());
  118. oser & pack;
  119. }
  120. void CConnection::disableStackSendingByID()
  121. {
  122. CSerializer::sendStackInstanceByIds = false;
  123. }
  124. void CConnection::enableStackSendingByID()
  125. {
  126. CSerializer::sendStackInstanceByIds = true;
  127. }
  128. void CConnection::disableSmartPointerSerialization()
  129. {
  130. iser.smartPointerSerialization = oser.smartPointerSerialization = false;
  131. }
  132. void CConnection::enableSmartPointerSerialization()
  133. {
  134. iser.smartPointerSerialization = oser.smartPointerSerialization = true;
  135. }
  136. void CConnection::enterLobbyConnectionMode()
  137. {
  138. iser.loadedPointers.clear();
  139. oser.savedPointers.clear();
  140. disableSmartVectorMemberSerialization();
  141. disableSmartPointerSerialization();
  142. }
  143. void CConnection::enterGameplayConnectionMode(CGameState * gs)
  144. {
  145. enableStackSendingByID();
  146. disableSmartPointerSerialization();
  147. addStdVecItems(gs);
  148. }
  149. void CConnection::disableSmartVectorMemberSerialization()
  150. {
  151. CSerializer::smartVectorMembersSerialization = false;
  152. }
  153. void CConnection::enableSmartVectorMemberSerializatoin()
  154. {
  155. CSerializer::smartVectorMembersSerialization = true;
  156. }
  157. std::string CConnection::toString() const
  158. {
  159. boost::format fmt("Connection with %s (ID: %d UUID: %s)");
  160. fmt % name % connectionID % uuid;
  161. return fmt.str();
  162. }
  163. VCMI_LIB_NAMESPACE_END