-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: refactoring of the transport module (#13)
* Code refactoring Instead of having separate structures for each transport mode which have the same data field refactor everything into single CoapEndpoint structure. Remove unused methods. * Further refactoring. Encapsulate unsafe in CoapEndpoint::new. Instead of every endpoint function making unsafe calls with these changes they utilize CoapEndpoint::new with the protocol that they require. Document safety for CaopEndpoint::new. * Fix module ordering in lib. * Code clean up Safety comments are not needed any more. No need to cast the endpoint since everything is a common structure now. * Refactoring Further simplify the creation of endpoints.
- Loading branch information
1 parent
0e17941
commit 287c26c
Showing
8 changed files
with
82 additions
and
414 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// SPDX-License-Identifier: BSD-2-Clause | ||
/* | ||
* transport/mod.rs - Module file for CoAP transports. | ||
* This file is part of the libcoap-rs crate, see the README and LICENSE files for | ||
* more information and terms of use. | ||
* Copyright © 2021-2023 The NAMIB Project Developers, all rights reserved. | ||
* See the README as well as the LICENSE file for more information. | ||
*/ | ||
|
||
use std::{net::SocketAddr, os::raw::c_uint}; | ||
|
||
use libcoap_sys::{ | ||
coap_endpoint_set_default_mtu, coap_endpoint_t, coap_free_endpoint, coap_new_endpoint, coap_proto_t, | ||
}; | ||
|
||
use crate::{error::EndpointCreationError, types::CoapAddress, CoapContext}; | ||
|
||
pub type EndpointMtu = c_uint; | ||
|
||
#[derive(Debug)] | ||
pub struct CoapEndpoint { | ||
raw_endpoint: *mut coap_endpoint_t, | ||
} | ||
|
||
/// Trait for functions common between all types of endpoints. | ||
impl CoapEndpoint { | ||
/// Sets the default MTU value of the endpoint. | ||
pub fn set_default_mtu(&mut self, mtu: EndpointMtu) { | ||
// SAFETY: as_mut_raw_endpoint cannot fail and will always return a valid reference. | ||
// Modifying the state of the endpoint is also fine, because we have a mutable reference | ||
// of the whole endpoint. | ||
unsafe { | ||
coap_endpoint_set_default_mtu(&mut *self.raw_endpoint, mtu); | ||
} | ||
} | ||
|
||
/// Method utilized by transport protocol specific constructors to actually create the endpoint in libcoap | ||
pub(crate) fn new_endpoint( | ||
context: &mut CoapContext, | ||
addr: SocketAddr, | ||
proto: coap_proto_t, | ||
) -> Result<Self, EndpointCreationError> { | ||
let endpoint = unsafe { | ||
// SAFETY: coap_new_endpoint will return null if it is unable to add new endpoint. | ||
// These states are processed further in the code | ||
coap_new_endpoint( | ||
context.as_mut_raw_context(), | ||
CoapAddress::from(addr).as_raw_address(), | ||
proto, | ||
) | ||
}; | ||
|
||
if endpoint.is_null() { | ||
Err(EndpointCreationError::Unknown) | ||
} else { | ||
Ok(Self { raw_endpoint: endpoint }) | ||
} | ||
} | ||
} | ||
|
||
impl Drop for CoapEndpoint { | ||
fn drop(&mut self) { | ||
// SAFETY: Raw endpoint is guaranteed to exist for as long as the container exists. | ||
unsafe { coap_free_endpoint(self.raw_endpoint) } | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.