1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
//! Copyright © 2025-2026 Wenze Wei. All Rights Reserved.
//!
//! This file is part of Ri.
//! The Ri project belongs to the Dunimd Team.
//!
//! Licensed under the Apache License, Version 2.0 (the "License");
//! You may not use this file except in compliance with the License.
//! You may obtain a copy of the License at
//!
//! http://www.apache.org/licenses/LICENSE-2.0
//!
//! Unless required by applicable law or agreed to in writing, software
//! distributed under the License is distributed on an "AS IS" BASIS,
//! WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//! See the License for the specific language governing permissions and
//! limitations under the License.
//! # gRPC Module C API
//!
//! This module provides C language bindings for Ri's gRPC communication layer. The gRPC module
//! delivers high-performance Remote Procedure Call (RPC) capabilities using Protocol Buffers for
//! efficient serialization and HTTP/2 for transport. This C API enables C/C++ applications to
//! leverage Ri's gRPC functionality for building distributed systems with strongly-typed service
//! contracts and bidirectional streaming support.
//!
//! ## Module Architecture
//!
//! The gRPC module comprises three primary components that together provide complete gRPC
//! functionality:
//!
//! - **RiGrpcServer**: gRPC server implementation handling service registration, request
//! processing, and response streaming. The server manages the complete lifecycle of gRPC
//! services including method dispatch, interceptor chaining, and connection management.
//!
//! - **RiGrpcClient**: gRPC client implementation for invoking remote procedures with automatic
//! serialization, connection pooling, and retry logic. The client supports all gRPC calling
//! patterns including unary calls, server streaming, client streaming, and bidirectional
//! streaming.
//!
//! - **RiGrpcChannel**: Low-level channel abstraction managing the underlying HTTP/2 connection
//! pool, transport security, and protocol negotiation. Channels provide the foundation for
//! client communication and can be shared across multiple client instances.
//!
//! ## gRPC Communication Patterns
//!
//! The module supports all four standard gRPC communication patterns:
//!
//! - **Unary Calls**: Simple request-response pattern where client sends a single request and
//! receives a single response. The most common pattern for traditional RPC operations.
//!
//! - **Server Streaming**: Client sends a single request and receives a stream of responses.
//! Useful for scenarios like streaming updates, real-time notifications, or large dataset
//! retrieval.
//!
//! - **Client Streaming**: Client sends a stream of requests and receives a single response.
//! Useful for scenarios like batch uploads, upload with aggregation, or time-series data
//! collection.
//!
//! - **Bidirectional Streaming**: Both client and server send streams of messages independently.
//! Each side can send messages in any order. Useful for real-time communication, chat
//! systems, or interactive data processing.
//!
//! ## Protocol Buffer Integration
//!
//! The gRPC module integrates tightly with Protocol Buffers:
//!
//! - **Message Serialization**: Automatic encoding and decoding of Protocol Buffer messages
//! using the generated code from .proto files.
//!
//! - **Service Definition**: Service interfaces generated from .proto files define the
//! available remote procedures and message types.
//!
//! - **Schema Evolution**: Support for backward and forward compatible schema changes
//! through Protocol Buffers' field rules and unknown field handling.
//!
//! - **Custom Serialization**: Extension points for using alternative serialization formats
//! like JSON, MessagePack, or custom binary formats.
//!
//! ## Transport Features
//!
//! The gRPC transport implements comprehensive HTTP/2 features:
//!
//! - **Connection Multiplexing**: Multiple requests and streams share a single TCP connection,
//! reducing connection overhead and improving throughput.
//!
//! - **Flow Control**: Automatic flow control prevents fast senders from overwhelming slow
//! receivers using HTTP/2 window updates.
//!
//! - **Header Compression**: HPACK header compression reduces bandwidth usage for repeated
//! metadata in requests and responses.
//!
//! - **Stream Prioritization**: Clients can prioritize streams to ensure important requests
//! get timely responses when connection capacity is limited.
//!
//! - **Ping/Pong Frames**: Keepalive mechanism detects dead connections and enables timely
//! cleanup of stale resources.
//!
//! ## Security Features
//!
//! The gRPC module provides comprehensive security capabilities:
//!
//! - **TLS Encryption**: Full TLS encryption for all communication with configurable
//! cipher suites and certificate validation.
//!
//! - **Certificate Management**: Support for PEM certificates, certificate chains, and
//! custom certificate authorities.
//!
//! - **Client Authentication**: Multiple authentication mechanisms including:
//! - JWT token authentication
//! - OAuth 2.0 token exchange
//! - mTLS (mutual TLS) with client certificates
//! - Custom authentication interceptor
//!
//! - **Authorization**: Fine-grained authorization using interceptors for checking
//! permissions before method execution.
//!
//! ## Connection Management
//!
//! The module implements sophisticated connection management:
//!
//! - **Connection Pooling**: Reuse of established connections to reduce latency and
//! resource consumption across multiple requests.
//!
//! - **Load Balancing**: Client-side load distribution across multiple server instances
//! with configurable policies (round-robin, pick-first, weighted).
//!
//! - **Health Checking**: Active health checks detect unhealthy server instances and
//! remove them from the load balancer rotation.
//!
//! - **Retry Logic**: Automatic retry for idempotent requests with configurable retry
//! policies, backoff strategies, and retry limits.
//!
//! - **Deadline Propagation**: Automatic deadline propagation across service boundaries
//! ensures requests don't wait indefinitely for responses.
//!
//! ## Interceptors
//!
//! Interceptors provide extensibility for cross-cutting concerns:
//!
//! - **Server Interceptors**: Process incoming requests before they reach the handler
//! and transform responses before they return to the client. Common uses include:
//! - Authentication and authorization
//! - Request/response logging
//! - Metrics collection
//! - Request validation
//! - Response transformation
//!
//! - **Client Interceptors**: Process outgoing requests before they are sent and
//! transform incoming responses before they reach the application. Common uses include:
//! - Authentication token injection
//! - Request tracing headers
//! - Metrics collection
//! - Retry handling
//! - Response validation
//!
//! ## Memory Management
//!
//! All C API objects use opaque pointers with manual memory management:
//!
//! - Constructor functions allocate new instances on the heap
//! - Destructor functions must be called to release memory
//! - Client stubs must be properly shutdown before freeing
//! - Stream objects must be properly closed when complete
//!
//! ## Thread Safety
//!
//! The underlying implementations are thread-safe:
//!
//! - Channels can be shared across threads for concurrent requests
//! - Client stubs support concurrent method invocations
//! - Server handlers are invoked concurrently for each request
//! - Interceptors should be stateless for thread safety
//!
//! ## Performance Characteristics
//!
//! gRPC operations have the following performance profiles:
//!
//! - Connection establishment: O(1) for pooled connections, O(1 TCP handshake + TLS)
//! - Unary call latency: O(message_size) for serialization, O(1) network
//! - Streaming throughput: O(message_size) per message with flow control
//! - Concurrent streams: Hundreds to thousands per connection
//!
//! ## Usage Example
//!
//! ```c
//! // Create gRPC channel with connection pooling
//! RiGrpcChannel* channel = ri_grpc_channel_new("localhost", 50051);
//! ri_grpc_channel_set_tls_enabled(channel, true);
//! ri_grpc_channel_set_connection_pool_size(channel, 10);
//!
//! // Create client stub
//! RiGrpcClient* client = ri_grpc_client_new(channel);
//!
//! // Configure request
//! RiUserRequest request = RiGUSER_REQUEST_INIT;
//! request.user_id = 12345;
//! request.include_profile = true;
//!
//! // Execute unary call
//! RiUserResponse response = RiGUSER_RESPONSE_INIT;
//! int status = ri_grpc_client_unary_call(
//! client,
//! "UserService.GetUser",
//! &request,
//! &response,
//! 5000 // timeout in milliseconds
//! );
//!
//! if (status == 0) {
//! printf("User: %s %s\n", response.first_name, response.last_name);
//! } else {
//! const char* error = ri_grpc_client_last_error(client);
//! fprintf(stderr, "gRPC error: %s (code: %d)\n", error, status);
//! }
//!
//! // Create stream for server streaming
//! RiNotificationRequest stream_request = RiGNOTIFICATION_REQUEST_INIT;
//! stream_request.notification_type = NOTIFICATION_TYPE_ALL;
//! stream_request.start_time = time(NULL);
//!
//! RiGrpcStream* stream = ri_grpc_client_server_stream(
//! client,
//! "NotificationService.Subscribe",
//! &stream_request
//! );
//!
//! // Read streaming responses
//! RiNotification notification;
//! while (ri_grpc_stream_read(stream, ¬ification) == 0) {
//! printf("Notification: %s\n", notification.message);
//! ri_grpc_notification_destroy(¬ification);
//! }
//!
//! ri_grpc_stream_free(stream);
//!
//! // Cleanup
//! ri_grpc_client_free(client);
//! ri_grpc_channel_free(channel);
//! ```
//!
//! ## Dependencies
//!
//! This module depends on the following Ri components and external libraries:
//!
//! - `crate::grpc`: Rust gRPC module implementation
//! - `crate::prelude`: Common types and traits
//! - tonic for gRPC protocol implementation
//! - prost for Protocol Buffer encoding/decoding
//! - tokio for asynchronous runtime
//! - h2 for HTTP/2 transport
//!
//! ## Feature Flags
//!
//! The gRPC module is enabled by the "grpc" feature flag:
//!
//! - grpc: Enable gRPC server and client functionality
//! - grpc-tls: Enable TLS support for gRPC (requires native-tls or rustls)
//!
//! Disable these features to reduce binary size when gRPC is not required.
use crate;
c_wrapper!;
c_wrapper!;