use std::ffi::{CStr, CString};
use std::os::raw::{c_char, c_double, c_int};
unsafe extern "C" {
fn td_create_client_id() -> c_int;
fn td_send(client_id: c_int, request: *const c_char);
fn td_receive(timeout: c_double) -> *const c_char;
}
pub(crate) fn create_client() -> i32 {
unsafe { td_create_client_id() }
}
pub(crate) fn send(client_id: i32, request: String) {
let cstring = CString::new(request).unwrap();
unsafe { td_send(client_id, cstring.as_ptr()) }
}
pub(crate) fn receive(timeout: f64) -> Option<String> {
unsafe {
td_receive(timeout)
.as_ref()
.map(|response| CStr::from_ptr(response).to_string_lossy().into_owned())
}
}