c++ - boost asio async_read 中的随机 EOF

标签 c++ multithreading boost

我从 boost::asio::async_read 收到了 EOF 错误。我已经阅读了以下帖子(以及其他一些类似的帖子),但没有任何帮助。

  1. EOF in async_read() in boost::asio
  2. Random EOF in boost asio in multi thread
  3. EOF in boost::async_read with thread_pull and boost 1.54

关于我的应用程序: 我开发了一个 WebBrowser(使用 IE WebBrowser 控件)和浏览器与之通信的代理服务器。一切都在 IE 引擎内部发生,因此无法在浏览器中检查任何内容。在代理服务器中,我随机收到 EOF 错误 async_read 以及任何其他读取版本 (async_read_some(), read ())。这个问题我只在发布版本中遇到,而不是在调试版本中遇到(至少现在还没有)。在调试版本中,一切正常。

以下是我的阅读功能。我不知道是否存在任何竞争条件或我遗漏的任何内容。

void SocksProxyConnection::HandleRead(const boost::system::error_code& errorCode, std::size_t bytesTransferred)
{
    //Check for the errors
    if (errorCode)
    {
        std::string connectionID = boost::lexical_cast<std::string>(m_connectionID);
        UNUSED(connectionID);

        if ((errorCode.value() == boost::asio::error::connection_reset))
        {
            TVLOG_INFO(LOG("SocksProxyConnection: Connection Reset ConnectionID: %1%") % (boost::lexical_cast<std::string>(m_connectionID)).c_str());
            m_removeConnectionFromMapFunction(m_connectionID);
            SendConnectionCloseCommand();
        } 
        else if (errorCode.value() == boost::asio::error::eof)
        {
            LOG_ERRORCODE(L"SocksProxyConnection: End Of File error", errorCode);
        }
        else if (errorCode.value() != boost::asio::error::operation_aborted)
        {
            //Case: Error occurred while reading
            LOG_ERRORCODE(L"SocksProxyConnection: Error in reading the data from the webBrowser", errorCode);
        }

        return;
    }

    //No errors

    //Send the data to the remote server
    ForwardBuffer(bytesTransferred);

    //Perform the async read operation.
    boost::asio::async_read(m_socket, boost::asio::buffer(m_readDataBuffer.get(), DataBufferSize), boost::asio::transfer_at_least(1),
                            m_Strand.wrap(boost::bind(&SocksProxyConnection::HandleRead,
                                        shared_from_this(),
                                        boost::asio::placeholders::error,
                                        boost::asio::placeholders::bytes_transferred)));

}

此代理服务器有一个专用的io_service,其中包括两个异步调用async_accept()async_read()。向浏览器发送数据不是异步的。我正在使用阻塞 write() 函数。我不知道这是否有任何区别。

还有这个 bug在导致类似问题的 boost 1.54 中。但那是 3 年前的事了,现在已经修好了。目前,我正在使用 Booset 1.62。我还没有发现此版本的任何类似错误报告。

我已经尝试解决这个问题一个多星期了。如果有人现在可以帮助我,我将不胜感激。

最佳答案

你能给我们看一个独立的例子吗?

我认为您基本上遇到了短读,即 perfectly normal对于非成帧协议(protocol)。它甚至解释了为什么 EOF 是一个“错误”:

Why EOF is an Error

The end of a stream can cause read, async_read, read_until or async_read_until functions to violate their contract. E.g. a read of N bytes may finish early due to EOF. An EOF error may be used to distinguish the end of a stream from a successful read of size 0.

关于c++ - boost asio async_read 中的随机 EOF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41365033/

相关文章:

c++ - 从多个线程收集结果的缓存友好方式

c++ - Boost线程 - 通过引用传递参数

c++ - map , lambda ,remove_if

c++ - 缺少像 std::forward 这样的东西

c++ - 如何解压空的可变参数模板列表

c++ - 是否定义为 C++ 标准算法提供倒置范围?

c# - 使用线程和 MVVM 将高速数据流式传输到 WPF UI

c++ - 这是在 C++ 中启动线程的正确方法吗

c++ - boost iostreams 断言失败

c++ - boost::variant将static_visitor应用于某些类型