boost-asio 相关问题

Boost.Asio是一个用于网络和低级I / O编程的跨平台C ++库,它使用现代C ++方法为开发人员提供一致的异步模型。

关于正确关闭 Boost Beast 服务器的说明

我正在使用 Boost Beast 高级服务器示例(可从 Boost.org 获取),并将其用作我自己的服务器的基础。我的理解是,在这个例子中,session 和 li...

回答 1 投票 0

在cpp中使用boost::asio以非阻塞异步方式读取文件内容

我有类似于此 boost:asio file read 的问题。 boot::asio 是否支持在异步读取功能中从文件系统读取常规文件? 我尝试了下面的代码,但它似乎不起作用。 #包括...

回答 1 投票 0

GCC 地址清理程序错误或无效移动?

我有一个 boost::asio::io_context 我想移动它。由于没有指针我无法移动它,因此我使用 std::unique_ptr。但是,我注意到当使用 -fsanitize=address heap-use-

回答 1 投票 0

如何知道 tcp::acceptor 何时准备好?

我正在编写一个HTTP服务器。我写了以下内容并且效果很好: 升压::asio::io_context io_context; tcp::acceptor 接受器(io_context, ...); 接受器.async_accept(...); 自动服务器_thr...

回答 1 投票 0

如何知道boost::asio::io_context是否准备好?

我正在编写一个HTTP服务器。我写了以下内容并且效果很好: 升压::asio::io_context io_context; tcp::acceptor 接受器(io_context, ...); 接受器.async_accept(...); 自动服务器_thr...

回答 1 投票 0

谁知道boost::asio::io_context是否准备好

我正在编写一个HTTP服务器。我写了以下内容并且效果很好: 升压::asio::io_context io_context; tcp::acceptor 接受器(io_context, ...); 接受器.async_accept(...); 自动服务器_thr...

回答 1 投票 0

在一个协程的子协程中使用 websocket 时卡在 co_await

我尝试在两个协程中使用一个 ws 对象,一个协程用于发送,另一个用于接收。然而,当我尝试在现有协程中使用子协程时,代码停留在

回答 1 投票 0

Boost ASIO:什么执行器与默认完成令牌相关联?他们应该在本地进行表演吗?

考虑以下示例(Godbolt): #包括 #包括 #包括 命名空间 asio = boost::asio; 内联无效输出(自动常量&消息){ 斯塔...

回答 1 投票 0

使用 Boost C++ 清理主机名

我试图将主机名/ipv4/ipv6 传递给 linux 中的 ping 实用程序(以避免使用原始套接字、root),但是,我想在运行命令之前确保它是有效的。例如,如果恶意

回答 1 投票 0

我可以使用 boost::asio::io_context 独立使用不同时间间隔的不同方法吗?

我正在尝试实现一个 C++ 代码,该代码应该以不同的时间间隔调用三个彼此独立的不同方法。 例如,假设 method_1 应该被称为 e...

回答 1 投票 0

我可以独立使用 boost::asio::io_context 来独立使用不同时间间隔的不同方法吗?

我正在尝试实现一个 C++ 代码,该代码应该以不同的时间间隔调用三个彼此独立的不同方法。 例如,假设 method_1 应该被称为 e...

回答 1 投票 0

boost asio 日期时间服务器示例和使用 std::async 不起作用

我正在尝试学习boost asio(boost 1.84,C ++ 20,Ubuntu 23.04),并对以下日间服务器示例进行了一些修改:https://www.boost.org/doc/libs/1_84_0 /doc/html/boost_asio/

回答 1 投票 0

boost.asio:如何取消可等待而不导致终止?

