From 99bceb5bff341be69a54fb95bd0befd70c47000c Mon Sep 17 00:00:00 2001 From: Klemens Date: Mon, 2 Jan 2023 10:00:03 +0800 Subject: [PATCH] examples & tests use context instead of coroutine. --- CMakeLists.txt | 4 +-- doc/qbk/03_core/1_refresher.qbk | 3 +++ example/http/client/coro-ssl/Jamfile | 2 +- .../client/coro-ssl/http_client_coro_ssl.cpp | 27 +++++++++++++------ example/http/client/coro/Jamfile | 2 +- example/http/client/coro/http_client_coro.cpp | 13 ++++++++- example/http/server/coro-ssl/Jamfile | 2 +- .../server/coro-ssl/http_server_coro_ssl.cpp | 19 +++++++++++-- example/http/server/coro/Jamfile | 2 +- example/http/server/coro/http_server_coro.cpp | 19 +++++++++++-- example/websocket/client/coro-ssl/Jamfile | 2 +- .../coro-ssl/websocket_client_coro_ssl.cpp | 13 ++++++++- example/websocket/client/coro/Jamfile | 2 +- .../client/coro/websocket_client_coro.cpp | 13 ++++++++- example/websocket/server/coro-ssl/Jamfile | 2 +- .../coro-ssl/websocket_server_coro_ssl.cpp | 19 +++++++++++-- example/websocket/server/coro/Jamfile | 2 +- .../server/coro/websocket_server_coro.cpp | 19 +++++++++++-- example/websocket/server/fast/Jamfile | 2 +- .../server/fast/websocket_server_fast.cpp | 5 ++-- test/Jamfile | 2 +- test/doc/core_1_refresher.cpp | 4 ++- test/doc/core_3_timeouts.cpp | 5 +++- .../include/boost/beast/test/yield_to.hpp | 10 +++++-- 24 files changed, 156 insertions(+), 37 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7ab384aa..12209f6a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -189,8 +189,8 @@ if (MSVC) else() set(BOOST_INCLUDEDIR ${BOOST_ROOT}) set(BOOST_LIBRARYDIR ${BOOST_ROOT}/stage/lib) - find_package(Boost COMPONENTS coroutine filesystem system thread REQUIRED) - link_libraries(Boost::coroutine Boost::filesystem Boost::system Boost::thread) + find_package(Boost COMPONENTS context filesystem system thread REQUIRED) + link_libraries(Boost::context Boost::filesystem Boost::system Boost::thread) endif() link_directories(${BOOST_ROOT}/stage/lib) diff --git a/doc/qbk/03_core/1_refresher.qbk b/doc/qbk/03_core/1_refresher.qbk index 5cd89649..a4f2f677 100644 --- a/doc/qbk/03_core/1_refresher.qbk +++ b/doc/qbk/03_core/1_refresher.qbk @@ -340,6 +340,9 @@ customization points in the initiating function signature. Here is the signature for [@boost:/doc/html/boost_asio/reference/async_write/overload1.html `net::async_write`]: +Note that a `spawn` function itself has a completion signature, +but we're ignoring it's result in the example by using `asio::detached`. + [code_core_1_refresher_9] The type of the function's return value is determined by the diff --git a/example/http/client/coro-ssl/Jamfile b/example/http/client/coro-ssl/Jamfile index f2c126c5..f1354203 100644 --- a/example/http/client/coro-ssl/Jamfile +++ b/example/http/client/coro-ssl/Jamfile @@ -19,5 +19,5 @@ exe http-client-coro-ssl : : coverage:no ubasan:no - /boost/coroutine//boost_coroutine + /boost/context//boost_context ; diff --git a/example/http/client/coro-ssl/http_client_coro_ssl.cpp b/example/http/client/coro-ssl/http_client_coro_ssl.cpp index 8454db90..adc6c05d 100644 --- a/example/http/client/coro-ssl/http_client_coro_ssl.cpp +++ b/example/http/client/coro-ssl/http_client_coro_ssl.cpp @@ -163,14 +163,25 @@ int main(int argc, char** argv) // Launch the asynchronous operation boost::asio::spawn(ioc, std::bind( - &do_session, - std::string(host), - std::string(port), - std::string(target), - version, - std::ref(ioc), - std::ref(ctx), - std::placeholders::_1)); + &do_session, + std::string(host), + std::string(port), + std::string(target), + version, + std::ref(ioc), + std::ref(ctx), + std::placeholders::_1), + // on completion, spawn will call this function + [](std::exception_ptr ex) + { + // if an exception occurred in the coroutine, + // it's something critical, e.g. out of memory + // we capture normal errors in the ec + // so we just rethrow the exception here, + // which will cause `ioc.run()` to throw + if (ex) + std::rethrow_exception(ex); + }); // Run the I/O service. The call will return when // the get operation is complete. diff --git a/example/http/client/coro/Jamfile b/example/http/client/coro/Jamfile index 25ebe2fd..66ee67b8 100644 --- a/example/http/client/coro/Jamfile +++ b/example/http/client/coro/Jamfile @@ -12,5 +12,5 @@ exe http-client-coro : : coverage:no ubasan:no - /boost/coroutine//boost_coroutine + /boost/context//boost_context ; diff --git a/example/http/client/coro/http_client_coro.cpp b/example/http/client/coro/http_client_coro.cpp index 274ec05e..9f083f37 100644 --- a/example/http/client/coro/http_client_coro.cpp +++ b/example/http/client/coro/http_client_coro.cpp @@ -134,7 +134,18 @@ int main(int argc, char** argv) std::string(target), version, std::ref(ioc), - std::placeholders::_1)); + std::placeholders::_1), + // on completion, spawn will call this function + [](std::exception_ptr ex) + { + // if an exception occurred in the coroutine, + // it's something critical, e.g. out of memory + // we capture normal errors in the ec + // so we just rethrow the exception here, + // which will cause `ioc.run()` to throw + if (ex) + std::rethrow_exception(ex); + }); // Run the I/O service. The call will return when // the get operation is complete. diff --git a/example/http/server/coro-ssl/Jamfile b/example/http/server/coro-ssl/Jamfile index 8870f65d..4f6c7a6a 100644 --- a/example/http/server/coro-ssl/Jamfile +++ b/example/http/server/coro-ssl/Jamfile @@ -19,5 +19,5 @@ exe http-server-coro-ssl : : coverage:no ubasan:no - /boost/coroutine//boost_coroutine + /boost/context//boost_context ; diff --git a/example/http/server/coro-ssl/http_server_coro_ssl.cpp b/example/http/server/coro-ssl/http_server_coro_ssl.cpp index 9d9ff2e4..7f24addc 100644 --- a/example/http/server/coro-ssl/http_server_coro_ssl.cpp +++ b/example/http/server/coro-ssl/http_server_coro_ssl.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -344,7 +345,10 @@ do_listen( beast::ssl_stream( std::move(socket), ctx), doc_root, - std::placeholders::_1)); + std::placeholders::_1), + // we ignore the result of the session, + // most errors are handled with error_code + boost::asio::detached); } } @@ -381,7 +385,18 @@ int main(int argc, char* argv[]) std::ref(ctx), tcp::endpoint{address, port}, doc_root, - std::placeholders::_1)); + std::placeholders::_1), + // on completion, spawn will call this function + [](std::exception_ptr ex) + { + // if an exception occurred in the coroutine, + // it's something critical, e.g. out of memory + // we capture normal errors in the ec + // so we just rethrow the exception here, + // which will cause `ioc.run()` to throw + if (ex) + std::rethrow_exception(ex); + }); // Run the I/O service on the requested number of threads std::vector v; diff --git a/example/http/server/coro/Jamfile b/example/http/server/coro/Jamfile index 600b0fa0..b3232583 100644 --- a/example/http/server/coro/Jamfile +++ b/example/http/server/coro/Jamfile @@ -12,5 +12,5 @@ exe http-server-coro : : coverage:no ubasan:no - /boost/coroutine//boost_coroutine + /boost/context//boost_context ; diff --git a/example/http/server/coro/http_server_coro.cpp b/example/http/server/coro/http_server_coro.cpp index f134feb7..213bb416 100644 --- a/example/http/server/coro/http_server_coro.cpp +++ b/example/http/server/coro/http_server_coro.cpp @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -308,7 +309,10 @@ do_listen( &do_session, beast::tcp_stream(std::move(socket)), doc_root, - std::placeholders::_1)); + std::placeholders::_1), + // we ignore the result of the session, + // most errors are handled with error_code + boost::asio::detached); } } @@ -338,7 +342,18 @@ int main(int argc, char* argv[]) std::ref(ioc), tcp::endpoint{address, port}, doc_root, - std::placeholders::_1)); + std::placeholders::_1), + // on completion, spawn will call this function + [](std::exception_ptr ex) + { + // if an exception occurred in the coroutine, + // it's something critical, e.g. out of memory + // we capture normal errors in the ec + // so we just rethrow the exception here, + // which will cause `ioc.run()` to throw + if (ex) + std::rethrow_exception(ex); + }); // Run the I/O service on the requested number of threads std::vector v; diff --git a/example/websocket/client/coro-ssl/Jamfile b/example/websocket/client/coro-ssl/Jamfile index f6f1bb92..578e623e 100644 --- a/example/websocket/client/coro-ssl/Jamfile +++ b/example/websocket/client/coro-ssl/Jamfile @@ -19,5 +19,5 @@ exe websocket-client-coro-ssl : : coverage:no ubasan:no - /boost/coroutine//boost_coroutine + /boost/context//boost_context ; diff --git a/example/websocket/client/coro-ssl/websocket_client_coro_ssl.cpp b/example/websocket/client/coro-ssl/websocket_client_coro_ssl.cpp index c649e865..8acd0334 100644 --- a/example/websocket/client/coro-ssl/websocket_client_coro_ssl.cpp +++ b/example/websocket/client/coro-ssl/websocket_client_coro_ssl.cpp @@ -175,7 +175,18 @@ int main(int argc, char** argv) std::string(text), std::ref(ioc), std::ref(ctx), - std::placeholders::_1)); + std::placeholders::_1), + // on completion, spawn will call this function + [](std::exception_ptr ex) + { + // if an exception occurred in the coroutine, + // it's something critical, e.g. out of memory + // we capture normal errors in the ec + // so we just rethrow the exception here, + // which will cause `ioc.run()` to throw + if (ex) + std::rethrow_exception(ex); + }); // Run the I/O service. The call will return when // the socket is closed. diff --git a/example/websocket/client/coro/Jamfile b/example/websocket/client/coro/Jamfile index 761dfe8f..a0d7c51e 100644 --- a/example/websocket/client/coro/Jamfile +++ b/example/websocket/client/coro/Jamfile @@ -12,5 +12,5 @@ exe websocket-client-coro : : coverage:no ubasan:no - /boost/coroutine//boost_coroutine + /boost/context//boost_context ; diff --git a/example/websocket/client/coro/websocket_client_coro.cpp b/example/websocket/client/coro/websocket_client_coro.cpp index 7a4b0616..0a4cdf32 100644 --- a/example/websocket/client/coro/websocket_client_coro.cpp +++ b/example/websocket/client/coro/websocket_client_coro.cpp @@ -143,7 +143,18 @@ int main(int argc, char** argv) std::string(port), std::string(text), std::ref(ioc), - std::placeholders::_1)); + std::placeholders::_1), + // on completion, spawn will call this function + [](std::exception_ptr ex) + { + // if an exception occurred in the coroutine, + // it's something critical, e.g. out of memory + // we capture normal errors in the ec + // so we just rethrow the exception here, + // which will cause `ioc.run()` to throw + if (ex) + std::rethrow_exception(ex); + }); // Run the I/O service. The call will return when // the socket is closed. diff --git a/example/websocket/server/coro-ssl/Jamfile b/example/websocket/server/coro-ssl/Jamfile index 621b0ce5..46a6bb31 100644 --- a/example/websocket/server/coro-ssl/Jamfile +++ b/example/websocket/server/coro-ssl/Jamfile @@ -19,5 +19,5 @@ exe websocket-server-coro-ssl : : coverage:no ubasan:no - /boost/coroutine//boost_coroutine + /boost/context//boost_context ; diff --git a/example/websocket/server/coro-ssl/websocket_server_coro_ssl.cpp b/example/websocket/server/coro-ssl/websocket_server_coro_ssl.cpp index 3e1785f0..1d3afc88 100644 --- a/example/websocket/server/coro-ssl/websocket_server_coro_ssl.cpp +++ b/example/websocket/server/coro-ssl/websocket_server_coro_ssl.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -154,7 +155,10 @@ do_listen( &do_session, websocket::stream>(std::move(socket), ctx), - std::placeholders::_1)); + std::placeholders::_1), + // we ignore the result of the session, + // most errors are handled with error_code + boost::asio::detached); } } @@ -189,7 +193,18 @@ int main(int argc, char* argv[]) std::ref(ioc), std::ref(ctx), tcp::endpoint{address, port}, - std::placeholders::_1)); + std::placeholders::_1), + // on completion, spawn will call this function + [](std::exception_ptr ex) + { + // if an exception occurred in the coroutine, + // it's something critical, e.g. out of memory + // we capture normal errors in the ec + // so we just rethrow the exception here, + // which will cause `ioc.run()` to throw + if (ex) + std::rethrow_exception(ex); + }); // Run the I/O service on the requested number of threads std::vector v; diff --git a/example/websocket/server/coro/Jamfile b/example/websocket/server/coro/Jamfile index 5e8cbf94..0ef1350f 100644 --- a/example/websocket/server/coro/Jamfile +++ b/example/websocket/server/coro/Jamfile @@ -12,5 +12,5 @@ exe websocket-server-coro : : coverage:no ubasan:no - /boost/coroutine//boost_coroutine + /boost/context//boost_context ; diff --git a/example/websocket/server/coro/websocket_server_coro.cpp b/example/websocket/server/coro/websocket_server_coro.cpp index e15b0107..ca62c56d 100644 --- a/example/websocket/server/coro/websocket_server_coro.cpp +++ b/example/websocket/server/coro/websocket_server_coro.cpp @@ -15,6 +15,7 @@ #include #include +#include #include #include #include @@ -135,7 +136,10 @@ do_listen( &do_session, websocket::stream< beast::tcp_stream>(std::move(socket)), - std::placeholders::_1)); + std::placeholders::_1), + // we ignore the result of the session, + // most errors are handled with error_code + boost::asio::detached); } } @@ -163,7 +167,18 @@ int main(int argc, char* argv[]) &do_listen, std::ref(ioc), tcp::endpoint{address, port}, - std::placeholders::_1)); + std::placeholders::_1), + // on completion, spawn will call this function + [](std::exception_ptr ex) + { + // if an exception occurred in the coroutine, + // it's something critical, e.g. out of memory + // we capture normal errors in the ec + // so we just rethrow the exception here, + // which will cause `ioc.run()` to throw + if (ex) + std::rethrow_exception(ex); + }); // Run the I/O service on the requested number of threads std::vector v; diff --git a/example/websocket/server/fast/Jamfile b/example/websocket/server/fast/Jamfile index 3e6cbf5b..a310f6c3 100644 --- a/example/websocket/server/fast/Jamfile +++ b/example/websocket/server/fast/Jamfile @@ -12,5 +12,5 @@ exe websocket-server-fast : : coverage:no ubasan:no - /boost/coroutine//boost_coroutine + /boost/context//boost_context ; diff --git a/example/websocket/server/fast/websocket_server_fast.cpp b/example/websocket/server/fast/websocket_server_fast.cpp index 7f2a5e3c..79a10e15 100644 --- a/example/websocket/server/fast/websocket_server_fast.cpp +++ b/example/websocket/server/fast/websocket_server_fast.cpp @@ -30,6 +30,7 @@ #include #include #include +#include #include #include #include @@ -411,7 +412,7 @@ do_coro_listen( &do_coro_session, websocket::stream< beast::tcp_stream>(std::move(socket)), - std::placeholders::_1)); + std::placeholders::_1), boost::asio::detached); } } @@ -463,7 +464,7 @@ int main(int argc, char* argv[]) tcp::endpoint{ address, static_cast(port + 2u)}, - std::placeholders::_1)); + std::placeholders::_1), boost::asio::detached); // Run the I/O service on the requested number of threads std::vector v; diff --git a/test/Jamfile b/test/Jamfile index d0bc4983..851f8df1 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -36,7 +36,7 @@ project /boost/beast/test /boost/beast//lib-asio/static on:/boost/beast//lib-beast/static /boost/filesystem//boost_filesystem - /boost/coroutine//boost_coroutine + /boost/context//boost_context ; path-constant ZLIB_SOURCES : diff --git a/test/doc/core_1_refresher.cpp b/test/doc/core_1_refresher.cpp index a42c7551..5169b347 100644 --- a/test/doc/core_1_refresher.cpp +++ b/test/doc/core_1_refresher.cpp @@ -74,12 +74,14 @@ snippets() { //[code_core_1_refresher_5s asio::spawn( + sock.get_executor(), [&sock](net::yield_context yield) { std::size_t bytes_transferred = net::async_write(sock, net::const_buffer("Hello, world!", 13), yield); (void)bytes_transferred; - }); + }, + asio::detached); //] } } diff --git a/test/doc/core_3_timeouts.cpp b/test/doc/core_3_timeouts.cpp index 5da9eb51..69956d9b 100644 --- a/test/doc/core_3_timeouts.cpp +++ b/test/doc/core_3_timeouts.cpp @@ -397,7 +397,10 @@ https_get (std::string const& host, std::string const& target, error_code& ec) // Set the string to return to the caller result = std::move(res.body()); - }); + }, + // this will capture exceptions thrown by the coroutine, + // which we're ignoring, since we're using error_codes to capture them. + asio::detached); // `run` will dispatch completion handlers, and block until there is // no more "work" remaining. When this call returns, the operations diff --git a/test/extras/include/boost/beast/test/yield_to.hpp b/test/extras/include/boost/beast/test/yield_to.hpp index 145e889d..c5d30f05 100644 --- a/test/extras/include/boost/beast/test/yield_to.hpp +++ b/test/extras/include/boost/beast/test/yield_to.hpp @@ -128,14 +128,20 @@ spawn(F0&& f, FN&&... fn) [&] { asio::spawn(ioc_, + std::allocator_arg, + boost::context::fixedsize_stack(2 * 1024 * 1024), [&](yield_context yield) { f(yield); std::lock_guard lock{m_}; if(--running_ == 0) cv_.notify_all(); - } - , boost::coroutines::attributes(2 * 1024 * 1024)); + }, + [](std::exception_ptr e) + { + if (e) + std::rethrow_exception(e); + }); }); spawn(fn...); }