There's no distinction between

message Foo {
  int32 foo = 1;
}

and

message Bar {
  int32 bar = 1;
}

It's all encoded as tags and values, the type and field names aren't
on the wire. Unknown tags are ignored (or put into a "unknown tag"
list). The framing is entirely up to you. One non-horrible thing to do
is to create e.g. a

message Header {
  string type = 1;
  int32 length = 2;
}

Then write a len(header) varint, header, payload. Then you can decode
them sequentially and get the right data on the other end. (The
horrible thing to do is to create a "container" message which has the
the payload data in a "bytes" field, since you end up
double-encoding/decoding the data.)

  -ilia

On Mon, Apr 30, 2018 at 3:49 PM, Andrew Bouchard <teddybo...@gmail.com> wrote:
> I'm using the MOOS communications framework, which allows me to send
> messages between modules in a binary, string, or numerical format using a
> centralized database. In order to minimize the number of new interfaces that
> I need to create, I'd like to send multiple messages over the same
> interface, then differentiate between them on the other end. The method I
> struck on to do this was to use ParseFromString to see whether the received
> message parsed successfully as a given type. Here's a simple example:
>
> void CModule::ProcessMessages( CMOOSMsg &msg )
> {
>     // Declare message types
>     MyMessages::TypeA type_a;
>     MyMessages::TypeB type_b;
>
>     // Parse a message of type A
>     if( type_a.ParseFromString( msg.GetString() ) )
>     {
>         printf( "Received a message of type A\n" );
>     }
>     // Parse a message of type B
>     else if( type_b.ParseFromString( msg.GetString() ) )
>     {
>         printf( "Received a message of type B\n" );
>     }
>     else
>     {
>         printf( "ERROR: Unrecognized message type" );
>     }
> }
>
> I think that I have misunderstood how ParseFromString works, because instead
> of the first call returning FALSE when msg is a serialized message of type
> B, it attempts to shoehorn the fields from type B into type A; as you might
> guess, this is causing all sorts of problems.
>
> I've been looking into the MessageLite class and its ParseFromCodedStream
> method. I think that it may do what I want, but I'm hesitant to go down the
> rabbit hole of learning a new tool if I can talk to someone else and learn
> if this is really what I want first. Does anyone here have any advice?
> Thanks very much!
>
> --
> You received this message because you are subscribed to the Google Groups
> "Protocol Buffers" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to protobuf+unsubscr...@googlegroups.com.
> To post to this group, send email to protobuf@googlegroups.com.
> Visit this group at https://groups.google.com/group/protobuf.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to protobuf+unsubscr...@googlegroups.com.
To post to this group, send email to protobuf@googlegroups.com.
Visit this group at https://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.

Reply via email to