Detect aborted connection during Boost.Asio request [duplicate]

时光总嘲笑我的痴心妄想 提交于 2019-12-04 19:33:56

The key to this problem is to avoid doing request processing in the receive handler. Previously, I was doing something like this:

async_receive(..., recv_handler)

void recv_handler(error) {
    if (!error) {
        parse input
        process input
        async_send(response, ...)

Instead, the appropriate pattern is more like this:

async_receive(..., recv_handler)

void async_recv(error) {
    if (error) {
        canceled_flag = true;
    } else {
        // start a processing event
        if (request_in_progress) {
            capture input from input buffer
            io_service.post(process_input)
        }
        // post another read request
        async_receive(..., recv_handler)
    }
}

void process_input() {
    while (!done && !canceled_flag) {
        process input
    }
    async_send(response, ...)
}

Obviously I have left out lots of detail, but the important part is to post the processing as a separate "event" in the io_service thread pool so that an additional receive can be run concurrently. This allows the "connection aborted" message to be received while processing is in progress. Be aware, however, that this means two threads must necessarily communicate with each other requiring some kind of synchronization and the input that's being processed must be kept separately from the input buffer into which the receive call is being placed, since more data may arrive due to the additional read call.

edit:

I should also note that, should you receive more data while the processing is happening, you probably do not want to start another asynchronous processing call. It's possible that this later processing could finish first, and the results could be sent to the client out-of-order. Unless you're using UDP, that's likely a serious error.

Here's some pseudo-code:

async_read (=> read_complete)
read_complete
    store new data in queue
    if not currently processing
         if a full request is in the queue
             async_process (=> process_complete)
    else ignore data for now
    async_read (=> read_complete)
async_process (=> process_complete)
    process data
process_complete
    async_write_result (=> write_complete)
write_complete
    if a full request is in the queue
        async_process (=> process_complete)

So, if data is received while a request is in process, it's queued up but not processed. Once processing completes and the result is sent, then we may start processing again with the data that was received earlier.

This can be optimized a bit more by allowing processing to occur while the result of the previous request is being written, but that requires even more care to ensure that the results are written in the same order as the requests were received.

Just check for boost::asio::error::eof error in your async_receive handler. It means the connection has been closed.

If the connection has went through an orderly shutdown, i.e. the client called close or shutdown on the socket, then you can do a non-blocking one byte read from the socket to determine if it's still connected:

int ret = recv(sockfd, buf, 1, MSG_DONTWAIT | MSG_PEEK);
  1. If it's connected but there's no data in the buffer you'll get a return of -1 with errno == EAGAIN
  2. If it's connected and there's data you'll get back 1, and the MSG_PEEK flag will leave the data in the socket buffer.
  3. otherwise ret will equal 0 indicating a graceful shutdown of the connection.

Now this technique isn't full proof, but it's work a significant portion of the time, as long as the FIN packet from the client has arrived.

You should be able to adapt this for use with Boost.Asio as long as it lets you pass socket flags to it's recv function.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!