简体   繁体   English

提升iostreams:如何创建缓冲(用于读取)TCP流?

[英]Boost iostreams: How to create buffered (for reading) TCP stream?

So my main question here is how to implement a structure on top of asio tcp::socket or tcp::iostream that would implement some kind of input-seekable filter 所以我的主要问题是如何在asio tcp :: socket或tcp :: iostream之上实现一个结构,它将实现某种输入可搜索的过滤器 在此输入图像描述

with buffer up to say 1kb ? 缓冲区高达1kb

I think something like "go to the end of the stream" won't be possible for a TCP connection. 我认为对于TCP连接来说,“转到流的末尾”是不可能的。 Should a call like this (see following code) wait (block) for the connection to close? 如果这样的调用(参见下面的代码)等待(阻止)连接关闭? And how should it store the response when it reaches the buffer size (for example 1Kb)? 如何在响应达到缓冲区大小时(例如1Kb)存储响应?

s.seekg (0, ios::end);

So it will be hard (/impossible?) to implement a seekable TCP stream in general. 因此,通常很难(/不可能?)实现可搜索的TCP流。 Even if you have a unlimited buffer (not only 1Kb). 即使你有一个无限的缓冲区(不仅是1Kb)。

It should be possible to implement something like input-seekable for specific protocols such as HTTP(S) when the Content-Length header is set. 当设置Content-Length头时,应该可以实现诸如HTTP(S)之类的特定协议的输入可搜索之类的东西。 But also in this scenario a fixed size buffer of 1Kb won't work unless you use the HTTP/1.1 Range header. 但在这种情况下,除非使用HTTP / 1.1 Range标头,否则1Kb的固定大小缓冲区将不起作用。

Perhaps it helps: Christopher M. Kohlhoff (author of Boost asio) implemented Urdl (marked as 'Prealpha' on SourceForge) where he modeled the HTTP connection as a istream. 也许它有所帮助:Christopher M. Kohlhoff(Boost asio的作者)实现了Urdl (在SourceForge上标记为'Prealpha'),他将HTTP连接建模为istream。 I think the method read_some could be interesting to you: https://github.com/jnorthrup/urdl/blob/master/include/urdl/detail/http_read_stream.hpp#L426 我认为read_some方法对您来说很有趣: https//github.com/jnorthrup/urdl/blob/master/include/urdl/detail/http_read_stream.hpp#L426

I'm not familiar with this particular boost module. 我不熟悉这个特殊的升压模块。 But, if you are look for a way to create a buffer which acts like a repository of sorts, I would create another thread to manage it. 但是,如果你正在寻找一种方法来创建一个缓冲区,它就像一个类型的存储库,我会创建另一个线程来管理它。 The thread could LIFO the incoming stream, handling filter requests and buffer management. 线程可以LIFO传入流,处理过滤器请求和缓冲区管理。 Keeping it on a separate thread would mean it would be attentive to the incoming packets before the systems buffer runs out, so you don't have to worry about missing anything. 将它保存在一个单独的线程上意味着它会在系统缓冲区用完之前关注传入的数据包,因此您不必担心丢失任何内容。 A message queue could be created to mediate between threads. 可以创建消息队列以在线程之间进行调解。

That said, in the end it would probably easiest to look to a prewritten libary to handle it and save yourself a little time. 也就是说,最终可能最容易找到一个预先写好的图书馆来处理它并节省一点时间。 Check out this post . 看看这篇文章

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

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