NetworkInterface.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * NetworkHandler.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 <boost/asio.hpp>
  12. // Windows headers included by boost/asio.hpp may define these macros which conflict with engine code
  13. #ifdef _WIN32
  14. #ifdef AUTO
  15. #undef AUTO
  16. #endif
  17. #ifdef IGNORE
  18. #undef IGNORE
  19. #endif
  20. #endif
  21. VCMI_LIB_NAMESPACE_BEGIN
  22. #if BOOST_VERSION >= 106600
  23. using NetworkContext = boost::asio::io_context;
  24. #else
  25. using NetworkContext = boost::asio::io_service;
  26. #endif
  27. struct DLL_LINKAGE DiscoveredServer
  28. {
  29. std::string address;
  30. uint16_t port;
  31. };
  32. /// Base class for connections with other services, either incoming or outgoing
  33. class DLL_LINKAGE INetworkConnection : boost::noncopyable
  34. {
  35. public:
  36. virtual ~INetworkConnection() = default;
  37. virtual void sendPacket(const std::vector<std::byte> & message) = 0;
  38. virtual void setAsyncWritesEnabled(bool on) = 0;
  39. virtual void close() = 0;
  40. };
  41. /// Class for internal connections within single process, for use when TCP is not possible or not desired
  42. class IInternalConnection : public INetworkConnection
  43. {
  44. public:
  45. virtual void receivePacket(const std::vector<std::byte> & message) = 0;
  46. virtual void disconnect() = 0;
  47. virtual void connectTo(std::shared_ptr<IInternalConnection> connection) = 0;
  48. };
  49. using NetworkConnectionPtr = std::shared_ptr<INetworkConnection>;
  50. using NetworkConnectionWeakPtr = std::weak_ptr<INetworkConnection>;
  51. /// Base class for outgoing connections support
  52. class DLL_LINKAGE INetworkClient : boost::noncopyable
  53. {
  54. public:
  55. virtual ~INetworkClient() = default;
  56. virtual bool isConnected() const = 0;
  57. virtual void sendPacket(const std::vector<std::byte> & message) = 0;
  58. };
  59. /// Base class for incoming connections support
  60. class DLL_LINKAGE INetworkServer : boost::noncopyable
  61. {
  62. public:
  63. virtual ~INetworkServer() = default;
  64. virtual uint16_t start(uint16_t port) = 0;
  65. virtual void receiveInternalConnection(std::shared_ptr<IInternalConnection> remoteConnection) = 0;
  66. };
  67. /// Base interface that must be implemented by user of networking API to handle any connection callbacks
  68. class DLL_LINKAGE INetworkConnectionListener
  69. {
  70. public:
  71. virtual void onDisconnected(const std::shared_ptr<INetworkConnection> & connection, const std::string & errorMessage) = 0;
  72. virtual void onPacketReceived(const std::shared_ptr<INetworkConnection> & connection, const std::vector<std::byte> & message) = 0;
  73. virtual ~INetworkConnectionListener() = default;
  74. };
  75. /// Interface that must be implemented by user of networking API to handle outgoing connection callbacks
  76. class DLL_LINKAGE INetworkClientListener : public INetworkConnectionListener
  77. {
  78. public:
  79. virtual void onConnectionFailed(const std::string & errorMessage) = 0;
  80. virtual void onConnectionEstablished(const std::shared_ptr<INetworkConnection> &) = 0;
  81. };
  82. /// Interface that must be implemented by user of networking API to handle incoming connection callbacks
  83. class DLL_LINKAGE INetworkServerListener : public INetworkConnectionListener
  84. {
  85. public:
  86. virtual void onNewConnection(const std::shared_ptr<INetworkConnection> &) = 0;
  87. };
  88. /// Interface that must be implemented by user of networking API to handle timers on network thread
  89. class DLL_LINKAGE INetworkTimerListener
  90. {
  91. public:
  92. virtual ~INetworkTimerListener() = default;
  93. virtual void onTimer() = 0;
  94. };
  95. /// Base interface that must be implemented by user of networking API to handle server discovery results
  96. class DLL_LINKAGE IServerDiscoveryObserver
  97. {
  98. public:
  99. virtual ~IServerDiscoveryObserver() = default;
  100. virtual void onServerDiscovered(const DiscoveredServer & server) = 0;
  101. };
  102. /// Base interface for server discovery component
  103. class DLL_LINKAGE IServerDiscovery : boost::noncopyable
  104. {
  105. public:
  106. virtual ~IServerDiscovery() = default;
  107. virtual void start() = 0;
  108. virtual void abort() = 0;
  109. };
  110. /// Base interface that provides data for responding to discovery requests
  111. class DLL_LINKAGE IServerDiscoveryAnnouncer
  112. {
  113. public:
  114. virtual ~IServerDiscoveryAnnouncer() = default;
  115. virtual bool isInLobby() const = 0;
  116. virtual uint16_t getPort() const = 0;
  117. };
  118. /// Base interface for server discovery listener component
  119. class DLL_LINKAGE IServerDiscoveryListener : boost::noncopyable
  120. {
  121. public:
  122. virtual ~IServerDiscoveryListener() = default;
  123. virtual void start() = 0;
  124. virtual void stop() = 0;
  125. };
  126. /// Main class for handling of all network activity
  127. class DLL_LINKAGE INetworkHandler : boost::noncopyable
  128. {
  129. public:
  130. virtual ~INetworkHandler() = default;
  131. /// Constructs default implementation
  132. static std::unique_ptr<INetworkHandler> createHandler();
  133. /// Creates an instance of TCP server that allows to receiving connections on a local port
  134. virtual std::unique_ptr<INetworkServer> createServerTCP(INetworkServerListener & listener) = 0;
  135. /// Creates an instance of TCP client that allows to establish single outgoing connection to a remote port
  136. /// On success: INetworkTimerListener::onConnectionEstablished() will be called, established connection provided as parameter
  137. /// On failure: INetworkTimerListener::onConnectionFailed will be called with human-readable error message
  138. virtual void connectToRemote(INetworkClientListener & listener, const std::string & host, uint16_t port) = 0;
  139. /// Creates an instance of internal connection that is connected to a network server, but uses intra-process communication instead of TCP
  140. /// On success INetworkTimerListener::onConnectionEstablished() will be called asynchronously, established connection provided as parameter
  141. virtual void createInternalConnection(INetworkClientListener & listener, INetworkServer & server) = 0;
  142. /// Creates one-way connection that allows sending messages to listener in async form
  143. virtual std::shared_ptr<INetworkConnection> createAsyncConnection(INetworkConnectionListener & listener) = 0;
  144. /// Creates a timer that will be called once, after specified interval has passed
  145. /// On success: INetworkTimerListener::onTimer() will be called
  146. /// On failure: no-op
  147. virtual void createTimer(INetworkTimerListener & listener, std::chrono::milliseconds duration) = 0;
  148. /// Creates a server discovery component that broadcasts discovery messages and reports results
  149. virtual std::shared_ptr<IServerDiscovery> createServerDiscovery(IServerDiscoveryObserver & listener) = 0;
  150. /// Creates a server discovery listener that responds to discovery requests
  151. virtual std::shared_ptr<IServerDiscoveryListener> createServerDiscoveryListener(IServerDiscoveryAnnouncer & announcer, uint16_t port = 3030) = 0;
  152. /// Starts network processing on this thread. Does not returns until networking processing has been terminated
  153. virtual void run() = 0;
  154. virtual void stop() = 0;
  155. };
  156. VCMI_LIB_NAMESPACE_END