UDP-WG Implementation
Loading...
Searching...
No Matches
network::queue Class Reference

A thread-safe queue. More...

#include <network.h>

Public Member Functions

void enqueue (const udp::packet &in)
 Enqueue a packet.
 
udp::packet pop (const tag type=NONE, const size_t &iterations=-1)
 Remove the first packet of the type byte.
 
udp::packet pop (const std::vector< uint8_t > &types, const size_t &iterations=-1)
 Pop with multiple valid types.
 
bool empty ()
 Check if the queue is empty.
 
size_t size ()
 Get the current size of the queue.
 
void flush ()
 Flush the queue.
 

Friends

void thread (port_t port, wireguard::config wg={})
 The Network Thread.
 

Detailed Description

A thread-safe queue.

Remarks
This is a wrapper that mediates access of packets through a mutex. This ensures that the main and network threads don't have race conditions when adding/removing information from queues.

Member Function Documentation

◆ empty()

bool network::queue::empty ( )
inline

Check if the queue is empty.

Returns
Whether the queue is empty.

◆ enqueue()

void network::queue::enqueue ( const udp::packet & in)
inline

Enqueue a packet.

Parameters
inThe packet to enqueue
Warning
This function is blocking.

◆ flush()

void network::queue::flush ( )
inline

Flush the queue.

Warning
Any messages in the queue will be dropped!
This function is blocking.

◆ pop() [1/2]

udp::packet network::queue::pop ( const std::vector< uint8_t > & types,
const size_t & iterations = -1 )
inline

Pop with multiple valid types.

Parameters
typesA list of types to search for.
iterationsThe iteration count
Returns
The first matching packet.

◆ pop() [2/2]

udp::packet network::queue::pop ( const tag type = NONE,
const size_t & iterations = -1 )
inline

Remove the first packet of the type byte.

Parameters
typeThe type to search for: NONE for any packet.
iterationsHow many times to check before failing. Defaults to -1 IE SIZE_MAX IE forever.
Returns
The first matching packet.

◆ size()

size_t network::queue::size ( )
inline

Get the current size of the queue.

Returns
The size.

Friends And Related Symbol Documentation

◆ thread

void thread ( port_t port,
wireguard::config wg = {} )
friend

The Network Thread.

Parameters
portThe port to bind to: 0 = randomized.
wgAn optional WireGuard configuration.

Send a packet across a FD.

Parameters
pThe packet to send.
fdThe FD to send across.
Exceptions
runtime_errorIf the packet failed to send.

Open a socket at the specified port.

Parameters
portThe port.
Returns
The socket FD.
Warning
This function can teardown the thread if the socket cannot be initialized.

Establish a new connection.

Parameters
destThe destination.
Returns
The FD of the connection.
Remarks
One function of the Network Thread is that it should transparently handle new connections, maintain existing connections, and remove dead connections. When a unknown destination is specified in a UDP packet, we call this lambda to reach out and establish a FD to communicate over. Network Threads are cooperative in the sense that they willingly exchange this information.

Cleanup the thread.

Remarks
This closes the listening socket and all active connections, and flushes the queues.
Only the Network Threads sets stat to TERMINATE.

Wait for a connection of a specific type.

Parameters
fdThe FD to listen on.
tagThe tag at the start of the packet data.
Returns
The UDP packet.
Remarks
This is for re-keying. See that section for a more apt explanation.

The documentation for this class was generated from the following file: