//! Networking primitives for TCP / UDP communication.

cfg_select! {
    docsrs => {
        pub mod general;
    }
    _ => {
        pub(crate) mod general;
    }
}

cfg_select! {
    unix => {
        pub use socket2::{Domain, SockAddr};

        #[cfg_attr(docsrs, doc(auto_cfg = false))]
        pub use self::general::unix::{
            OwnedReadHalf, OwnedWriteHalf, TcpListener, TcpSocket, TcpStream, UdpSocket,
        };
    }
    windows => {
        pub use socket2::{Domain, SockAddr};

        #[cfg_attr(docsrs, doc(auto_cfg = false))]
        pub use self::general::windows::{
            OwnedReadHalf, OwnedWriteHalf, TcpListener, TcpSocket, TcpStream, UdpSocket,
        };
    }
}

/// Define the `listen` backlog parameters as in the standard library. This
/// helps avoid hardcoded unsynchronized values and allows better control of
/// default values depending on the target.
///
/// Selecting a “valid” default value can be tricky due to:
///
/// - It often serves only as a hint and may be rounded, trimmed, or ignored by
///   the OS.
/// - It is sometimes provided as a "magic" value, for example, -1. This value
///   is undocumented and not standard, but it is often used to represent the
///   largest possible backlog size. This happens due to signed/unsigned
///   conversion and rounding to the upper bound performed by the OS.
/// - Default values vary depending on the OS and its version. Common defaults
///   include: -1, 128, 1024, and 4096.
///
/// Currently, the default backlog size is set as follows:
///
/// - `linux`: -1 (capped to `/proc/sys/net/core/somaxconn`).
/// - `freebsd`: -1 (capped to `kern.ipc.soacceptqueue`).
/// - `openbsd`: -1 (capped to `kern.somaxconn sysctl`).
/// - `apple`: -1 (capped to the default 128).
/// - `windows`: 128
/// - `redox`: 128
/// - `espidf`: 128
/// - `horizon`: 128
/// - `hermit`: 1024.
/// - Other targets: `libc::SOMAXCONN`.
pub const DEFAULT_LISTEN_BACKLOG_SIZE: i32 = cfg_select! {
    any(
        target_os = "linux",
        target_os = "freebsd",
        target_os = "openbsd",
        target_vendor = "apple",
    ) => -1,
    any(
        target_os = "windows",
        target_os = "redox",
        target_os = "espidf",
        target_os = "horizon"
    ) => 128,
    target_os = "hermit" => 1024,
    _ => libc::SOMAXCONN,
};
