|
| 1 | +/// Config flags for the underlying socket of a client. |
| 2 | +pub struct SocketConfig { |
| 3 | + /// Whether the socket should be blocking. |
| 4 | + pub blocking: Option<bool>, |
| 5 | + /// The read timeout for the socket. |
| 6 | + pub read_timeout: Option<std::time::Duration>, |
| 7 | + /// The write timeout for the socket. |
| 8 | + pub write_timeout: Option<std::time::Duration>, |
| 9 | + /// The time-to-live for the socket. |
| 10 | + pub ttl: Option<u32>, |
| 11 | + /// Whether the socket should have the Nagle algorithm disabled |
| 12 | + pub nodelay: Option<bool>, |
| 13 | +} |
| 14 | + |
| 15 | +impl Default for SocketConfig { |
| 16 | + fn default() -> Self { |
| 17 | + Self { |
| 18 | + blocking: None, |
| 19 | + read_timeout: None, |
| 20 | + write_timeout: None, |
| 21 | + ttl: None, |
| 22 | + nodelay: None, |
| 23 | + } |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +impl SocketConfig { |
| 28 | + /// Creates a new `SocketConfig` with all fields set to `None`. |
| 29 | + /// This is equivalent to `SocketConfig::default()`. |
| 30 | + pub fn new() -> Self { |
| 31 | + Self::default() |
| 32 | + } |
| 33 | + |
| 34 | + /// Applies the configuration to the given socket. Any fields that are `None` are ignored. |
| 35 | + pub fn apply_stream(&self, socket: &std::net::TcpStream) -> std::io::Result<()> { |
| 36 | + if let Some(blocking) = self.blocking { |
| 37 | + socket.set_nonblocking(!blocking)?; |
| 38 | + } |
| 39 | + if let Some(read_timeout) = self.read_timeout { |
| 40 | + socket.set_read_timeout(Some(read_timeout))?; |
| 41 | + } |
| 42 | + if let Some(write_timeout) = self.write_timeout { |
| 43 | + socket.set_write_timeout(Some(write_timeout))?; |
| 44 | + } |
| 45 | + if let Some(ttl) = self.ttl { |
| 46 | + socket.set_ttl(ttl)?; |
| 47 | + } |
| 48 | + if let Some(nodelay) = self.nodelay { |
| 49 | + socket.set_nodelay(nodelay)?; |
| 50 | + } |
| 51 | + Ok(()) |
| 52 | + } |
| 53 | + |
| 54 | + /// Applies the configuration to the given listener. Any fields that are `None` are ignored. |
| 55 | + pub fn apply_listener(&self, listener: &std::net::TcpListener) -> std::io::Result<()> { |
| 56 | + if let Some(blocking) = self.blocking { |
| 57 | + listener.set_nonblocking(!blocking)?; |
| 58 | + } |
| 59 | + if let Some(ttl) = self.ttl { |
| 60 | + listener.set_ttl(ttl)?; |
| 61 | + } |
| 62 | + Ok(()) |
| 63 | + } |
| 64 | + |
| 65 | + /// Sets the blocking flag for the socket. |
| 66 | + pub fn blocking(mut self, blocking: bool) -> Self { |
| 67 | + self.blocking = Some(blocking); |
| 68 | + self |
| 69 | + } |
| 70 | + |
| 71 | + /// Sets the read timeout for the socket. |
| 72 | + pub fn read_timeout(mut self, read_timeout: std::time::Duration) -> Self { |
| 73 | + self.read_timeout = Some(read_timeout); |
| 74 | + self |
| 75 | + } |
| 76 | + |
| 77 | + /// Sets the write timeout for the socket. |
| 78 | + pub fn write_timeout(mut self, write_timeout: std::time::Duration) -> Self { |
| 79 | + self.write_timeout = Some(write_timeout); |
| 80 | + self |
| 81 | + } |
| 82 | + |
| 83 | + /// Sets the time-to-live for the socket. |
| 84 | + pub fn ttl(mut self, ttl: u32) -> Self { |
| 85 | + self.ttl = Some(ttl); |
| 86 | + self |
| 87 | + } |
| 88 | + |
| 89 | + /// Sets the nodelay flag for the socket. |
| 90 | + pub fn nodelay(mut self, nodelay: bool) -> Self { |
| 91 | + self.nodelay = Some(nodelay); |
| 92 | + self |
| 93 | + } |
| 94 | +} |
0 commit comments