2013/01/31

[工作點滴] protocol buffer message

The statusapi.pb-c.h and statusapi.pb-c.c are generated. Only header file is printing here for further explaination.
statusap.pb-c.h
/* Generated by the protocol buffer compiler.  DO NOT EDIT! */

#ifndef PROTOBUF_C_statusapi_2eproto__INCLUDED
#define PROTOBUF_C_statusapi_2eproto__INCLUDED

#include <google/protobuf-c/protobuf-c.h>

PROTOBUF_C_BEGIN_DECLS


typedef struct _Statusapi__StatusRequestType Statusapi__StatusRequestType;
typedef struct _Statusapi__StatusResponseType Statusapi__StatusResponseType;
typedef struct _Statusapi__StatusParams Statusapi__StatusParams;


/* --- enums --- */


/* --- messages --- */

struct  _Statusapi__StatusRequestType
{
  ProtobufCMessage base;
};
#define STATUSAPI__STATUS_REQUEST_TYPE__INIT \
 { PROTOBUF_C_MESSAGE_INIT (&statusapi__status_request_type__descriptor) \
     }


struct  _Statusapi__StatusResponseType
{
  ProtobufCMessage base;
};
#define STATUSAPI__STATUS_RESPONSE_TYPE__INIT \
 { PROTOBUF_C_MESSAGE_INIT (&statusapi__status_response_type__descriptor) \
     }


struct  _Statusapi__StatusParams
{
  ProtobufCMessage base;
  protobuf_c_boolean has_zoom_status;
  int32_t zoom_status;
  protobuf_c_boolean has_focus_status;
  int32_t focus_status;
};
#define STATUSAPI__STATUS_PARAMS__INIT \
 { PROTOBUF_C_MESSAGE_INIT (&statusapi__status_params__descriptor) \
    , 0,0, 0,0 }


/* Statusapi__StatusRequestType methods */
void   statusapi__status_request_type__init
                     (Statusapi__StatusRequestType         *message);
size_t statusapi__status_request_type__get_packed_size
                     (const Statusapi__StatusRequestType   *message);
size_t statusapi__status_request_type__pack
                     (const Statusapi__StatusRequestType   *message,
                      uint8_t             *out);
size_t statusapi__status_request_type__pack_to_buffer
                     (const Statusapi__StatusRequestType   *message,
                      ProtobufCBuffer     *buffer);
Statusapi__StatusRequestType *
       statusapi__status_request_type__unpack
                     (ProtobufCAllocator  *allocator,
                      size_t               len,
                      const uint8_t       *data);
void   statusapi__status_request_type__free_unpacked
                     (Statusapi__StatusRequestType *message,
                      ProtobufCAllocator *allocator);
/* Statusapi__StatusResponseType methods */
void   statusapi__status_response_type__init
                     (Statusapi__StatusResponseType         *message);
size_t statusapi__status_response_type__get_packed_size
                     (const Statusapi__StatusResponseType   *message);
size_t statusapi__status_response_type__pack
                     (const Statusapi__StatusResponseType   *message,
                      uint8_t             *out);
size_t statusapi__status_response_type__pack_to_buffer
                     (const Statusapi__StatusResponseType   *message,
                      ProtobufCBuffer     *buffer);
Statusapi__StatusResponseType *
       statusapi__status_response_type__unpack
                     (ProtobufCAllocator  *allocator,
                      size_t               len,
                      const uint8_t       *data);
void   statusapi__status_response_type__free_unpacked
                     (Statusapi__StatusResponseType *message,
                      ProtobufCAllocator *allocator);
/* Statusapi__StatusParams methods */
void   statusapi__status_params__init
                     (Statusapi__StatusParams         *message);
size_t statusapi__status_params__get_packed_size
                     (const Statusapi__StatusParams   *message);
size_t statusapi__status_params__pack
                     (const Statusapi__StatusParams   *message,
                      uint8_t             *out);
size_t statusapi__status_params__pack_to_buffer
                     (const Statusapi__StatusParams   *message,
                      ProtobufCBuffer     *buffer);
Statusapi__StatusParams *
       statusapi__status_params__unpack
                     (ProtobufCAllocator  *allocator,
                      size_t               len,
                      const uint8_t       *data);
void   statusapi__status_params__free_unpacked
                     (Statusapi__StatusParams *message,
                      ProtobufCAllocator *allocator);
/* --- per-message closures --- */

typedef void (*Statusapi__StatusRequestType_Closure)
                 (const Statusapi__StatusRequestType *message,
                  void *closure_data);
typedef void (*Statusapi__StatusResponseType_Closure)
                 (const Statusapi__StatusResponseType *message,
                  void *closure_data);
typedef void (*Statusapi__StatusParams_Closure)
                 (const Statusapi__StatusParams *message,
                  void *closure_data);

/* --- services --- */

