发送的Protobuf与消息的boost :: ASIO [英] Sending Protobuf Messages with boost::asio
问题描述
我想在C一起下锅客户++使用谷歌的协议缓冲器和boost :: ASIO。
I'm trying to hack a client together in C++ using Google's Protocol Buffers and boost::asio.
我的问题是,我不知道我怎么能养活的protobuf消息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?
一般来说,我有一个很难搞清楚如何提高:: ASIO的作品。有一些教程,但他们通常只涵盖发送标准数据格式,如int型,其工作外的开箱。我想,我的问题是的系列化的,但在另一方面,我了解到的protobuf要帮我这个忙......现在我很困惑;)
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 ;)
感谢您的帮助!
- >丹尼尔Gehriger提供的解决方案,非常感谢
--> Daniel Gehriger provided the solution, thanks a lot!
推荐答案
我不知道很多关于谷歌的协议缓冲区,但请尝试以下操作:
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);
这篇关于发送的Protobuf与消息的boost :: ASIO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!