Connection.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. struct ConnectionBuffers;
  47. /// Main class for network communication
  48. /// Allows establishing connection and bidirectional read-write
  49. class DLL_LINKAGE CConnection
  50. : public IBinaryReader, public IBinaryWriter, public std::enable_shared_from_this<CConnection>
  51. {
  52. void init();
  53. void reportState(vstd::CLoggerBase * out) override;
  54. int write(const void * data, unsigned size) override;
  55. int read(void * data, unsigned size) override;
  56. void flushBuffers();
  57. std::shared_ptr<boost::asio::io_service> io_service; //can be empty if connection made from socket
  58. bool enableBufferedWrite;
  59. bool enableBufferedRead;
  60. std::unique_ptr<ConnectionBuffers> connectionBuffers;
  61. public:
  62. BinaryDeserializer iser;
  63. BinarySerializer oser;
  64. std::shared_ptr<boost::mutex> mutexRead;
  65. std::shared_ptr<boost::mutex> mutexWrite;
  66. std::shared_ptr<TSocket> socket;
  67. bool connected;
  68. bool myEndianess, contactEndianess; //true if little endian, if endianness is different we'll have to revert received multi-byte vars
  69. std::string contactUuid;
  70. std::string name; //who uses this connection
  71. std::string uuid;
  72. int connectionID;
  73. std::shared_ptr<boost::thread> handler;
  74. CConnection(std::string host, ui16 port, std::string Name, std::string UUID);
  75. CConnection(std::shared_ptr<TAcceptor> acceptor, std::shared_ptr<boost::asio::io_service> Io_service, std::string Name, std::string UUID);
  76. CConnection(std::shared_ptr<TSocket> Socket, std::string Name, std::string UUID); //use immediately after accepting connection into socket
  77. void close();
  78. bool isOpen() const;
  79. template<class T>
  80. CConnection &operator&(const T&);
  81. virtual ~CConnection();
  82. CPack * retrievePack();
  83. void sendPack(const CPack * pack);
  84. void disableStackSendingByID();
  85. void enableStackSendingByID();
  86. void disableSmartPointerSerialization();
  87. void enableSmartPointerSerialization();
  88. void disableSmartVectorMemberSerialization();
  89. void enableSmartVectorMemberSerializatoin();
  90. void enterLobbyConnectionMode();
  91. void enterGameplayConnectionMode(CGameState * gs);
  92. std::string toString() const;
  93. template<class T>
  94. CConnection & operator>>(T &t)
  95. {
  96. iser & t;
  97. return * this;
  98. }
  99. template<class T>
  100. CConnection & operator<<(const T &t)
  101. {
  102. oser & t;
  103. return * this;
  104. }
  105. };
  106. VCMI_LIB_NAMESPACE_END