randomgood
randomgood

Reputation: 566

Padding extra bytes to flatbuffer's buffer pointer which is to be sent over the network

Please look up this code for context.

auto normalized_log = CreateNormalizedLog(builder, pairs);
builder.Finish(normalized_log);
auto buffPtr = builder.GetBufferPointer();
SendUdpPacket(&ipInfo, reinterpret_cast<SOCKET>(ipInfo.hdle), buffPtr, builder.GetSize());

I need to pack the size of the created buffPtr (with fixed two bytes). Is there any preferred way of appending/offsetting without copying the entire buffer?

I think I cannot add size to the schema, because after receiving I should know the size without calling getRootAsNormalizedLog.

Is there any way to add extra bytes to the resulting buffer?

Upvotes: 0

Views: 1012

Answers (1)

Aardappel
Aardappel

Reputation: 6074

There's no built-in facility to make a buffer length-prefixed. You shouldn't need to either: UDP packets (and most transfer mechanisms) know the size of their payload, so prefixing it yourself would just be duplicate information.

That said, if you insist to do this without copying, you could do something like this:

auto size = static_cast<uint16_t>(builder.GetSize());
builder.PushElement(size);

This will prefix the buffer with a 16bit size. The problem with this approach is that the buffer will already have been aligned for their largest elements, so the buffer is now possibly unaligned at the destination. Hence, you're better off using a 32bit (or 64bit) length, depending on what are the largest scalars in your buffer.

Upvotes: 1

Related Questions