Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending Protobuf Messages with boost::asio

I'm trying to hack a client together in C++ using Google's Protocol Buffers and boost::asio.

My problem is that I don't know how I can feed the protobuf message to asio. What I have is this:

// set up *sock - works
PlayerInfo info;
info.set_name(name);
// other stuff

Now I know that the following is wrong, but I'll post it anyways:

size_t request_length = info.ByteSize();
boost::asio::write(*sock, boost::asio::buffer(info, request_length));

I got as far as that I know that I have to pack my message differently into the buffer - but how?

Generally speaking, I'm having a hard time figuring out how boost::asio works. There are some tutorials, but they normally just cover sending standard data formats such as ints, which works out-of-the-box. I figured that my problem is serialization, but on the other hand I learned that protobuf should do this for me... and now I'm confused ;)

Thanks for your help!

--> Daniel Gehriger provided the solution, thanks a lot!

like image 850
adi64 Avatar asked Jan 26 '11 21:01

adi64


2 Answers

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.

like image 140
Florian Wolters Avatar answered Oct 24 '22 13:10

Florian Wolters


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);
like image 40
Daniel Gehriger Avatar answered Oct 24 '22 13:10

Daniel Gehriger