From: Scott Woods (scottw_at_[hidden])
Date: 2005-04-26 15:59:56
----- Original Message -----
From: "Rob Stewart" <stewart_at_[hidden]>
Sent: Wednesday, April 27, 2005 4:57 AM
Subject: Re: [boost] Re: Re: Re: Re: (Another) socket streams library
> > As the application protocol writter you have to know how much you have
> > the buffer *at all times* and you must know when an overflow / flush
> > Errors can *only* happen when the buffer writes to the socket (
> > therefore if a programmer does his/her job correctly there will not be
> > operator << traversing and overflow boundary. This is irrespective of
> According to this, there is no way to use streams with sockets.
> Otherwise, clients of the stream interface would need to keep
> track of the size of formatted output of each object inserted on
> the stream to avoid overflow between insertion operators. How
> can they do that?
Yeah, tricky stuff. After several attempts with varying levels of success
following is the most complete and efficient that I have come up with. This
is specifically to deal with "streaming" over async sockets. This may ramble
but hopefully with purpose :-)
The insertion operator (<<) is pre-defined for all "standard" or "built-in"
Templates are defined for the standard containers. This is consistent with
the approach taken by many, including Boost serialization. Insertion
are defined for any application types. These have the appearance of;
operator<<( stream &s, application_type &a )
s << a.member_1;
s << a.member_2;
s << a.member_3;
Nothing new there :-)
But (!) rather than the expected conversion to a formatted byte stream,
operators transform the application object to its generic form (i.e. a
and place it on an outgoing queue of variants. This activity occurs in the
zone", a name coined to separate application object activity from the
behind-the-scenes processing of network/socket events.
In the "socket zone" a "WRITE" notification advises that its a "good time to
The socket code manages the transfer of those queued variants to a byte
for sending. This activity obviously requires the conversion of variants to
byte-by-byte encoding, or format.
The task of this "transfer" code is to present optimal blocks to the network
its raw material from the queue of outgoing variants. The buffering strategy
point is really the essence of this solution.
The underlying container for my buffering is a std::vector<char>. While my
not contain a block of optimal network size I attempt to take a variant off
the queue. If the
queue is non-empty I "stream" a varaint onto the end of the buffer, i.e.
append the formatted
representation of the variant using vector<>::push_back.
This specifically allows the buffer to temporarily contain more than the
system requires. This may sound like a crippling design decision but in
it works very well. It brings (IMO) the huge advantage of allowing the
"streaming" of any application object.
A block is written to the network. The amount accepted by the network is
to adjust a "skip" value, the number of leading buffered bytes that dont
be written again.
The last piece of this solution is a "wrap" operation that tidies the buffer
the end of a phase of writing (i.e. the socket code handling a "WRITE"
If the remaining bytes in the buffer are less than an optimal network block
are all shifted down to the zero-th position and the skip value is reset to
The underlying vector will (worst case) be as big as the largest streamed
object plus odd bits and pieces totalling less than an optimal network
block, i.e. if
application objects tend to "max out" at 8K then the vector may approach 16K
size. Of course "reserve" can be used to minimize some initial memory
Hopefully this sketch is enough to describe the somewhat bizarre solution I
arrived at to deal with the conflicting requirements in this area.
Trying to couple the application event of streaming an object to the network
event of writing a byte block is (only my opinion :-) doomed. I would even
go as far as saying that this is true for all streaming, i.e. to files. But
story. And dont even start me on input streams.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk