NetworkClient.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /*
  2. * NetworkClient.cpp, 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. #include "StdInc.h"
  11. #include "NetworkClient.h"
  12. #include "NetworkConnection.h"
  13. VCMI_LIB_NAMESPACE_BEGIN
  14. NetworkClient::NetworkClient()
  15. : io(new NetworkService)
  16. , socket(new NetworkSocket(*io))
  17. // , timer(new NetworkTimer(*io))
  18. {
  19. }
  20. void NetworkClient::start(const std::string & host, uint16_t port)
  21. {
  22. boost::asio::ip::tcp::resolver resolver(*io);
  23. auto endpoints = resolver.resolve(host, std::to_string(port));
  24. boost::asio::async_connect(*socket, endpoints, std::bind(&NetworkClient::onConnected, this, _1));
  25. }
  26. void NetworkClient::onConnected(const boost::system::error_code & ec)
  27. {
  28. connection = std::make_shared<NetworkConnection>(socket);
  29. connection->start();
  30. }
  31. void NetworkClient::run()
  32. {
  33. io->run();
  34. }
  35. void NetworkClient::sendPacket(const std::vector<uint8_t> & message)
  36. {
  37. connection->sendPacket(message);
  38. }
  39. VCMI_LIB_NAMESPACE_END