Core Library  1.7.0.0
Library containing core utilities and tools for threading, networking, logging, INI and CSV file management etc.
core_lib::asio::udp::MulticastReceiver Class Referencefinal

A general purpose multicast receiver. More...

#include <MulticastReceiver.h>

Public Member Functions

 MulticastReceiver ()=delete
 Default constructor - deleted.
 
 MulticastReceiver (boost_iocontext_t &ioContext, const defs::connection_t &multicastConnection, const defs::check_bytes_left_to_read_t &checkBytesLeftToRead, const defs::message_received_handler_t &messageReceivedHandler, const std::string &interfaceAddress="", size_t receiveBufferSize=DEFAULT_UDP_BUF_SIZE)
 Initialisation constructor. More...
 
 MulticastReceiver (const defs::connection_t &multicastConnection, const defs::check_bytes_left_to_read_t &checkBytesLeftToRead, const defs::message_received_handler_t &messageReceivedHandler, const std::string &interfaceAddress="", size_t receiveBufferSize=DEFAULT_UDP_BUF_SIZE)
 Initialisation constructor. More...
 
 MulticastReceiver (const MulticastReceiver &)=delete
 Copy constructor - deleted.
 
MulticastReceiveroperator= (const MulticastReceiver &)=delete
 Copy assignment operator - deleted.
 
 MulticastReceiver (MulticastReceiver &&)=delete
 Move constructor - deleted.
 
MulticastReceiveroperator= (MulticastReceiver &&)=delete
 Move assignment operator - deleted.
 
 ~MulticastReceiver ()
 Destructor.
 
defs::connection_t MulticastConnection () const
 Retrieve multicast connection details. More...
 
std::string InterfaceAddress () const
 Retrieve interface IP address. More...
 
void CloseSocket ()
 Close socket.
 

Private Member Functions

void CreateMulticastSocket (size_t receiveBufferSize)
 Create multicast 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< IoContextThreadGroupm_ioThreadGroup {}
 I/O context thread group.
 
boost_iocontext_t::strand m_strand
 I/O context strand.
 
defs::connection_t m_multicastConnection {}
 Multicast connection details.
 
std::string m_interfaceAddress {}
 Interface IP address of outgoing network adaptor.
 
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
 The multicast socket.
 

Detailed Description

A general purpose multicast receiver.

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

Constructor & Destructor Documentation

◆ MulticastReceiver() [1/2]

core_lib::asio::udp::MulticastReceiver::MulticastReceiver ( boost_iocontext_t ioContext,
const defs::connection_t multicastConnection,
const defs::check_bytes_left_to_read_t checkBytesLeftToRead,
const defs::message_received_handler_t messageReceivedHandler,
const std::string &  interfaceAddress = "",
size_t  receiveBufferSize = DEFAULT_UDP_BUF_SIZE 
)

Initialisation constructor.

Parameters
[in]ioContext- External boost IO context to manage ASIO.
[in]multicastConnection- Connection object describing target multicast group address and port.
[in]checkBytesLeftToRead- Function object capable of decoding the message and computing how many bytes are left until a complete message.
[in]messageReceivedHandler- Function object capable of handling a received message and dispatching it accordingly.
[in]interfaceAddress- Optional interface IP address for incoming network messages.
[in]receiveBufferSize- Optional socket receive option to control receive buffer size.

Typically use this constructor when managing a pool 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 executed using this thread pool managed by a single IO context. This is the recommended constructor.

◆ MulticastReceiver() [2/2]

core_lib::asio::udp::MulticastReceiver::MulticastReceiver ( const defs::connection_t multicastConnection,
const defs::check_bytes_left_to_read_t checkBytesLeftToRead,
const defs::message_received_handler_t messageReceivedHandler,
const std::string &  interfaceAddress = "",
size_t  receiveBufferSize = DEFAULT_UDP_BUF_SIZE 
)

Initialisation constructor.

Parameters
[in]multicastConnection- Connection object describing target multicast group address and port.
[in]checkBytesLeftToRead- Function object capable of decoding the message and computing how many bytes are left until a complete message.
[in]messageReceivedHandler- Function object capable of handling a received message and dispatching it accordingly.
[in]interfaceAddress- Optional interface IP address for incoming network messages.
[in]receiveBufferSize- Optional 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 recommended.

Member Function Documentation

◆ CreateMulticastSocket()

void core_lib::asio::udp::MulticastReceiver::CreateMulticastSocket ( size_t  receiveBufferSize)
private

Create multicast socket.

Parameters
[in]receiveBufferSize- Receive buffer size.

◆ InterfaceAddress()

std::string core_lib::asio::udp::MulticastReceiver::InterfaceAddress ( ) const

Retrieve interface IP address.

Returns
- Interface IP address.

◆ IsClosing()

bool core_lib::asio::udp::MulticastReceiver::IsClosing ( ) const
private

Get closing state.

Returns
True if closing socket, false otherwise.

◆ MulticastConnection()

defs::connection_t core_lib::asio::udp::MulticastReceiver::MulticastConnection ( ) const

Retrieve multicast connection details.

Returns
- Connection object describing target multicast group address and port.

◆ ReadComplete()

void core_lib::asio::udp::MulticastReceiver::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::MulticastReceiver::SetClosing ( bool  closing)
private

Set closing state.

Parameters
[in]closing- Closing socket flag.

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