Connection.h 3.4 KB

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