Boost.Asio的:操作上取消async_read [英] Boost.Asio: Operation Cancelled on async_read

查看:316
本文介绍了Boost.Asio的:操作上取消async_read的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

另外一个我自己的传奇继续与Boost.Asio的...

Another one in the continuing saga of myself vs. Boost.Asio...

我有一个简单的异步客户端和服务器,利用async_write和async_read沟通。客户端可以成功写入字节到插座,但从来没有服务器看到他们。在服务器上读取我的处理失败,操作被取消。

I have a simple asynchronous client and server that utilise async_write and async_read to communicate. The client can successfully write bytes to the socket, but the server never sees them; my read handler on the server fails with "Operation cancelled".

我倾向于认为,这可能是与客户端写入数据的计时问题的服务器后曾试图阅读并失败了,但我还以为得到的数据将等待在插座反正(除非插座已在此期间关闭)。

I'm inclined to believe that this may be a timing issue with the client writing the data after the server has tried to read it and failed, but I would have thought the data would be waiting on the socket anyway (unless the socket has been closed in the meantime).

要测试这个,我只是重新运行在错误处理程序读取操作,即

To test this I simply re-ran the read operation in the error handler, i.e.

read_handler()
{
    if (!error) {
        /* bytes read */
    } else {
        async_read(socket, buffer, read_handler)
    }
}

但是,这一切让我经来电的pthread_mutex_lock A段错误,以 async_receive

任何人都可以指出我的任何相关信息的方向(或更好的,告诉我,我做什么错了;)?)

Could anyone point me in the direction of any relevant information (or, better yet, tell me exactly what I'm doing wrong ;) )?

更新:服务器和客户端都是基于在短耳文档聊天服务器为例,在客户端和服务器在同一进程(下运行会这样一个问题,思考多一点?它们都使用同一个io_service对象...);异步和使用Boost 1.44.0。我工作在OS X但这是在Linux上可重现了。

UPDATE: The server and client are based around the chat server example in the Asio docs, with the client and server both running under the same process (could this be an issue? Thinking a bit more they both use the same io_service...); both asynchronous and using Boost 1.44.0. I'm working on OS X but this is reproducible on Linux, too.

更新II :我的预感是正确的,如果服务器和客户端被赋予独立的io_service对象的对象async_read看到插座上的字节数。但这仍然给在升压段错误:: ASIO ::详细:: kqueue_reactor :: post_immediate_completion ,似乎从干 io_service.run() 。之前我去任何进一步,使用独立的 io_service对象对象的正确方法呢?

UPDATE II: My hunch was correct and if the server and client are given separate io_service objects the async_read sees the bytes on the socket. This does still give a segfault in boost::asio::detail::kqueue_reactor::post_immediate_completion that seems to stem from the io_service.run(). Before I go any further, is using separate io_service objects the correct approach?

推荐答案

操作被取消(operation_aborted错误code)在套接字<一个被发送href=\"http://www.boost.org/doc/libs/1_50_0/doc/html/boost_asio/reference/basic_stream_socket/close/overload1.html\">closed或取消。

Operation cancelled (operation_aborted error code) is sent when the socket is closed or cancelled.

最有可能您的连接莫名其妙地走出去的范围。

Most likely your connection is somehow going out of scope.

也许因为它发生在我身上,你忘了附加async_handlers到<一个href=\"http://www.boost.org/doc/libs/1_50_0/libs/smart_ptr/enable_shared_from_this.html\">shared_from_this()指针。
即你应该附上您的处理程序是这样的:

Perhaps as it happened to me you forgot to attach the async_handlers to a shared_from_this() pointer. I.e. You should be attaching your handlers like this:

async_read(m_socket,
           boost::asio::buffer((void*)m_buffer, m_header_size),
           boost::bind(&TcpConnection::handleRead, 
           shared_from_this(),
           boost::asio::placeholders::error,
           boost::asio::placeholders::bytes_transferred));

不可以是这样的:

async_read(m_socket,
           boost::asio::buffer((void*)m_buffer, m_header_size),
           boost::bind(&TcpConnection::handleRead, 
           this, //<- This will go out of scope and the socket will be closed
           boost::asio::placeholders::error,
           boost::asio::placeholders::bytes_transferred));

这篇关于Boost.Asio的:操作上取消async_read的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