What about creating a CodedOutputStream over a StringOutputStream then,
and writing your messages to that?

Chris

On Thu, 13 Oct 2011 01:45:23 -0700 (PDT)
Vlunk <laurent...@gmail.com> wrote:

> That's what i was using before having to send several messages at
> once.
> 
> MessageLite::SerializeToString() expects a string as argument, what
> can i give him since i'm actually building the message from a stream.
> I'm using WriteLittleEndian32 & WriteVarint32 to build the message,
> how would i do working with strings ?
> 
> On Oct 13, 8:10 am, Christopher Head <hea...@gmail.com> wrote:
> > -----BEGIN PGP SIGNED MESSAGE-----
> > Hash: RIPEMD160
> >
> > Why not just use the "MessageLite::SerializeToString()" function?
> >
> > Chris
> >
> > On Wed, 12 Oct 2011 04:18:02 -0700 (PDT)
> >
> >
> >
> >
> >
> >
> >
> >
> >
> > Vlunk <laurent...@gmail.com> wrote:
> > > Hello,
> >
> > > I'm trying to send a string representation of several different
> > > protobuf objects
> >
> > > Here are two objects:
> >
> > > test::Arrow arrow;
> > > arrow.set_id(5);
> > > arrow.set_x(256.00321);
> > > arrow.set_y(128.21689);
> >
> > > test::Char char;
> > > char.set_id(5);
> > > char.set_x(25.21356);
> > > char.set_y(501.21359);
> >
> > > I know the doc is showing this :
> > > int fd = open("myfile", O_WRONLY);
> > > ZeroCopyOutputStream* raw_output = new FileOutputStream(fd);
> > > CodedOutputStream* coded_output = new
> > > CodedOutputStream(raw_output);
> >
> > > But I have no idea how to apply that in my case, since i'm not
> > > using a file. I think my problem is that i'm a bit in the blur
> > > with the *stream* concept.
> >
> > > int id = 1;
> > > coded_output ->WriteLittleEndian32(id);
> > > coded_output ->WriteVarint32(arrow.ByteSize());
> > > arrow.SerializeToCodedStream(coded_output);
> >
> > > int id = 2;
> > > coded_output ->WriteLittleEndian32(id);
> > > coded_output ->WriteVarint32(char.ByteSize());
> > > char.SerializeToCodedStream(coded_output);
> >
> > > coded_output is a stream but i need a string how do serialize it
> > > that way now !? (server is python)
> > > * Sending as string*
> >
> > > delete coded_output;
> > > delete raw_output;
> > > close(fd);
> >
> > > Then the reading part :
> >
> > > *"Converting" a string to a stream*
> > > *Stream initialization*
> > > int32 type;
> > > int32 size;
> > > coded_input_stream.ReadLittleEndian32(&type);
> > > coded_input_stream.ReadVarint32(&size);
> > > CodedInputStream::Limit old_limit =
> > > coded_input_stream.PushLimit(size);
> > > if type==1 (pseudocode)
> > >     arrow.ParseFromCodedStream(&coded_input_stream);
> > >     coded_input_stream.PopLimit(old_limit);
> >
> > > coded_input_stream.ReadLittleEndian32(&type);
> > > coded_input_stream.ReadVarint32(&size);
> > > CodedInputStream::Limit old_limit =
> > > coded_input_stream.PushLimit(size);
> > > if type==2 (pseudocode)
> > >     char.ParseFromCodedStream(&coded_input_stream);
> > >     coded_input_stream.PopLimit(old_limit);
> >
> > > Does reading the stream actually *eat* it or i need to play a bit
> > > more on the limits ?
> >
> > > I'm aware that those question are actually more related to c++
> > > than protobuf, I don't expect you to answer them but I would like
> > > at least to know if i'm on the right track.
> >
> > > Thanks.
> >
> > -----BEGIN PGP SIGNATURE-----
> > Version: GnuPG v2.0.17 (GNU/Linux)
> >
> > iEYEAREDAAYFAk6WgMgACgkQXUF6hOTGP7e8bACgnEWiDIJcWX2FjEGRBoYya+TA
> > ZZMAmgJD50SOsck95wX54fXqUDlF8Eji
> > =8/HS
> > -----END PGP SIGNATURE-----
> 

-- 
You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To post to this group, send email to protobuf@googlegroups.com.
To unsubscribe from this group, send email to 
protobuf+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/protobuf?hl=en.

Reply via email to