Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
364 views
in Technique[技术] by (71.8m points)

c++ - Flatbuffers how to send in one message uint16_t header, size_t body size and uint8_t buffer

I am using FlatBuffers & Boost ASIO

I want to form a message with the following sequence:

  1. uint16_t Header identifier. (fixed size message)
  2. size_t body size <- so the reading part can know how long is the message that it needs to read. (fixed size message)
  3. Flatbuffers object. (dynamic size message, size of which is sent at 2)

So for the first two here is how I combine them to be sent:

size_t const header_length = 8;
size_t const body_size_b = 8;

ServerOpcode opc;
opc = ServerOpcode::SMSG_LOGIN_REQUEST_RESPONSE_TEST;
std::string header = std::to_string(opc);
flatbuffers::FlatBufferBuilder builder;
auto name = builder.CreateString("Orc MONSTER");
auto accountRole = Vibranium::CreateAccountRole_Packet(builder,1,name);
builder.Finish(accountRole);
size_t size = builder.GetSize();
uint8_t *buf = builder.GetBufferPointer();

std::array<char, header_length + body_size_b> buffer{};
std::copy(header.begin(), header.end(), buffer.begin());
std::copy(std::to_string(size).begin(), std::to_string(size).end(), buffer.begin() + header_length);
// How can I add builder here at third place ?
boost::asio::write(s, boost::asio::buffer(buffer,sizeof(buffer)));

I don't know how to append the flatbuffers buffer after the first two.

My question is:

How can I unify all three in sequence and send them in one boost::asio::write ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You don't need 1) and 2). To get 1), declare a file_identifier in your schema, then pass that identifier to Finish. To get a 2) use FinishSizePrefixed. All this is in the docs that you're still not wanting to read.

And again, this is a binary format, so I am not sure what you're doing with std::to_string but it is not going to work. You just need to write the contents of buf. I have no idea how to use it with Boost.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...