dmsc 0.1.9

Ri - A high-performance Rust middleware framework with modular architecture
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
//! 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.

//! # Protocol Module C API
//!
//! This module provides C language bindings for Ri's protocol handling infrastructure. The protocol
//! module delivers comprehensive support for encoding, decoding, and transforming data across various
//! wire formats and communication protocols. This C API enables C/C++ applications to leverage Ri's
//! protocol capabilities for building interoperable distributed systems with standardized data exchange.
//!
//! ## Module Architecture
//!
//! The protocol module comprises three primary components that together provide complete protocol
//! management capabilities:
//!
//! - **RiProtocolConfig**: Configuration container for protocol codec parameters including encoding
//!   formats, framing options, compression settings, and validation rules. The configuration object
//!   controls how data is serialized and deserialized, ensuring consistent behavior across the
//!   application.
//!
//! - **RiProtocolManager**: Central manager for protocol registration, codec lookup, and protocol
//!   negotiation. The manager handles the complete lifecycle of protocol operations including codec
//!   selection, error handling, and protocol switching.
//!
//! - **RiFrame**: Low-level frame abstraction for message framing and boundary management. Frames
//!   provide the foundation for streaming protocols, handling message boundaries, chunking, and
//!   reassembly.
//!
//! ## Supported Protocols
//!
//! The protocol system supports a comprehensive range of wire formats:
//!
//! - **JSON (JavaScript Object Notation)**: Human-readable data interchange format widely used in
//!   web APIs and microservices. Supports schema validation and transformation.
//!
//! - **MessagePack**: Binary serialization format providing compact representation with fast
//!   encoding and decoding. Ideal for bandwidth-constrained environments.
//!
//! - **Protocol Buffers**: Google's language-neutral, platform-neutral, extensible mechanism for
//!   serializing structured data. Provides strong typing and backward/forward compatibility.
//!
//! - **CBOR (Concise Binary Object Representation)**: Binary JSON-like format designed for small
//!   code size and small message size. IETF standard (RFC 8949).
//!
//! - **BSON (Binary JSON)**: MongoDB's binary-encoded JSON format with additional data types
//!   like dates and binary blobs.
//!
//! - **Avro**: Apache Avro data serialization format with schema evolution support and
//!   compact binary encoding.
//!
//! ## Framing Protocols
//!
//! The module provides various message framing approaches:
//!
//! - **Length-Prefixed Framing**: Each message is prefixed with its length in bytes. Enables
//!   streaming parsing and message boundary detection without special delimiters.
//!
//! - **Delimiter-Based Framing**: Messages are separated by special delimiter bytes (e.g., newline
//!   for line-based protocols). Simple but requires escaping for binary data.
//!
//! - **Fixed-Size Framing**: All messages have identical length. Simplifies parsing but wastes
//!   bandwidth for variable-sized data.
//!
//! - **HTTP/1.1 Chunked Transfer**: Standard HTTP chunked encoding for streaming responses.
//!   Supports incremental processing of large payloads.
//!
//! - **WebSocket Framing**: Full WebSocket frame handling including control frames, continuation
//!   frames, and fragmentation support.
//!
//! ## Compression
//!
//! Built-in compression support reduces bandwidth usage:
//!
//! - **Gzip**: GNU zip compression with wide compatibility. Good balance of compression ratio
//!   and CPU usage.
//!
//! - **Snappy**: Google's compression library designed for high speeds. Lower compression
//!   ratio but very fast encoding and decoding.
//!
//! - **LZ4**: Extremely fast compression with reasonable ratios. Ideal for real-time systems
//!   with limited CPU budget.
//!
//! - **Zstandard (zstd)**: Facebook's compression algorithm offering excellent compression ratios
//!   at high speeds. Supports dictionary compression for repetitive data.
//!
//! - **Brotli**: Google's next-generation compression with best-in-class compression ratios.
//!   Slightly slower but excellent for static content delivery.
//!
//! ## Validation
//!
//! Comprehensive data validation ensures protocol integrity:
//!
//! - **Schema Validation**: Validate messages against predefined schemas before processing.
//!   Catches malformed or unexpected data early.
//!
//! - **Type Checking**: Verify message types match expected types for each field.
//!   Supports optional fields and type coercion.
//!
//! - **Range Validation**: Ensure numeric values fall within acceptable ranges.
//!   Prevents overflow and underflow issues.
//!
//! - **Pattern Matching**: Validate string fields against regex patterns or format strings.
//!   Ensures email, UUID, and other formatted data validity.
//!
//! - **Custom Validators**: User-defined validation functions for domain-specific rules.
//!   Extend built-in validation with application logic.
//!
//! ## Serialization Features
//!
//! Advanced serialization capabilities:
//!
//! - **Polymorphism**: Handle tagged unions and inheritance hierarchies through type tags.
//!   Enables message routing based on message type.
//!
//! - **Optional Fields**: Gracefully handle missing fields with optional type support.
//!   Backward compatibility maintained across schema versions.
//!
//! - **Default Values**: Automatic default values for missing fields when defined in schema.
//!   Simplifies client code with sensible fallbacks.
//!
//! - **Unknown Field Handling**: Option to preserve unknown fields during deserialization.
//!   Enables forward compatibility without data loss.
//!
//! - **Circular Reference Handling**: Detect and properly serialize graph structures with
//!   references between objects.
//!
//! ## Performance Characteristics
//!
//! Protocol operations are optimized for various use cases:
//!
//! - **JSON Parsing**: O(n) where n is message size, optimized with SIMD instructions
//! - **Binary Codecs**: O(n) with low constant factors, ideal for high-throughput scenarios
//! - **Framing**: O(1) per frame boundary detection
//! - **Compression**: O(n * compression_level), configurable trade-off
//! - **Validation**: O(n) with early termination on first error
//!
//! ## 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
//! - Codec instances are managed by the protocol manager
//! - Frame buffers are recycled for performance
//!
//! ## Thread Safety
//!
//! The underlying implementations are thread-safe:
//!
//! - Protocol manager supports concurrent codec registration
//! - Codec instances are immutable after creation
//! - Frame allocation uses thread-local pools
//! - Validation can be performed concurrently
//!
//! ## Usage Example
//!
//! ```c
//! // Create protocol configuration
//! RiProtocolConfig* config = ri_protocol_config_new();
//! if (config == NULL) {
//!     fprintf(stderr, "Failed to create protocol config\n");
//!     return ERROR_INIT;
//! }
//!
//! // Configure protocol settings
//! ri_protocol_config_set_format(config, PROTOCOL_FORMAT_MSGPACK);
//! ri_protocol_config_set_compression(config, COMPRESSION_SNAPPY);
//! ri_protocol_config_set_validation_enabled(config, true);
//!
//! // Create protocol manager
//! RiProtocolManager* manager = ri_protocol_manager_new(config);
//! if (manager == NULL) {
//!     fprintf(stderr, "Failed to create protocol manager\n");
//!     ri_protocol_config_free(config);
//!     return ERROR_INIT;
//! }
//!
//! // Register custom schema
//! int result = ri_protocol_manager_register_schema(
//!     manager,
//!     "UserMessage",
//!     user_schema_definition,
//!     sizeof(user_schema_definition)
//! );
//!
//! if (result != 0) {
//!     fprintf(stderr, "Failed to register schema\n");
//! }
//!
//! // Create frame for streaming
//! RiFrame* frame = ri_frame_new();
//! if (frame == NULL) {
//!     fprintf(stderr, "Failed to create frame\n");
//!     ri_protocol_manager_free(manager);
//!     ri_protocol_config_free(config);
//!     return ERROR_INIT;
//! }
//!
//! // Encode message
//! const char* input_data = "{\"user_id\": 12345, \"name\": \"John\"}";
//! size_t input_len = strlen(input_data);
//!
//! char* output_buffer = NULL;
//! size_t output_len = 0;
//!
//! result = ri_protocol_manager_encode(
//!     manager,
//!     "UserMessage",
//!     input_data,
//!     input_len,
//!     &output_buffer,
//!     &output_len
//! );
//!
//! if (result == 0 && output_buffer != NULL) {
//!     printf("Encoded %zu bytes\n", output_len);
//!
//!     // Decode message back
//!     char* decoded_buffer = NULL;
//!     size_t decoded_len = 0;
//!
//!         int decode_result = ri_protocol_manager_decode(
//!             manager,
//!             "UserMessage",
//!             output_buffer,
//!             output_len,
//!             &decoded_buffer,
//!             &decoded_len
//!         );
//!
//!         if (decode_result == 0) {
//!             printf("Decoded: %.*s\n", (int)decoded_len, decoded_buffer);
//!             ri_string_free(decoded_buffer);
//!         }
//!
//!     ri_string_free(output_buffer);
//! }
//!
//! // Frame the message for transport
//! ri_frame_reset(frame);
//! ri_frame_append(frame, output_buffer, output_len);
//!
//! // Read framed data
//! const char* frame_data = ri_frame_data(frame);
//! size_t frame_size = ri_frame_size(frame);
//!
//! // Cleanup
//! ri_frame_free(frame);
//! ri_protocol_manager_free(manager);
//! ri_protocol_config_free(config);
//! ```
//!
//! ## Protocol Negotiation
//!
//! The protocol manager supports dynamic protocol negotiation:
//!
//! - **Capability Exchange**: During connection establishment, both ends advertise supported
//!   protocols and versions.
//!
//! - **Common Protocol Selection**: Automatically select the best mutually-supported protocol
//!   based on priority and capabilities.
//!
//! - **Protocol Upgrades**: Support for upgrading from a base protocol (like HTTP/1.1) to
//!   a more efficient protocol (like WebSocket or gRPC).
//!
//! - **Version Handling**: Manage multiple protocol versions simultaneously for backward
//!   compatibility during migrations.
//!
//! ## Dependencies
//!
//! This module depends on the following Ri components:
//!
//! - `crate::protocol`: Rust protocol module implementation
//! - `crate::prelude`: Common types and traits
//! - serde for serialization frameworks
//! - Various codec libraries (serde_json, rmp-serde, prost, etc.)
//!
//! ## Feature Flags
//!
//! The protocol module is enabled by default with comprehensive format support.
//! Additional formats enabled by feature flags:
//!
//! - `protocol-protobuf`: Enable Protocol Buffer support (requires prost)
//! - `protocol-avro`: Enable Apache Avro support
//! - `protocol-cbor`: Enable CBOR support
//! - `protocol-bson`: Enable BSON support
//! - `protocol-compression`: Enable compression codecs

