insim/
lib.rs

1#![doc = include_str!("../README.md")]
2#![cfg_attr(test, deny(warnings, unreachable_pub))]
3#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
4
5#[cfg(any(feature = "blocking", feature = "tokio"))]
6use std::net::SocketAddr;
7
8#[macro_use]
9mod macros;
10
11#[cfg(any(feature = "blocking", feature = "tokio"))]
12pub mod address;
13#[cfg(any(feature = "blocking", feature = "tokio"))]
14pub mod builder;
15#[doc(hidden)]
16pub mod error;
17pub mod identifiers;
18pub mod insim;
19pub mod net;
20pub mod packet;
21#[doc(hidden)]
22pub mod result;
23
24/// The Insim Protocol Version Number supported by this library
25pub const VERSION: u8 = 9;
26
27/// Why 255 * 4? Because the size of a packet is a u8, with a max byte size of 255.
28/// In "compressed" mode the raw size is multiplied by 4.
29pub(crate) const MAX_SIZE_PACKET: usize = (u8::MAX as usize) * 4;
30
31pub(crate) const DEFAULT_BUFFER_CAPACITY: usize = MAX_SIZE_PACKET * 6;
32
33pub use error::Error;
34/// Rexport insim_core
35pub use insim_core as core;
36pub use packet::{Packet, WithRequestId};
37pub use result::Result;
38
39/// Shortcut method to create a TCP connection
40///
41/// # Examples
42///
43/// Supports both blocking and tokio. Swap about `connect_async` for `connect` and remove the
44/// `.await` annotations.
45///
46/// ```rust
47/// let conn = insim::tcp("127.0.0.1:29999").connect_async().await?;
48/// loop {
49///     let packet = conn.read().await?;
50///     println!("{:?}", packet);
51/// }
52/// ```
53#[cfg(any(feature = "blocking", feature = "tokio"))]
54pub fn tcp<R: Into<address::Addr>>(remote_addr: R) -> builder::Builder {
55    builder::Builder::default().tcp(remote_addr)
56}
57
58/// Shortcut method to create a UDP connection.
59/// If local_addr is not provided then we will bind to "0.0.0.0:0" (all addresses, random port).
60///
61/// # Examples
62///
63/// Supports both blocking and tokio. Swap about `connect_async` for `connect` and remove the
64/// `.await` annotations.
65///
66/// ```rust
67/// let conn = insim::udp("127.0.0.1:29999", None).connect_async().await?;
68/// loop {
69///     let packet = conn.read().await?;
70///     println!("{:?}", packet);
71/// }
72/// ```
73#[cfg(any(feature = "blocking", feature = "tokio"))]
74pub fn udp<L: Into<Option<SocketAddr>>, R: Into<address::Addr>>(
75    remote_addr: R,
76    local_addr: L,
77) -> builder::Builder {
78    builder::Builder::default().udp(remote_addr, local_addr)
79}