Skip to main content

toe_beans/v4/message/socket/
config.rs

1use std::net::SocketAddrV4;
2
3/// All configuration for a `Socket`.
4#[derive(Debug)]
5#[cfg_attr(
6    feature = "v4_server",
7    derive(serde::Serialize, serde::Deserialize),
8    serde(default = "SocketConfig::default_server")
9)]
10#[cfg_attr(feature = "v4_client", derive(clap::Args))]
11pub struct SocketConfig {
12    /// The socket will bind to this address and port when calling `Socket::new`.
13    #[cfg_attr(feature = "v4_client", arg(long, default_value_t = Self::default_client().listen_address))]
14    pub listen_address: SocketAddrV4,
15    /// The name of the network interface to broadcast to.
16    /// Currently only takes one interface.
17    #[cfg_attr(feature = "v4_client", arg(long))]
18    pub interface: Option<String>,
19}
20
21impl SocketConfig {
22    /// Similar to a `Default` impl, but specifically for use with `Server`.
23    /// Uses the `RESOLVED_SERVER_PORT`.
24    #[cfg(feature = "v4_server")]
25    pub fn default_server() -> Self {
26        use std::net::Ipv4Addr;
27
28        SocketConfig {
29            listen_address: SocketAddrV4::new(
30                Ipv4Addr::new(0, 0, 0, 0),
31                crate::v4::RESOLVED_SERVER_PORT,
32            ),
33            interface: None,
34        }
35    }
36
37    /// Similar to a `Default` impl, but specifically for use with `Client`.
38    /// Uses the `RESOLVED_CLIENT_PORT`.
39    #[cfg(feature = "v4_client")]
40    pub fn default_client() -> Self {
41        use std::net::Ipv4Addr;
42
43        SocketConfig {
44            listen_address: SocketAddrV4::new(
45                Ipv4Addr::new(0, 0, 0, 0),
46                crate::v4::RESOLVED_CLIENT_PORT,
47            ),
48            interface: None,
49        }
50    }
51}