Got a bit of confusion with using stringstream and the CodedOutputStream of 
protobuf. Essentially, for each packet i put in a packetid that is stored 
inside a protobuf message, then i put in the actual contents which is also 
a protobuf message.

This code used to work when i was creating an std::stringstream just like 
you see below, and passing a pointer to it into this method and having it 
use that. Now that I removed that functionality, for some reason the stream 
is just entirely empty. The only thing I could think of is maybe the 
protobuf stream doesn't flush itself out until the destructor is hit and 
since that never happens until after the fact, it never gets flushed?

That was the only theory I could grab. Some tips would be great.

std::string serialize(google::protobuf::Message* message, uint32_t 
packetType)
{
    std::stringstream ss(std::stringstream::out | 
std::stringstream::binary);

    google::protobuf::io::OstreamOutputStream raw_out(&ss);
    google::protobuf::io::CodedOutputStream coded_out(&raw_out);

    std::string headerString;

    // write packet header, containing type of message we're sending
    PacketBuf::Packet p;
    p.set_type(packetType);
    p.SerializeToString(&headerString);

    coded_out.WriteVarint32(headerString.size());
    coded_out.WriteRaw(headerString.data(), headerString.size());

    std::string contentsString;
    // write actual contents
    message->SerializeToString(&contentsString);

    coded_out.WriteVarint32(contentsString.size());
    coded_out.WriteString(contentsString);

    assert(ss.str().size() > 0); // assertion is triggered :-(

    return ss.str();
}

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


Reply via email to