Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
kcenon::network::protocols::grpc Namespace Reference

gRPC protocol implementation More...

Namespaces

namespace  compression
 gRPC compression algorithms
 
namespace  header_names
 gRPC request header names
 
namespace  trailer_names
 gRPC trailing header names
 

Classes

class  bidi_stream_impl
 
struct  call_options
 Options for individual RPC calls. More...
 
struct  channel_credentials_config
 Configuration for gRPC channel credentials. More...
 
class  client_stream_writer_impl
 
class  generic_service
 A service that allows dynamic method registration. More...
 
struct  grpc_channel_config
 Configuration for gRPC channel. More...
 
class  grpc_client
 gRPC client for making RPC calls More...
 
struct  grpc_message
 gRPC message with compression flag and payload More...
 
class  grpc_server
 gRPC server for handling RPC requests More...
 
struct  grpc_server_config
 Configuration for gRPC server. More...
 
class  grpc_service
 Base class for gRPC service implementations. More...
 
struct  grpc_status
 gRPC status with code, message, and optional details More...
 
struct  grpc_trailers
 gRPC trailing metadata containing status information More...
 
class  health_service
 Implementation of gRPC health checking protocol. More...
 
struct  method_descriptor
 Describes a single RPC method within a service. More...
 
struct  method_handler
 
struct  registry_config
 Configuration for service registry. More...
 
class  server_context
 Context for handling a single RPC request. More...
 
class  server_reader
 Reader interface for client streaming. More...
 
class  server_reader_writer
 Reader/writer interface for bidirectional streaming. More...
 
class  server_stream_reader_impl
 
class  server_writer
 Writer interface for server streaming. More...
 
class  service_base
 Base class for all gRPC service implementations. More...
 
struct  service_descriptor
 Describes a gRPC service and its methods. More...
 
class  service_registry
 Central registry for managing gRPC services. More...
 

Typedefs

using grpc_metadata = std::vector<std::pair<std::string, std::string>>
 Metadata key-value pair for gRPC requests/responses.
 
using unary_handler
 Handler function type for unary RPC.
 
using server_streaming_handler
 Handler function type for server streaming RPC.
 
using client_streaming_handler
 Handler function type for client streaming RPC.
 
using bidi_streaming_handler
 Handler function type for bidirectional streaming RPC.
 

Enumerations

enum class  method_type {
  unary , server_streaming , client_streaming , bidi_streaming ,
  unary , server_streaming , client_streaming , bidi_streaming
}
 Type of RPC method. More...
 
enum class  health_status { unknown , serving , not_serving , service_unknown }
 Health status for a service. More...
 
enum class  status_code : uint32_t {
  ok = 0 , cancelled = 1 , unknown = 2 , invalid_argument = 3 ,
  deadline_exceeded = 4 , not_found = 5 , already_exists = 6 , permission_denied = 7 ,
  resource_exhausted = 8 , failed_precondition = 9 , aborted = 10 , out_of_range = 11 ,
  unimplemented = 12 , internal = 13 , unavailable = 14 , data_loss = 15 ,
  unauthenticated = 16
}
 gRPC status codes (as defined in grpc/status.h) More...
 
enum class  method_type {
  unary , server_streaming , client_streaming , bidi_streaming ,
  unary , server_streaming , client_streaming , bidi_streaming
}
 

Functions

auto parse_timeout (std::string_view timeout_str) -> uint64_t
 Parse timeout string (e.g., "10S", "100m", "1000u")
 
auto format_timeout (uint64_t timeout_ms) -> std::string
 Format timeout as gRPC timeout string.
 
auto parse_method_path (std::string_view full_path) -> std::optional< std::pair< std::string, std::string > >
 Parse full method path into service and method names.
 
auto build_method_path (std::string_view service_name, std::string_view method_name) -> std::string
 Build full method path from service and method names.
 
constexpr auto status_code_to_string (status_code code) -> std::string_view
 Convert status code to string.
 

Variables

constexpr size_t grpc_header_size = 5
 gRPC message header size (5 bytes)
 
constexpr size_t default_max_message_size = 4 * 1024 * 1024
 Maximum gRPC message size (default 4MB)
 
constexpr const char * grpc_content_type = "application/grpc"
 gRPC content-type header value
 
constexpr const char * grpc_content_type_proto = "application/grpc+proto"
 gRPC content-type with proto encoding
 

