refactored_echo_server.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. //
  2. // refactored_echo_server.cpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #include <boost/asio/co_spawn.hpp>
  11. #include <boost/asio/detached.hpp>
  12. #include <boost/asio/io_context.hpp>
  13. #include <boost/asio/ip/tcp.hpp>
  14. #include <boost/asio/signal_set.hpp>
  15. #include <boost/asio/write.hpp>
  16. #include <cstdio>
  17. using boost::asio::ip::tcp;
  18. using boost::asio::awaitable;
  19. using boost::asio::co_spawn;
  20. using boost::asio::detached;
  21. using boost::asio::use_awaitable;
  22. namespace this_coro = boost::asio::this_coro;
  23. awaitable<void> echo_once(tcp::socket& socket)
  24. {
  25. char data[128];
  26. std::size_t n = co_await socket.async_read_some(boost::asio::buffer(data), use_awaitable);
  27. co_await async_write(socket, boost::asio::buffer(data, n), use_awaitable);
  28. }
  29. awaitable<void> echo(tcp::socket socket)
  30. {
  31. try
  32. {
  33. for (;;)
  34. {
  35. // The asynchronous operations to echo a single chunk of data have been
  36. // refactored into a separate function. When this function is called, the
  37. // operations are still performed in the context of the current
  38. // coroutine, and the behaviour is functionally equivalent.
  39. co_await echo_once(socket);
  40. }
  41. }
  42. catch (std::exception& e)
  43. {
  44. std::printf("echo Exception: %s\n", e.what());
  45. }
  46. }
  47. awaitable<void> listener()
  48. {
  49. auto executor = co_await this_coro::executor;
  50. tcp::acceptor acceptor(executor, {tcp::v4(), 55555});
  51. for (;;)
  52. {
  53. tcp::socket socket = co_await acceptor.async_accept(use_awaitable);
  54. co_spawn(executor,
  55. [socket = std::move(socket)]() mutable
  56. {
  57. return echo(std::move(socket));
  58. },
  59. detached);
  60. }
  61. }
  62. int main()
  63. {
  64. try
  65. {
  66. boost::asio::io_context io_context(1);
  67. boost::asio::signal_set signals(io_context, SIGINT, SIGTERM);
  68. signals.async_wait([&](auto, auto){ io_context.stop(); });
  69. co_spawn(io_context, listener, detached);
  70. io_context.run();
  71. }
  72. catch (std::exception& e)
  73. {
  74. std::printf("Exception: %s\n", e.what());
  75. }
  76. }