Repository navigation
Expand file tree
/
Copy pathsocket_utils.h
More file actions
110 lines (95 loc) · 4.6 KB
/
Copy pathsocket_utils.h
File metadata and controls
110 lines (95 loc) · 4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#ifndef SOCKET_UTILS_H
#define SOCKET_UTILS_H
#include "common.h"
#include <asio/ip/tcp.hpp>
#include <asio/ip/udp.hpp>
#include <asio/post.hpp>
#include <cstddef>
#include <cstdint>
#include <utility>
#include <vector>
using udp_socket = asio::basic_datagram_socket<asio::ip::udp, asio::io_context::executor_type>;
using tcp_socket = asio::basic_stream_socket<asio::ip::tcp, asio::io_context::executor_type>;
using tcp_acceptor = asio::basic_socket_acceptor<asio::ip::tcp, asio::io_context::executor_type>;
namespace lsl {
inline asio::chrono::milliseconds timeout_sec(double timeout_seconds) {
return asio::chrono::milliseconds(static_cast<unsigned int>(1000 * timeout_seconds));
}
/// Bind a socket to a free port in the configured port range or throw an error otherwise.
uint16_t bind_port_in_range(udp_socket &sock, asio::ip::udp protocol);
/// Bind and listen to an acceptor on a free port in the configured port range or throw an error.
uint16_t bind_and_listen_to_port_in_range(
tcp_acceptor &acc, asio::ip::tcp protocol, int backlog);
/// Receive timestamps for one UDP datagram in the lsl_clock() domain.
struct rx_timestamps {
double user{0.0};
double kernel{0.0};
bool has_kernel{false};
double best() const { return has_kernel ? kernel : user; }
};
/// Transmit timestamps for one UDP datagram in the lsl_clock() domain.
struct tx_timestamps {
double before{0.0};
double after{0.0};
uint32_t ts_id{0};
bool has_ts_id{false};
double midpoint() const { return 0.5 * (before + after); }
};
/// Kernel timestamping capabilities enabled on a UDP socket.
struct socket_timestamp_caps {
bool rx_kernel{false};
bool tx_kernel{false};
};
/// Enable best-effort kernel RX/TX software timestamping on a UDP socket.
/// Linux: SO_TIMESTAMPING (RX + per-message TX via MSG_ERRQUEUE), fallback SO_TIMESTAMPNS.
/// macOS: SO_TIMESTAMP_MONOTONIC (RX). Windows/other: none.
socket_timestamp_caps enable_socket_timestamps(udp_socket &sock);
/// Receive one datagram without blocking (recvmsg() on Linux/macOS), capturing both user-space
/// and kernel receive timestamps in the lsl_clock() domain.
std::size_t receive_with_rx_timestamp(udp_socket &sock, char *buf, std::size_t len,
asio::ip::udp::endpoint &sender, rx_timestamps &rx, asio::error_code &ec);
/// Like sock.async_receive_from(), but populates rx (user + kernel receive timestamps when
/// supported). The socket, buffer, sender, and rx must stay alive until the handler is called.
template <typename Handler>
void async_receive_with_rx_timestamp(udp_socket &sock, char *buf, std::size_t len,
asio::ip::udp::endpoint &sender, rx_timestamps &rx, Handler handler) {
#if defined(__linux__) || defined(__APPLE__)
// Read first: async_wait() alone can miss datagrams that arrived before the wait was armed.
asio::error_code ec;
std::size_t n = receive_with_rx_timestamp(sock, buf, len, sender, rx, ec);
if (ec != asio::error::would_block && ec != asio::error::try_again) {
asio::post(sock.get_executor(), [handler, ec, n]() mutable { handler(ec, n); });
return;
}
sock.async_wait(udp_socket::wait_read,
[&sock, buf, len, &sender, &rx, handler](const asio::error_code &err) mutable {
asio::error_code ec = err;
std::size_t n = ec ? 0 : receive_with_rx_timestamp(sock, buf, len, sender, rx, ec);
if (ec == asio::error::would_block || ec == asio::error::try_again)
async_receive_with_rx_timestamp(sock, buf, len, sender, rx, std::move(handler));
else
handler(ec, n);
});
#else
sock.async_receive_from(asio::buffer(buf, len), sender,
[&rx, handler](const asio::error_code &ec, std::size_t n) mutable {
rx = rx_timestamps{};
if (!ec) rx.user = lsl_clock();
handler(ec, n);
});
#endif
}
/// Send one datagram synchronously, bracketing the syscall with lsl_clock() and (on Linux when
/// want_tx_kernel is true) requesting a per-message software TX timestamp via SO_TIMESTAMPING.
std::size_t send_with_tx_timestamp(udp_socket &sock, const char *data, std::size_t len,
const asio::ip::udp::endpoint &dest, bool want_tx_kernel, uint32_t &tx_ts_counter,
tx_timestamps &tx, asio::error_code &ec);
/// Drain any completed Linux SO_TIMESTAMPING TX timestamps from MSG_ERRQUEUE (non-blocking),
/// appending (tx_ts_id, lsl_clock_seconds) pairs to `out`. No-op on non-Linux platforms.
void drain_tx_timestamps(udp_socket &sock, std::vector<std::pair<uint32_t, double>> &out);
/// Zero-allocation, locale-independent C formatting/parsing helpers for LSL:timedata wire text.
int format_c_double(char *buf, std::size_t cap, double val);
bool parse_c_int(const char *&p, const char *end, int &out);
bool parse_c_double(const char *&p, const char *end, double &out);
} // namespace lsl
#endif