Skip to content

Commit

Permalink
Don't error when calling listen again.
Browse files Browse the repository at this point in the history
Provided the local endpoint we are listening to is the same, it is safe
to call `listen` multiple times.
  • Loading branch information
MabezDev committed Sep 16, 2023
1 parent 28a5dd1 commit c8fe32f
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/socket/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,7 @@ impl<'a> Socket<'a> {

/// Start listening on the given endpoint.
///
/// This function returns `Err(Error::Illegal)` if the socket was already open
/// This function returns `Err(Error::InvalidState)` if the socket was already open
/// (see [is_open](#method.is_open)), and `Err(Error::Unaddressable)`
/// if the port in the given endpoint is zero.
pub fn listen<T>(&mut self, local_endpoint: T) -> Result<(), ListenError>
Expand All @@ -757,7 +757,12 @@ impl<'a> Socket<'a> {
}

if self.is_open() {
return Err(ListenError::InvalidState);
// If we were already listening, to same endpoint, there is nothing to do.
if matches!(self.state, State::Listen) && self.listen_endpoint == local_endpoint {
return Ok(());
} else {
return Err(ListenError::InvalidState);
}
}

self.reset();
Expand Down Expand Up @@ -2911,6 +2916,9 @@ mod test {
fn test_listen_twice() {
let mut s = socket();
assert_eq!(s.listen(80), Ok(()));
// multiple calls to listen are okay if its the same local endpoint and the state is still in listening
assert_eq!(s.listen(80), Ok(()));
s.set_state(State::SynReceived); // state change, simulate incoming connection
assert_eq!(s.listen(80), Err(ListenError::InvalidState));
}

Expand Down

0 comments on commit c8fe32f

Please sign in to comment.