STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
TCPClient.hpp
Go to the documentation of this file.
1#ifndef DAQ_TCP_CLIENT_HPP
2#define DAQ_TCP_CLIENT_HPP
3
4#include <fcntl.h>
5#include <sys/socket.h>
6#include <unistd.h>
7
8#include <cstdint>
9#include <memory>
10#include <mutex>
11#include <string>
12#include <vector>
13
14namespace daq_comms {
15namespace transport {
16
17/**
18 * @brief Simple TCP client for Elodin database connection
19 *
20 * Standalone implementation without FSW dependencies.
21 */
22class TCPClient {
23public:
24 TCPClient();
25 ~TCPClient();
26
27 /**
28 * @brief Connect to TCP server
29 * @param host Server hostname or IP address
30 * @param port Server port
31 * @return true if connection successful, false otherwise
32 */
33 bool connect(const std::string& host, uint16_t port);
34
35 /**
36 * @brief Disconnect from server
37 */
38 void disconnect();
39
40 /**
41 * @brief Check if connected
42 */
43 bool is_connected() const;
44
45 /**
46 * @brief Write data directly (for VTable registration)
47 * @param data Data to write
48 * @param len Length of data
49 * @return true if successful
50 */
51 bool write_all(const void* data, size_t len);
52
53 /**
54 * @brief Write data with buffering (for data publishing)
55 * @param data Data to write
56 * @param len Length of data
57 * @return true if successful
58 */
59 bool write_buffered(const void* data, size_t len);
60
61 /**
62 * @brief Flush buffered data
63 */
64 void flush();
65
66 /**
67 * @brief Read data from socket (non-blocking)
68 * @param buffer Buffer to read into
69 * @param max_len Maximum bytes to read
70 * @return Number of bytes read, 0 if no data available, -1 on error
71 */
72 ssize_t read(void* buffer, size_t max_len);
73
74 /**
75 * @brief Read exactly len bytes (blocking)
76 * @param buffer Buffer to read into
77 * @param len Exact number of bytes to read
78 * @return true if successful, false on error
79 */
80 bool read_exact(void* buffer, size_t len);
81
82 /**
83 * @brief Get last error message
84 */
85 std::string last_error() const;
86
87 /**
88 * @brief Set socket receive timeout (0 = blocking)
89 * @param timeout_ms Receive timeout in milliseconds
90 */
91 void set_recv_timeout_ms(int timeout_ms);
92
93private:
96 std::string last_error_;
97
98 // Buffering for efficient writes
99 static constexpr size_t BUFFER_SIZE = 8192;
100 std::vector<uint8_t> write_buffer_;
102 std::mutex write_mutex_;
103
104 bool _write_all(const void* data, size_t len);
105 void _flush_buffer();
106};
107
108} // namespace transport
109} // namespace daq_comms
110
111#endif // DAQ_TCP_CLIENT_HPP
void flush()
Flush buffered data.
static constexpr size_t BUFFER_SIZE
Definition TCPClient.hpp:99
std::vector< uint8_t > write_buffer_
ssize_t read(void *buffer, size_t max_len)
Read data from socket (non-blocking).
void disconnect()
Disconnect from server.
Definition TCPClient.cpp:76
void set_recv_timeout_ms(int timeout_ms)
Set socket receive timeout (0 = blocking).
bool _write_all(const void *data, size_t len)
bool write_all(const void *data, size_t len)
Write data directly (for VTable registration).
Definition TCPClient.cpp:89
bool read_exact(void *buffer, size_t len)
Read exactly len bytes (blocking).
bool connect(const std::string &host, uint16_t port)
Connect to TCP server.
Definition TCPClient.cpp:24
bool write_buffered(const void *data, size_t len)
Write data with buffering (for data publishing).
Definition TCPClient.cpp:99
std::string last_error() const
Get last error message.
bool is_connected() const
Check if connected.
Definition TCPClient.cpp:85
static const emmc_port_t port
Definition emmc_stm32.c:210