Core Library 1.7.0.0 Library containing core utilities and tools for threading, networking, logging, INI and CSV file management etc.

Public Member Functions | Private Member Functions | Private Attributes | List of all members
core_lib::asio::udp::UdpReceiver Class Referencefinal

A general purpose UDP receiver. More...

`#include <UdpReceiver.h>`

## Public Member Functions
| UdpReceiver ()=delete
| Default constructor - deleted.
| UdpReceiver (boost_iocontext_t &ioContext, uint16_t listenPort, const defs::check_bytes_left_to_read_t &checkBytesLeftToRead, const defs::message_received_handler_t &messageReceivedHandler, eUdpOption receiveOptions=eUdpOption::broadcast, size_t receiveBufferSize=DEFAULT_UDP_BUF_SIZE)
| Initialisation constructor. More...
| UdpReceiver (uint16_t listenPort, const defs::check_bytes_left_to_read_t &checkBytesLeftToRead, const defs::message_received_handler_t &messageReceivedHandler, eUdpOption receiveOptions=eUdpOption::broadcast, size_t receiveBufferSize=DEFAULT_UDP_BUF_SIZE)
| Initialisation constructor. More...
| UdpReceiver (const UdpReceiver &)=delete
| Copy constructor - deleted.
UdpReceiver & | operator= (const UdpReceiver &)=delete
| Copy assignment operator - deleted.
| UdpReceiver (UdpReceiver &&)=delete
| Move constructor - deleted.
UdpReceiver & | operator= (UdpReceiver &&)=delete
| Move assignment operator - deleted.
| ~UdpReceiver ()
| Destructor.
uint16_t | ListenPort () const
| Retrieve this receiver's listen port. More...
void | CloseSocket ()
| Close the socket.

## Private Member Functions
void | CreateUdpSocket (eUdpOption receiveOptions, size_t receiveBufferSize)
| Create UDP socket. More...
void | StartAsyncRead ()
| Start asynchronous read.
void | ReadComplete (const boost_sys::error_code &error, size_t bytesReceived)
| Read completion handler. More...
void | SetClosing (bool closing)
| Set closing state. More...
bool | IsClosing () const
| Get closing state. More...
void | ProcessCloseSocket ()
| Process asynchronous close socket.

## Private Attributes
std::mutex | m_closingMutex
| Mutex to protect shutdown of receiver.
threads::SyncEvent | m_closedEvent {}
| Event to synchronise shutdown of receiver.
bool | m_closing {false}
| Flag to show were are closing socket.
std::unique_ptr< IoContextThreadGroup > | m_ioThreadGroup {}
| I/O context thread group.
boost_iocontext_t::strand | m_strand
| I/O context strand.
uint16_t | m_listenPort {0}
| Receiver listen port.
defs::check_bytes_left_to_read_t | m_checkBytesLeftToRead {}
| Callback to check number of bytes left to read.
defs::message_received_handler_t | m_messageReceivedHandler {}
| Callback to handle received message.
defs::char_buffer_t | m_receiveBuffer {}
| Socket receive buffer.
defs::char_buffer_t | m_messageBuffer {}
| Message buffer.
boost_udp_t::endpoint | m_senderEndpoint {}
| Sender end-point.
boost_udp_t::socket | m_socket
| UDP socket.

## Detailed Description

A general purpose UDP receiver.

This class is used as the underpinnings of the SimpleUdpReceiver class.

## Constructor & Destructor Documentation

## ◆ UdpReceiver() [1/2]

core_lib::asio::udp::UdpReceiver::UdpReceiver | ( | boost_iocontext_t & | ioContext,
|  | uint16_t | listenPort,
|  | const defs::check_bytes_left_to_read_t & | checkBytesLeftToRead,
|  | const defs::message_received_handler_t & | messageReceivedHandler,
|  | eUdpOption | receiveOptions = `eUdpOption::broadcast`,
|  | size_t | receiveBufferSize = `DEFAULT_UDP_BUF_SIZE`
| ) |  |

Initialisation constructor.

Parameters

  [in] | ioContext | - External boost IO context to manage ASIO.
  [in] | listenPort | - Our listen port for all detected networks.
  [in] | checkBytesLeftToRead | - Function object capable of decoding the message and computing how many bytes are left until a complete message.
  [in] | messageReceivedHandler | - Function object cpable of handling a received message and disptaching it accordingly.
  [in] | receiveOptions | - Socket receive option to control the use of broadcasts/unicast.
  [in] | receiveBufferSize | - Socket receive option to control receive buffer size.

Typically use this constructor when managing a bool of threads using an instance of core_lib::asio::IoContextThreadGroup in your application to manage a pool of std::threads. This means you can use a single thread pool and all ASIO operations will be exectued using this thread pool managed by a single IO context. This is the recommended constructor.

## ◆ UdpReceiver() [2/2]

core_lib::asio::udp::UdpReceiver::UdpReceiver | ( | uint16_t | listenPort,
|  | const defs::check_bytes_left_to_read_t & | checkBytesLeftToRead,
|  | const defs::message_received_handler_t & | messageReceivedHandler,
|  | eUdpOption | receiveOptions = `eUdpOption::broadcast`,
|  | size_t | receiveBufferSize = `DEFAULT_UDP_BUF_SIZE`
| ) |  |

Initialisation constructor.

Parameters

  [in] | listenPort | - Our listen port for all detected networks.
  [in] | checkBytesLeftToRead | - Function object capable of decoding the message and computing how many bytes are left until a complete message.
  [in] | messageReceivedHandler | - Function object cpable of handling a received message and disptaching it accordingly.
  [in] | receiveOptions | - Socket receive option to control the use of broadcasts/unicast.
  [in] | receiveBufferSize | - Socket receive option to control receive buffer size.

This constructor does not require an external IO context to run instead it creates its own IO context object along with its own thread. For very simple cases this version will be fine but in more performance and resource critical situations the external IO context constructor is recommened.

## Member Function Documentation

## ◆ CreateUdpSocket()

void core_lib::asio::udp::UdpReceiver::CreateUdpSocket | ( | eUdpOption | receiveOptions,
|  | size_t | receiveBufferSize
| ) |  |
| private

Create UDP socket.

Parameters

  [in] | receiveOptions | - UDP receive options.
  [in] | receiveBufferSize | - Receive buffer size.

## ◆ IsClosing()

bool core_lib::asio::udp::UdpReceiver::IsClosing | ( |  | ) | const
| private

Get closing state.

Returns
  True if closing socket, false otherwise.

## ◆ ListenPort()

uint16_t core_lib::asio::udp::UdpReceiver::ListenPort | ( |  | ) | const

Retrieve this receiver's listen port.

Returns
  The listen port.

## ◆ ReadComplete()

void core_lib::asio::udp::UdpReceiver::ReadComplete | ( | const boost_sys::error_code & | error,
|  | size_t | bytesReceived
| ) |  |
| private

Read completion handler.

Parameters

  [in] | error | - Error code if one has happened.
  [in] | bytesReceived | - Number of bytes received.

## ◆ SetClosing()

void core_lib::asio::udp::UdpReceiver::SetClosing | ( | bool | closing | ) |
| private

Set closing state.

Parameters

  [in] | closing | - Closing socket flag.

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

- D:/Projects/CoreLibrary/Include/Asio/UdpReceiver.h
- D:/Projects/CoreLibrary/Source/Asio/UdpReceiver.cpp