17

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?

Sam Miller
  • 23,808
  • 4
  • 67
  • 87
myWallJSON
  • 9,110
  • 22
  • 78
  • 149
  • 6
    A problem is that it's hard to use streams on asynchronous sockets. For example, you read a string from the stream until there is no more in the buffer. But how can the you (or the stream) know if it's really the end of the string? The rest might come in another packet, and there is no way of knowing when, or indeed if, it will be delivered. – Some programmer dude Feb 10 '12 at 14:05
  • Out of curiosity have you taken a look at this? http://stackoverflow.com/questions/3668128/how-to-create-a-boost-ssl-iostream – NothingMore Feb 19 '12 at 05:05
  • @JoachimPileborg: It is extremely easy to know - until you reach end of stream or error on the socket. The rest is business logic that heavily depends on the high-level protocol in use. That being said, buffering is needed, but having C++ iostream for that is braindead. Libevent provides nice general purpose buffer API for that reason. –  Feb 24 '12 at 18:17
  • you cannot have a seekable stream for that, because you would have to read the entire response in order to seek to end, and then the data would not be available. Thus your buffer should have to be as big as the position to seek to. –  Feb 28 '12 at 21:26
  • are you trying to create something for a library or for some application of your own with well defined msg structure? – Kashyap Feb 29 '12 at 18:52
  • 3
    Pretty diagram, but this isn't a particularly good SO question, and certainly not one worthy of a score of 12. What have you tried? – Lightness Races in Orbit Mar 01 '12 at 19:03

2 Answers2

1

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

0

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.

Community
  • 1
  • 1
BenE
  • 67
  • 5