Skip to content

Commit

Permalink
TCP support SO_REUSEADDR
Browse files Browse the repository at this point in the history
  • Loading branch information
baiqiaosen committed Aug 6, 2024
1 parent b7134a3 commit f96b759
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/socket/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ pub enum ListenError {
Unaddressable,
}

/// Error returned by [`Socket::bind`]
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum BindError {
InvalidState,
Unaddressable,
}

impl Display for ListenError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Expand Down Expand Up @@ -770,6 +778,38 @@ impl<'a> Socket<'a> {
Some(self.tuple?.remote)
}

/// get listen endpoint.
#[inline]
pub fn get_listen_endpoint(&self) -> IpListenEndpoint {
self.listen_endpoint
}

/// Bind the socket to the given endpoint.
///
/// This function returns `Err(Error::Illegal)` if the socket was open
/// (see [is_open](#method.is_open)), and `Err(Error::Unaddressable)`
/// if the port in the given endpoint is zero.
pub fn bind<T: Into<IpListenEndpoint>>(&mut self, endpoint: T) -> Result<(), BindError> {
let endpoint = endpoint.into();
if endpoint.port == 0 {
return Err(BindError::Unaddressable);
}

if self.is_open() {
return Err(BindError::InvalidState);
}

self.listen_endpoint = endpoint;

#[cfg(feature = "async")]
{
self.rx_waker.wake();
self.tx_waker.wake();
}

Ok(())
}

/// Return the connection state, in terms of the TCP state machine.
#[inline]
pub fn state(&self) -> State {
Expand Down

0 comments on commit f96b759

Please sign in to comment.