Detailed Description

gRPC protocol implementation

This namespace contains all gRPC-related types and functions:

All types use the Result<T>/VoidResult pattern for error handling, providing consistent error reporting across the library.

Typedef Documentation

◆ bidi_streaming_handler

Initial value:
std::function<
grpc_status(
server_context& ctx,
server_reader_writer& stream)>

Handler function type for bidirectional streaming RPC.

Parameters
ctxServer context
streamReader/writer for messages
Returns
Final status

Definition at line 222 of file server.h.

◆ client_streaming_handler

Initial value:
std::function<
std::pair<grpc_status, std::vector<uint8_t>>(
server_context& ctx,
server_reader& reader)>

Handler function type for client streaming RPC.

Parameters
ctxServer context
readerReader for incoming messages
Returns
Pair of status and serialized response

Definition at line 197 of file server.h.

◆ grpc_metadata

using kcenon::network::protocols::grpc::grpc_metadata = std::vector<std::pair<std::string, std::string>>

Metadata key-value pair for gRPC requests/responses.

Definition at line 38 of file client.h.

◆ server_streaming_handler

Initial value:
std::function<
grpc_status(
server_context& ctx,
const std::vector<uint8_t>& request,
server_writer& writer)>

Handler function type for server streaming RPC.

Parameters
ctxServer context
requestSerialized request message
writerWriter to send response messages
Returns
Final status

Definition at line 163 of file server.h.

◆ unary_handler

Initial value:
std::function<
std::pair<grpc_status, std::vector<uint8_t>>(
server_context& ctx,
const std::vector<uint8_t>& request)>

Handler function type for unary RPC.

Parameters
ctxServer context
requestSerialized request message
Returns
Pair of status and serialized response

Definition at line 134 of file server.h.

Enumeration Type Documentation

◆ health_status

Health status for a service.

Enumerator
unknown 

Status unknown.

serving 

Service is serving.

not_serving 

Service is not serving.

service_unknown 

Service is not registered.

Definition at line 563 of file service_registry.h.

◆ method_type [1/2]

Type of RPC method.

Enumerator
unary 

Unary RPC (single request, single response)

server_streaming 

Server streaming (single request, multiple responses)

client_streaming 

Client streaming (multiple requests, single response)

bidi_streaming 

Bidirectional streaming (multiple requests and responses)

unary 
server_streaming 
client_streaming 
bidi_streaming 

Definition at line 60 of file service_registry.h.

61{
62 unary,
66};
@ server_streaming
Server streaming (single request, multiple responses)
@ client_streaming
Client streaming (multiple requests, single response)
@ bidi_streaming
Bidirectional streaming (multiple requests and responses)
@ unary
Unary RPC (single request, single response)

◆ method_type [2/2]

Enumerator
unary 

Unary RPC (single request, single response)

server_streaming 

Server streaming (single request, multiple responses)

client_streaming 

Client streaming (multiple requests, single response)

bidi_streaming 

Bidirectional streaming (multiple requests and responses)

unary 
server_streaming 
client_streaming 
bidi_streaming 

Definition at line 594 of file server.cpp.

595{
596 unary,
600};

◆ status_code

enum class kcenon::network::protocols::grpc::status_code : uint32_t
strong

gRPC status codes (as defined in grpc/status.h)

Standard gRPC status codes for RPC operations. See: https://grpc.github.io/grpc/core/md_doc_statuscodes.html

Enumerator
ok 

Not an error; returned on success.

cancelled 

The operation was cancelled.

unknown 

Unknown error.

invalid_argument 

Client specified an invalid argument.

deadline_exceeded 

Deadline expired before operation completed.

not_found 

Some requested entity was not found.

already_exists 

Entity already exists.

permission_denied 

Permission denied.

resource_exhausted 

Resource exhausted.

failed_precondition 

Operation rejected due to system state.

aborted 

Operation aborted.

out_of_range 

Operation attempted past valid range.

unimplemented 

Operation not implemented.

internal 

Internal error.

unavailable 

Service unavailable.

data_loss 

Unrecoverable data loss.

unauthenticated 

Request lacks valid authentication.

Definition at line 35 of file status.h.

