简体   繁体   English

Boost.Asio:在async_read上取消了操作

[英]Boost.Asio: Operation Cancelled on async_read

Another one in the continuing saga of myself vs. Boost.Asio... 我自己与Boost.Asio继续传奇的另一个...

I have a simple asynchronous client and server that utilise async_write and async_read to communicate. 我有一个简单的异步客户端和服务器,它使用async_write和async_read进行通信。 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, ie 为了测试这个,我只是在错误处理程序中重新运行读取操作,即

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

But all this got me was a segfault in pthread_mutex_lock via a call to async_receive . 但是这一切都让我通过调用async_receivepthread_mutex_lock出现了段async_receive

Could anyone point me in the direction of any relevant information (or, better yet, tell me exactly what I'm doing wrong ;) )? 任何人都可以指出我的任何相关信息的方向(或者,更好的是,告诉我到底我做错了什么;))?

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...); 更新 :服务器和客户端基于Asio文档中的聊天服务器示例,客户端和服务器都在相同的进程下运行(这可能是一个问题吗?想多一点,他们都使用相同的io_service ...) ; both asynchronous and using Boost 1.44.0. 异步和使用Boost 1.44.0。 I'm working on OS X but this is reproducible on Linux, too. 我正在研究OS X,但这在Linux上也是可重现的。

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. 更新II :我的预感是正确的,如果给服务器和客户端单独的io_service对象,async_read会看到套接字上的字节。 This does still give a segfault in boost::asio::detail::kqueue_reactor::post_immediate_completion that seems to stem from the io_service.run() . 这仍然会在boost::asio::detail::kqueue_reactor::post_immediate_completion中给出一个boost::asio::detail::kqueue_reactor::post_immediate_completion ,它似乎源于io_service.run() Before I go any further, is using separate io_service objects the correct approach? 在我进一步讨论之前,使用单独的io_service对象是正确的方法吗?

Operation cancelled (operation_aborted error code) is sent when the socket is closed or cancelled. 当套接字关闭或取消时,将发送取消操作(operation_aborted错误代码)。

Most likely your connection is somehow going out of scope. 很可能你的连接超出了范围。

Perhaps as it happened to me you forgot to attach the async_handlers to a shared_from_this() pointer. 也许因为它发生在我身上,你忘了将async_handlers附加到shared_from_this ()指针。 Ie 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));

And NOT like this: 不是这样的:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM