Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
grpc_service_example.cpp
Go to the documentation of this file.
1/*****************************************************************************
2BSD 3-Clause License
3
4Copyright (c) 2024, 🍀☀🌕🌥 🌊
5All rights reserved.
6*****************************************************************************/
7
26
27#include <iostream>
28#include <string>
29
30using namespace kcenon::network;
31
32int main()
33{
34 std::cout << "=== gRPC Service Example ===" << std::endl;
35
36 // 1. Server configuration
37 std::cout << "\n1. gRPC Server configuration:" << std::endl;
39 config.max_concurrent_streams = 100;
40 config.num_threads = 4;
41 config.keepalive_time = std::chrono::milliseconds(7200000);
42 config.keepalive_timeout = std::chrono::milliseconds(20000);
43
44 std::cout << " Max concurrent streams: " << config.max_concurrent_streams << std::endl;
45 std::cout << " Worker threads: " << config.num_threads << std::endl;
46 std::cout << " Keepalive: " << config.keepalive_time.count() << "ms" << std::endl;
47
48 // 2. Create server and register methods
49 std::cout << "\n2. Creating gRPC server:" << std::endl;
51
52 auto reg_result = server.register_unary_method(
53 "/example.Greeter/SayHello",
54 [](const protocols::grpc::grpc_message& request,
55 protocols::grpc::server_context& ctx) -> kcenon::common::Result<protocols::grpc::grpc_message>
56 {
57 std::cout << " [Handler] Received request (" << request.data.size()
58 << " bytes)" << std::endl;
59
61 std::string body = "Hello from gRPC server!";
62 response.data.assign(body.begin(), body.end());
63 return kcenon::common::ok(std::move(response));
64 });
65
66 if (reg_result.is_ok())
67 {
68 std::cout << " Method '/example.Greeter/SayHello' registered" << std::endl;
69 }
70 else
71 {
72 std::cout << " Method registration: " << reg_result.error().message << std::endl;
73 }
74
75 // 3. Client configuration
76 std::cout << "\n3. gRPC Client:" << std::endl;
77 std::cout << " Target: localhost:50051" << std::endl;
78 std::cout << " (Connection requires a running server)" << std::endl;
79
80 // 4. API overview
81 std::cout << "\n4. gRPC API overview:" << std::endl;
82 std::cout << " Server: register_unary_method(), start(port), stop()" << std::endl;
83 std::cout << " Client: connect(), call_raw(method, request, options)" << std::endl;
84 std::cout << " Message: grpc_message { data: vector<byte> }" << std::endl;
85
86 std::cout << "\nDone." << std::endl;
87 return 0;
88}
gRPC server for handling RPC requests
Definition server.h:273
Context for handling a single RPC request.
Definition server.h:77
tracing_config config
Definition exporters.cpp:29
Unified gRPC protocol header.
int main()
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