typedef struct _Statusapi__Status_Service Statusapi__Status_Service;
struct _Statusapi__Status_Service
{
  ProtobufCService base;
  void (*set_status)(Statusapi__Status_Service *service,
                     const Statusapi__StatusParams *input,
                     Statusapi__StatusResponseType_Closure closure,
                     void *closure_data);
  void (*get_status)(Statusapi__Status_Service *service,
                     const Statusapi__StatusRequestType *input,
                     Statusapi__StatusParams_Closure closure,
                     void *closure_data);
};
typedef void (*Statusapi__Status_ServiceDestroy)(Statusapi__Status_Service *);
void statusapi__status__init (Statusapi__Status_Service *service,
                              Statusapi__Status_ServiceDestroy destroy);
#define STATUSAPI__STATUS__BASE_INIT \
    { &statusapi__status__descriptor, protobuf_c_service_invoke_internal, NULL }
#define STATUSAPI__STATUS__INIT(function_prefix__) \
    { STATUSAPI__STATUS__BASE_INIT,\
      function_prefix__ ## set_status,\
      function_prefix__ ## get_status  }
void statusapi__status__set_status(ProtobufCService *service,
                                   const Statusapi__StatusParams *input,
                                   Statusapi__StatusResponseType_Closure closure,
                                   void *closure_data);
void statusapi__status__get_status(ProtobufCService *service,
                                   const Statusapi__StatusRequestType *input,
                                   Statusapi__StatusParams_Closure closure,
                                   void *closure_data);

/* --- descriptors --- */

extern const ProtobufCMessageDescriptor statusapi__status_request_type__descriptor;
extern const ProtobufCMessageDescriptor statusapi__status_response_type__descriptor;
extern const ProtobufCMessageDescriptor statusapi__status_params__descriptor;
extern const ProtobufCServiceDescriptor statusapi__status__descriptor;

PROTOBUF_C_END_DECLS


#endif  /* PROTOBUF_statusapi_2eproto__INCLUDED */


Message Methods


Following content mostly from http://code.google.com/p/protobuf-c/wiki/Generated_Code.
To initialize a message for yourself, use the uppercased __INIT macro: For example:
   Statusapi__StatusParams message = STATUSAPI__STATUS_PARAMS__INIT;
This will properly handle default values.

Sometimes, a function that initializes a message of a give type is useful. For that purpose, we provide an __init() function
    Statusapi__StatusParams message;
    statusapi__status_params__init (&message);

We generate some functions for each message are:
  • init(). Equivalent to assigning the __INIT macro to its argument. It's useful in some contexts, like initializing a block of memory allocated by the user.
  • unpack(). Unpack data for a particular message-format, using an optional allocator for the data: Statusapi__StatusParams * statusapi__status_params__unpack (ProtobufCAllocator *allocator, size_t length, const unsigned char *data);
    It's a good idea to not modify the unpacked message. That's because we haven't decided exactly whether the message's fields are allocated separately or together. The only thing you can really assume is that free_unpacked() will always to the right thing with the return value from unpack(). In practice, we will never be allocating memory for required and optional primitive types, so you CAN modify those values before calling free_unpacked().
    To make other kinds of changes, make a copy and track your own allocations:
    Statusapi__StatusParams *unpacked;
    Statusapi__StatusParams my_params;
    unpacked = statusapi__status_params__unpack (NULL, len, data);
    my_params = *unpacked;
    my_params.zoom_status = 1;

    ... when done with unpacked and my_params, call free_unpacked()
  • free_unpacked(). Free a message that you obtained with the unpack method:
void statusapi__status_params__free_unpacked (Statusapi__StatusParams *message, ProtobufCAllocator *allocator);
  • get_packed_size(). Find how long the serialized representation of the data will be:
size_t statusapi__status_params__get_packed_size (const Statusapi__StatusParams *message);
  • pack(). Pack message into buffer; assumes that buffer is long enough (use get_packed_size first!).
size_t statusapi__status_params__pack(const Statusapi__StatusParams *message, unsigned char *packed_data_out);
  • pack_to_buffer(). Pack message into virtualized buffer.
size_t statusapi__status_params__pack_to_buffer(const Statusapi__StatusParams *message, ProtobufCBuffer *buffer);
For the definitions of ProtobufCBuffer and ProtobufCAllocator, see the libprotobuf_c page.
Finally, note that default-value handling is a little subtle- it'll probably mostly work as you expect (it's mostly modeled after the c++ implementation), but it's got a quirk regarding optional string values. There is no "has_field" for a string field. To determine if a string-field is set to the default value, use something like:
#define my_struct_has_field(my_struct) \
   (my_struct.field != NULL            \
 && my_struct.field != my_struct__field__default_value)

Now our method is judge the string if it's NULL or not. So it depends on what you want.

Next topic is to talk about the service and RPC system of the protobuf-c.



沒有留言: