2
0

Connection.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Connection.h, 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. #pragma once
  11. #include "BinaryDeserializer.h"
  12. #include "BinarySerializer.h"
  13. #if BOOST_VERSION >= 107000 // Boost version >= 1.70
  14. #include <boost/asio.hpp>
  15. typedef boost::asio::basic_stream_socket < boost::asio::ip::tcp > TSocket;
  16. typedef boost::asio::basic_socket_acceptor < boost::asio::ip::tcp > TAcceptor;
  17. #else
  18. namespace boost
  19. {
  20. namespace asio
  21. {
  22. namespace ip
  23. {
  24. class tcp;
  25. }
  26. #if BOOST_VERSION >= 106600 // Boost version >= 1.66
  27. class io_context;
  28. typedef io_context io_service;
  29. #else
  30. class io_service;
  31. #endif
  32. template <typename Protocol> class stream_socket_service;
  33. template <typename Protocol,typename StreamSocketService>
  34. class basic_stream_socket;
  35. template <typename Protocol> class socket_acceptor_service;
  36. template <typename Protocol,typename SocketAcceptorService>
  37. class basic_socket_acceptor;
  38. }
  39. class mutex;
  40. }
  41. typedef boost::asio::basic_stream_socket < boost::asio::ip::tcp , boost::asio::stream_socket_service<boost::asio::ip::tcp> > TSocket;
  42. typedef boost::asio::basic_socket_acceptor<boost::asio::ip::tcp, boost::asio::socket_acceptor_service<boost::asio::ip::tcp> > TAcceptor;
  43. #endif
  44. VCMI_LIB_NAMESPACE_BEGIN
  45. struct CPack;
  46. /// Main class for network communication
  47. /// Allows establishing connection and bidirectional read-write
  48. class DLL_LINKAGE CConnection
  49. : public IBinaryReader, public IBinaryWriter, public std::enable_shared_from_this<CConnection>
  50. {
  51. void init();
  52. void reportState(vstd::CLoggerBase * out) override;
  53. int write(const void * data, unsigned size) override;
  54. int read(void * data, unsigned size) override;
  55. std::shared_ptr<boost::asio::io_service> io_service; //can be empty if connection made from socket
  56. public:
  57. BinaryDeserializer iser;
  58. BinarySerializer oser;
  59. std::shared_ptr<boost::mutex> mutexRead;
  60. std::shared_ptr<boost::mutex> mutexWrite;
  61. std::shared_ptr<TSocket> socket;
  62. bool connected;
  63. bool myEndianess, contactEndianess; //true if little endian, if endianness is different we'll have to revert received multi-byte vars
  64. std::string contactUuid;
  65. std::string name; //who uses this connection
  66. std::string uuid;
  67. int connectionID;
  68. std::shared_ptr<boost::thread> handler;
  69. CConnection(std::string host, ui16 port, std::string Name, std::string UUID);
  70. CConnection(std::shared_ptr<TAcceptor> acceptor, std::shared_ptr<boost::asio::io_service> Io_service, std::string Name, std::string UUID);
  71. CConnection(std::shared_ptr<TSocket> Socket, std::string Name, std::string UUID); //use immediately after accepting connection into socket
  72. void close();
  73. bool isOpen() const;
  74. template<class T>
  75. CConnection &operator&(const T&);
  76. virtual ~CConnection();
  77. CPack * retrievePack();
  78. void sendPack(const CPack * pack);
  79. void disableStackSendingByID();
  80. void enableStackSendingByID();
  81. void disableSmartPointerSerialization();
  82. void enableSmartPointerSerialization();
  83. void disableSmartVectorMemberSerialization();
  84. void enableSmartVectorMemberSerializatoin();
  85. void enterLobbyConnectionMode();
  86. void enterGameplayConnectionMode(CGameState * gs);
  87. std::string toString() const;
  88. template<class T>
  89. CConnection & operator>>(T &t)
  90. {
  91. iser & t;
  92. return * this;
  93. }
  94. template<class T>
  95. CConnection & operator<<(const T &t)
  96. {
  97. oser & t;
  98. return * this;
  99. }
  100. };
  101. VCMI_LIB_NAMESPACE_END