Class NetworkInterface

Inheritance Relationships

Derived Types

Class Documentation

class NetworkInterface

Abstract base class for network interfaces.

This class defines the interface for network communication. It provides methods for opening and closing the connection, writing data, and setting a receive callback function.

Subclassed by network_bridge::TcpInterface, network_bridge::UdpInterface

Public Functions

inline NetworkInterface()

Constructor for NetworkInterface.

virtual ~NetworkInterface() = default

Virtual destructor for NetworkInterface.

inline void initialize(const rclcpp::Node::SharedPtr &node, std::function<void(std::span<const uint8_t>)> recv_cb)

Initializes the network interface.

This function sets the node and receive callback for the network interface, and then calls the protected initialize_ function to perform any additional initialization steps for a specific implementation.

Parameters:
  • node – A shared pointer to the ROS 2 node.

  • recv_cb – The callback function to be called when data is received.

virtual void open() = 0

Opens the network connection.

This method should be implemented by derived classes to establish the network connection. Should handle all network setup, including sockets, starting threads, etc

virtual void close() = 0

Closes the network connection.

This method should be implemented by derived classes to close the network connection. Cleanly stop threads, close sockets, etc.

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

Writes data to the network.

This method should be implemented by derived classes to send data over the network.

Parameters:

data – The data to be written.

Protected Functions

virtual void initialize_() = 0

Initializes the network interface.

This function is pure virtual and must be implemented by derived classes. Should declare and load all parameters, such as IP addresses, ports, etc.

Protected Attributes

rclcpp::Node::SharedPtr node_

Shared pointer to the ROS 2 node.

std::vector<uint8_t> receive_buffer_

The data buffer for received data.

std::function<void(std::span<const uint8_t>)> recv_cb_

The receive callback function.