rust-raknet 1.3.0

RakNet Protocol implementation by Rust.
Documentation
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
//! Synchronous adapters backed by a shared, continuously running Tokio runtime.
//!
//! Enable the `blocking` feature. Construct one [`RaknetRuntime`] and reuse it
//! for listeners and clients. All blocking operations reject calls made inside
//! a Tokio runtime; use the asynchronous API there instead.
//!
//! Sending accepts data into the existing transport path. Only `flush` waits
//! for reliable acknowledgements. A timeout cancels the wait, not data already
//! queued: retrying a timed-out send can duplicate application messages, and a
//! timed-out batch may have sent a prefix. Concurrent sends retain the async
//! API's ordering semantics; serialize dependent messages on the same channel.
//!
//! ```no_run
//! use rust_raknet::{blocking::RaknetRuntime, Reliability};
//! # fn main() -> rust_raknet::blocking::Result<()> {
//! let runtime = RaknetRuntime::new()?;
//! let socket = runtime.connect(&"127.0.0.1:19132".parse().unwrap())?;
//! socket.send(&[0xfe, 1], Reliability::ReliableOrdered)?;
//! socket.flush()?;
//! socket.close()?;
//! # Ok(()) }
//! ```

use crate::{Bytes, Reliability, error::RaknetError};
use std::{future::Future, net::SocketAddr, num::NonZeroUsize, sync::Arc, time::Duration};

/// Errors from the synchronous adapter or the underlying protocol.
#[derive(Debug)]
pub enum Error {
    /// A protocol or socket operation failed.
    Raknet(RaknetError),
    /// Creating the runtime failed.
    Runtime(std::io::Error),
    /// Blocking inside a Tokio runtime is unsupported.
    AsyncContext,
    /// The operation did not complete before its deadline.
    Timeout,
}
/// Result of a synchronous transport operation.
pub type Result<T> = std::result::Result<T, Error>;
impl From<RaknetError> for Error {
    fn from(error: RaknetError) -> Self {
        Self::Raknet(error)
    }
}
impl std::fmt::Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Raknet(e) => e.fmt(f),
            Self::Runtime(e) => write!(f, "failed to create Tokio runtime: {e}"),
            Self::AsyncContext => {
                f.write_str("blocking RakNet operations cannot run inside a Tokio runtime")
            }
            Self::Timeout => f.write_str("blocking RakNet operation timed out"),
        }
    }
}
impl std::error::Error for Error {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Raknet(e) => Some(e),
            Self::Runtime(e) => Some(e),
            _ => None,
        }
    }
}
struct RuntimeOwner(Option<tokio::runtime::Runtime>);
impl Drop for RuntimeOwner {
    fn drop(&mut self) {
        // Dropping adapters in an async context must not panic or block a worker.
        // Explicit close/flush is required for graceful protocol shutdown.
        if let Some(runtime) = self.0.take() {
            runtime.shutdown_background();
        }
    }
}

