English 中文(简体)
Sending Protobuf 增强的讯息:asio
原标题:Sending Protobuf Messages with boost::asio

I m 试图利用谷歌议定书Buffers和推力在C++中 together一个客户: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

现在我知道,下面是错的,但我要说的是:

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

我很想知道,我必须把我的信息不同地带入缓冲——但如何?

总的来说,我花了很强的时间,可以说明如何加强:阿索。 有一些辅导,但通常只包括发送标准数据格式,如在箱外工作的斜体。 我的印象是,我的问题是serialization,但另一方面,我得知,为了我,protobuf应该这样做......,现在我很混淆;)

感谢你们的帮助!

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

最佳回答

我对“谷歌议定书”的缓冲不甚了解,但试图做到以下几点:

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

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

boost::asio::write(*sock, b);
问题回答

我刚刚开始使用《谷歌议定书》Buffers(protobuf),在计算机网络上发送(和接收)信息也有问题。

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.

缩略语:protobuf=

以下源代码显示,如何通过Boost.Asio在电线上发出一个被限制的protobuf信息。 例如,UDP使用。 自2006年以来 我没有看到关于妇W的工作榜样,我在这里也这样说。

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

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

两者都与这一问题有关,还包含(部分)答案。 我希望我的回答可能是有益的。

  • Using asio::streambuf to storage data(also can include custom header)
  • Then using boost::smart_ptr::local_shared_ptr() for single thread or std::shared_ptr to delegate the owner of data.

然后在任何时候发出,例如:

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




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签