Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
grpc_service_example.cpp
Category
Protocol - gRPC service

Demonstrates:

See also
kcenon::network::protocols::grpc::grpc_server
kcenon::network::protocols::grpc::grpc_client
/*****************************************************************************
BSD 3-Clause License
Copyright (c) 2024, 🍀☀🌕🌥 🌊
All rights reserved.
*****************************************************************************/
#include <iostream>
#include <string>
using namespace kcenon::network;
int main()
{
std::cout << "=== gRPC Service Example ===" << std::endl;
// 1. Server configuration
std::cout << "\n1. gRPC Server configuration:" << std::endl;
config.num_threads = 4;
config.keepalive_time = std::chrono::milliseconds(7200000);
config.keepalive_timeout = std::chrono::milliseconds(20000);
std::cout << " Max concurrent streams: " << config.max_concurrent_streams << std::endl;
std::cout << " Worker threads: " << config.num_threads << std::endl;
std::cout << " Keepalive: " << config.keepalive_time.count() << "ms" << std::endl;
// 2. Create server and register methods
std::cout << "\n2. Creating gRPC server:" << std::endl;
auto reg_result = server.register_unary_method(
"/example.Greeter/SayHello",
[](const protocols::grpc::grpc_message& request,
protocols::grpc::server_context& ctx) -> kcenon::common::Result<protocols::grpc::grpc_message>
{
std::cout << " [Handler] Received request (" << request.data.size()
<< " bytes)" << std::endl;
std::string body = "Hello from gRPC server!";
response.data.assign(body.begin(), body.end());
return kcenon::common::ok(std::move(response));
});
if (reg_result.is_ok())
{
std::cout << " Method '/example.Greeter/SayHello' registered" << std::endl;
}
else
{
std::cout << " Method registration: " << reg_result.error().message << std::endl;
}
// 3. Client configuration
std::cout << "\n3. gRPC Client:" << std::endl;
std::cout << " Target: localhost:50051" << std::endl;
std::cout << " (Connection requires a running server)" << std::endl;
// 4. API overview
std::cout << "\n4. gRPC API overview:" << std::endl;
std::cout << " Server: register_unary_method(), start(port), stop()" << std::endl;
std::cout << " Client: connect(), call_raw(method, request, options)" << std::endl;
std::cout << " Message: grpc_message { data: vector<byte> }" << std::endl;
std::cout << "\nDone." << std::endl;
return 0;
}
gRPC server for handling RPC requests
Definition server.h:273
Context for handling a single RPC request.
Definition server.h:77
int main()
tracing_config config
Definition exporters.cpp:29
Unified gRPC protocol header.
Main namespace for all Network System components.
gRPC message with compression flag and payload
Definition frame.h:50
std::vector< uint8_t > data
Message payload.
Definition frame.h:52
size_t max_concurrent_streams
Maximum number of concurrent streams per connection.
Definition server.h:48