myWallJSON
myWallJSON

Reputation: 9502

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 enter image description here

with buffer up to say 1kb?

Upvotes: 17

Views: 2002

Answers (2)

I think something like "go to the end of the stream" won't be possible for a TCP connection. 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)?

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

So it will be hard (/impossible?) to implement a seekable TCP stream in general. Even if you have a unlimited buffer (not only 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. But also in this scenario a fixed size buffer of 1Kb won't work unless you use the HTTP/1.1 Range header.

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. 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

Upvotes: 1

BenE
BenE

Reputation: 67

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. 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.

Upvotes: 0

Related Questions