boost-asio 相关问题

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

在一个协程的子协程中使用 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

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

我稍微更改了此处找到的示例,从单个 io_context 对象运行所有进程,该对象在 main 中定义,并传递给每个任务,从不同的线程运行。 我期待着...

回答 1 投票 0

更改活动截止日期_计时器的到期时间未按预期工作

我尝试重新启动 boost asio 计时器,但它没有按预期工作 该代码基于www.boost.org/doc/libs/1_84_0/doc/html/boost_asio/reference/steady_timer.html#boost_asio.reference。

回答 1 投票 0

async_write 仅在服务器关闭后发送

我正在尝试通过 async_write 发送消息,但它们仅在我关闭服务器(ctrl-c)后发送 例如: 作为客户,我发送“test”和“test2”,并且仅在关闭后...

回答 1 投票 0

从 COM 端口读取数据 - 缓冲区溢出

我在处理来自 COM 端口的数据包时遇到问题。该端口的读取速度为115200。该端口的大量数据丢失。缓冲区几乎立即溢出。在我附加的代码中,我...

回答 1 投票 0

使用asio库读取com端口。校验和不匹配。 Python->c++

我有一个C++小项目。读取 COM 端口。我有打开 COM 端口并从中读取值的代码。在使用生成的校验和进行校验和检查期间压缩缓冲区时,va...

回答 1 投票 0

找不到主机(非权威) - Boost::asio udp 解析器

我目前正在开发一个项目,我需要解析端点,并且我正在使用 Boost Asio 的 udp 解析器。但是,我遇到以下错误:“找不到主机(非授权...

回答 1 投票 0

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