NetworkInterface.h 3.6 KB

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