2

I have the following two schemas. I send data over a socket; on receiving is there any way to determine which one was sent?

namespace Objects;
    table Login {
      email:string;
      password:string;
    }

    root_type Login;
    file_identifier "LOGN";

namespace Objects;
    table Register{
      email:string;
      password:string;
    }

    root_type Register;
    file_identifier "REGR";
TT.
  • 15,774
  • 6
  • 47
  • 88
Lundira
  • 169
  • 3
  • 12

2 Answers2

1

In general case, no, FlatBuffers are strongly typed (not self-describing), so you must know what you're receiving.

But as your question already shows, a convenient way to tag a FlatBuffer binary is using file_identifier. You can test it with BufferHasIdentifier or any of the generated functions of the same name.

A better solution for sending a variety of messages is using the union feature.

Aardappel
  • 5,559
  • 1
  • 19
  • 22
0

I found a solution.

Just making a rule, int16 variable is always on top of every packet. like this,

    namespace Packets;
    table Packet1 {
      id:int16;
      text1:string;
    }

    table Packet2 {
      id:int16;
      cost:int32;
      text:string;
    }

Then you can fetch first int16 value from bytes

golang example

    tPos := flatbuffers.GetUOffsetT(packetBytes)
    offset := flatbuffers.UOffsetT(flatbuffers.SOffsetT(tPos) - flatbuffers.GetSOffsetT(packetBytes[tPos:]))
    offset = flatbuffers.UOffsetT(4) + offset
    o := flatbuffers.UOffsetT(flatbuffers.GetVOffsetT(packetBytes[offset:]))
    pos := tPos + o
    id := flatbuffers.GetInt16(packetBytes[pos:])

    switch id {
      case 1:
        fmt.Println("packet1")
      case 2:
        fmt.Println("packet2")
    }
Lion.k
  • 2,519
  • 8
  • 28
  • 43