How about using extensions to solve this? You could define a generic
message:
message Packet {
extensions n to m;
}
// Payload messages
message TestMessage {
extend Packet {
optional TestMessage test = 10;
}
required string msg = 2;
}
message TestMessage2 {
extend Packet {
optional TestMessage2 test2 = 11;
}
required int32 int = 2;
}
Then rather than transmitting bare TestMessage or TestMessage2's, you
would transmit Packet messages, with only one of the extensions in the
field. Something like: (assuming Java since you mentioned ByteBuffer)
// Send a TestMessage
TestMessage.Builder payload_builder = TestMessage.newBuilder();
// Build payload.
TestMessage payload = payload_builder.build();
Packet packet = Packet.newBuilder().setExtension(TestMessage.test,
payload);
// Transmit packet
packet.writeTo(...);
This is effectively equivalent to your proposal - you would assign a
unique id to each message type, and the proto library will take care
of transmitting that id on the wire. You would just have to go through
an extra accessor to get at the payload.
On Jul 21, 6:12 am, Benedikt Hallinger
<[email protected]> wrote:
> Just to be more precise: the overall goal is, that the messagetype
> identification stuff should be managed entirely in the proto files. the
> user should only be concerned to write his handlers and the network
> protocol definitions.
>
>
>
> -------- Original Message --------
> Subject: Detecting message type prior parsing a ByteBuffer containing the
>
> message
> Date: Tue, 21 Jul 2009 15:01:38 +0200
> From: Benedikt Hallinger <[email protected]>
> To: <[email protected]>
>
> Hello,
> i currently try to build some small framework that eases the usage of
> protocol buffer for a project of me.
>
> The system allows for easier protocol handling and is composed of this:
> - a PacketManager responsible for dispatching recieved messages
> (bytebuffers) to PacketHandlers
> - several PacketHandlers that are able to handle one specific message
> described by a .proto file
>
> The PacketHandlers know which message type of the available they can handle
> and tell this information to the PacketManager when they are registered.
> The overall logic already works, but the main problem is how to detect
> which kind of message is recieved (and therefore which PacketHandler must
> be called to handle the message). I tried to solve this by an header
> message with an id-field with a default value, but this wont work because
> the id field is not set and therefore cant be extracted from the
> bytebuffer.
>
> This problem however can be overcome when at message construction time the
> type is explicitely set. But i wanted to implement a way the user need not
> to deal with message ids but can concentrate on the payload data.
> It would be cool if one can define "hardcoded" fields in the protocol
> definition file so that those values are always sendet over the wire and
> are (optionally) immutable.
>
> ---File: net.proto---
>
> message Header {
> optional int32 type = 1;
> }
> message TestMessage {
> optional int32 type = 1 [default=10];
>
> required string msg = 2;
> }
> ---------------------
> ^^ since TestMessage.type is not set in source code, it will not be sent
> over the wire. I know that with "required" i could force the user to set
> that field, but it would be good if he didnt need to deal with that:
>
> ---File: net.proto.advanced---
>
> message Header {
> required int32 type = 1;
> }
> message TestMessage {
> optional int32 type = 1 [static=10];
>
> required string msg = 2;
> }
> ---------------------
>
> Most probably im just missing something important as im new to this topic.
> With best regards!
> Beni
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Protocol Buffers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/protobuf?hl=en
-~----------~----~----~----~------~----~------~--~---