use std::collections::HashMap;
use super::DeRecProtocolHandle;
use crate::interop::ffi::common::{DeRecBuffer, empty_buffer, vec_into_buffer};
use crate::interop::ffi::error::{
DEREC_CODE_FFI_BAD_PROTO, DEREC_CODE_FFI_NULL_PTR, DeRecError, ffi_error, from_lib_error,
success,
};
#[unsafe(no_mangle)]
pub unsafe extern "C" fn derec_protocol_set_communication_info(
handle: *mut DeRecProtocolHandle,
info_json_ptr: *const u8,
info_json_len: usize,
) -> DeRecError {
if handle.is_null() {
return ffi_error(DEREC_CODE_FFI_NULL_PTR, "handle is null");
}
let info: HashMap<String, String> = if info_json_len == 0 {
HashMap::new()
} else if info_json_ptr.is_null() {
return ffi_error(DEREC_CODE_FFI_NULL_PTR, "info_json_ptr null with len > 0");
} else {
let bytes = unsafe { std::slice::from_raw_parts(info_json_ptr, info_json_len) };
match serde_json::from_slice(bytes) {
Ok(m) => m,
Err(e) => {
return ffi_error(
DEREC_CODE_FFI_BAD_PROTO,
format!("invalid communication_info JSON: {e}"),
);
}
}
};
let h = unsafe { &*handle };
let mut inner = h.lock_inner();
inner.set_communication_info(info);
success()
}
#[unsafe(no_mangle)]
#[deprecated(
since = "0.0.3",
note = "use derec_protocol_set_own_transports, which takes the whole \
preference list; removed at 0.0.5"
)]
pub unsafe extern "C" fn derec_protocol_set_own_transport(
handle: *mut DeRecProtocolHandle,
uri_ptr: *const u8,
uri_len: usize,
protocol: i32,
) -> DeRecError {
if handle.is_null() {
return ffi_error(DEREC_CODE_FFI_NULL_PTR, "handle is null");
}
if uri_len == 0 || uri_ptr.is_null() {
return ffi_error(DEREC_CODE_FFI_NULL_PTR, "uri_ptr null or len == 0");
}
let uri = {
let bytes = unsafe { std::slice::from_raw_parts(uri_ptr, uri_len) };
match std::str::from_utf8(bytes) {
Ok(s) => s.to_owned(),
Err(_) => return ffi_error(DEREC_CODE_FFI_BAD_PROTO, "uri is not valid UTF-8"),
}
};
let validated_tp = match super::validate_transport(&uri, protocol) {
Ok(tp) => tp,
Err(e) => return e,
};
let h = unsafe { &*handle };
let mut inner = h.lock_inner();
match inner.set_own_transports([validated_tp]) {
Ok(()) => success(),
Err(e) => crate::interop::ffi::error::from_lib_error(e),
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn derec_protocol_set_own_transports(
handle: *mut DeRecProtocolHandle,
json_ptr: *const u8,
json_len: usize,
) -> DeRecError {
if handle.is_null() {
return ffi_error(DEREC_CODE_FFI_NULL_PTR, "handle is null");
}
if json_len == 0 || json_ptr.is_null() {
return ffi_error(DEREC_CODE_FFI_NULL_PTR, "json_ptr null or len == 0");
}
let bytes = unsafe { std::slice::from_raw_parts(json_ptr, json_len) };
let entries: Vec<OwnTransportEntry> = match serde_json::from_slice(bytes) {
Ok(v) => v,
Err(e) => {
return ffi_error(
DEREC_CODE_FFI_BAD_PROTO,
format!("invalid own_transports JSON: {e}"),
);
}
};
let mut validated = Vec::with_capacity(entries.len());
for entry in entries {
match super::validate_transport(&entry.uri, entry.protocol) {
Ok(tp) => validated.push(tp),
Err(e) => return e,
}
}
let h = unsafe { &*handle };
let mut inner = h.lock_inner();
match inner.set_own_transports(validated) {
Ok(()) => success(),
Err(e) => from_lib_error(e),
}
}
#[derive(serde::Deserialize)]
struct OwnTransportEntry {
uri: String,
protocol: i32,
}
#[repr(C)]
pub struct DeRecRemovedChannelsResult {
pub error: DeRecError,
pub channels: DeRecBuffer,
}
impl From<DeRecError> for DeRecRemovedChannelsResult {
fn from(error: DeRecError) -> Self {
Self {
error,
channels: empty_buffer(),
}
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn derec_protocol_remove_expired_channels(
handle: *mut DeRecProtocolHandle,
older_than_secs: u64,
) -> DeRecRemovedChannelsResult {
if handle.is_null() {
return ffi_error(DEREC_CODE_FFI_NULL_PTR, "handle is null").into();
}
let h = unsafe { &*handle };
let mut inner = h.lock_inner();
match h
.runtime
.block_on(inner.remove_expired_channels(older_than_secs))
{
Ok(ids) => {
let decimal: Vec<String> = ids.iter().map(|c| c.0.to_string()).collect();
match serde_json::to_vec(&decimal) {
Ok(bytes) => DeRecRemovedChannelsResult {
error: success(),
channels: vec_into_buffer(bytes),
},
Err(e) => ffi_error(
DEREC_CODE_FFI_BAD_PROTO,
format!("failed to encode removed channel ids: {e}"),
)
.into(),
}
}
Err(e) => from_lib_error(e).into(),
}
}