Message Identifier in Protobug Message
Is there a way to get something like an ID for a protobuf message.
E.g. I have:
syntax = "proto3";
package protobuf;
message parameters_t
string audio_device = 1;
message master_t
enum type_t
unknown = 0;
login_rsp = 1;
type_t type = 1;
What I want to do in C++ is send the size over the wire, then some ID of the message and the the buffer.
E.g. paramters_t
is ID 0 (statically accessible) and master_t
is ID 1, so I can do in my code:
if (id == protobuf::master_t::id)
...
else if (id == protobuf::paramters_t::id)
...
Is there any way to achieve this without manually assigning the values? I want that to be set in the protofile somehow. It can be a constant I define myself, i dont care.
c++ protocol-buffers
add a comment |
Is there a way to get something like an ID for a protobuf message.
E.g. I have:
syntax = "proto3";
package protobuf;
message parameters_t
string audio_device = 1;
message master_t
enum type_t
unknown = 0;
login_rsp = 1;
type_t type = 1;
What I want to do in C++ is send the size over the wire, then some ID of the message and the the buffer.
E.g. paramters_t
is ID 0 (statically accessible) and master_t
is ID 1, so I can do in my code:
if (id == protobuf::master_t::id)
...
else if (id == protobuf::paramters_t::id)
...
Is there any way to achieve this without manually assigning the values? I want that to be set in the protofile somehow. It can be a constant I define myself, i dont care.
c++ protocol-buffers
add a comment |
Is there a way to get something like an ID for a protobuf message.
E.g. I have:
syntax = "proto3";
package protobuf;
message parameters_t
string audio_device = 1;
message master_t
enum type_t
unknown = 0;
login_rsp = 1;
type_t type = 1;
What I want to do in C++ is send the size over the wire, then some ID of the message and the the buffer.
E.g. paramters_t
is ID 0 (statically accessible) and master_t
is ID 1, so I can do in my code:
if (id == protobuf::master_t::id)
...
else if (id == protobuf::paramters_t::id)
...
Is there any way to achieve this without manually assigning the values? I want that to be set in the protofile somehow. It can be a constant I define myself, i dont care.
c++ protocol-buffers
Is there a way to get something like an ID for a protobuf message.
E.g. I have:
syntax = "proto3";
package protobuf;
message parameters_t
string audio_device = 1;
message master_t
enum type_t
unknown = 0;
login_rsp = 1;
type_t type = 1;
What I want to do in C++ is send the size over the wire, then some ID of the message and the the buffer.
E.g. paramters_t
is ID 0 (statically accessible) and master_t
is ID 1, so I can do in my code:
if (id == protobuf::master_t::id)
...
else if (id == protobuf::paramters_t::id)
...
Is there any way to achieve this without manually assigning the values? I want that to be set in the protofile somehow. It can be a constant I define myself, i dont care.
c++ protocol-buffers
c++ protocol-buffers
asked Nov 5 at 19:02
Nidhoegger
2,26611939
2,26611939
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
A typical way is to just define an enum:
enum MsgId
MSGID_MASTER = 0;
MSGID_PARAMETERS = 1;
You can then access them in C++ like constants.
Thank you, ive done it in a pretty similar way, but with my way you can use it in templates.
– Nidhoegger
Nov 14 at 10:46
add a comment |
What I have done to solve this now is this (arguably not very elegant) solution of putting an enum in every message like this:
enum pt
invalid = 0;
packet_type = 3;
This enum is in every message, but with another value for packet_type
. It looks like this:
syntax = "proto3";
package protobuf;
message parameters_t
enum pt
invalid = 0;
packet_type = 1;
string audio_device = 1;
message login_message_t
enum pt
invalid = 0;
packet_type = 2;
enum process_type_t
invalid_process = 0;
player = 1;
display = 2;
process_type_t process_type = 1;
uint32 player_id = 2;
message generic_message_t
enum pt
invalid = 0;
packet_type = 3;
enum type_t
unknown = 0;
login_done = 1;
audio_init_success = 2;
type_t type = 1;
message error_message_t
enum pt
invalid = 0;
packet_type = 4;
enum error_t
unknown = 0;
could_not_init = 1;
could_not_open_audio_device = 2;
error_t error = 1;
bool critical = 2;
The advantage of this is that I can use it in templates for a class that automatically detects the packet type and identifies it to give it to a registered handler, as it can be accessed via protobuf::error_message_t::packet_type
to get the value.
There is very similar to a GOF pattern? It's very similar to 'chain of responsability'
– José Manuel Ramos
Nov 14 at 10:52
It is indeed...
– Nidhoegger
Nov 14 at 10:55
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53160556%2fmessage-identifier-in-protobug-message%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
A typical way is to just define an enum:
enum MsgId
MSGID_MASTER = 0;
MSGID_PARAMETERS = 1;
You can then access them in C++ like constants.
Thank you, ive done it in a pretty similar way, but with my way you can use it in templates.
– Nidhoegger
Nov 14 at 10:46
add a comment |
A typical way is to just define an enum:
enum MsgId
MSGID_MASTER = 0;
MSGID_PARAMETERS = 1;
You can then access them in C++ like constants.
Thank you, ive done it in a pretty similar way, but with my way you can use it in templates.
– Nidhoegger
Nov 14 at 10:46
add a comment |
A typical way is to just define an enum:
enum MsgId
MSGID_MASTER = 0;
MSGID_PARAMETERS = 1;
You can then access them in C++ like constants.
A typical way is to just define an enum:
enum MsgId
MSGID_MASTER = 0;
MSGID_PARAMETERS = 1;
You can then access them in C++ like constants.
answered Nov 11 at 6:00
jpa
5,2791226
5,2791226
Thank you, ive done it in a pretty similar way, but with my way you can use it in templates.
– Nidhoegger
Nov 14 at 10:46
add a comment |
Thank you, ive done it in a pretty similar way, but with my way you can use it in templates.
– Nidhoegger
Nov 14 at 10:46
Thank you, ive done it in a pretty similar way, but with my way you can use it in templates.
– Nidhoegger
Nov 14 at 10:46
Thank you, ive done it in a pretty similar way, but with my way you can use it in templates.
– Nidhoegger
Nov 14 at 10:46
add a comment |
What I have done to solve this now is this (arguably not very elegant) solution of putting an enum in every message like this:
enum pt
invalid = 0;
packet_type = 3;
This enum is in every message, but with another value for packet_type
. It looks like this:
syntax = "proto3";
package protobuf;
message parameters_t
enum pt
invalid = 0;
packet_type = 1;
string audio_device = 1;
message login_message_t
enum pt
invalid = 0;
packet_type = 2;
enum process_type_t
invalid_process = 0;
player = 1;
display = 2;
process_type_t process_type = 1;
uint32 player_id = 2;
message generic_message_t
enum pt
invalid = 0;
packet_type = 3;
enum type_t
unknown = 0;
login_done = 1;
audio_init_success = 2;
type_t type = 1;
message error_message_t
enum pt
invalid = 0;
packet_type = 4;
enum error_t
unknown = 0;
could_not_init = 1;
could_not_open_audio_device = 2;
error_t error = 1;
bool critical = 2;
The advantage of this is that I can use it in templates for a class that automatically detects the packet type and identifies it to give it to a registered handler, as it can be accessed via protobuf::error_message_t::packet_type
to get the value.
There is very similar to a GOF pattern? It's very similar to 'chain of responsability'
– José Manuel Ramos
Nov 14 at 10:52
It is indeed...
– Nidhoegger
Nov 14 at 10:55
add a comment |
What I have done to solve this now is this (arguably not very elegant) solution of putting an enum in every message like this:
enum pt
invalid = 0;
packet_type = 3;
This enum is in every message, but with another value for packet_type
. It looks like this:
syntax = "proto3";
package protobuf;
message parameters_t
enum pt
invalid = 0;
packet_type = 1;
string audio_device = 1;
message login_message_t
enum pt
invalid = 0;
packet_type = 2;
enum process_type_t
invalid_process = 0;
player = 1;
display = 2;
process_type_t process_type = 1;
uint32 player_id = 2;
message generic_message_t
enum pt
invalid = 0;
packet_type = 3;
enum type_t
unknown = 0;
login_done = 1;
audio_init_success = 2;
type_t type = 1;
message error_message_t
enum pt
invalid = 0;
packet_type = 4;
enum error_t
unknown = 0;
could_not_init = 1;
could_not_open_audio_device = 2;
error_t error = 1;
bool critical = 2;
The advantage of this is that I can use it in templates for a class that automatically detects the packet type and identifies it to give it to a registered handler, as it can be accessed via protobuf::error_message_t::packet_type
to get the value.
There is very similar to a GOF pattern? It's very similar to 'chain of responsability'
– José Manuel Ramos
Nov 14 at 10:52
It is indeed...
– Nidhoegger
Nov 14 at 10:55
add a comment |
What I have done to solve this now is this (arguably not very elegant) solution of putting an enum in every message like this:
enum pt
invalid = 0;
packet_type = 3;
This enum is in every message, but with another value for packet_type
. It looks like this:
syntax = "proto3";
package protobuf;
message parameters_t
enum pt
invalid = 0;
packet_type = 1;
string audio_device = 1;
message login_message_t
enum pt
invalid = 0;
packet_type = 2;
enum process_type_t
invalid_process = 0;
player = 1;
display = 2;
process_type_t process_type = 1;
uint32 player_id = 2;
message generic_message_t
enum pt
invalid = 0;
packet_type = 3;
enum type_t
unknown = 0;
login_done = 1;
audio_init_success = 2;
type_t type = 1;
message error_message_t
enum pt
invalid = 0;
packet_type = 4;
enum error_t
unknown = 0;
could_not_init = 1;
could_not_open_audio_device = 2;
error_t error = 1;
bool critical = 2;
The advantage of this is that I can use it in templates for a class that automatically detects the packet type and identifies it to give it to a registered handler, as it can be accessed via protobuf::error_message_t::packet_type
to get the value.
What I have done to solve this now is this (arguably not very elegant) solution of putting an enum in every message like this:
enum pt
invalid = 0;
packet_type = 3;
This enum is in every message, but with another value for packet_type
. It looks like this:
syntax = "proto3";
package protobuf;
message parameters_t
enum pt
invalid = 0;
packet_type = 1;
string audio_device = 1;
message login_message_t
enum pt
invalid = 0;
packet_type = 2;
enum process_type_t
invalid_process = 0;
player = 1;
display = 2;
process_type_t process_type = 1;
uint32 player_id = 2;
message generic_message_t
enum pt
invalid = 0;
packet_type = 3;
enum type_t
unknown = 0;
login_done = 1;
audio_init_success = 2;
type_t type = 1;
message error_message_t
enum pt
invalid = 0;
packet_type = 4;
enum error_t
unknown = 0;
could_not_init = 1;
could_not_open_audio_device = 2;
error_t error = 1;
bool critical = 2;
The advantage of this is that I can use it in templates for a class that automatically detects the packet type and identifies it to give it to a registered handler, as it can be accessed via protobuf::error_message_t::packet_type
to get the value.
answered Nov 14 at 10:48
Nidhoegger
2,26611939
2,26611939
There is very similar to a GOF pattern? It's very similar to 'chain of responsability'
– José Manuel Ramos
Nov 14 at 10:52
It is indeed...
– Nidhoegger
Nov 14 at 10:55
add a comment |
There is very similar to a GOF pattern? It's very similar to 'chain of responsability'
– José Manuel Ramos
Nov 14 at 10:52
It is indeed...
– Nidhoegger
Nov 14 at 10:55
There is very similar to a GOF pattern? It's very similar to 'chain of responsability'
– José Manuel Ramos
Nov 14 at 10:52
There is very similar to a GOF pattern? It's very similar to 'chain of responsability'
– José Manuel Ramos
Nov 14 at 10:52
It is indeed...
– Nidhoegger
Nov 14 at 10:55
It is indeed...
– Nidhoegger
Nov 14 at 10:55
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53160556%2fmessage-identifier-in-protobug-message%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown