-2

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 ?

Venelin
  • 2,905
  • 7
  • 53
  • 117
  • You are wasting your time, you have to learn before trying to go directly to "flat buffer", I bet that after understanding asio you will discover that you don't need flat buffer... – Jean Davy Sep 29 '20 at 07:03
  • @JeanDavy how come? Boost Asio is library providing networking solution on the other side FlatBuffers provides convenient way for serialization/deserialization of the messages. How come Boost Asio makes FlatBuffers unnecessary ? – Venelin Sep 29 '20 at 07:47
  • "How come Boost Asio ..." this is exactly why you have to learn asio ! – Jean Davy Sep 30 '20 at 06:55

1 Answers1

1

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.

Aardappel
  • 5,559
  • 1
  • 19
  • 22
  • Also I understand that I can set file identifier like this `auto accountRole = Vibranium::CreateAccountRole_Packet(builder,1,name);const char *HEADER = "ABCD"; builder.FinishSizePrefixed(accountRole, HEADER);` however how can I firstly check the indetifier so I know in which format to decode it? – Venelin Sep 23 '20 at 16:10
  • Is there any general way to take the buffer identifier ? I see that once I generate with flatc I get function like `*AccountRole_PacketIdentifier()` but that is only for that table. What if I have plenty and the server should use their identifiers to know what type exactly is that? – Venelin Sep 23 '20 at 16:26