STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
NetworkSocket.hpp
Go to the documentation of this file.
1#ifndef DAQ_NETWORK_SOCKET_HPP
2#define DAQ_NETWORK_SOCKET_HPP
3
4#include <cstdint>
5#include <string>
6
7namespace daq_comms {
8namespace transport {
9
10/**
11 * @brief Abstract base for network socket operations
12 *
13 * Provides a clean interface for UDP/TCP socket operations
14 * without exposing platform-specific details.
15 */
17public:
18 virtual ~NetworkSocket() = default;
19
20 /**
21 * @brief Receive data from the socket
22 * @param buffer Buffer to store received data
23 * @param max_size Maximum bytes to receive
24 * @return Number of bytes received, or -1 on error
25 */
26 virtual ssize_t receive(uint8_t* buffer, size_t max_size) = 0;
27
28 /**
29 * @brief Send data through the socket
30 * @param data Data to send
31 * @param size Number of bytes to send
32 * @return Number of bytes sent, or -1 on error
33 */
34 virtual ssize_t send(const uint8_t* data, size_t size) = 0;
35
36 /**
37 * @brief Check if socket is valid and ready
38 */
39 virtual bool is_valid() const = 0;
40
41 /**
42 * @brief Get the last error message
43 */
44 virtual std::string last_error() const = 0;
45};
46
47/**
48 * @brief UDP socket implementation
49 */
50class UDPSocket : public NetworkSocket {
51public:
52 /**
53 * @brief Create UDP socket bound to local address
54 * @param bind_address Local address to bind to (e.g., "0.0.0.0")
55 * @param bind_port Local port to bind to
56 */
57 UDPSocket(const std::string& bind_address, uint16_t bind_port);
58
59 /**
60 * @brief Create UDP socket for sending to remote address
61 * @param remote_address Remote address to send to
62 * @param remote_port Remote port to send to
63 * @param is_sender Tag selecting this overload
64 * @param bind_address Local address to send from, pinning the egress NIC. Empty or "0.0.0.0"
65 * leaves the interface to the kernel — which is only safe on a host with one route to
66 * the boards. See lib/include/net/DaqInterface.hpp.
67 */
68 UDPSocket(const std::string& remote_address, uint16_t remote_port, bool is_sender,
69 const std::string& bind_address = "");
70
71 ~UDPSocket() override;
72
73 ssize_t receive(uint8_t* buffer, size_t max_size) override;
74 ssize_t send(const uint8_t* data, size_t size) override;
75 bool is_valid() const override;
76 std::string last_error() const override;
77
78 /**
79 * @brief Receive with source address information
80 * @param buffer Buffer to store received data
81 * @param max_size Maximum bytes to receive
82 * @param source_address Output parameter for source IP
83 * @param source_port Output parameter for source port
84 * @return Number of bytes received, or -1 on error
85 */
86 ssize_t receive_from(uint8_t* buffer, size_t max_size, std::string& source_address,
87 uint16_t& source_port);
88
89 /**
90 * @brief Send UDP packet to specific destination (works on bound sockets)
91 * @param dest_address Destination IP (e.g. "255.255.255.255" for broadcast)
92 * @param dest_port Destination port
93 * @param data Data to send
94 * @param size Number of bytes
95 * @return Bytes sent, or -1 on error. Set SO_BROADCAST before sending to broadcast addr.
96 */
97 ssize_t send_to(const std::string& dest_address, uint16_t dest_port, const uint8_t* data,
98 size_t size);
99
100 /** @brief Enable broadcast (required before send_to to 255.255.255.255) */
101 bool set_broadcast(bool enable);
102
103private:
106 std::string remote_address_;
107 uint16_t remote_port_;
108 std::string last_error_;
109
111};
112
113/**
114 * @brief TCP socket implementation (for Elodin connection)
115 */
116class TCPSocket : public NetworkSocket {
117public:
118 /**
119 * @brief Create TCP socket connected to remote address
120 * @param remote_address Remote address to connect to
121 * @param remote_port Remote port to connect to
122 */
123 TCPSocket(const std::string& remote_address, uint16_t remote_port);
124
125 ~TCPSocket() override;
126
127 ssize_t receive(uint8_t* buffer, size_t max_size) override;
128 ssize_t send(const uint8_t* data, size_t size) override;
129 bool is_valid() const override;
130 std::string last_error() const override;
131
132private:
134 std::string last_error_;
135
136 void setup_socket(const std::string& remote_address, uint16_t remote_port);
137};
138
139} // namespace transport
140} // namespace daq_comms
141
142#endif // DAQ_NETWORK_SOCKET_HPP
static void enable(board_bringup_t *b, uint16_t device_id)
Abstract base for network socket operations.
virtual std::string last_error() const =0
Get the last error message.
virtual ssize_t send(const uint8_t *data, size_t size)=0
Send data through the socket.
virtual ssize_t receive(uint8_t *buffer, size_t max_size)=0
Receive data from the socket.
virtual bool is_valid() const =0
Check if socket is valid and ready.
void setup_socket(const std::string &remote_address, uint16_t remote_port)
TCPSocket(const std::string &remote_address, uint16_t remote_port)
Create TCP socket connected to remote address.
ssize_t send(const uint8_t *data, size_t size) override
Send data through the socket.
std::string last_error() const override
Get the last error message.
ssize_t receive(uint8_t *buffer, size_t max_size) override
Receive data from the socket.
bool is_valid() const override
Check if socket is valid and ready.
std::string last_error() const override
Get the last error message.
ssize_t send_to(const std::string &dest_address, uint16_t dest_port, const uint8_t *data, size_t size)
Send UDP packet to specific destination (works on bound sockets).
UDPSocket(const std::string &bind_address, uint16_t bind_port)
Create UDP socket bound to local address.
bool is_valid() const override
Check if socket is valid and ready.
bool set_broadcast(bool enable)
Enable broadcast (required before send_to to 255.255.255.255).
ssize_t receive(uint8_t *buffer, size_t max_size) override
Receive data from the socket.
ssize_t receive_from(uint8_t *buffer, size_t max_size, std::string &source_address, uint16_t &source_port)
Receive with source address information.
ssize_t send(const uint8_t *data, size_t size) override
Send data through the socket.