NetworkInterface.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. VCMI_LIB_NAMESPACE_BEGIN
  12. /// Base class for connections with other services, either incoming or outgoing
  13. class DLL_LINKAGE INetworkConnection : boost::noncopyable
  14. {
  15. public:
  16. virtual ~INetworkConnection() = default;
  17. virtual void sendPacket(const std::vector<std::byte> & message) = 0;
  18. virtual void setAsyncWritesEnabled(bool on) = 0;
  19. virtual void close() = 0;
  20. };
  21. /// Class for internal connections within single process, for use when TCP is not possible or not desired
  22. class IInternalConnection : public INetworkConnection
  23. {
  24. public:
  25. virtual void receivePacket(const std::vector<std::byte> & message) = 0;
  26. virtual void disconnect() = 0;
  27. virtual void connectTo(std::shared_ptr<IInternalConnection> connection) = 0;
  28. };
  29. using NetworkConnectionPtr = std::shared_ptr<INetworkConnection>;
  30. using NetworkConnectionWeakPtr = std::weak_ptr<INetworkConnection>;
  31. /// Base class for outgoing connections support
  32. class DLL_LINKAGE INetworkClient : boost::noncopyable
  33. {
  34. public:
  35. virtual ~INetworkClient() = default;
  36. virtual bool isConnected() const = 0;
  37. virtual void sendPacket(const std::vector<std::byte> & message) = 0;
  38. };
  39. /// Base class for incoming connections support
  40. class DLL_LINKAGE INetworkServer : boost::noncopyable
  41. {
  42. public:
  43. virtual ~INetworkServer() = default;
  44. virtual uint16_t start(uint16_t port) = 0;
  45. virtual void receiveInternalConnection(std::shared_ptr<IInternalConnection> remoteConnection) = 0;
  46. };
  47. /// Base interface that must be implemented by user of networking API to handle any connection callbacks
  48. class DLL_LINKAGE INetworkConnectionListener
  49. {
  50. public:
  51. virtual void onDisconnected(const std::shared_ptr<INetworkConnection> & connection, const std::string & errorMessage) = 0;
  52. virtual void onPacketReceived(const std::shared_ptr<INetworkConnection> & connection, const std::vector<std::byte> & message) = 0;
  53. virtual ~INetworkConnectionListener() = default;
  54. };
  55. /// Interface that must be implemented by user of networking API to handle outgoing connection callbacks
  56. class DLL_LINKAGE INetworkClientListener : public INetworkConnectionListener
  57. {
  58. public:
  59. virtual void onConnectionFailed(const std::string & errorMessage) = 0;
  60. virtual void onConnectionEstablished(const std::shared_ptr<INetworkConnection> &) = 0;
  61. };
  62. /// Interface that must be implemented by user of networking API to handle incoming connection callbacks
  63. class DLL_LINKAGE INetworkServerListener : public INetworkConnectionListener
  64. {
  65. public:
  66. virtual void onNewConnection(const std::shared_ptr<INetworkConnection> &) = 0;
  67. };
  68. /// Interface that must be implemented by user of networking API to handle timers on network thread
  69. class DLL_LINKAGE INetworkTimerListener
  70. {
  71. public:
  72. virtual ~INetworkTimerListener() = default;
  73. virtual void onTimer() = 0;
  74. };
  75. /// Main class for handling of all network activity
  76. class DLL_LINKAGE INetworkHandler : boost::noncopyable
  77. {
  78. public:
  79. virtual ~INetworkHandler() = default;
  80. /// Constructs default implementation
  81. static std::unique_ptr<INetworkHandler> createHandler();
  82. /// Creates an instance of TCP server that allows to receiving connections on a local port
  83. virtual std::unique_ptr<INetworkServer> createServerTCP(INetworkServerListener & listener) = 0;
  84. /// Creates an instance of TCP client that allows to establish single outgoing connection to a remote port
  85. /// On success: INetworkTimerListener::onConnectionEstablished() will be called, established connection provided as parameter
  86. /// On failure: INetworkTimerListener::onConnectionFailed will be called with human-readable error message
  87. virtual void connectToRemote(INetworkClientListener & listener, const std::string & host, uint16_t port) = 0;
  88. /// Creates an instance of internal connection that is connected to a network server, but uses intra-process communication instead of TCP
  89. /// On success INetworkTimerListener::onConnectionEstablished() will be called asynchronously, established connection provided as parameter
  90. virtual void createInternalConnection(INetworkClientListener & listener, INetworkServer & server) = 0;
  91. /// Creates one-way connection that allows sending messages to listener in async form
  92. virtual std::shared_ptr<INetworkConnection> createAsyncConnection(INetworkConnectionListener & listener) = 0;
  93. /// Creates a timer that will be called once, after specified interval has passed
  94. /// On success: INetworkTimerListener::onTimer() will be called
  95. /// On failure: no-op
  96. virtual void createTimer(INetworkTimerListener & listener, std::chrono::milliseconds duration) = 0;
  97. /// Starts network processing on this thread. Does not returns until networking processing has been terminated
  98. virtual void run() = 0;
  99. virtual void stop() = 0;
  100. };
  101. VCMI_LIB_NAMESPACE_END