Adding UDP socket close funcionality

This commit is contained in:
Ryan Summers 2021-05-31 14:25:52 +02:00
parent 03429a8b4e
commit 108543a2f6
2 changed files with 17 additions and 0 deletions

View File

@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased] ## [Unreleased]
- Update `managed` from 0.7 to 0.8 ([442](https://github.com/smoltcp-rs/smoltcp/pull/442)) - Update `managed` from 0.7 to 0.8 ([442](https://github.com/smoltcp-rs/smoltcp/pull/442))
- udp: Add `close()` method to unbind socket.
## [0.7.3] - 2021-05-29 ## [0.7.3] - 2021-05-29

View File

@ -146,6 +146,11 @@ impl<'a> UdpSocket<'a> {
Ok(()) Ok(())
} }
/// Close the socket.
pub fn close(&mut self) {
self.endpoint = IpEndpoint::default();
}
/// Check whether the socket is open. /// Check whether the socket is open.
#[inline] #[inline]
pub fn is_open(&self) -> bool { pub fn is_open(&self) -> bool {
@ -624,4 +629,15 @@ mod test {
assert_eq!(socket.process(&remote_ip_repr(), &repr, &[]), Ok(())); assert_eq!(socket.process(&remote_ip_repr(), &repr, &[]), Ok(()));
assert_eq!(socket.recv(), Ok((&[][..], REMOTE_END))); assert_eq!(socket.recv(), Ok((&[][..], REMOTE_END)));
} }
#[test]
fn test_closing() {
let recv_buffer = UdpSocketBuffer::new(vec![UdpPacketMetadata::EMPTY; 1], vec![]);
let mut socket = socket(recv_buffer, buffer(0));
assert_eq!(socket.bind(LOCAL_PORT), Ok(()));
assert!(socket.is_open());
socket.close();
assert!(!socket.is_open());
}
} }