use crate::protocol::{RiFrame, RiProtocolConfig, RiProtocolManager, RiProtocolStats, RiConnectionInfo, RiProtocolType, RiSecurityLevel, RiConnectionState};


c_wrapper!(CRiProtocolConfig, RiProtocolConfig);
c_wrapper!(CRiProtocolManager, RiProtocolManager);
c_wrapper!(CRiFrame, RiFrame);
c_wrapper!(CRiProtocolStats, RiProtocolStats);
c_wrapper!(CRiConnectionInfo, RiConnectionInfo);

// RiProtocolConfig constructors and destructors
c_constructor!(
    ri_protocol_config_new,
    CRiProtocolConfig,
    RiProtocolConfig,
    RiProtocolConfig::default()
);
c_destructor!(ri_protocol_config_free, CRiProtocolConfig);

// RiProtocolConfig setters
#[no_mangle]
pub extern "C" fn ri_protocol_config_set_protocol_type(config: *mut CRiProtocolConfig, protocol_type: std::ffi::c_int) -> std::ffi::c_int {
    if config.is_null() {
        return -1;
    }
    unsafe {
        let pt = match protocol_type {
            0 => RiProtocolType::Global,
            1 => RiProtocolType::Private,
            _ => RiProtocolType::Global,
        };
        (*config).inner.default_protocol = pt;
    }
    0
}

