Connection.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. class streambuf;
  23. namespace ip
  24. {
  25. class tcp;
  26. }
  27. #if BOOST_VERSION >= 106600 // Boost version >= 1.66
  28. class io_context;
  29. typedef io_context io_service;
  30. #else
  31. class io_service;
  32. #endif
  33. template <typename Protocol> class stream_socket_service;
  34. template <typename Protocol,typename StreamSocketService>
  35. class basic_stream_socket;
  36. template <typename Protocol> class socket_acceptor_service;
  37. template <typename Protocol,typename SocketAcceptorService>
  38. class basic_socket_acceptor;
  39. }
  40. class mutex;
  41. }
  42. typedef boost::asio::basic_stream_socket < boost::asio::ip::tcp , boost::asio::stream_socket_service<boost::asio::ip::tcp> > TSocket;
  43. typedef boost::asio::basic_socket_acceptor<boost::asio::ip::tcp, boost::asio::socket_acceptor_service<boost::asio::ip::tcp> > TAcceptor;
  44. #endif
  45. VCMI_LIB_NAMESPACE_BEGIN
  46. struct CPack;
  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. boost::asio::streambuf writeBuffer;
  60. bool enableBufferedRead;
  61. boost::asio::streambuf readBuffer;
  62. public:
  63. BinaryDeserializer iser;
  64. BinarySerializer oser;
  65. std::shared_ptr<boost::mutex> mutexRead;
  66. std::shared_ptr<boost::mutex> mutexWrite;
  67. std::shared_ptr<TSocket> socket;
  68. bool connected;
  69. bool myEndianess, contactEndianess; //true if little endian, if endianness is different we'll have to revert received multi-byte vars
  70. std::string contactUuid;
  71. std::string name; //who uses this connection
  72. std::string uuid;
  73. int connectionID;
  74. std::shared_ptr<boost::thread> handler;
  75. CConnection(std::string host, ui16 port, std::string Name, std::string UUID);
  76. CConnection(std::shared_ptr<TAcceptor> acceptor, std::shared_ptr<boost::asio::io_service> Io_service, std::string Name, std::string UUID);
  77. CConnection(std::shared_ptr<TSocket> Socket, std::string Name, std::string UUID); //use immediately after accepting connection into socket
  78. void close();
  79. bool isOpen() const;
  80. template<class T>
  81. CConnection &operator&(const T&);
  82. virtual ~CConnection();
  83. CPack * retrievePack();
  84. void sendPack(const CPack * pack);
  85. void disableStackSendingByID();
  86. void enableStackSendingByID();
  87. void disableSmartPointerSerialization();
  88. void enableSmartPointerSerialization();
  89. void disableSmartVectorMemberSerialization();
  90. void enableSmartVectorMemberSerializatoin();
  91. void enterLobbyConnectionMode();
  92. void enterGameplayConnectionMode(CGameState * gs);
  93. std::string toString() const;
  94. template<class T>
  95. CConnection & operator>>(T &t)
  96. {
  97. iser & t;
  98. return * this;
  99. }
  100. template<class T>
  101. CConnection & operator<<(const T &t)
  102. {
  103. oser & t;
  104. return * this;
  105. }
  106. };
  107. VCMI_LIB_NAMESPACE_END