/// Shared runtime owner. Clones share workers; sockets keep it alive themselves.
/// No global runtime is created. Dropping the last owner stops background work.
#[derive(Clone)]
pub struct RaknetRuntime {
    inner: Arc<RuntimeOwner>,
}
impl RaknetRuntime {
    /// Create a continuously running runtime with one network worker.
    pub fn new() -> Result<Self> {
        Self::with_worker_threads(NonZeroUsize::new(1).unwrap())
    }
    /// Create a shared runtime with an explicit worker count.
    pub fn with_worker_threads(workers: NonZeroUsize) -> Result<Self> {
        Self::check_context()?;
        let runtime = tokio::runtime::Builder::new_multi_thread()
            .worker_threads(workers.get())
            .enable_all()
            .build()
            .map_err(Error::Runtime)?;
        Ok(Self {
            inner: Arc::new(RuntimeOwner(Some(runtime))),
        })
    }
    fn check_context() -> Result<()> {
        if tokio::runtime::Handle::try_current().is_ok() {
            Err(Error::AsyncContext)
        } else {
            Ok(())
        }
    }
    fn run<T>(&self, future: impl Future<Output = crate::error::Result<T>>) -> Result<T> {
        Self::check_context()?;
        self.inner
            .0
            .as_ref()
            .unwrap()
            .block_on(future)
            .map_err(Error::Raknet)
    }
    fn run_timeout<T>(
        &self,
        duration: Duration,
        future: impl Future<Output = crate::error::Result<T>>,
    ) -> Result<T> {
        Self::check_context()?;
        // Create the timer while entered into the owned runtime.
        self.inner.0.as_ref().unwrap().block_on(async {
            tokio::time::timeout(duration, future)
                .await
                .map_err(|_| Error::Timeout)?
                .map_err(Error::Raknet)
        })
    }
    /// Connect using the default RakNet version.
    pub fn connect(&self, address: &SocketAddr) -> Result<RaknetSocket> {
        let inner = self.run(crate::RaknetSocket::connect(address))?;
        Ok(RaknetSocket {
            inner,
            runtime: self.clone(),
        })
    }
    /// Connect with an overall handshake deadline.
    pub fn connect_timeout(
        &self,
        address: &SocketAddr,
        duration: Duration,
    ) -> Result<RaknetSocket> {
        let inner = self.run_timeout(duration, crate::RaknetSocket::connect(address))?;
        Ok(RaknetSocket {
            inner,
            runtime: self.clone(),
        })
    }
    /// Connect with an explicit protocol version.
    pub fn connect_with_version(&self, address: &SocketAddr, version: u8) -> Result<RaknetSocket> {
        let inner = self.run(crate::RaknetSocket::connect_with_version(address, version))?;
        Ok(RaknetSocket {
            inner,
            runtime: self.clone(),
        })
    }
    /// Connect with an explicit protocol version and requested MTU.
    pub fn connect_with_version_and_mtu(
        &self,
        address: &SocketAddr,
        version: u8,
        mtu: u16,
    ) -> Result<RaknetSocket> {
        let inner = self.run(crate::RaknetSocket::connect_with_version_and_mtu(
            address, version, mtu,
        ))?;
        Ok(RaknetSocket {
            inner,
            runtime: self.clone(),
        })
    }
    /// Bind a listener. Configure it, then call `listen` before `accept`.
    pub fn bind(&self, address: &SocketAddr) -> Result<RaknetListener> {
        let inner = self.run(crate::RaknetListener::bind(address))?;
        Ok(RaknetListener {
            inner,
            runtime: self.clone(),
        })
    }
    /// Bind with a maximum negotiated MTU.
    pub fn bind_with_maximum_mtu(&self, address: &SocketAddr, mtu: u16) -> Result<RaknetListener> {
        let inner = self.run(crate::RaknetListener::bind_with_maximum_mtu(address, mtu))?;
        Ok(RaknetListener {
            inner,
            runtime: self.clone(),
        })
    }
    /// Bind with a requested per-socket receive buffer size.
    pub fn bind_with_receive_buffer_size(
        &self,
        address: &SocketAddr,
        size: NonZeroUsize,
    ) -> Result<RaknetListener> {
        let inner = self.run(crate::RaknetListener::bind_with_receive_buffer_size(
            address, size,
        ))?;
        Ok(RaknetListener {
            inner,
            runtime: self.clone(),
        })
    }
    /// Bind a fixed group of Linux SO_REUSEPORT sockets.
    #[cfg(target_os = "linux")]
    pub fn bind_with_socket_shards(
        &self,
        address: &SocketAddr,
        count: NonZeroUsize,
    ) -> Result<RaknetListener> {
        let inner = self.run(crate::RaknetListener::bind_with_socket_shards(
            address, count,
        ))?;
        Ok(RaknetListener {
            inner,
            runtime: self.clone(),
        })
    }
    /// Adopt a standard UDP socket, preserving its buffer settings.
    pub fn from_std(&self, socket: std::net::UdpSocket) -> Result<RaknetListener> {
        let inner = self.run(crate::RaknetListener::from_std(socket))?;
        Ok(RaknetListener {
            inner,
            runtime: self.clone(),
        })
    }
    /// Discover a server using an unconnected ping, with a deadline.
    pub fn ping_timeout(&self, address: &SocketAddr, duration: Duration) -> Result<(i64, String)> {
        self.run_timeout(duration, crate::RaknetSocket::ping(address))
    }
}