#[no_mangle]
pub extern "C" fn ri_protocol_config_set_security_enabled(config: *mut CRiProtocolConfig, enabled: bool) -> std::ffi::c_int {
    if config.is_null() {
        return -1;
    }
    unsafe {
        (*config).inner.enable_security = enabled;
    }
    0
}

#[no_mangle]
pub extern "C" fn ri_protocol_config_set_security_level(config: *mut CRiProtocolConfig, level: std::ffi::c_int) -> std::ffi::c_int {
    if config.is_null() {
        return -1;
    }
    unsafe {
        let sl = match level {
            0 => RiSecurityLevel::None,
            1 => RiSecurityLevel::Standard,
            2 => RiSecurityLevel::High,
            3 => RiSecurityLevel::Military,
            _ => RiSecurityLevel::Standard,
        };
        (*config).inner.security_level = sl;
    }
    0
}

// RiProtocolManager C bindings
#[no_mangle]
pub extern "C" fn ri_protocol_manager_new() -> *mut CRiProtocolManager {
    Box::into_raw(Box::new(CRiProtocolManager::new(RiProtocolManager::new())))
}
c_destructor!(ri_protocol_manager_free, CRiProtocolManager);

#[no_mangle]
pub extern "C" fn ri_protocol_manager_send(
    manager: *mut CRiProtocolManager,
    target: *const std::ffi::c_char,
    data: *const std::ffi::c_char,
    data_len: usize,
    out_response: *mut *mut std::ffi::c_char,
    out_len: *mut usize,
) -> std::ffi::c_int {
    if manager.is_null() || target.is_null() || data.is_null() || out_response.is_null() || out_len.is_null() {
        return -1;
    }
    let target_str = match unsafe { std::ffi::CStr::from_ptr(target).to_str() } {
        Ok(s) => s,
        Err(_) => return -2,
    };
    let data_slice = unsafe { std::slice::from_raw_parts(data as *const u8, data_len) };
    let response = unsafe { (*manager).inner.send_message(target_str, data_slice) };
    unsafe { *out_len = response.len(); }
    match std::ffi::CString::new(response) {
        Ok(c_str) => {
            unsafe { *out_response = c_str.into_raw(); }
            0
        }
        Err(_) => -3,
    }
}

