examples & tests use context instead of coroutine.

This commit is contained in:
Klemens
2023-01-02 10:00:03 +08:00
committed by Klemens Morgenstern
parent 1841a592d6
commit 99bceb5bff
24 changed files with 156 additions and 37 deletions

View File

@ -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)

View File

@ -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

View File

@ -19,5 +19,5 @@ exe http-client-coro-ssl :
:
<variant>coverage:<build>no
<variant>ubasan:<build>no
<library>/boost/coroutine//boost_coroutine
<library>/boost/context//boost_context
;

View File

@ -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.

View File

@ -12,5 +12,5 @@ exe http-client-coro :
:
<variant>coverage:<build>no
<variant>ubasan:<build>no
<library>/boost/coroutine//boost_coroutine
<library>/boost/context//boost_context
;

View File

@ -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.

View File

@ -19,5 +19,5 @@ exe http-server-coro-ssl :
:
<variant>coverage:<build>no
<variant>ubasan:<build>no
<library>/boost/coroutine//boost_coroutine
<library>/boost/context//boost_context
;

View File

@ -19,6 +19,7 @@
#include <boost/beast/http.hpp>
#include <boost/beast/ssl.hpp>
#include <boost/beast/version.hpp>
#include <boost/asio/detached.hpp>
#include <boost/asio/spawn.hpp>
#include <boost/config.hpp>
#include <algorithm>
@ -344,7 +345,10 @@ do_listen(
beast::ssl_stream<beast::tcp_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<std::thread> v;

View File

@ -12,5 +12,5 @@ exe http-server-coro :
:
<variant>coverage:<build>no
<variant>ubasan:<build>no
<library>/boost/coroutine//boost_coroutine
<library>/boost/context//boost_context
;

View File

@ -17,6 +17,7 @@
#include <boost/beast/http.hpp>
#include <boost/beast/version.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/asio/detached.hpp>
#include <boost/asio/spawn.hpp>
#include <boost/config.hpp>
#include <algorithm>
@ -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<std::thread> v;

View File

@ -19,5 +19,5 @@ exe websocket-client-coro-ssl :
:
<variant>coverage:<build>no
<variant>ubasan:<build>no
<library>/boost/coroutine//boost_coroutine
<library>/boost/context//boost_context
;

View File

@ -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.

View File

@ -12,5 +12,5 @@ exe websocket-client-coro :
:
<variant>coverage:<build>no
<variant>ubasan:<build>no
<library>/boost/coroutine//boost_coroutine
<library>/boost/context//boost_context
;

View File

@ -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.

View File

@ -19,5 +19,5 @@ exe websocket-server-coro-ssl :
:
<variant>coverage:<build>no
<variant>ubasan:<build>no
<library>/boost/coroutine//boost_coroutine
<library>/boost/context//boost_context
;

View File

@ -19,6 +19,7 @@
#include <boost/beast/ssl.hpp>
#include <boost/beast/websocket.hpp>
#include <boost/beast/websocket/ssl.hpp>
#include <boost/asio/detached.hpp>
#include <boost/asio/spawn.hpp>
#include <algorithm>
#include <cstdlib>
@ -154,7 +155,10 @@ do_listen(
&do_session,
websocket::stream<beast::ssl_stream<
beast::tcp_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<std::thread> v;

View File

@ -12,5 +12,5 @@ exe websocket-server-coro :
:
<variant>coverage:<build>no
<variant>ubasan:<build>no
<library>/boost/coroutine//boost_coroutine
<library>/boost/context//boost_context
;

View File

@ -15,6 +15,7 @@
#include <boost/beast/core.hpp>
#include <boost/beast/websocket.hpp>
#include <boost/asio/detached.hpp>
#include <boost/asio/spawn.hpp>
#include <algorithm>
#include <cstdlib>
@ -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<std::thread> v;

View File

@ -12,5 +12,5 @@ exe websocket-server-fast :
:
<variant>coverage:<build>no
<variant>ubasan:<build>no
<library>/boost/coroutine//boost_coroutine
<library>/boost/context//boost_context
;

View File

@ -30,6 +30,7 @@
#include <boost/beast/http.hpp>
#include <boost/beast/version.hpp>
#include <boost/beast/websocket.hpp>
#include <boost/asio/detached.hpp>
#include <boost/asio/spawn.hpp>
#include <boost/asio/strand.hpp>
#include <algorithm>
@ -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<unsigned short>(port + 2u)},
std::placeholders::_1));
std::placeholders::_1), boost::asio::detached);
// Run the I/O service on the requested number of threads
std::vector<std::thread> v;

View File

@ -36,7 +36,7 @@ project /boost/beast/test
<library>/boost/beast//lib-asio/<link>static
<boost.beast.separate-compilation>on:<library>/boost/beast//lib-beast/<link>static
<library>/boost/filesystem//boost_filesystem
<library>/boost/coroutine//boost_coroutine
<library>/boost/context//boost_context
;
path-constant ZLIB_SOURCES :

View File

@ -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);
//]
}
}

View File

@ -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

View File

@ -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<std::mutex> 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...);
}