Skip to content

Instantly share code, notes, and snippets.

@wackoisgod
Created June 14, 2012 05:15
Show Gist options
  • Save wackoisgod/2928117 to your computer and use it in GitHub Desktop.
Save wackoisgod/2928117 to your computer and use it in GitHub Desktop.
C++ Protobufs
public void Socket::WriteDelimitedTo(protoBuf& msg) {
std::ostream requestStream(&someInputStream);
google::protobuf::io::OstreamOutputStream rawBuffer(&requestStream);
google::protobuf::io::CodedOutputStream codedOutput(&rawBuffer);
codedOutput.WriteVarint32(msg.ByteSize());
msg.SerializeToCodedStream(&codedOutput);
// WRITE TO SOME SOCKET!
boost::asio::write(socket, request);
}
public void Socket::parseDelimitedFrom(protobuf& msg) {
char buf[5000];
google::protobuf::io::ArrayInputStream arrayInputStream(buf, 5000);
google::protobuf::io::CodedInputStream codedInputStream(&arrayInputStream);
size_t messageSize;
codedInputStream.ReadVarint32(&messageSize);
msg.ParseFromCodedStream(&codedInputStream);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment