use std::ffi::CStr;
use std::{cmp, str};
macro_rules! visit {
($ptr:expr, $visitor:ident) => {{
let s_ptr = $ptr;
let res;
if !s_ptr.is_null() {
let c_s = CStr::from_ptr(s_ptr);
let s = str::from_utf8_unchecked(c_s.to_bytes());
res = $visitor(s);
grpcio_sys::gpr_free(s_ptr as _);
} else {
res = $visitor("");
}
res
}};
}
pub fn get_top_channels<V, R>(start_channel_id: u64, visitor: V) -> R
where
V: FnOnce(&str) -> R,
{
unsafe {
visit!(
grpcio_sys::grpc_channelz_get_top_channels(start_channel_id as _),
visitor
)
}
}
pub fn get_servers<V, R>(start_server_id: u64, visitor: V) -> R
where
V: FnOnce(&str) -> R,
{
unsafe {
visit!(
grpcio_sys::grpc_channelz_get_servers(start_server_id as _),
visitor
)
}
}
pub fn get_server<V, R>(server_id: u64, visitor: V) -> R
where
V: FnOnce(&str) -> R,
{
unsafe {
visit!(
grpcio_sys::grpc_channelz_get_server(server_id as _),
visitor
)
}
}
pub fn get_server_sockets<V, R>(
server_id: u64,
start_socket_id: u64,
max_results: usize,
visitor: V,
) -> R
where
V: FnOnce(&str) -> R,
{
let max_results = cmp::min(isize::MAX as usize, max_results) as isize;
unsafe {
visit!(
grpcio_sys::grpc_channelz_get_server_sockets(
server_id as _,
start_socket_id as _,
max_results
),
visitor
)
}
}
pub fn get_channel<V, R>(channel_id: u64, visitor: V) -> R
where
V: FnOnce(&str) -> R,
{
unsafe {
visit!(
grpcio_sys::grpc_channelz_get_channel(channel_id as _),
visitor
)
}
}
pub fn get_subchannel<V, R>(subchannel_id: u64, visitor: V) -> R
where
V: FnOnce(&str) -> R,
{
unsafe {
visit!(
grpcio_sys::grpc_channelz_get_subchannel(subchannel_id as _),
visitor
)
}
}
pub fn get_socket<V, R>(socket_id: u64, visitor: V) -> R
where
V: FnOnce(&str) -> R,
{
unsafe {
visit!(
grpcio_sys::grpc_channelz_get_socket(socket_id as _),
visitor
)
}
}