gpt4 book ai didi

c++ - Boost tcp stream with filtering input stream 导致挂起

转载 作者:可可西里 更新时间:2023-11-01 02:52:37 25 4
gpt4 key购买 nike

考虑以下几点:

std::istream& operator>>(std::istream& is, message& p)
{
typedef boost::archive::binary_iarchive in;
boost::iostreams::filtering_istream fis;
fis.push(is, 0);
in stream(fis);
stream >> p;

return is;
}

其中message& p是一个可序列化结构,std::istream&是是一个tcp流。

服务器等待客户端在 in stream(fis); 构造函数中发送数据,并在收到数据后继续。 stream 对象将来自 tcp 流的数据序列化为 message 对象。

现在,当通过添加以下内容实际过滤接收到的数据时:

std::istream& operator>>(std::istream& is, message& p)
{
typedef boost::archive::binary_iarchive in;
boost::iostreams::filtering_istream fis;
fis.push(boost::iostreams::zlib_decompressor(), 0);
fis.push(is, 0);
in stream(fis);
stream >> p;

return is;
}

应该从 tcp 流中读取数据,将其解压缩并将其序列化到 message 对象。但相反,它卡在 in stream(fis); 上,并在数据发送 10-20 次或客户端断开连接后返回。

我已经尝试将 zlib 解压缩器的内部缓冲区设置为 0 和 1,其中 0 使断言失败,而 1 以某种方式仅工作一次(在第二次迭代时抛出)。我还将 filtering_istream 缓冲区设置为 0,这样就不会导致挂起。我还注意到,如果 std::istream& 是 是一个文件流,它就可以正常工作。

那么为什么使用tcp流时函数会卡在in stream(fis);上呢?它与 zlib 处理缓冲区的方式有关,还是 filtering_istream 特定的?我还想听听一些解决此问题的解决方法。

最佳答案

你已经自己回答了问题;)

I also noticed, that if the std::istream& is is a file stream, it works correctly.

filtering_istream 一直在轮询直到收到 EOF,因此因为 TCP 流仅在客户端断开连接时结束 - 它不会结束流读取。在 ifstream 上,它很容易得到他的 EOF 并结束处理。

关于c++ - Boost tcp stream with filtering input stream 导致挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13332049/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com