使用boost :: asio发送Protobuf消息

时间:2011-01-26 21:06:07

标签: c++ networking boost boost-asio protocol-buffers

我正在尝试使用Google的Protocol Buffers和boost :: asio在C ++中一起破解客户端。

我的问题是我不知道如何将protobuf消息提供给asio。 我有这个:

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

现在我知道以下是错误的,但无论如何我都会发布:

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

据我所知,我必须将我的信息以不同方式打包到缓冲区 - 但是如何?

一般来说,我很难搞清楚boost :: asio是如何工作的。有一些教程,但它们通常只包括发送标准数据格式,如ints,它是开箱即用的。我认为我的问题是序列化,但另一方面我了解到protobuf应该为我做这件事......现在我很困惑;)

感谢您的帮助!

- > Daniel Gehriger提供了解决方案,非常感谢!

3 个答案:

答案 0 :(得分:10)

我对Google的协议缓冲区了解不多,但请尝试以下方法:

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

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

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

答案 1 :(得分:2)

我刚刚开始使用Google Protocol Buffers (protobuf),但在通过计算机网络发送(和接收)消息方面也遇到了问题。

与Java API相比,C ++ API没有writeDelimitedTo方法来发送带分隔符的protobuf消息。 如果没有分隔符,我们还必须发送消息的大小,以便能够在接收端点对其进行反序列化。

C ++ API提供了标题文件google/protobuf/io/coded_stream.h中定义的类::google::protobuf::io::CodedOutputStream

以下源代码演示了如何通过线路通过Boost.Asio发送带分隔符的protobuf消息。该示例使用UDP。由于我没有在WWW上找到一个有效的例子,我在这里分享。

#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;
}

在撰写本文时,我还发现了以下两个问题:

两者都与此问题相关,并且还包含(部分)答案。我希望无论如何我的答案都可能有用。

答案 2 :(得分:0)

  • 使用 asio::streambuf 存储数据(也可以包含自定义标头)
  • 然后将 boost::smart_ptr::local_shared_ptr() 用于单线程或使用 std::shared_ptr 来委托数据的所有者。

然后随时发送,例如:

boost::local_shared_ptr<asio::streambuf> wbuf;
asio::async_write(sock, *wbuf, [&, wbuf](const asio::error_code &ec, std::size_t len){});
相关问题