Connection.h 3.1 KB

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