There is protobuf and protobuf-c website:
We are implementing remote control from back-end applications using protobuf-c. The protobuf-c has extensions from Google’s protobuf for RPC functionality.
The first experiment is using the message directly by packing messages into shared memory between the server and the client. Both sides can get the message without issues.
Our target is to use the RPC approach that protobuf-c provides. First, we create a .proto file — here’s an example statusapi.proto:
package statusapi;
message StatusRequestType {}
message StatusResponseType {}
message StatusParams {
optional int32 zoom_status = 1 [default = 0];
optional int32 focus_status = 2 [default = 0];
}
service Status {
rpc set_status (StatusParams) returns (StatusResponseType);
rpc get_status (StatusRequestType) returns (StatusParams);
}
Use the protobuf-c tool to generate source code:
./protobuf-c --c_out=. statusapi.proto
This creates statusapi.pb-c.c and statusapi.pb-c.h. Now let’s build the RPC server and client:
status_server.c:
#include <stdio.h>
#include <string.h>
#include <statusapi.pb-c.h>
PROTOBUF_C_BEGIN_DECLS
#include <google/protobuf-c/protobuf-c-rpc.h>
PROTOBUF_C_END_DECLS
static ProtobufC_RPC_Server *server;
static Statusapi__StatusParams status_params = STATUSAPI__STATUS_PARAMS__INIT;
void statusapi__set_status(Statusapi__Status_Service *service,
const Statusapi__StatusParams *input,
Statusapi__StatusResponseType_Closure closure,
void *closure_data)
{
fprintf(stderr,"set status.\n");
if (input->has_zoom_status) {
status_params.has_zoom_status=input->has_zoom_status;
status_params.zoom_status=input->zoom_status;
}
if (input->has_focus_status) {
status_params.has_focus_status=input->has_focus_status;
status_params.focus_status=input->focus_status;
}
closure (NULL, closure_data);
}
void statusapi__get_status(Statusapi__Status_Service *service,
const Statusapi__StatusRequestType *input,
Statusapi__StatusParams_Closure closure,
void *closure_data)
{
fprintf(stderr,"get status!!\n");
closure (&status_params, closure_data);
}
Statusapi__Status_Service statusapi_service = STATUSAPI__STATUS__INIT(statusapi__);
int main(int argc, char *argv[])
{
server = protobuf_c_rpc_server_new (PROTOBUF_C_RPC_ADDRESS_LOCAL,
"status_rpc.socket",
(ProtobufCService *)&statusapi_service,
NULL);
while(1) {
protobuf_c_dispatch_run(protobuf_c_dispatch_default());
}
return 0;
}
Prepare two terminal windows. One runs the status_server and the other runs the status_client. The client sends a set_status message and the server saves the content in local variables. Then the client sends a get_status message and the server returns the stored values.
More detailed discussion of protobuf-c functions to follow.
Comments & Feedback