简体   繁体   English

通过Boost Asio的async_read_some回调函数向服务器发出有关用户-客户端错误的信号

[英]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 我正在尝试使用boost::asio::ip::tcp::socket的成员函数async_read_some从HTTP服务器异步读取数据

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. 例如,假设我需要10个递归的async_read_some函数调用来从服务器接收整个答案。 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. 当第4个async_read_some调用其handler handler函数时,由于抛出某种异常,我无法在此handler函数中解析接收到的数据。 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? 但是当我进入此handler函数时,该怎么办呢?

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. 我可以用ip::tcp::socket::shutdown_type::shutdown_receive参数调用ip::tcp::socket::shutdown ,但是我以后不能使用相同的连接了。

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. 我也不能调用ip::tcp::socketcancel成员函数,该函数取消正在进行的异步操作,因为在那时,该套接字上实际上没有处理任何异步操作-仅在处理异步操作的回调函数。

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. 尤其是在shutdown时。

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. 我认为,如果async_read_some (对async_read_some除外)的任何错误对此连接均致命,则该错误。

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

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