use std::collections::HashMap;
use std::slice;
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 { 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)]
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 { 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,
#[serde(
deserialize_with = "crate::interop::protocol_names::protocol_from_name_or_discriminant"
)]
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(),
}
}