36 {
37 ok = 0,
38 cancelled = 1,
39 unknown = 2,
40 invalid_argument = 3,
42 not_found = 5,
43 already_exists = 6,
44 permission_denied = 7,
47 aborted = 10,
48 out_of_range = 11,
49 unimplemented = 12,
50 internal = 13,
51 unavailable = 14,
52 data_loss = 15,
53 unauthenticated = 16
54 };
@ deadline_exceeded
Deadline expired before operation completed.
@ unimplemented
Operation not implemented.
@ out_of_range
Operation attempted past valid range.
@ unauthenticated
Request lacks valid authentication.
@ failed_precondition
Operation rejected due to system state.

Function Documentation

◆ build_method_path()

auto kcenon::network::protocols::grpc::build_method_path ( std::string_view service_name,
std::string_view method_name ) -> std::string

Build full method path from service and method names.

Parameters
service_nameFull service name (e.g., "package.Service")
method_nameMethod name
Returns
Full method path

Definition at line 58 of file service_registry.cpp.

60{
61 std::string result;
62 result.reserve(1 + service_name.size() + 1 + method_name.size());
63 result += '/';
64 result += service_name;
65 result += '/';
66 result += method_name;
67 return result;
68}

Referenced by kcenon::network::protocols::grpc::generic_service::impl::register_bidi_streaming_method(), kcenon::network::protocols::grpc::generic_service::impl::register_client_streaming_method(), kcenon::network::protocols::grpc::generic_service::impl::register_server_streaming_method(), and kcenon::network::protocols::grpc::generic_service::impl::register_unary_method().

Here is the caller graph for this function:

◆ format_timeout()

auto kcenon::network::protocols::grpc::format_timeout ( uint64_t timeout_ms) -> std::string

Format timeout as gRPC timeout string.

Parameters
timeout_msTimeout in milliseconds
Returns
Formatted timeout string

Definition at line 131 of file frame.cpp.

132{
133 std::ostringstream oss;
134
135 if (timeout_ms == 0)
136 {
137 return "0m";
138 }
139
140 // Use appropriate unit for best precision
141 if (timeout_ms >= 3600000 && timeout_ms % 3600000 == 0)
142 {
143 oss << (timeout_ms / 3600000) << "H";
144 }
145 else if (timeout_ms >= 60000 && timeout_ms % 60000 == 0)
146 {
147 oss << (timeout_ms / 60000) << "M";
148 }
149 else if (timeout_ms >= 1000 && timeout_ms % 1000 == 0)
150 {
151 oss << (timeout_ms / 1000) << "S";
152 }
153 else
154 {
155 oss << timeout_ms << "m";
156 }
157
158 return oss.str();
159}

Referenced by kcenon::network::protocols::grpc::grpc_client::impl::bidi_stream_raw(), kcenon::network::protocols::grpc::grpc_client::impl::call_raw(), kcenon::network::protocols::grpc::grpc_client::impl::client_stream_raw(), and kcenon::network::protocols::grpc::grpc_client::impl::server_stream_raw().

Here is the caller graph for this function:

◆ parse_method_path()

auto kcenon::network::protocols::grpc::parse_method_path ( std::string_view full_path) -> std::optional<std::pair<std::string, std::string>>

Parse full method path into service and method names.

Parameters
full_pathFull method path (e.g., "/package.Service/Method")
Returns
Pair of service name and method name, or nullopt if invalid

Definition at line 33 of file service_registry.cpp.

35{
36 // Expected format: "/package.Service/Method"
37 if (full_path.empty() || full_path[0] != '/')
38 {
39 return std::nullopt;
40 }
41
42 // Remove leading slash
43 auto path = full_path.substr(1);
44
45 // Find the separator between service and method
46 auto pos = path.rfind('/');
47 if (pos == std::string_view::npos || pos == 0 || pos == path.size() - 1)
48 {
49 return std::nullopt;
50 }
51
52 std::string service_name(path.substr(0, pos));
53 std::string method_name(path.substr(pos + 1));
54
55 return std::make_pair(std::move(service_name), std::move(method_name));
56}

Referenced by kcenon::network::protocols::grpc::service_registry::impl::find_method().

Here is the caller graph for this function:

◆ parse_timeout()

auto kcenon::network::protocols::grpc::parse_timeout ( std::string_view timeout_str) -> uint64_t

Parse timeout string (e.g., "10S", "100m", "1000u")