#[no_mangle]
pub extern "C" fn ri_protocol_manager_get_stats(
    manager: *mut CRiProtocolManager,
    out_messages_sent: *mut u64,
    out_messages_received: *mut u64,
    out_bytes_sent: *mut u64,
    out_bytes_received: *mut u64,
    out_errors: *mut u64,
) -> std::ffi::c_int {
    if manager.is_null() {
        return -1;
    }
    let rt = match tokio::runtime::Runtime::new() {
        Ok(rt) => rt,
        Err(_) => return -2,
    };
    unsafe {
        let stats = rt.block_on(async { (*manager).inner.stats.read().await.clone() });
        if !out_messages_sent.is_null() {
            *out_messages_sent = stats.messages_sent;
        }
        if !out_messages_received.is_null() {
            *out_messages_received = stats.messages_received;
        }
        if !out_bytes_sent.is_null() {
            *out_bytes_sent = stats.bytes_sent;
        }
        if !out_bytes_received.is_null() {
            *out_bytes_received = stats.bytes_received;
        }
        if !out_errors.is_null() {
            *out_errors = stats.errors;
        }
        0
    }
}

#[no_mangle]
pub extern "C" fn ri_protocol_manager_get_connection_count(manager: *mut CRiProtocolManager) -> usize {
    if manager.is_null() {
        return 0;
    }
    unsafe { (*manager).inner.get_connection_count() }
}

// RiFrame C bindings
#[no_mangle]
pub extern "C" fn ri_frame_new() -> *mut CRiFrame {
    Box::into_raw(Box::new(CRiFrame::new(RiFrame::default())))
}
c_destructor!(ri_frame_free, CRiFrame);

#[no_mangle]
pub extern "C" fn ri_frame_get_payload_size(frame: *mut CRiFrame) -> usize {
    if frame.is_null() {
        return 0;
    }
    unsafe { (*frame).inner.payload.len() }
}

#[no_mangle]
pub extern "C" fn ri_frame_get_payload(frame: *mut CRiFrame, out_data: *mut *mut std::ffi::c_char, out_len: *mut usize) -> std::ffi::c_int {
    if frame.is_null() || out_data.is_null() || out_len.is_null() {
        return -1;
    }
    unsafe {
        let payload = (*frame).inner.payload.clone();
        *out_len = payload.len();
        let ptr = Box::into_raw(payload.into_boxed_slice()) as *mut std::ffi::c_char;
        *out_data = ptr;
        0
    }
}

#[no_mangle]
pub extern "C" fn ri_frame_get_sequence(frame: *mut CRiFrame) -> u64 {
    if frame.is_null() {
        return 0;
    }
    unsafe { (*frame).inner.header.sequence_number }
}

#[no_mangle]
pub extern "C" fn ri_frame_get_timestamp(frame: *mut CRiFrame) -> u64 {
    if frame.is_null() {
        return 0;
    }
    unsafe { (*frame).inner.header.timestamp }
}

