如何将 Windows 系统错误代码映射到 boost::error_condition?

发布于 2024-11-27 14:07:10 字数 639 浏览 1 评论 0原文

在下面的代码中,我想将 Windows WinSock 错误 WSAEINTR=10004 替换为通用 boost 系统错误代码,但如何将我在调试器中找到的代码与通用枚举映射?

timeout_.expires_from_now(posix_time::seconds(15));
timeout_.async_wait( boost::bind(&cancel_socket_fn,this,_1) );
asio::streambuf rd_buf;
unsigned length = read_until( socket_, rd_buf,delimiter_string, error );
timeout_.cancel();

if(error) 
{
    // how do I make this portable?
    if(error.value()==WSAEINTR) throw request_timeout_exception()
    else throw my_asio_exception(error,"Unable to read header");
}

...

cancel_socket_fn(system::error_code e) { socket_.cancel(); }

In the code below I would like to replace Windows WinSock error WSAEINTR=10004 with a generic boost system error code, but how do I map the code I found in the debugger, with the generic enums?

timeout_.expires_from_now(posix_time::seconds(15));
timeout_.async_wait( boost::bind(&cancel_socket_fn,this,_1) );
asio::streambuf rd_buf;
unsigned length = read_until( socket_, rd_buf,delimiter_string, error );
timeout_.cancel();

if(error) 
{
    // how do I make this portable?
    if(error.value()==WSAEINTR) throw request_timeout_exception()
    else throw my_asio_exception(error,"Unable to read header");
}

...

cancel_socket_fn(system::error_code e) { socket_.cancel(); }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

疧_╮線 2024-12-04 14:07:10

if (error == boost::asio::error::interrupted)...

我认为这是一个设计错误,因为如果从调用 io_service::run() (或类似)的线程调用此代码,则在 read_until() 完成之前不会调用 cancel_socket_fn()。或者,如果它们位于不同的线程中,那么就会出现同步问题,因为计时器方法不是线程安全的。

if (error == boost::asio::error::interrupted)...

And I think here is a design error because if this code is called from the thread where io_service::run() (or similar) is called then cancel_socket_fn() will not be called until read_until() finishes. Or if they are in different threads then here are synchronization problems because timer methods are not thread-safe.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文