(ProtoBuf) Can't parse message of type "data.Data" because it is missing required fields: ID

StackOverflow https://stackoverflow.com/questions/23373546

  •  12-07-2023
  •  | 
  •  

Domanda

I am trying to send a protocolbuffer file through winsock but I'm having the following problem atm. When I send the data it might be a serialized version of proto packet "A" or "B" but the server doesn't know that. All proto packets have an ID field as first field so I made a packet "C" that only has an ID as field.

When the server receives a packet it deserializes it first to a "C" proto object just to check the ID it has. After that, when the server knows the ID, it can be deserialized again to the right proto object.

So, at the moment, I do this but when the packet arrives (which is also a "C" object to test it out) it gets deserialized to a "C" object and it returns this error:

[librotobuf] Can't parse message of type "C" because it is missing reauired fields: ID

This is seems weird because I have it filled in when I send it. Does this have something to do with the byteorder? I'm kind of lost on this one.

Any help is appreciated. I hope the problem is clear.

È stato utile?

Soluzione

In general, the "standard" way to tell between different incoming message types in protobuf is to define a union type that would contain these different messages and also a field to determine which of them is present. See official docs on this here.
Example for your case:

message A
{
    // ...
}

message B
{
    // ...
}

message C
{
    enum Type { A = 1; B = 2; }

    required Type type = 1;   
    optional A a = 2;
    optional B b = 3;
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top