use crate::rpc::error::RpcError;
use llama_cpp_sys_4 as sys;
use std::ffi::CString;
use std::ptr::NonNull;
pub struct RpcServer {
backend: NonNull<sys::ggml_backend>,
endpoint: String,
}
impl RpcServer {
pub fn start(
backend: NonNull<sys::ggml_backend>,
endpoint: &str,
free_mem: usize,
total_mem: usize,
) -> Result<Self, RpcError> {
let c_endpoint = CString::new(endpoint).map_err(|e| RpcError::StringConversion(e))?;
unsafe {
sys::ggml_backend_rpc_start_server(
backend.as_ptr(),
c_endpoint.as_ptr(),
free_mem,
total_mem,
);
}
Ok(Self {
backend,
endpoint: endpoint.to_string(),
})
}
pub fn endpoint(&self) -> &str {
&self.endpoint
}
pub fn backend(&self) -> NonNull<sys::ggml_backend> {
self.backend
}
}
impl std::fmt::Debug for RpcServer {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("RpcServer")
.field("endpoint", &self.endpoint)
.finish()
}
}
unsafe impl Send for RpcServer {}
unsafe impl Sync for RpcServer {}
pub fn add_rpc_device(endpoint: &str) -> Result<NonNull<sys::ggml_backend_device>, RpcError> {
let c_endpoint = CString::new(endpoint).map_err(|e| RpcError::StringConversion(e))?;
let device = unsafe { sys::ggml_backend_rpc_add_device(c_endpoint.as_ptr()) };
NonNull::new(device).ok_or_else(|| RpcError::InitializationFailed {
endpoint: endpoint.to_string(),
})
}