NetworkInterface.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 close() = 0;
  19. };
  20. using NetworkConnectionPtr = std::shared_ptr<INetworkConnection>;
  21. using NetworkConnectionWeakPtr = std::weak_ptr<INetworkConnection>;
  22. /// Base class for outgoing connections support
  23. class DLL_LINKAGE INetworkClient : boost::noncopyable
  24. {
  25. public:
  26. virtual ~INetworkClient() = default;
  27. virtual bool isConnected() const = 0;
  28. virtual void sendPacket(const std::vector<std::byte> & message) = 0;
  29. };
  30. /// Base class for incoming connections support
  31. class DLL_LINKAGE INetworkServer : boost::noncopyable
  32. {
  33. public:
  34. virtual ~INetworkServer() = default;
  35. virtual void start(uint16_t port) = 0;
  36. };
  37. /// Base interface that must be implemented by user of networking API to handle any connection callbacks
  38. class DLL_LINKAGE INetworkConnectionListener
  39. {
  40. public:
  41. virtual void onDisconnected(const std::shared_ptr<INetworkConnection> & connection, const std::string & errorMessage) = 0;
  42. virtual void onPacketReceived(const std::shared_ptr<INetworkConnection> & connection, const std::vector<std::byte> & message) = 0;
  43. virtual ~INetworkConnectionListener() = default;
  44. };
  45. /// Interface that must be implemented by user of networking API to handle outgoing connection callbacks
  46. class DLL_LINKAGE INetworkClientListener : public INetworkConnectionListener
  47. {
  48. public:
  49. virtual void onConnectionFailed(const std::string & errorMessage) = 0;
  50. virtual void onConnectionEstablished(const std::shared_ptr<INetworkConnection> &) = 0;
  51. };
  52. /// Interface that must be implemented by user of networking API to handle incoming connection callbacks
  53. class DLL_LINKAGE INetworkServerListener : public INetworkConnectionListener
  54. {
  55. public:
  56. virtual void onNewConnection(const std::shared_ptr<INetworkConnection> &) = 0;
  57. };
  58. /// Interface that must be implemented by user of networking API to handle timers on network thread
  59. class DLL_LINKAGE INetworkTimerListener
  60. {
  61. public:
  62. virtual ~INetworkTimerListener() = default;
  63. virtual void onTimer() = 0;
  64. };
  65. /// Main class for handling of all network activity
  66. class DLL_LINKAGE INetworkHandler : boost::noncopyable
  67. {
  68. public:
  69. virtual ~INetworkHandler() = default;
  70. /// Constructs default implementation
  71. static std::unique_ptr<INetworkHandler> createHandler();
  72. /// Creates an instance of TCP server that allows to receiving connections on a local port
  73. virtual std::unique_ptr<INetworkServer> createServerTCP(INetworkServerListener & listener) = 0;
  74. /// Creates an instance of TCP client that allows to establish single outgoing connection to a remote port
  75. /// On success: INetworkTimerListener::onConnectionEstablished() will be called, established connection provided as parameter
  76. /// On failure: INetworkTimerListener::onConnectionFailed will be called with human-readable error message
  77. virtual void connectToRemote(INetworkClientListener & listener, const std::string & host, uint16_t port) = 0;
  78. /// Creates a timer that will be called once, after specified interval has passed
  79. /// On success: INetworkTimerListener::onTimer() will be called
  80. /// On failure: no-op
  81. virtual void createTimer(INetworkTimerListener & listener, std::chrono::milliseconds duration) = 0;
  82. /// Starts network processing on this thread. Does not returns until networking processing has been terminated
  83. virtual void run() = 0;
  84. virtual void stop() = 0;
  85. };
  86. VCMI_LIB_NAMESPACE_END