考虑以下代码: #包括 #包括 #包括 #包括 考虑以下代码: #include <boost/asio/awaitable.hpp> #include <boost/asio/bind_cancellation_slot.hpp> #include <boost/asio/cancellation_signal.hpp> #include <boost/asio/co_spawn.hpp> #include <boost/asio/detached.hpp> #include <boost/asio/io_context.hpp> #include <boost/asio/use_awaitable.hpp> namespace ba = boost::asio; ba::cancellation_signal cancel_sub; void subscribe(ba::io_context &context) { ba::co_spawn( context, []() -> ba::awaitable<void> { co_return; }, ba::bind_cancellation_slot(cancel_sub.slot(), ba::detached)); cancel_sub.emit(ba::cancellation_type::all); } int main() { ba::io_context ctx; subscribe(ctx); ctx.run(); return 0; } 它使我的程序退出,并从 boost 中的 co_await 实现的内部抛出异常: terminate called after throwing an instance of 'boost::wrapexcept<boost::system::system_error>' what(): co_await: Operation canceled [system:125] 如何避免这种情况? 我尝试过: 在协程内禁用取消异常: co_await ba::this_coro::throw_if_cancelled(false); 在我的协程的开头添加此代码以启用各种取消: co_await ba::this_coro::reset_cancellation_state(ba::enable_total_cancellation()); 没有任何变化,我的应用程序仍然退出。 准确地说,在boost的代码中:(用// !!! 标记我的评论) template <typename Handler, typename Executor, typename Function> awaitable<awaitable_thread_entry_point, Executor> co_spawn_entry_point( awaitable<void, Executor>*, co_spawn_state<Handler, Executor, Function> s) { (void) co_await co_spawn_dispatch{}; (co_await awaitable_thread_has_context_switched{}) = false; std::exception_ptr e = nullptr; try { // !!! here an exception is thrown as the cancellation is observered. co_await s.function(); } catch (...) { // !!! caught here, all is fine e = std::current_exception(); } bool switched = (co_await awaitable_thread_has_context_switched{}); if (!switched) (void) co_await co_spawn_post(); // !!! exception thrown again here as the cancellation state is checked again in await_transform! But now there is nothing to catch it... (dispatch)(s.handler_work.get_executor(), [handler = std::move(s.handler), e]() mutable { std::move(handler)(e); }); } 没有任何变化,我的应用程序仍然退出。 如果您遇到异常,但不希望应用程序退出,如何将原始协程逻辑包装在 try-catch 块中来处理所述异常,并防止其传播到协程之外? 它可以工作,因为您面临的问题与取消操作引发的异常有关,该异常会导致异常,如果不处理该异常,则会导致您的程序终止,而不是由 return_value 或 return_void (与协程相关的异常) return 机制),如 Raymond Chen 的文章“C++ 协程:如果我的 return_value 中发生异常会发生什么?”中讨论。 void subscribe(ba::io_context& context) { ba::co_spawn(context, []() -> ba::awaitable<void> { try { // Your original coroutine logic co_return; } catch (const boost::system::system_error& e) { // Handle cancellation or other system errors // For example, log the error and continue without rethrowing std::cerr << "Coroutine canceled or failed: " << e.what() << std::endl; // You can also handle or log the cancellation specifically if (e.code() == boost::asio::error::operation_aborted) { std::cerr << "Operation was explicitly canceled.\n"; } } }, ba::bind_cancellation_slot(cancel_sub.slot(), ba::detached)); cancel_sub.emit(ba::cancellation_type::all); }

回答 1 投票 0

无法使用boost/asio在C++中启动服务器

我试图在Windows上使用boost/asio编写一个简单的服务器,但是编译器抛出错误“Exception throwing at 0x00007FF90EE05B0C in Server.exe: Microsoft C++ exception: boost::

回答 1 投票 0

Boost::Asio 截止时间计时器阻止 UDP 套接字读取

我有一个简单的类,其目的是始终通过UDP读取数据,并每X秒发送一个请求数据包。我尝试通过 boost::asio::deadline_timer 实现预定的请求发送,

回答 1 投票 0

如何与 Boost.Asio 同时从 TAP 设备读取(通过 posix::stream_descriptor)?

我的程序应该同时从生成的 TAP 设备读取数据包并处理它们。为此,我使用 LaKabane 的 tuntap 库以及 Boost.Asio 的 posix::stream_descriptor。 豪...

回答 1 投票 0

boost asio tcp 连接重新连接并读取后没有收到数据

我正在使用 boost asio 连接到 TCP 服务器。 当我运行代码时,它在启动后工作正常。我发送请求并得到响应。 当我关闭 tcp 服务器(它是一个设备)时,我正在运行...

回答 1 投票 0

使用单个io_context并行运行多个超时进程

我尝试修改此处的示例并使进程并行运行,因为在我的用例中,进程在其生命周期的大部分时间里可能处于空闲状态,因此CPU资源可以更好

回答 1 投票 0

C++20 协程读/写 websocket

我想使用协程和 boost::asio 使 websocket 在单线程上运行。一个协程负责写入 (async_write),另一个协程负责读取 (async_...

回答 1 投票 0

并行运行多个 URL 的 boost monster 解析器

我想知道,如果我们运行多个 async_resolve 调用,那么实际上由于已知的限制,io_context 不会运行多个请求。 这是一个示例,其中...

回答 1 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.