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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
use std::net::UdpSocket;
use std::thread::sleep;
use std::time::Duration;
#[cfg(feature = "tokio")]
use tokio::net::UdpSocket as TokioUdpSocket;
use super::*;
impl Sender {
/// Sends with a caller-provided UDP socket.
///
/// Prefer this in repeated/high-throughput paths so the socket can be
/// reused across calls, avoiding per-call bind overhead and ephemeral-port
/// churn.
///
/// This API is blocking. If `SendOptions::with_delay(...)` is non-zero, it
/// uses `std::thread::sleep` between packet sends.
///
/// This method only requires `&self`, so one `Sender` can be shared via
/// `Arc<Sender>` across threads to keep a single monotonic identity/ID
/// sequence.
///
/// **Note:** the `message_id` is reserved before network I/O begins. If
/// emission fails (e.g. socket error), the reserved ID is consumed and the
/// next send will use the following ID. This is intentional — rolling back
/// IDs is not possible when multiple threads share a sender. The receiver
/// tolerates ID gaps via its freshness window.
pub fn send_with_socket(
&self,
socket: &UdpSocket,
request: SendRequest<'_>,
) -> std::result::Result<MessageKey, SendFailure> {
self.send_with_socket_with_pacer(socket, request, sleep)
}
/// Sends with a caller-provided UDP socket and reusable scratch buffers.
///
/// Prefer this in hot loops to avoid per-call packet buffer allocations.
pub fn send_with_socket_with_scratch(
&self,
socket: &UdpSocket,
request: SendRequest<'_>,
scratch: &mut SendScratch,
) -> std::result::Result<MessageKey, SendFailure> {
self.send_with_socket_with_pacer_and_scratch(socket, request, sleep, scratch)
}
/// Sends with a caller-provided UDP socket and custom pacing callback.
pub fn send_with_socket_with_pacer<F>(
&self,
socket: &UdpSocket,
request: SendRequest<'_>,
pace: F,
) -> std::result::Result<MessageKey, SendFailure>
where
F: FnMut(Duration),
{
let mut scratch = SendScratch::default();
self.send_with_socket_with_pacer_and_scratch(socket, request, pace, &mut scratch)
}
fn send_with_socket_with_pacer_and_scratch<F>(
&self,
socket: &UdpSocket,
request: SendRequest<'_>,
pace: F,
scratch: &mut SendScratch,
) -> std::result::Result<MessageKey, SendFailure>
where
F: FnMut(Duration),
{
let SendRequest {
destination,
data,
options,
identity,
} = request;
let plan = self
.prepare_send_plan(data, &options, &identity)
.map_err(SendFailure::preflight)?;
emit::emit_with_socket_and_pacer_with_scratch(
socket,
destination,
data,
identity.packet_auth(),
plan,
pace,
scratch,
)
.map_err(SendFailure::emission_from_emit)
}
/// One-shot convenience API that binds a fresh ephemeral UDP socket per
/// call.
///
/// Prefer [`Sender::send_with_socket`] in loops/high-throughput paths to
/// avoid repeated bind overhead and ephemeral port churn.
pub fn send_oneshot(
&self,
request: SendRequest<'_>,
) -> std::result::Result<MessageKey, SendFailure> {
let bind_addr = if request.destination.is_ipv4() {
"0.0.0.0:0"
} else {
"[::]:0"
};
let socket = UdpSocket::bind(bind_addr)
.map_err(UniUdpError::from)
.map_err(SendFailure::preflight)?;
self.send_with_socket(&socket, request)
}
/// Async variant of [`Sender::send_with_socket`] for Tokio sockets.
#[cfg(feature = "tokio")]
pub async fn send_with_tokio_socket(
&self,
socket: &TokioUdpSocket,
request: SendRequest<'_>,
) -> std::result::Result<MessageKey, SendFailure> {
let mut scratch = SendScratch::default();
self.send_with_tokio_socket_with_scratch(socket, request, &mut scratch)
.await
}
/// Async variant of [`Sender::send_with_socket_with_scratch`] for Tokio
/// sockets.
#[cfg(feature = "tokio")]
pub async fn send_with_tokio_socket_with_scratch(
&self,
socket: &TokioUdpSocket,
request: SendRequest<'_>,
scratch: &mut SendScratch,
) -> std::result::Result<MessageKey, SendFailure> {
let SendRequest {
destination,
data,
options,
identity,
} = request;
let plan = self
.prepare_send_plan(data, &options, &identity)
.map_err(SendFailure::preflight)?;
emit::emit_with_tokio_socket_with_scratch(
socket,
destination,
data,
identity.packet_auth(),
plan,
scratch,
)
.await
.map_err(SendFailure::emission_from_emit)
}
/// Async one-shot convenience API that binds a fresh ephemeral Tokio UDP
/// socket per call.
///
/// Prefer [`Sender::send_with_tokio_socket`] in loops/high-throughput paths
/// to avoid repeated bind overhead and ephemeral port churn.
#[cfg(feature = "tokio")]
pub async fn send_async_oneshot(
&self,
request: SendRequest<'_>,
) -> std::result::Result<MessageKey, SendFailure> {
let bind_addr = if request.destination.is_ipv4() {
"0.0.0.0:0"
} else {
"[::]:0"
};
let socket = TokioUdpSocket::bind(bind_addr)
.await
.map_err(UniUdpError::from)
.map_err(SendFailure::preflight)?;
self.send_with_tokio_socket(&socket, request).await
}
}