/// Synchronous connection. Receive operations share one serialized consumer.
/// Batch receives wait for the first message, then drain ready messages.
/// This is a blocking API, not a nonblocking game-loop adapter.
/// Drop signals closure; call `flush` before `close` for acknowledged delivery.
pub struct RaknetSocket {
    inner: crate::RaknetSocket,
    runtime: RaknetRuntime,
}
impl RaknetSocket {
    /// Synchronous adapter for [`crate::RaknetSocket::send`].
    pub fn send(&self, buf: &[u8], reliability: Reliability) -> Result<()> {
        self.runtime.run(self.inner.send(buf, reliability))
    }
    /// Wait at most `duration`. Already queued data is not withdrawn on timeout.
    pub fn send_timeout(
        &self,
        buf: &[u8],
        reliability: Reliability,
        duration: Duration,
    ) -> Result<()> {
        self.runtime
            .run_timeout(duration, self.inner.send(buf, reliability))
    }
    /// Synchronous adapter for [`crate::RaknetSocket::send_with_order_channel`].
    pub fn send_with_order_channel(
        &self,
        buf: &[u8],
        reliability: Reliability,
        channel: u8,
    ) -> Result<()> {
        self.runtime.run(
            self.inner
                .send_with_order_channel(buf, reliability, channel),
        )
    }
    /// Synchronous adapter for [`crate::RaknetSocket::send_bytes`].
    pub fn send_bytes(&self, data: Bytes, reliability: Reliability) -> Result<()> {
        self.runtime.run(self.inner.send_bytes(data, reliability))
    }
    /// Wait at most `duration`. Already queued data is not withdrawn on timeout.
    pub fn send_bytes_timeout(
        &self,
        data: Bytes,
        reliability: Reliability,
        duration: Duration,
    ) -> Result<()> {
        self.runtime
            .run_timeout(duration, self.inner.send_bytes(data, reliability))
    }
    /// Synchronous adapter for [`crate::RaknetSocket::send_bytes_with_order_channel`].
    pub fn send_bytes_with_order_channel(
        &self,
        data: Bytes,
        reliability: Reliability,
        channel: u8,
    ) -> Result<()> {
        self.runtime.run(
            self.inner
                .send_bytes_with_order_channel(data, reliability, channel),
        )
    }
    /// Synchronous adapter for [`crate::RaknetSocket::send_batch`].
    pub fn send_batch(&self, messages: &[&[u8]], reliability: Reliability) -> Result<()> {
        self.runtime
            .run(self.inner.send_batch(messages, reliability))
    }
    /// Wait at most `duration`. Already queued data is not withdrawn on timeout.
    pub fn send_batch_timeout(
        &self,
        messages: &[&[u8]],
        reliability: Reliability,
        duration: Duration,
    ) -> Result<()> {
        self.runtime
            .run_timeout(duration, self.inner.send_batch(messages, reliability))
    }
    /// Synchronous adapter for [`crate::RaknetSocket::send_batch_with_order_channel`].
    pub fn send_batch_with_order_channel(
        &self,
        messages: &[&[u8]],
        reliability: Reliability,
        channel: u8,
    ) -> Result<()> {
        self.runtime.run(
            self.inner
                .send_batch_with_order_channel(messages, reliability, channel),
        )
    }
    /// Synchronous adapter for [`crate::RaknetSocket::send_bytes_batch`].
    pub fn send_bytes_batch(&self, messages: &[Bytes], reliability: Reliability) -> Result<()> {
        self.runtime
            .run(self.inner.send_bytes_batch(messages, reliability))
    }
    /// Synchronous adapter for [`crate::RaknetSocket::send_bytes_batch_with_order_channel`].
    pub fn send_bytes_batch_with_order_channel(
        &self,
        messages: &[Bytes],
        reliability: Reliability,
        channel: u8,
    ) -> Result<()> {
        self.runtime
            .run(
                self.inner
                    .send_bytes_batch_with_order_channel(messages, reliability, channel),
            )
    }
    /// Synchronous adapter for [`crate::RaknetSocket::recv`].
    pub fn recv(&self) -> Result<Vec<u8>> {
        self.runtime.run(self.inner.recv())
    }
    /// Wait at most `duration`. Already queued data is not withdrawn on timeout.
    pub fn recv_timeout(&self, duration: Duration) -> Result<Vec<u8>> {
        self.runtime.run_timeout(duration, self.inner.recv())
    }
    /// Synchronous adapter for [`crate::RaknetSocket::recv_bytes`].
    pub fn recv_bytes(&self) -> Result<Bytes> {
        self.runtime.run(self.inner.recv_bytes())
    }
    /// Wait at most `duration`. Already queued data is not withdrawn on timeout.
    pub fn recv_bytes_timeout(&self, duration: Duration) -> Result<Bytes> {
        self.runtime.run_timeout(duration, self.inner.recv_bytes())
    }
    /// Synchronous adapter for [`crate::RaknetSocket::recv_batch`].
    pub fn recv_batch(&self, output: &mut Vec<Vec<u8>>, limit: usize) -> Result<usize> {
        self.runtime.run(self.inner.recv_batch(output, limit))
    }
    /// Wait at most `duration`. Already queued data is not withdrawn on timeout.
    pub fn recv_batch_timeout(
        &self,
        output: &mut Vec<Vec<u8>>,
        limit: usize,
        duration: Duration,
    ) -> Result<usize> {
        self.runtime
            .run_timeout(duration, self.inner.recv_batch(output, limit))
    }
    /// Synchronous adapter for [`crate::RaknetSocket::recv_bytes_batch`].
    pub fn recv_bytes_batch(&self, output: &mut Vec<Bytes>, limit: usize) -> Result<usize> {
        self.runtime.run(self.inner.recv_bytes_batch(output, limit))
    }
    /// Synchronous adapter for [`crate::RaknetSocket::flush`].
    pub fn flush(&self) -> Result<()> {
        self.runtime.run(self.inner.flush())
    }
    /// Wait at most `duration`. Already queued data is not withdrawn on timeout.
    pub fn flush_timeout(&self, duration: Duration) -> Result<()> {
        self.runtime.run_timeout(duration, self.inner.flush())
    }
    /// Synchronous adapter for [`crate::RaknetSocket::close`].
    pub fn close(&self) -> Result<()> {
        self.runtime.run(self.inner.close())
    }
    /// Return the underlying connection metadata.
    pub fn peer_addr(&self) -> Result<SocketAddr> {
        self.inner.peer_addr().map_err(Error::Raknet)
    }
    /// Return the underlying connection metadata.
    pub fn local_addr(&self) -> Result<SocketAddr> {
        self.inner.local_addr().map_err(Error::Raknet)
    }
    /// Return the underlying connection metadata.
    pub fn raknet_version(&self) -> Result<u8> {
        self.inner.raknet_version().map_err(Error::Raknet)
    }
    /// Return the negotiated MTU.
    pub fn mtu(&self) -> u16 {
        self.inner.mtu()
    }
    /// Configure idle maintenance without blocking.
    pub fn set_idle_maintenance(&self, enabled: bool) {
        self.inner.set_idle_maintenance(enabled);
    }
    /// Apply optional per-connection policy through the shared runtime.
    #[cfg(feature = "send-policy")]
    pub fn set_send_options(&self, options: crate::SendOptions) -> Result<()> {
        self.runtime.run(self.inner.set_send_options(options))
    }
    /// Read the current per-connection policy.
    #[cfg(feature = "send-policy")]
    pub fn send_options(&self) -> Result<Option<crate::SendOptions>> {
        self.runtime
            .run(async { Ok(self.inner.send_options().await) })
    }
    /// Apply optional per-connection policy through the shared runtime.
    #[cfg(feature = "recovery-policy")]
    pub fn set_recovery_options(&self, options: crate::RecoveryOptions) -> Result<()> {
        self.runtime.run(self.inner.set_recovery_options(options))
    }
    /// Read the current per-connection policy.
    #[cfg(feature = "recovery-policy")]
    pub fn recovery_options(&self) -> Result<crate::RecoveryOptions> {
        self.runtime
            .run(async { Ok(self.inner.recovery_options().await) })
    }
}

