alejandrofsevilla / grpc-server-client

Generic gRPC Server and Client example.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

LinuxWorkflow

GRPC Server and Client

Asynchronous gRPC Server and Client based on the HelloWorld example, adapted to handle unary RPCs from any type of Protobuf Service.

gRPC is cloned and built as part of the project using CMake FetchContent.

Requirements

  • C++17 compiler.
  • CMake 3.22.0.
  • gRPC 1.53.1 requirements.

Example

Protobuf Service

syntax = "proto3";

service SimpleService {
  rpc SendRequest (SimpleRequest) returns (SimpleReply) {}
}

message SimpleRequest {
  string message = 1;
}

message SimpleReply {
  string message = 1;
}

Server

#include <GrpcServer.hpp>
#include <iostream>

#include "SimpleService.grpc.pb.h"

//...

GrpcServer server;
server.addListeningPort("127.0.0.1:8800", grpc::InsecureServerCredentials());
SimpleService::AsyncService service;
server.registerService(&service);
server.buildAndStart();
server.requestCallFromService<SimpleService::AsyncService, SimpleRequest,
                              SimpleReply>(
    service, &SimpleService::AsyncService::RequestSendRequest,
    [](const auto& call, [[maybe_unused]] auto& reply) {
      std::cout << call.message() << std::endl;
    });
server.startHandlingCalls();

//...

Client

#include <GrpcClient.hpp>
#include <iostream>
#include <thread>

#include "SimpleService.grpc.pb.h"

//...

GrpcClient<SimpleService> client{grpc::CreateChannel(
    "127.0.0.1:8800", grpc::InsecureChannelCredentials())};
auto thread =
    std::thread(&GrpcClient<SimpleService>::startHandlingReplies, &client);
while (true) {
  SimpleRequest call;
  std::string message;
  std::cin >> message;
  call.set_message(message);
  client.makeCall<SimpleRequest, SimpleReply>(
      call, &SimpleService::Stub::PrepareAsyncSendRequest,
      [](const auto& reply) { std::cout << reply.message() << std::endl; });
}

//...

About

Generic gRPC Server and Client example.


Languages

Language:C++ 87.5%Language:CMake 12.5%