NetworkServer.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * NetworkServer.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 "NetworkServer.h"
  12. #include "NetworkConnection.h"
  13. VCMI_LIB_NAMESPACE_BEGIN
  14. NetworkServer::NetworkServer(INetworkServerListener & listener)
  15. :listener(listener)
  16. {
  17. }
  18. void NetworkServer::start(uint16_t port)
  19. {
  20. io = std::make_shared<boost::asio::io_service>();
  21. acceptor = std::make_shared<NetworkAcceptor>(*io, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port));
  22. startAsyncAccept();
  23. }
  24. void NetworkServer::startAsyncAccept()
  25. {
  26. std::shared_ptr<NetworkSocket> upcomingConnection = std::make_shared<NetworkSocket>(*io);
  27. acceptor->async_accept(*upcomingConnection, std::bind(&NetworkServer::connectionAccepted, this, upcomingConnection, _1));
  28. }
  29. void NetworkServer::run()
  30. {
  31. io->run();
  32. }
  33. void NetworkServer::run(std::chrono::milliseconds duration)
  34. {
  35. io->run_for(duration);
  36. }
  37. void NetworkServer::connectionAccepted(std::shared_ptr<NetworkSocket> upcomingConnection, const boost::system::error_code & ec)
  38. {
  39. if(ec)
  40. {
  41. throw std::runtime_error("Something wrong during accepting: " + ec.message());
  42. }
  43. logNetwork->info("We got a new connection! :)");
  44. auto connection = std::make_shared<NetworkConnection>(upcomingConnection, *this);
  45. connections.insert(connection);
  46. connection->start();
  47. listener.onNewConnection(connection);
  48. startAsyncAccept();
  49. }
  50. void NetworkServer::sendPacket(const std::shared_ptr<NetworkConnection> & connection, const std::vector<uint8_t> & message)
  51. {
  52. connection->sendPacket(message);
  53. }
  54. void NetworkServer::closeConnection(const std::shared_ptr<NetworkConnection> & connection)
  55. {
  56. assert(connections.count(connection));
  57. connections.erase(connection);
  58. }
  59. void NetworkServer::onDisconnected(const std::shared_ptr<NetworkConnection> & connection)
  60. {
  61. assert(connections.count(connection));
  62. connections.erase(connection);
  63. listener.onDisconnected(connection);
  64. }
  65. void NetworkServer::onPacketReceived(const std::shared_ptr<NetworkConnection> & connection, const std::vector<uint8_t> & message)
  66. {
  67. listener.onPacketReceived(connection, message);
  68. }
  69. void NetworkServer::setTimer(std::chrono::milliseconds duration)
  70. {
  71. auto timer = std::make_shared<NetworkTimer>(*io, duration);
  72. timer->async_wait([this, timer](const boost::system::error_code& error){
  73. if (!error)
  74. listener.onTimer();
  75. });
  76. }
  77. VCMI_LIB_NAMESPACE_END