Boost logo

Boost Users :

From: eg (egoots_at_[hidden])
Date: 2007-02-14 12:48:43


Darren Garvey wrote:
> Hi,
>
> I was wondering about tcp::iostreams. I need to have the ability to
> write code like:
> obj<< "Hello world"
> and at intervals (of size) have a header attached and then have it
> written to an open socket.
>
> I have been looking into Boost.Iostreams to do this, but I was wondering
> if rather than implementing the whole streambuf/sink, I could just
> create a filtering_streambuf (naming convention taken from
> Boost.Iostreams ) - which would attach the header when needed - and just
> instantiate a tcp::iostream object with that streambuf.
>
> I'm still very foggy on the Iostreams library - and streams/buffers in
> general - but does that sound like it could work?
>

This comment is about [iostreams] not [asio] as your message header
indicates.

I haven't used it (IoStreams) for tcp streams but I have for encryption.
  I wrote a Multi Character Dual Use filter (it reads and writes).

If you only need to write, you could build a Multi Character Output Filter.

You need to write your Filter class and implement a Write function of
the form

template<typename Sink>
std::streamsize write(Sink& dest, const char* s, std::streamsize n)
{
...
}

To handle the header stuff. Just keep a state member variable in your
class and count how many characters you write... then add your header
when necessary and reset the count.

I think you may need to to some flushing too?

Check out the Multi-character filter section of the Tutorial in:
http://boost.org/libs/iostreams/doc/index.html

It has some good example code


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net