/// Synchronous listener. Accepted sockets retain the same runtime owner.
pub struct RaknetListener {
    inner: crate::RaknetListener,
    runtime: RaknetRuntime,
}
impl RaknetListener {
    /// Configure the backlog before listening.
    pub fn with_accept_backlog(mut self, backlog: NonZeroUsize) -> Self {
        self.inner = self.inner.with_accept_backlog(backlog);
        self
    }
    /// Configure idle maintenance before listening.
    pub fn with_idle_maintenance(mut self, enabled: bool) -> Self {
        self.inner = self.inner.with_idle_maintenance(enabled);
        self
    }
    /// Configure receive batching before listening.
    #[cfg(target_os = "linux")]
    pub fn with_receive_batching(mut self, enabled: bool) -> Self {
        self.inner = self.inner.with_receive_batching(enabled);
        self
    }
    /// Start accepting incoming handshakes.
    pub fn listen(&mut self) -> Result<()> {
        self.runtime.run(async {
            self.inner.listen().await;
            Ok(())
        })
    }
    /// Wait for the next connection.
    pub fn accept(&mut self) -> Result<RaknetSocket> {
        let inner = self.runtime.run(self.inner.accept())?;
        Ok(RaknetSocket {
            inner,
            runtime: self.runtime.clone(),
        })
    }
    /// Wait for a connection for at most `duration`.
    pub fn accept_timeout(&mut self, duration: Duration) -> Result<RaknetSocket> {
        let inner = self.runtime.run_timeout(duration, self.inner.accept())?;
        Ok(RaknetSocket {
            inner,
            runtime: self.runtime.clone(),
        })
    }
    /// Close the listener and its connections, waiting for background cleanup.
    pub fn close(&mut self) -> Result<()> {
        self.runtime.run(self.inner.close())
    }
    /// Bound listener address.
    pub fn local_addr(&self) -> Result<SocketAddr> {
        self.inner.local_addr().map_err(Error::Raknet)
    }
    /// Server GUID.
    pub fn get_guid(&self) -> u64 {
        self.inner.get_guid()
    }
    /// Set the complete discovery response.
    pub fn set_full_motd(&mut self, motd: String) -> Result<()> {
        self.runtime.run(self.inner.set_full_motd(motd))
    }
    /// Read the discovery response.
    pub fn get_motd(&self) -> Result<String> {
        self.runtime.run(async { Ok(self.inner.get_motd().await) })
    }
    /// Read a peer's negotiated protocol version.
    pub fn get_peer_raknet_version(&self, peer: &SocketAddr) -> Result<u8> {
        self.runtime.run(self.inner.get_peer_raknet_version(peer))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn sockets_share_workers_and_receive_progress_needs_no_block_on_call() {
        let runtime = RaknetRuntime::new().unwrap();
        let mut listener = runtime.bind(&"127.0.0.1:0".parse().unwrap()).unwrap();
        listener.listen().unwrap();
        let client = runtime
            .connect_timeout(&listener.local_addr().unwrap(), Duration::from_secs(10))
            .unwrap();
        let server = Arc::new(listener.accept_timeout(Duration::from_secs(10)).unwrap());
        assert!(Arc::ptr_eq(&runtime.inner, &client.runtime.inner));
        assert!(Arc::ptr_eq(&runtime.inner, &server.runtime.inner));
        let (delivered, received) = std::sync::mpsc::channel();
        let receiving = server.clone();
        runtime.inner.0.as_ref().unwrap().spawn(async move {
            delivered.send(receiving.inner.recv().await).unwrap();
        });
        drop(runtime);
        client
            .send(&[0xfe, 7], Reliability::ReliableOrdered)
            .unwrap();
        // Waiting on a standard channel never enters Tokio. Delivery must
        // progress on the shared runtime's background worker alone.
        assert_eq!(
            received
                .recv_timeout(Duration::from_secs(5))
                .unwrap()
                .unwrap(),
            [0xfe, 7]
        );
        client.flush_timeout(Duration::from_secs(5)).unwrap();
        listener.close().unwrap();
    }

    #[test]
    fn flush_timeout_does_not_withdraw_data_and_loss_recovery_keeps_running() {
        let runtime = RaknetRuntime::new().unwrap();
        let mut listener = runtime.bind(&"127.0.0.1:0".parse().unwrap()).unwrap();
        listener.listen().unwrap();
        let client = runtime
            .connect_timeout(&listener.local_addr().unwrap(), Duration::from_secs(10))
            .unwrap();
        let mut server = listener.accept_timeout(Duration::from_secs(10)).unwrap();
        // Discard all server-to-client packets, including reliable ACKs.
        server.inner.set_loss_rate(10);
        let payload = vec![0xfe; 4096];
        client.send(&payload, Reliability::ReliableOrdered).unwrap();
        assert_eq!(
            server.recv_timeout(Duration::from_secs(10)).unwrap(),
            payload
        );
        assert!(matches!(
            client.flush_timeout(Duration::from_millis(30)),
            Err(Error::Timeout)
        ));
        server.inner.set_loss_rate(0);
        // The queued data survives cancellation and background retries recover
        // the missing ACK without another application send.
        client.flush_timeout(Duration::from_secs(10)).unwrap();
        assert!(
            matches!(
                server.recv_timeout(Duration::from_millis(50)),
                Err(Error::Timeout)
            ),
            "reliable retransmission was delivered to the application twice"
        );
        listener.close().unwrap();
    }
}