Mbed OS Reference
Loading...
Searching...
No Matches
MsgHeaderIterator Struct Reference

Allows iteration through the list of message headers received in the control parameter of the socket_sendto_control / socket_recvfrom_control methods. More...

#include <MsgHeader.h>

Public Member Functions

 MsgHeaderIterator (nsapi_msghdr_t *hdr, nsapi_size_t size)
 Create a MsgHeaderIterator over given nsapi_msghdr_t list. More...
 
bool has_next ()
 Checks if the next address of the iterator is a valid list member. More...
 
nsapi_msghdr_tnext ()
 Returns next element of the list. More...
 

Detailed Description

Allows iteration through the list of message headers received in the control parameter of the socket_sendto_control / socket_recvfrom_control methods.

Members types

MsgHeaderIterator works on the list which members are of type nsapi_msghdr_t or other types extending this struct. For example nsapi_pktinfo:

typedef struct nsapi_pktinfo {
nsapi_addr_t ipi_addr;
int ipi_ifindex;
void *network_interface;
struct nsapi_pktinfo nsapi_pktinfo_t
nsapi_pktinfo structure
IP address structure for passing IP addresses by value.
Definition: nsapi_types.h:247
Header structure for control info.
Definition: nsapi_types.h:426
nsapi_pktinfo structure
Definition: nsapi_types.h:437

There are two requirements for such structures to work well with MsgHeaderIterator.

  • First element needs to be of type nsapi_msghdr_t.
  • Value of the field len of the nsapi_msghdr_t needs to bet set to the size of the whole extending type. For example:
nsapi_pktinfo_t pkt_info;
pkt_info.hdr.len = sizeof(nsapi_pktinfo_t);

This value is used in the MsgHeaderIterator to calculate proper addresses of the list elements.

Example

Code presenting minimal usage example.

struct default_buffer_t {
default_buffer_t()
{
el1.hdr.len = sizeof(nsapi_pktinfo_t);
el2.len = sizeof(nsapi_msghdr_t);
el3.len = sizeof(nsapi_msghdr_t);
el4.hdr.len = sizeof(nsapi_pktinfo_t);
}
};
default_buffer buff;
nsapi_msghdr_t *hdr_p = reinterpret_cast<nsapi_msghdr_t *>(&buff);
MsgHeaderIterator it(hdr_p, sizeof(buff));
it.has_next() // returns true
auto p1 = it.next() // returns pointer to el1
auto p2 = it.next() // returns pointer to el2
auto p3 = it.next() // returns pointer to el3
auto p4 = it.next() // returns pointer to el4
it.has_next() // returns false
auto p5 = it.next() // returns nullptr
struct nsapi_msghdr nsapi_msghdr_t
Header structure for control info.
Allows iteration through the list of message headers received in the control parameter of the socket_...
Definition: MsgHeader.h:91
Note
More usage examples are implemented in the MsgHeaderIterator unit test in netsocket/tests/UNITTESTS/NetworkStack/test_MsgHeaderIterator.cpp

Definition at line 91 of file MsgHeader.h.

Constructor & Destructor Documentation

◆ MsgHeaderIterator()

Create a MsgHeaderIterator over given nsapi_msghdr_t list.

Parameters
hdrPointer to the first list element.
sizeSize of the whole list.

Definition at line 97 of file MsgHeader.h.

Member Function Documentation

◆ has_next()

bool has_next ( )

Checks if the next address of the iterator is a valid list member.

Return values
Trueif the next address is a valid member.
Falseotherwise.

Definition at line 108 of file MsgHeader.h.

◆ next()

nsapi_msghdr_t * next ( )

Returns next element of the list.

Return values
nullptrif the list doesn't contain next element.
Pointerto the next element otherwise.

Definition at line 134 of file MsgHeader.h.