-2

documentaion have one simple example "serialization-unserialization" with one packet. (i not found more examples)

if i have a many structs of packet, i need use union to this? or this bad idea?

how i need verify income packets, before use it in program?

method Verify - all i need to validate packet? (struct is correct, fields are permissible)

include "body1.fbs";
include "body2.fbs";

union PacketBody {
   body1,
   body2
}

table Packet {
   body: PacketBody
}

root_type Packet;
auto request = Packet::GetPacket(&buf);
auto ver = new flatbuffers::Verifier(buf, len);
if(request->Verify(*ver) {
   //good ?
   auto body = request->body();
   auto body_type = request->body_type();
   map[body_type](body);
} else {
   //wrong packet
}
noadev
  • 97
  • 2
  • You do not need to repeat, as first paragraph, what was written on title – Amadeus Sep 20 '19 at 02:47
  • 1
    Repeating the title within the question is a valid, and I prefer it to seeing things like "the question in the title". However, The first paragraph should contain more than just the title; see the "Introduce the problem before you post any code" section of [ask]. (The title serves as an attention-grabbing summary, while the paragraph should explain the details.) If writing a fuller summary causes the question to be rephrased somewhat, so much the better. – JaMiT Sep 20 '19 at 05:07
  • sorry, i expanded my question – noadev Sep 21 '19 at 21:14

1 Answers1

0

Try replacing map[body_type](body) (not sure what that's intended to do?) by something like if (body_type == PacketBody_body1) { ... reinterpret_cast<const body1 *>(body) ... }

Also, the verifier is for detecting data corruption, I wouldn't use it for testing if you have the right packet.

Aardappel
  • 5,559
  • 1
  • 19
  • 22
  • thanks for the answer, i expanded my question on the remarks on its formulation. what u mean about "for testing"? method verify check only struct, not fields for permissible? (and therefore use map[body_type](body) is unsafe)? – noadev Sep 21 '19 at 21:18