Parameters
timeout_strTimeout string with unit suffix
Returns
Timeout in milliseconds, or 0 if invalid

Supported units:

  • H: Hours
  • M: Minutes
  • S: Seconds
  • m: Milliseconds
  • u: Microseconds
  • n: Nanoseconds

Definition at line 88 of file frame.cpp.

89{
90 if (timeout_str.empty())
91 {
92 return 0;
93 }
94
95 // Get the unit suffix
96 char unit = timeout_str.back();
97
98 // Parse the numeric value
99 std::string_view num_str = timeout_str.substr(0, timeout_str.size() - 1);
100 uint64_t value = 0;
101
102 auto result = std::from_chars(num_str.data(),
103 num_str.data() + num_str.size(),
104 value);
105
106 if (result.ec != std::errc{})
107 {
108 return 0;
109 }
110
111 // Convert to milliseconds based on unit
112 switch (unit)
113 {
114 case 'H': // Hours
115 return value * 3600000;
116 case 'M': // Minutes
117 return value * 60000;
118 case 'S': // Seconds
119 return value * 1000;
120 case 'm': // Milliseconds
121 return value;
122 case 'u': // Microseconds
123 return value / 1000;
124 case 'n': // Nanoseconds
125 return value / 1000000;
126 default:
127 return 0;
128 }
129}

◆ status_code_to_string()

auto kcenon::network::protocols::grpc::status_code_to_string ( status_code code) -> std::string_view
constexpr

Convert status code to string.

Parameters
codeStatus code to convert
Returns
String representation of the status code

Definition at line 61 of file status.h.

62 {
63 switch (code)
64 {
65 case status_code::ok: return "OK";
66 case status_code::cancelled: return "CANCELLED";
67 case status_code::unknown: return "UNKNOWN";
68 case status_code::invalid_argument: return "INVALID_ARGUMENT";
69 case status_code::deadline_exceeded: return "DEADLINE_EXCEEDED";
70 case status_code::not_found: return "NOT_FOUND";
71 case status_code::already_exists: return "ALREADY_EXISTS";
72 case status_code::permission_denied: return "PERMISSION_DENIED";
73 case status_code::resource_exhausted: return "RESOURCE_EXHAUSTED";
74 case status_code::failed_precondition: return "FAILED_PRECONDITION";
75 case status_code::aborted: return "ABORTED";
76 case status_code::out_of_range: return "OUT_OF_RANGE";
77 case status_code::unimplemented: return "UNIMPLEMENTED";
78 case status_code::internal: return "INTERNAL";
79 case status_code::unavailable: return "UNAVAILABLE";
80 case status_code::data_loss: return "DATA_LOSS";
81 case status_code::unauthenticated: return "UNAUTHENTICATED";
82 default: return "UNKNOWN";
83 }
84 }

References aborted, already_exists, cancelled, data_loss, deadline_exceeded, failed_precondition, internal, invalid_argument, not_found, ok, out_of_range, permission_denied, resource_exhausted, unauthenticated, unavailable, unimplemented, and unknown.

Referenced by kcenon::network::protocols::grpc::grpc_client::impl::call_raw(), and kcenon::network::protocols::grpc::grpc_status::code_string().

Here is the caller graph for this function:

Variable Documentation

◆ default_max_message_size

size_t kcenon::network::protocols::grpc::default_max_message_size = 4 * 1024 * 1024
constexpr

Maximum gRPC message size (default 4MB)

Definition at line 40 of file frame.h.

Referenced by kcenon::network::protocols::grpc::grpc_message::parse().

◆ grpc_content_type

◆ grpc_content_type_proto

const char* kcenon::network::protocols::grpc::grpc_content_type_proto = "application/grpc+proto"
constexpr

gRPC content-type with proto encoding

Definition at line 114 of file frame.h.

◆ grpc_header_size

size_t kcenon::network::protocols::grpc::grpc_header_size = 5
constexpr

gRPC message header size (5 bytes)

gRPC messages are prefixed with a 5-byte header:

  • 1 byte: Compressed flag (0 = uncompressed, 1 = compressed)
  • 4 bytes: Message length (big-endian)

Definition at line 35 of file frame.h.

Referenced by kcenon::network::protocols::grpc::grpc_message::parse(), kcenon::network::protocols::grpc::grpc_message::serialize(), and kcenon::network::protocols::grpc::grpc_message::serialized_size().