Sending Protobuf Messages with boost::asio(使用 boost::asio 发送 Protobuf 消息)
问题描述
我正在尝试使用 Google 的协议缓冲区和 boost::asio 在 C++ 中一起破解客户端.
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?
一般来说,我很难弄清楚 boost::asio 是如何工作的.有一些教程,但它们通常只涵盖发送标准数据格式,例如 ints,它是开箱即用的.我认为我的问题是序列化,但另一方面我了解到 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 ;)
感谢您的帮助!
--> Daniel Gehriger 提供了解决方案,非常感谢!
--> Daniel Gehriger provided the solution, thanks a lot!
推荐答案
我不太了解 Google 的 Protocol buffer,但可以尝试以下操作:
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);
这篇关于使用 boost::asio 发送 Protobuf 消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 boost::asio 发送 Protobuf 消息


- 从python回调到c++的选项 2022-11-16
- 如何对自定义类的向量使用std::find()? 2022-11-07
- 使用/clr 时出现 LNK2022 错误 2022-01-01
- 静态初始化顺序失败 2022-01-01
- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01
- Stroustrup 的 Simple_window.h 2022-01-01
- STL 中有 dereference_iterator 吗? 2022-01-01
- 近似搜索的工作原理 2021-01-01
- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
- C++ 协变模板 2021-01-01