Class UdpInterface

Inheritance Relationships

Base Type

Class Documentation

class UdpInterface : public network_bridge::NetworkInterface

Represents a UDP network interface.

The UdpInterface class is a concrete implementation of the NetworkInterface abstract class. It provides functionality for opening, closing, receiving and writing data to a UDP interface. It also handles receiving data asynchronously and provides error handling capabilities.

Public Functions

inline UdpInterface()
inline virtual ~UdpInterface()
virtual void open() override

Opens the UDP interface.

This function is responsible for opening the UDP interface and preparing it for communication. It should be called before any other operations are performed on the interface.

virtual void close() override

Closes the UDP interface.

After calling this function, the UDP interface will no longer be usable.

virtual void write(const std::vector<uint8_t> &data) override

Writes the given data to the UDP interface.

Parameters:

data – The data to be written.

Protected Functions

virtual void initialize_() override

Initializes interface by loading parameters.

Called from NetworkInterface::initialize()

void start_receive()

Starts receiving data on the UDP interface.

Re-binds the receive socket and starts an asynchronous receive operation.

void receive(const boost::system::error_code &ec, std::size_t bytes_recvd)

Callback function for receiving data.

It calls the receive callback function with the received data and starts another receive operation.

Parameters:
  • ec – The error code from the receive operation.

  • bytes_recvd – The number of bytes received.

void error_handler(const boost::system::error_code &ec, const std::string &error_message, bool fatal = false)

Handles errors from the UDP interface.

Parameters:
  • ec – The error code from the operation.

  • error_message – The error message to log.

  • fatal – Whether the error is fatal.

Protected Attributes

io_context io_context_
std::thread io_thread_
udp::socket send_socket_
udp::endpoint send_endpoint_
udp::socket receive_socket_
udp::endpoint receive_endpoint_
std::string local_address_
int receive_port_
std::string remote_address_
int send_port_