c language grpc

For installation, see https://blog.csdn.net/linimbus/article/details/90720086

testone.proto

syntax = "proto3";

package testone;

service Greeter {
  rpc MyTestOne (HelloRequest) returns (HelloReply) {}
}

message HelloRequest {
  string name = 1;
}

message HelloReply {
  string message = 1;
}

protoc --grpc_out=./ --plugin=protoc-gen-grpc=/usr/local/bin/protoc-gen-grpc-c testone.proto 

生成testone.grpc-c.h、testone.grpc-c.c、testone.grpc-c.service.c

Client code testone_client.c

#include<stdio.h>
#include "testone.grpc-c.h"

void testone_client_call_one(grpc_c_client_t *client)
{
        int i;
        int ret;
        grpc_c_status_t status;


        Testone__HelloRequest h;
        testone__HelloRequest__init(&h);
        Testone__HelloReply *r;

        char str[32] = {0};
        strcpy(str,"this is client");
        h.name = str;


        ret = testone__greeter__my_test_one__sync(client, NULL, 0, &h, &r, &status, -1);
        if ( ret != 0 )
        {
                printf("testone__greeter__my_test_one__sync error[%d]\n",ret);
        }
        else
        {
                printf("sync ok\n");
                printf("client get [%s]\n",r->message);
        }
        testone__hello_reply_free(r);
}

int main(int argc,char *argv[])
{
        if ( argc < 2 )
        {
                printf("please run with ip:port\n");
                return ( -1 );
        }

        int i = 0;
        for(i=0;i<1;i++)
        {
                grpc_c_init();

                grpc_c_client_t *client = grpc_c_client_init(argv[1], NULL, NULL);

                testone_client_call_one(client);

                grpc_c_client_stop(client);
                grpc_c_client_wait(client);
                grpc_c_client_free(client);
                grpc_c_shutdown();
        }

        return ( 0 );
}

Server code testone_server.c

#include<stdio.h>
#include"testone.grpc-c.h"

static grpc_c_server_t *test_server;

void testone__greeter__my_test_one_cb(grpc_c_context_t *context)
{
        int ret;
        Testone__HelloRequest *h = NULL;
        Testone__HelloReply r;
        testone__hello_reply__init(&r);

        char buf[1024] = {0};
        strcpy(buf,"this is server");
        r.message = buf;


        ret = grpc_c_read(context, (void **)&h, 0, -1);
        printf("grpc_c_read ret[%d] h->name[%s]\n",ret,h->name);

        testone__hello_request_free(h);


        ret = grpc_c_write(context, &r, 0, -1);
        if ( ret )
        {
                printf("Failed to write %d\n", ret);
        }

        grpc_c_status_t status;
        status.code = 0;
        status.message[0] = '\0';

        if (grpc_c_finish(context, &status, 0))
        {
                printf("Failed to write status\n");
        }

}



int main(int argc,char *argv[])
{
        grpc_c_init();

        test_server = grpc_c_server_create("192.168.193.140:3000", NULL, NULL);
        if (test_server == NULL)
        {
                printf("Failed to create server\n");
                exit(1);
        }

        testone__greeter__service_init(test_server);

        grpc_c_server_start(test_server);

        grpc_c_server_wait(test_server);

        grpc_c_server_destroy(test_server);

        grpc_c_shutdown();

        return ( 0 );
}

Compile:

gcc testone.grpc-c.c testone.grpc-c.service.c testone_server.c -o server -lgrpc-c -lprotobuf-c -lgrpc 
gcc testone.grpc-c.c testone_client.c -o client -lgrpc-c -lprotobuf-c -lgrpc 

 

Guess you like

Origin blog.csdn.net/woailp___2005/article/details/113605686