use crate::prelude_internal::*;
use std::ptr::{null_mut, NonNull};
#[repr(transparent)]
pub struct UdpSocket<'a>(pub otUdpSocket, PhantomData<*mut otUdpSocket>, PhantomData<&'a ()>);
impl_ot_castable!(opaque lifetime UdpSocket<'_>, otUdpSocket, Default::default(), Default::default());
impl std::fmt::Debug for UdpSocket<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("otUdpSocket")
.field("peer_name", &self.peer_name())
.field("sock_name", &self.sock_name())
.field("handle", &self.get_handle())
.finish()
}
}
impl<'a> UdpSocket<'a> {
pub fn peer_name(&self) -> SockAddr {
self.0.mPeerName.into()
}
pub fn sock_name(&self) -> SockAddr {
self.0.mSockName.into()
}
pub fn get_handle(&self) -> Option<NonNull<::std::os::raw::c_void>> {
NonNull::new(self.0.mHandle)
}
pub fn set_handle(&mut self, handle: Option<NonNull<::std::os::raw::c_void>>) {
self.0.mHandle = handle.map(NonNull::as_ptr).unwrap_or(null_mut());
}
pub fn handle_receive(&self, msg: &ot::Message<'a>, info: &ot::message::Info) {
if let Some(handler) = self.0.mHandler {
unsafe {
handler(self.0.mContext, msg.as_ot_ptr(), info.as_ot_ptr());
}
}
}
}
#[derive(Debug, Clone)]
#[repr(transparent)]
pub struct UdpSocketIterator<'a>(*mut otUdpSocket, PhantomData<&'a ()>);
impl<'a> Iterator for UdpSocketIterator<'a> {
type Item = &'a UdpSocket<'a>;
fn next(&mut self) -> Option<Self::Item> {
let ret = unsafe { UdpSocket::ref_from_ot_ptr(self.0) };
if ret.is_some() {
self.0 = unsafe { (*self.0).mNext };
}
ret
}
}
pub trait Udp {
fn udp_get_sockets(&self) -> UdpSocketIterator<'_>;
}
impl<T: Udp + ot::Boxable> Udp for ot::Box<T> {
fn udp_get_sockets(&self) -> UdpSocketIterator<'_> {
self.as_ref().udp_get_sockets()
}
}
impl Udp for Instance {
fn udp_get_sockets(&self) -> UdpSocketIterator<'_> {
UdpSocketIterator(unsafe { otUdpGetSockets(self.as_ot_ptr()) }, PhantomData)
}
}