使用 boost::asio 发送 Protobuf 消息 [英] Sending Protobuf Messages with boost::asio

查看:70
本文介绍了使用 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 消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