composed_1.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. //
  2. // composed_1.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/io_context.hpp>
  11. #include <boost/asio/ip/tcp.hpp>
  12. #include <boost/asio/use_future.hpp>
  13. #include <boost/asio/write.hpp>
  14. #include <cstring>
  15. #include <iostream>
  16. #include <string>
  17. #include <type_traits>
  18. #include <utility>
  19. using boost::asio::ip::tcp;
  20. //------------------------------------------------------------------------------
  21. // This is the simplest example of a composed asynchronous operation, where we
  22. // simply repackage an existing operation. The asynchronous operation
  23. // requirements are met by delegating responsibility to the underlying
  24. // operation.
  25. template <typename CompletionToken>
  26. auto async_write_message(tcp::socket& socket,
  27. const char* message, CompletionToken&& token)
  28. // The return type of the initiating function is deduced from the combination
  29. // of CompletionToken type and the completion handler's signature. When the
  30. // completion token is a simple callback, the return type is void. However,
  31. // when the completion token is boost::asio::yield_context (used for stackful
  32. // coroutines) the return type would be std::size_t, and when the completion
  33. // token is boost::asio::use_future it would be std::future<std::size_t>.
  34. -> typename boost::asio::async_result<
  35. typename std::decay<CompletionToken>::type,
  36. void(boost::system::error_code, std::size_t)>::return_type
  37. {
  38. // When delegating to the underlying operation we must take care to perfectly
  39. // forward the completion token. This ensures that our operation works
  40. // correctly with move-only function objects as callbacks, as well as other
  41. // completion token types.
  42. return boost::asio::async_write(socket,
  43. boost::asio::buffer(message, std::strlen(message)),
  44. std::forward<CompletionToken>(token));
  45. }
  46. //------------------------------------------------------------------------------
  47. void test_callback()
  48. {
  49. boost::asio::io_context io_context;
  50. tcp::acceptor acceptor(io_context, {tcp::v4(), 55555});
  51. tcp::socket socket = acceptor.accept();
  52. // Test our asynchronous operation using a lambda as a callback.
  53. async_write_message(socket, "Testing callback\r\n",
  54. [](const boost::system::error_code& error, std::size_t n)
  55. {
  56. if (!error)
  57. {
  58. std::cout << n << " bytes transferred\n";
  59. }
  60. else
  61. {
  62. std::cout << "Error: " << error.message() << "\n";
  63. }
  64. });
  65. io_context.run();
  66. }
  67. //------------------------------------------------------------------------------
  68. void test_future()
  69. {
  70. boost::asio::io_context io_context;
  71. tcp::acceptor acceptor(io_context, {tcp::v4(), 55555});
  72. tcp::socket socket = acceptor.accept();
  73. // Test our asynchronous operation using the use_future completion token.
  74. // This token causes the operation's initiating function to return a future,
  75. // which may be used to synchronously wait for the result of the operation.
  76. std::future<std::size_t> f = async_write_message(
  77. socket, "Testing future\r\n", boost::asio::use_future);
  78. io_context.run();
  79. try
  80. {
  81. // Get the result of the operation.
  82. std::size_t n = f.get();
  83. std::cout << n << " bytes transferred\n";
  84. }
  85. catch (const std::exception& e)
  86. {
  87. std::cout << "Error: " << e.what() << "\n";
  88. }
  89. }
  90. //------------------------------------------------------------------------------
  91. int main()
  92. {
  93. test_callback();
  94. test_future();
  95. }