简体   繁体   中英

Signal server about user-client error from Boost Asio's async_read_some callback function

I'm trying to asynchronously read data from the HTTP server using boost::asio::ip::tcp::socket 's member function async_read_some

template<
    typename MutableBufferSequence,
    typename ReadHandler>
void-or-deduced async_read_some(
    const MutableBufferSequence & buffers,
    ReadHandler handler);

and I want to know a way how to signal the server that error happened on client's side of an application so that server stops trying to send the rest of the data to the client.

For example suppose I need 10 recursive async_read_some function calls to receive the whole answer from the server. When 4th async_read_some has called its handler function I fail to parse the received data within this handler function because exception of some kind is thrown. Now I want to inform the server that error happened and I don't want to receive the rest of the message. But how should I do this when I'm inside this handler function?

I could call ip::tcp::socket::shutdown with ip::tcp::socket::shutdown_type::shutdown_receive argument but than I can't use the same connection for future communication.

I also cant call ip::tcp::socket 's cancel member function that cancels ongoing asynchronous operations because at that point no asynchronous operations is actually being handled on that socket - only the callback function of asynchronous operation is being processed.

How should I handle this situation correctly if I don't want to close this connection?

I think you should close connection. In general, you cannot use this connection for future communication. Especially if you do shutdown on it.

If you try to stay with this connection it can receive trash (because of reading part of a previous answer), send trash (because non-complete previous write) or just stay with broken connection (peer host think connection is closed).

I think any error for async_read_some (except operation_aborted) if fatal to this connection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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