NetworkInterface.h 3.9 KB

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