Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding UDP socket close functionality #482

Merged
merged 6 commits into from
Jun 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]

- 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

Expand Down
27 changes: 27 additions & 0 deletions src/socket/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,22 @@ impl<'a> UdpSocket<'a> {
Ok(())
}

/// Close the socket.
pub fn close(&mut self) {
// Clear the bound endpoint of the socket.
self.endpoint = IpEndpoint::default();
ryan-summers marked this conversation as resolved.
Show resolved Hide resolved

// Reset the RX and TX buffers of the socket.
self.tx_buffer.reset();
self.rx_buffer.reset();

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

/// Check whether the socket is open.
#[inline]
pub fn is_open(&self) -> bool {
Expand Down Expand Up @@ -624,4 +640,15 @@ mod test {
assert_eq!(socket.process(&remote_ip_repr(), &repr, &[]), Ok(()));
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());
}
}
21 changes: 21 additions & 0 deletions src/storage/packet_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,13 @@ impl<'a, H> PacketBuffer<'a, H> {
pub fn payload_capacity(&self) -> usize {
self.payload_ring.capacity()
}

/// Reset the packet buffer and clear any staged.
#[allow(unused)]
pub(crate) fn reset(&mut self) {
self.payload_ring.clear();
self.metadata_ring.clear();
}
}

#[cfg(test)]
Expand Down Expand Up @@ -304,4 +311,18 @@ mod test {
assert!(buffer.dequeue().is_ok());
assert!(buffer.enqueue(5, ()).is_ok());
}

#[test]
fn clear() {
let mut buffer = buffer();

// Ensure enqueuing data in teh buffer fills it somewhat.
assert!(buffer.is_empty());
assert!(buffer.enqueue(6, ()).is_ok());

// Ensure that resetting the buffer causes it to be empty.
assert!(!buffer.is_empty());
buffer.reset();
assert!(buffer.is_empty());
}
}