#[no_mangle]
pub extern "C" fn ri_frame_get_type(frame: *mut CRiFrame) -> std::ffi::c_int {
    if frame.is_null() {
        return -1;
    }
    unsafe {
        match (*frame).inner.header.frame_type {
            crate::protocol::RiFrameType::Data => 0,
            crate::protocol::RiFrameType::Control => 1,
            crate::protocol::RiFrameType::Heartbeat => 2,
            crate::protocol::RiFrameType::Ack => 3,
            crate::protocol::RiFrameType::Error => 4,
        }
    }
}

c_string_getter!(
    ri_frame_get_source_id,
    CRiFrame,
    |inner: &RiFrame| inner.source_id.clone()
);

c_string_getter!(
    ri_frame_get_target_id,
    CRiFrame,
    |inner: &RiFrame| inner.target_id.clone()
);

// RiConnectionInfo C bindings
c_destructor!(ri_connection_info_free, CRiConnectionInfo);

c_string_getter!(
    ri_connection_info_get_id,
    CRiConnectionInfo,
    |inner: &RiConnectionInfo| inner.connection_id.clone()
);

c_string_getter!(
    ri_connection_info_get_device_id,
    CRiConnectionInfo,
    |inner: &RiConnectionInfo| inner.device_id.clone()
);

c_string_getter!(
    ri_connection_info_get_address,
    CRiConnectionInfo,
    |inner: &RiConnectionInfo| inner.address.clone()
);

#[no_mangle]
pub extern "C" fn ri_connection_info_get_state(info: *mut CRiConnectionInfo) -> std::ffi::c_int {
    if info.is_null() {
        return -1;
    }
    unsafe {
        match (*info).inner.state {
            RiConnectionState::Disconnected => 0,
            RiConnectionState::Connecting => 1,
            RiConnectionState::Connected => 2,
            RiConnectionState::Disconnecting => 3,
        }
    }
}

#[no_mangle]
pub extern "C" fn ri_connection_info_get_security_level(info: *mut CRiConnectionInfo) -> std::ffi::c_int {
    if info.is_null() {
        return -1;
    }
    unsafe {
        match (*info).inner.security_level {
            RiSecurityLevel::None => 0,
            RiSecurityLevel::Standard => 1,
            RiSecurityLevel::High => 2,
            RiSecurityLevel::Military => 3,
        }
    }
}

#[no_mangle]
pub extern "C" fn ri_connection_info_get_protocol_type(info: *mut CRiConnectionInfo) -> std::ffi::c_int {
    if info.is_null() {
        return -1;
    }
    unsafe {
        match (*info).inner.protocol_type {
            RiProtocolType::Global => 0,
            RiProtocolType::Private => 1,
        }
    }
}

// RiProtocolStats C bindings
c_destructor!(ri_protocol_stats_free, CRiProtocolStats);

#[no_mangle]
pub extern "C" fn ri_protocol_stats_get_messages_sent(stats: *mut CRiProtocolStats) -> u64 {
    if stats.is_null() {
        return 0;
    }
    unsafe { (*stats).inner.messages_sent }
}

#[no_mangle]
pub extern "C" fn ri_protocol_stats_get_messages_received(stats: *mut CRiProtocolStats) -> u64 {
    if stats.is_null() {
        return 0;
    }
    unsafe { (*stats).inner.messages_received }
}

#[no_mangle]
pub extern "C" fn ri_protocol_stats_get_bytes_sent(stats: *mut CRiProtocolStats) -> u64 {
    if stats.is_null() {
        return 0;
    }
    unsafe { (*stats).inner.bytes_sent }
}

#[no_mangle]
pub extern "C" fn ri_protocol_stats_get_bytes_received(stats: *mut CRiProtocolStats) -> u64 {
    if stats.is_null() {
        return 0;
    }
    unsafe { (*stats).inner.bytes_received }
}

#[no_mangle]
pub extern "C" fn ri_protocol_stats_get_errors(stats: *mut CRiProtocolStats) -> u64 {
    if stats.is_null() {
        return 0;
    }
    unsafe { (*stats).inner.errors }
}

#[no_mangle]
pub extern "C" fn ri_protocol_stats_get_avg_latency_ms(stats: *mut CRiProtocolStats) -> f64 {
    if stats.is_null() {
        return 0.0;
    }
    unsafe { (*stats).inner.avg_latency_ms }
}