|
| 1 | +use crate::client::{FluvioClient, fluvio_connect}; |
| 2 | +use crate::producer::{FluvioProducer, create_producer, producer_send, producer_flush}; |
| 3 | +use crate::consumer::{FluvioConsumer, FluvioStream, FluvioRecord, partition_consumer, consumer_stream, stream_next}; |
| 4 | +use crate::produce_output::{FluvioProduceOutput, produce_output_wait}; |
| 5 | +use std::os::raw::c_char; |
| 6 | +use std::ffi::CStr; |
| 7 | + |
| 8 | +#[unsafe(no_mangle)] |
| 9 | +pub extern "C" fn fluvio_c_connect(out_client: *mut *mut FluvioClient) -> i32 { |
| 10 | + match fluvio_connect() { |
| 11 | + Ok(client) => { |
| 12 | + unsafe { *out_client = Box::into_raw(client); } |
| 13 | + 0 |
| 14 | + } |
| 15 | + Err(_) => -1, |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +#[unsafe(no_mangle)] |
| 20 | +pub extern "C" fn fluvio_c_client_free(client: *mut FluvioClient) { |
| 21 | + if !client.is_null() { unsafe { let _ = Box::from_raw(client); } } |
| 22 | +} |
| 23 | + |
| 24 | +#[unsafe(no_mangle)] |
| 25 | +pub extern "C" fn fluvio_c_create_producer(client: *mut FluvioClient, topic: *const c_char, out_producer: *mut *mut FluvioProducer) -> i32 { |
| 26 | + if client.is_null() || topic.is_null() || out_producer.is_null() { return -1; } |
| 27 | + let topic_str = unsafe { CStr::from_ptr(topic).to_str() }.unwrap_or(""); |
| 28 | + match create_producer(unsafe { &*client }, topic_str) { |
| 29 | + Ok(producer) => { unsafe { *out_producer = Box::into_raw(producer); } 0 } |
| 30 | + Err(_) => -1, |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +#[unsafe(no_mangle)] |
| 35 | +pub extern "C" fn fluvio_c_producer_send(producer: *mut FluvioProducer, key: *const u8, key_len: usize, val: *const u8, val_len: usize, out: *mut *mut FluvioProduceOutput) -> i32 { |
| 36 | + if producer.is_null() || (key.is_null() && key_len > 0) || (val.is_null() && val_len > 0) { return -1; } |
| 37 | + let key_slice = if key_len > 0 { unsafe { std::slice::from_raw_parts(key, key_len) } } else { &[] }; |
| 38 | + let val_slice = if val_len > 0 { unsafe { std::slice::from_raw_parts(val, val_len) } } else { &[] }; |
| 39 | + match producer_send(unsafe { &*producer }, key_slice, val_slice) { |
| 40 | + Ok(o) => { if !out.is_null() { unsafe { *out = Box::into_raw(o); } } 0 } |
| 41 | + Err(_) => -1, |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +#[unsafe(no_mangle)] |
| 46 | +pub extern "C" fn fluvio_c_produce_output_wait(out: *mut FluvioProduceOutput) -> i32 { |
| 47 | + if out.is_null() { return -1; } |
| 48 | + match produce_output_wait(unsafe { &mut *out }) { Ok(_) => 0, Err(_) => -1 } |
| 49 | +} |
| 50 | + |
| 51 | +#[unsafe(no_mangle)] |
| 52 | +pub extern "C" fn fluvio_c_producer_flush(producer: *mut FluvioProducer) -> i32 { |
| 53 | + if producer.is_null() { return -1; } |
| 54 | + match producer_flush(unsafe { &*producer }) { Ok(_) => 0, Err(_) => -1 } |
| 55 | +} |
| 56 | + |
| 57 | +#[unsafe(no_mangle)] |
| 58 | +pub extern "C" fn fluvio_c_producer_free(producer: *mut FluvioProducer) { |
| 59 | + if !producer.is_null() { unsafe { let _ = Box::from_raw(producer); } } |
| 60 | +} |
| 61 | + |
| 62 | +#[unsafe(no_mangle)] |
| 63 | +pub extern "C" fn fluvio_c_produce_output_free(out: *mut FluvioProduceOutput) { |
| 64 | + if !out.is_null() { unsafe { let _ = Box::from_raw(out); } } |
| 65 | +} |
| 66 | + |
| 67 | +#[unsafe(no_mangle)] |
| 68 | +pub extern "C" fn fluvio_c_partition_consumer(client: *mut FluvioClient, topic: *const c_char, partition: u32, out_consumer: *mut *mut FluvioConsumer) -> i32 { |
| 69 | + if client.is_null() || topic.is_null() || out_consumer.is_null() { return -1; } |
| 70 | + let topic_str = unsafe { CStr::from_ptr(topic).to_str() }.unwrap_or(""); |
| 71 | + match partition_consumer(unsafe { &*client }, topic_str, partition) { |
| 72 | + Ok(consumer) => { unsafe { *out_consumer = Box::into_raw(consumer); } 0 } |
| 73 | + Err(_) => -1, |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +#[unsafe(no_mangle)] |
| 78 | +pub extern "C" fn fluvio_c_consumer_stream(consumer: *mut FluvioConsumer, offset_index: i64, out_stream: *mut *mut FluvioStream) -> i32 { |
| 79 | + if consumer.is_null() || out_stream.is_null() { return -1; } |
| 80 | + match consumer_stream(unsafe { &*consumer }, offset_index) { |
| 81 | + Ok(stream) => { unsafe { *out_stream = Box::into_raw(stream); } 0 } |
| 82 | + Err(_) => -1, |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +#[unsafe(no_mangle)] |
| 87 | +pub extern "C" fn fluvio_c_stream_next(stream: *mut FluvioStream, out_record: *mut *mut FluvioRecord) -> i32 { |
| 88 | + if stream.is_null() || out_record.is_null() { return -1; } |
| 89 | + match stream_next(unsafe { &mut *stream }) { |
| 90 | + Ok(record) => { unsafe { *out_record = Box::into_raw(record); } 0 } |
| 91 | + Err(_) => -1, |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +#[unsafe(no_mangle)] |
| 96 | +pub extern "C" fn fluvio_c_record_value(record: *mut FluvioRecord, out_buf: *mut *const u8, out_len: *mut usize) -> i32 { |
| 97 | + if record.is_null() || out_buf.is_null() || out_len.is_null() { return -1; } |
| 98 | + let val = unsafe { &*record }.inner.value(); |
| 99 | + unsafe { *out_buf = val.as_ptr(); *out_len = val.len(); } |
| 100 | + 0 |
| 101 | +} |
| 102 | + |
| 103 | +#[unsafe(no_mangle)] |
| 104 | +pub extern "C" fn fluvio_c_record_free(rec: *mut FluvioRecord) { |
| 105 | + if !rec.is_null() { unsafe { let _ = Box::from_raw(rec); } } |
| 106 | +} |
| 107 | + |
| 108 | +#[unsafe(no_mangle)] |
| 109 | +pub extern "C" fn fluvio_c_stream_free(stream: *mut FluvioStream) { |
| 110 | + if !stream.is_null() { unsafe { let _ = Box::from_raw(stream); } } |
| 111 | +} |
| 112 | + |
| 113 | +#[unsafe(no_mangle)] |
| 114 | +pub extern "C" fn fluvio_c_consumer_free(consumer: *mut FluvioConsumer) { |
| 115 | + if !consumer.is_null() { unsafe { let _ = Box::from_raw(consumer); } } |
| 116 | +} |
0 commit comments