Message Identifier in Protobug Message










2














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.










share|improve this question


























    2














    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.










    share|improve this question
























      2












      2








      2


      1





      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.










      share|improve this question













      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 5 at 19:02









      Nidhoegger

      2,26611939




      2,26611939






















          2 Answers
          2






          active

          oldest

          votes


















          1














          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.






          share|improve this answer




















          • 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


















          0














          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.






          share|improve this answer




















          • 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










          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
          );



          );













          draft saved

          draft discarded


















          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









          1














          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.






          share|improve this answer




















          • 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















          1














          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.






          share|improve this answer




















          • 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













          1












          1








          1






          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.






          share|improve this answer












          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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          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
















          • 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













          0














          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.






          share|improve this answer




















          • 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















          0














          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.






          share|improve this answer




















          • 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













          0












          0








          0






          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.






          share|improve this answer












          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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          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
















          • 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

















          draft saved

          draft discarded
















































          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.




          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          Use pre created SQLite database for Android project in kotlin

          Darth Vader #20

          Ondo