NetworkClient.h 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /*
  2. * NetworkClient.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 "NetworkDefines.h"
  12. #include "NetworkListener.h"
  13. VCMI_LIB_NAMESPACE_BEGIN
  14. /// Function that attempts to open specified port on local system to determine whether port is in use
  15. /// Returns: true if port is free and can be used to receive connections
  16. DLL_LINKAGE bool checkNetworkPortIsFree(const std::string & host, uint16_t port);
  17. class NetworkConnection;
  18. class DLL_LINKAGE NetworkClient : boost::noncopyable, public INetworkConnectionListener
  19. {
  20. std::shared_ptr<NetworkService> io;
  21. std::shared_ptr<NetworkSocket> socket;
  22. std::shared_ptr<NetworkConnection> connection;
  23. INetworkClientListener & listener;
  24. void onConnected(const boost::system::error_code & ec);
  25. void onDisconnected(const std::shared_ptr<NetworkConnection> & connection) override;
  26. void onPacketReceived(const std::shared_ptr<NetworkConnection> & connection, const std::vector<uint8_t> & message) override;
  27. public:
  28. NetworkClient(INetworkClientListener & listener);
  29. virtual ~NetworkClient() = default;
  30. void sendPacket(const std::vector<uint8_t> & message);
  31. void start(const std::string & host, uint16_t port);
  32. void run();
  33. void poll();
  34. void stop();
  35. };
  36. VCMI_LIB_NAMESPACE_END