"Chris" <ch***@tuxweb.o rg> wrote in message
news:10******** *****@corp.supe rnews.com...
Hi,
I'm playing/developing a simple p2p file sharing client for an existing
protocol.
A 2 second description of the protocol is
Message Length = int, 4 bytes
Message Code = int, 4 bytes
Data
When creating messages is easy to use a std::ostringstr eam and when
reading received messages, it easy to use a std::vector<cha r>.
I unclear as to which is more appropriate/efficient for the task and
was hoping some light could be shared on this, i have asked on irc
and been told 'it depends'. What does it depend on? :)
Thanks.
Chris
Well the most important point is that you create a Message class and that
whatever representation you choose is internal to that. Apart from anything
else it will isolate any changes to the representation from the rest of the
program.
The advantage of ostringstream is that its give you nice formatting, the
advantage of vector<char> is that you can resize to fit the message length.
Maybe the best answer would be a combination of the two. Write a
std::streambuf derived class which wraps a vector<char> held as a reference
(have a look at std::stringbuf, your code will be very similar). Now your
Message class can use a vector<char> directly for reading and wrap the same
vector<char> in your streambuf derived class for reading.
Or something like that.
john