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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
//! 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.
//! # C/C++ API Module
//!
//! This module provides comprehensive C language bindings for the Ri framework, enabling
//! C and C++ applications to leverage Ri's capabilities including application lifecycle
//! management, authentication, caching, database access, file operations, gateway services,
//! gRPC communication, event hooks, logging, module RPC, observability, protocol handling,
//! message queuing, service mesh integration, validation, and WebSocket communication.
//!
//! The C API follows C conventions for memory management, error handling, and type conventions
//! while providing access to Ri's robust Rust implementation. This allows applications written
//! in C or C++ to benefit from Ri's safety guarantees, performance optimizations, and
//! architectural patterns without requiring a full Rust codebase.
//!
//! ## Module Architecture
//!
//! The C API module is organized into functional submodules, each providing bindings for a
//! specific Ri capability. The module also provides global initialization functions,
//! version information, and utility functions for managing C strings returned by the API.
//!
//! ### Core Submodules
//!
//! - **core**: Application initialization, configuration management, and lifecycle control.
//! Provides the entry point for Ri-based applications with automatic resource management.
//!
//! - **auth**: Authentication and authorization services including JWT token handling,
//! session management, credential validation, and permission checking.
//!
//! - **cache**: In-memory caching with configurable eviction policies (LRU, LFU, TTL-based).
//! Provides high-performance data caching with thread-safe concurrent access.
//!
//! - **database**: Database connection pooling and query execution support. Manages
//! connection lifecycle with automatic health checking and failover.
//!
//! - **device**: Device abstraction layer for managing computational resources with
//! scheduling, capability tracking, and state management.
//!
//! - **fs**: Cross-platform file system operations including path manipulation, file I/O,
//! directory management, and symbolic link handling.
//!
//! ### Communication Submodules
//!
//! - **gateway**: HTTP API gateway with request routing, middleware chains, load balancing,
//! rate limiting, and circuit breaker patterns.
//!
//! - **grpc**: gRPC server and client support for RPC communication with Protocol Buffer
//! serialization, streaming, and connection management.
//!
//! - **module_rpc**: Inter-module RPC communication within the Ri framework for
//! distributed service coordination.
//!
//! - **ws**: WebSocket protocol support for full-duplex communication with session
//! management and heartbeat mechanisms.
//!
//! ### System Submodules
//!
//! - **hooks**: Event hook system for extensibility through callback registration and
//! event dispatching at key lifecycle points.
//!
//! - **log**: Structured logging infrastructure with multiple output destinations,
//! configurable log levels, and structured field support.
//!
//! - **observability**: Metrics collection, tracing integration, and health check
//! endpoints for system monitoring.
//!
//! - **protocol**: Protocol handling for various wire formats and serialization
//! schemes with codec implementations.
//!
//! - **queue**: Message queue operations for asynchronous task processing with
//! reliable delivery semantics.
//!
//! - **service_mesh**: Service mesh integration for distributed systems with service
//! discovery, load balancing, and traffic management.
//!
//! - **validation**: Data validation with schema definitions, type checking, and
//! custom validation rules.
//!
//! ## Global Initialization
//!
//! The Ri library must be initialized before using any other API functions:
//!
//! ```c
//! int result = ri_init();
//! if (result != 0) {
//! fprintf(stderr, "Failed to initialize Ri library\n");
//! return result;
//! }
//! ```
//!
//! Initialization prepares internal resources including:
//! - Global thread pool configuration
//! - Default logger setup
//! - Signal handler registration
//! - Runtime state initialization
//!
//! When the application is finished using Ri, cleanup must be called:
//!
//! ```c
//! ri_cleanup();
//! ```
//!
//! Cleanup releases all global resources and ensures proper shutdown sequence.
//! All Ri objects must be freed before calling cleanup to prevent resource leaks.
//!
//! ## Memory Management
//!
//! The C API uses manual memory management following C conventions:
//!
//! - **Object Creation**: Constructor functions return newly allocated objects.
//! All objects must be freed using the corresponding destructor function.
//!
//! - **String Handling**: String-returning functions allocate C strings that must
//! be freed using ri_string_free(). Do not use standard free() on these strings.
//!
//! - **NULL Safety**: All functions handle NULL pointers gracefully, returning
//! error codes or NULL outputs rather than causing undefined behavior.
//!
//! - **Error Codes**: Functions return 0 for success, negative values for errors.
//! Specific error codes are documented for each function.
//!
//! ### Memory Management Pattern
//!
//! ```c
//! // Create an object
//! RiLogger* logger = ri_logger_new(config);
//! if (logger == NULL) {
//! fprintf(stderr, "Failed to create logger\n");
//! ri_log_config_free(config);
//! return ERROR_ALLOCATION;
//! }
//!
//! // Use the object...
//!
//! // Free when done
//! ri_logger_free(logger);
//!
//! // For strings returned by the API
//! const char* version = ri_version();
//! printf("Ri version: %s\n", version);
//! ri_string_free((char*)version); // Cast to non-const for free
//! ```
//!
//! ## Error Handling
//!
//! All Ri C API functions follow consistent error handling patterns:
//!
//! - **Return Codes**: Integer return codes where 0 indicates success
//! - **NULL Objects**: Constructor functions return NULL on allocation failure
//! - **Error Messages**: Last error information available through type-specific functions
//! - **Error Propagation**: Errors should be checked and handled at each API call
//!
//! Standard error codes:
//! - 0: Success
//! - -1: General error
//! - -2: Invalid argument
//! - -3: Memory allocation failure
//! - -4: Resource not found
//! - -5: Permission denied
//! - -6: Timeout
//! - -7: Network error
//!
//! ## Thread Safety
//!
//! The Ri C API is designed for thread-safe concurrent access:
//!
//! - **Object-Level Safety**: Individual objects are safe for concurrent use from
//! multiple threads unless documented otherwise.
//!
//! - **Global State**: Global initialization is thread-safe; subsequent calls to
//! ri_init() from multiple threads are handled correctly.
//!
//! - **Resource Sharing**: Objects can be shared across threads following the
//! same patterns as the underlying Rust implementation.
//!
//! - **Synchronization**: Internal synchronization primitives prevent data races
//! in multi-threaded scenarios.
//!
//! ## Usage Example
//!
//! A complete example demonstrating Ri C API usage:
//!
//! ```c
//! #include <stdio.h>
//! #include "ri.h"
//!
//! int main(int argc, char* argv[]) {
//! // Initialize Ri library
//! int result = ri_init();
//! if (result != 0) {
//! fprintf(stderr, "Ri initialization failed: %d\n", result);
//! return 1;
//! }
//!
//! // Get version information
//! const char* version = ri_version();
//! printf("Ri Version: %s\n", version);
//! ri_string_free((char*)version);
//!
//! // Create configuration
//! RiAppConfig* config = ri_app_config_new();
//! ri_app_config_set_name(config, "MyApplication");
//! ri_app_config_set_environment(config, "production");
//!
//! // Create application instance
//! RiApplication* app = ri_application_new(config);
//! ri_app_config_free(config);
//!
//! if (app == NULL) {
//! fprintf(stderr, "Failed to create application\n");
//! ri_cleanup();
//! return 1;
//! }
//!
//! // Start application
//! result = ri_application_start(app);
//! if (result != 0) {
//! fprintf(stderr, "Failed to start application: %d\n", result);
//! ri_application_free(app);
//! ri_cleanup();
//! return 1;
//! }
//!
//! printf("Application running. Press Ctrl+C to stop.\n");
//!
//! // Wait for shutdown signal
//! // Application runs until ri_application_stop() is called
//!
//! // Graceful shutdown
//! ri_application_stop(app);
//! ri_application_free(app);
//!
//! // Cleanup library
//! ri_cleanup();
//!
//! printf("Ri shutdown complete.\n");
//! return 0;
//! }
//! ```
//!
//! ## Build Integration
//!
//! To use the Ri C API in a C/C++ project:
//!
//! 1. **Compilation**: Include the generated C headers and link against the
//! Ri shared or static library.
//!
//! 2. **Header Files**: Include the main Ri header which provides access
//! to all submodule interfaces through a unified API surface.
//!
//! 3. **Linking**: Link against the Ri library using appropriate linker flags
//! for your build system.
//!
//! ## Dependencies
//!
//! The C API module depends on:
//!
//! - **Rust Core**: Standard Rust library types and traits
//! - **FFI Bindings**: Rust's foreign function interface capabilities
//! - **Submodule Implementations**: Each submodule's corresponding Rust implementation
//!
//! No external C/C++ dependencies are required beyond the C standard library.
//!
//! ## Feature Flags
//!
//! The Ri C API supports feature flags for conditional compilation:
//!
//! - **default**: Core functionality with common features
//! - **gateway**: Enable API gateway features
//! - **grpc**: Enable gRPC server and client support
//! - **observability**: Enable metrics and tracing
//! - **service-mesh**: Enable service mesh integration
//! - **websocket**: Enable WebSocket support
//!
//! Feature flags control which submodule bindings are compiled into the library.
//! Disable unused features to reduce binary size.
use ;
use OnceLock;
use HashSet;
use ;
/// Global pointer registry for C bindings to prevent Use-After-Free and Double-Free attacks.
/// This registry tracks all valid pointers returned to C code.
/// Uses Arc<Mutex<>> to ensure thread-safe atomic operations (check-then-act).
static C_POINTER_REGISTRY: = new;
/// Get or initialize the global pointer registry
/// Register a pointer as valid
/// Returns the pointer for convenience
pub
/// Unregister a pointer (mark as freed)
/// Returns true if the pointer was registered, false if it wasn't (double-free attempt)
pub
/// Check if a pointer is valid (registered and not freed)
pub
/// Initialize the Ri library
pub extern "C"
/// Cleanup the Ri library
pub extern "C"
/// Get Ri version
pub extern "C"
/// Free a string returned by Ri
pub extern "C"