Skip to content

Commit

Permalink
creating AddrIncoming from an existed TcpListener
Browse files Browse the repository at this point in the history
  • Loading branch information
zonyitoo authored Feb 18, 2021
1 parent 4258705 commit 5f24075
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/server/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,19 @@ impl AddrIncoming {
pub fn bind(addr: &SocketAddr) -> crate::Result<Self> {
AddrIncoming::new(addr)
}

/// Creates a new `AddrIncoming` from an existed `TcpListener`
pub fn from_listener(listener: TcpListener) -> crate::Result<Self> {
let addr = listener.local_addr().map_err(crate::Error::new_listen)?;
Ok(AddrIncoming {
listener,
addr,
sleep_on_errors: true,
tcp_keepalive_timeout: None,
tcp_nodelay: false,
timeout: None,
})
}

/// Get the local address bound to this listener.
pub fn local_addr(&self) -> SocketAddr {
Expand Down

0 comments on commit 5f24075

Please sign in to comment.