2
0

Connection.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. class io_service;
  23. template <typename Protocol> class stream_socket_service;
  24. template <typename Protocol,typename StreamSocketService>
  25. class basic_stream_socket;
  26. template <typename Protocol> class socket_acceptor_service;
  27. template <typename Protocol,typename SocketAcceptorService>
  28. class basic_socket_acceptor;
  29. }
  30. class mutex;
  31. }
  32. typedef boost::asio::basic_stream_socket < boost::asio::ip::tcp , boost::asio::stream_socket_service<boost::asio::ip::tcp> > TSocket;
  33. typedef boost::asio::basic_socket_acceptor<boost::asio::ip::tcp, boost::asio::socket_acceptor_service<boost::asio::ip::tcp> > TAcceptor;
  34. /// Main class for network communication
  35. /// Allows establishing connection and bidirectional read-write
  36. class DLL_LINKAGE CConnection
  37. : public IBinaryReader, public IBinaryWriter
  38. {
  39. CConnection(void);
  40. void init();
  41. void reportState(CLogger * out) override;
  42. int write(const void * data, unsigned size) override;
  43. int read(void * data, unsigned size) override;
  44. public:
  45. BinaryDeserializer iser;
  46. BinarySerializer oser;
  47. boost::mutex *rmx, *wmx; // read/write mutexes
  48. TSocket * socket;
  49. bool logging;
  50. bool connected;
  51. bool myEndianess, contactEndianess; //true if little endian, if endianness is different we'll have to revert received multi-byte vars
  52. boost::asio::io_service *io_service;
  53. std::string name; //who uses this connection
  54. int connectionID;
  55. boost::thread *handler;
  56. bool receivedStop, sendStop;
  57. CConnection(std::string host, std::string port, std::string Name);
  58. CConnection(TAcceptor * acceptor, boost::asio::io_service *Io_service, std::string Name);
  59. CConnection(TSocket * Socket, std::string Name); //use immediately after accepting connection into socket
  60. void close();
  61. bool isOpen() const;
  62. template<class T>
  63. CConnection &operator&(const T&);
  64. virtual ~CConnection(void);
  65. CPack *retreivePack(); //gets from server next pack (allocates it with new)
  66. void sendPackToServer(const CPack &pack, PlayerColor player, ui32 requestID);
  67. void disableStackSendingByID();
  68. void enableStackSendingByID();
  69. void disableSmartPointerSerialization();
  70. void enableSmartPointerSerializatoin();
  71. void disableSmartVectorMemberSerialization();
  72. void enableSmartVectorMemberSerializatoin();
  73. void prepareForSendingHeroes(); //disables sending vectorized, enables smart pointer serialization, clears saved/loaded ptr cache
  74. void enterPregameConnectionMode();
  75. template<class T>
  76. CConnection & operator>>(T &t)
  77. {
  78. iser & t;
  79. return * this;
  80. }
  81. template<class T>
  82. CConnection & operator<<(const T &t)
  83. {
  84. oser & t;
  85. return * this;
  86. }
  87. };
  88. DLL_LINKAGE std::ostream &operator<<(std::ostream &str, const CConnection &cpc);