pub struct CoapRequestHandler<D: Any + ?Sized + Debug> { /* private fields */ }
Expand description
A handler for CoAP requests on a resource.
This handler can be associated with a CoapResource in order to be called when a request for the associated resource and the provided method arrives. The handler is then able to generate and send a response to the request accordingly.
§Creating a CoapRequestHandler
There are multiple ways to create a CoapRequestHandler:
- Using the resource_handler! macro: Preferred for handlers with a static lifetime (i.e., function pointers, not closures).
- Using CoapRequestHandler::new: Preferred for closures if you don’t need access to the CoapResource itself (but can be used for function pointers as well).
- Using CoapRequestHandler::new_resource_ref: Preferred for closures if you need access to the CoapResource itself (but can be used for function pointers as well).
For method 2, the provided handler has to be a FnMut(&mut D, &mut CoapServerSession, &CoapRequest, CoapResponse)
,
while for the other two methods, the handler has to be a FnMut(&CoapResource<D>, &mut CoapServerSession, &CoapRequest, CoapResponse)
,
with the following arguments:
- Either the associated CoapResource or the user data depending on the type of handler. Getting the user data directly without the associated resource has the advantage that it is easy to pass a method as a handler, while getting the CoapResource gives you the option to manipulate the resource (you can still get the user data from the resource using CoapResource::user_data.
- The server-side session with the peer this request was received from. You may want to store or retrieve additional information about the peer using CoapSessionCommon::set_app_data() and CoapSessionCommon::app_data().
- The incoming CoapRequest received from the client.
- A prepared CoapResponse instance that is already set to the correct token value to be treated as a response to the request by the client.
Implementations§
Source§impl<D: 'static + ?Sized + Debug> CoapRequestHandler<D>
impl<D: 'static + ?Sized + Debug> CoapRequestHandler<D>
Sourcepub fn new<F: 'static + FnMut(&mut D, &mut CoapServerSession<'_>, &CoapRequest, CoapResponse)>(
handler: F,
) -> CoapRequestHandler<D>
pub fn new<F: 'static + FnMut(&mut D, &mut CoapServerSession<'_>, &CoapRequest, CoapResponse)>( handler: F, ) -> CoapRequestHandler<D>
Creates a new CoapResourceHandler with the given function as the handler function to call.
Sourcepub fn new_resource_ref<F: 'static + FnMut(&CoapResource<D>, &mut CoapServerSession<'_>, &CoapRequest, CoapResponse)>(
handler: F,
) -> CoapRequestHandler<D>
pub fn new_resource_ref<F: 'static + FnMut(&CoapResource<D>, &mut CoapServerSession<'_>, &CoapRequest, CoapResponse)>( handler: F, ) -> CoapRequestHandler<D>
Creates a new CoapResourceHandler with the given function as the handler function to call.
In contrast to CoapRequestHandler::new, the handler for this function is not provided with
a direct reference to the user data, but instead with a reference to the associated
CoapResource
. This way, you can perform actions on the resource directly (e.g., notify
observers).
Sourcepub unsafe fn from_raw_handler(
raw_handler: unsafe extern "C" fn(resource: *mut coap_resource_t, session: *mut coap_session_t, incoming_pdu: *const coap_pdu_t, query: *const coap_string_t, response_pdu: *mut coap_pdu_t),
) -> CoapRequestHandler<D>
pub unsafe fn from_raw_handler( raw_handler: unsafe extern "C" fn(resource: *mut coap_resource_t, session: *mut coap_session_t, incoming_pdu: *const coap_pdu_t, query: *const coap_string_t, response_pdu: *mut coap_pdu_t), ) -> CoapRequestHandler<D>
Creates a new request handler using the given raw handler function.
The handler function provided here is called directly by libcoap.
§Safety
The handler function must not modify the user data value inside of the provided raw resource
in a way that would break normal handler functions. Also, neither the resource nor the
session may be freed by calling coap_delete_resource
or coap_session_release
.