use std::net::SocketAddr;
use libcoap_sys::{coap_endpoint_t, coap_free_endpoint, coap_new_endpoint, coap_proto_t::COAP_PROTO_DTLS};
use crate::{context::CoapContext, error::EndpointCreationError, transport::EndpointCommon, types::CoapAddress};
#[derive(Debug)]
pub struct CoapDtlsEndpoint {
raw_endpoint: *mut coap_endpoint_t,
}
impl CoapDtlsEndpoint {
pub(crate) unsafe fn new(
context: &mut CoapContext,
addr: SocketAddr,
) -> Result<CoapDtlsEndpoint, EndpointCreationError> {
let endpoint = coap_new_endpoint(
context.as_mut_raw_context(),
CoapAddress::from(addr).as_raw_address(),
COAP_PROTO_DTLS,
);
if endpoint.is_null() {
return Err(EndpointCreationError::Unknown);
}
Ok(CoapDtlsEndpoint { raw_endpoint: endpoint })
}
}
impl EndpointCommon for CoapDtlsEndpoint {
unsafe fn as_raw_endpoint(&self) -> &coap_endpoint_t {
&*self.raw_endpoint
}
unsafe fn as_mut_raw_endpoint(&mut self) -> &mut coap_endpoint_t {
&mut *self.raw_endpoint
}
}
impl Drop for CoapDtlsEndpoint {
fn drop(&mut self) {
unsafe { coap_free_endpoint(self.raw_endpoint) }
}
}