I will be sending multiple buffer types over a connection. To keep it simple, imagine a schema like:
namespace MyEvents;
table EventOne
{
id:uint;
timestamp:ulong;
adress:string;
}
table EventTwo
{
id:uint;
timestamp:ulong;
strength:float;
}
union Events {EventOne, EventTwo}
table EventHolder
{
theEvent:Events;
}
root_type EventHolder;
I generate the needed files for C++ and C#, and include them as required in my respective projects.
Here is how I am encoding events in C++ - here, an event of type EventOne:
...
uint32_t anId(645);
uint64_t aTimestamp(1234567);
string anAddress("00::00::45::56::47::e5");
flatbuffers::FlatBufferBuilder builder;
auto addressOffset= builder.CreateString(anAddress);
auto eventOneOffset= MyEvents::CreateEventOne(builder, anId, aTimestamp, addressOfset);
// Here is where I am confused - how to create the EventHolder object.
// Original code I posted about - commented out, but has error.
//auto eventHolderOffset= MyEvents::CreateEventHolder(builder, MyEvents::Events_EventOne, eventOneOffset); // Compile error here.
auto eventHolderOffset= MyEvents::CreateEventHolder(builder, MyEvents::Events_EventOne, eventOneOffset.Union()); // No compile error here.
builder.Finish(eventHolderOffset);
// Access buffer pointer and size and send it on its way.
Note that I have a problem with creating the EventHolder object: I have an offset that is type FlatBuffers::Offset<MyEvents::EventOne> but the CreateEventHolder function takes an offset of type FlatBuffers::Offset<void>.
Any help would be appreciated. FWIW, I am encoding in C++, and doing test decoding there, but sending data over UDP to a C# application to do the final decoding there. On that end, I do a type test on the packet and interpret accordingly.
Update: I found an example, and saw that I need to add the Union function to the end of my offset in CreateEventHolder. I may be good to go now.
Try eventOneOffset.Union()
to get the untyped version of that offset. Maybe this is not documented well enough.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With