Sending Protobuf Messages with boost::asio

I don't know much about Google's Protocol buffer, but try the following:

PlayerInfo info;
info.set_name(name);
// ...

boost::asio::streambuf b;
std::ostream os(&b);
info.SerializeToOstream(&os);

boost::asio::write(*sock, b);

I've just started using Google Protocol Buffers (protobuf) and also had problems sending (and receiving) messages over a computer network.

In contrast to the Java API, the C++ API does not have a writeDelimitedTo method to send a protobuf message with a delimiter. Without a delimiter we also have to send the size of the message, to be able to de-serialize it at the receive endpoint.

The C++ API offers the class ::google::protobuf::io::CodedOutputStream, defined in the header file google/protobuf/io/coded_stream.h.

The following source code demonstrates how-to send a delimited protobuf message via Boost.Asio over the wire. The example uses UDP. Since I haven't found a working example on the WWW, I share it here.

#include "boost/asio.hpp"
#include "google/protobuf/io/coded_stream.h"
#include "google/protobuf/io/zero_copy_stream_impl.h"

using ::boost::asio::ip::udp;

int main() {
  PlayerInfo message;
  message.set_name("Player 1");
  // ...

  const boost::asio::ip::address_v4 kIpAddress = boost::asio::ip::address_v4::loopback();
  const unsigned short kPortNumber = 65535;

  try {
    boost::asio::io_service io_service;
    udp::socket socket(io_service, boost::asio::ip::udp::v4());

    udp::endpoint endpoint(kIpAddress, kPortNumber);
    boost::system::error_code error;

    boost::asio::streambuf stream_buffer;
    std::ostream output_stream(&stream_buffer);

    {
      ::google::protobuf::io::OstreamOutputStream raw_output_stream(&output_stream);
      ::google::protobuf::io::CodedOutputStream coded_output_stream(&raw_output_stream);
      coded_output_stream.WriteVarint32(message.ByteSize());

      message.SerializeToCodedStream(&coded_output_stream);
      // IMPORTANT: In order to flush a CodedOutputStream it has to be deleted,
      // otherwise a 0 bytes package is send over the wire.
    }
  }

  size_t len = socket.send_to(stream_buffer.data(), endpoint, 0, error);

  if (error && error != boost::asio::error::message_size) {
    throw boost::system::system_error(error);
  }

  std::cout << "Sent " << len << " bytes data to " << kIpAddress.to_string() << "." << std::endl;
} catch (const std::exception& ex) {
  std::cerr << ex.what() << std::endl;
}

While writing this article, I've also discovered the following two questions:

  • Google Protocol Buffers: parseDelimitedFrom and writeDelimitedTo for C++
  • boost::asio::streambuf empty?

Both are related to this question and also contain (partial) answers. I hope my answer may be useful anyway.