Connection.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. void init();
  45. void reportState(vstd::CLoggerBase * out) override;
  46. int write(const void * data, unsigned size) override;
  47. int read(void * data, unsigned size) override;
  48. std::shared_ptr<boost::asio::io_service> io_service; //can be empty if connection made from socket
  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::string name; //who uses this connection
  59. std::string uuid;
  60. int connectionID;
  61. std::shared_ptr<boost::thread> handler;
  62. CConnection(std::string host, ui16 port, std::string Name, std::string UUID);
  63. CConnection(std::shared_ptr<TAcceptor> acceptor, std::shared_ptr<boost::asio::io_service> Io_service, std::string Name, std::string UUID);
  64. CConnection(std::shared_ptr<TSocket> Socket, std::string Name, std::string UUID); //use immediately after accepting connection into socket
  65. void close();
  66. bool isOpen() const;
  67. template<class T>
  68. CConnection &operator&(const T&);
  69. virtual ~CConnection();
  70. CPack * retrievePack();
  71. void sendPack(const CPack * pack);
  72. void disableStackSendingByID();
  73. void enableStackSendingByID();
  74. void disableSmartPointerSerialization();
  75. void enableSmartPointerSerialization();
  76. void disableSmartVectorMemberSerialization();
  77. void enableSmartVectorMemberSerializatoin();
  78. void enterLobbyConnectionMode();
  79. void enterGameplayConnectionMode(CGameState * gs);
  80. std::string toString() const;
  81. template<class T>
  82. CConnection & operator>>(T &t)
  83. {
  84. iser & t;
  85. return * this;
  86. }
  87. template<class T>
  88. CConnection & operator<<(const T &t)
  89. {
  90. oser & t;
  91. return * this;
  92. }
  93. };