diff --git a/src/socket/udp.rs b/src/socket/udp.rs index 34071f0..f39b0dd 100644 --- a/src/socket/udp.rs +++ b/src/socket/udp.rs @@ -148,7 +148,18 @@ impl<'a> UdpSocket<'a> { /// Close the socket. pub fn close(&mut self) { + // Clear the bound endpoint of the socket. self.endpoint = IpEndpoint::default(); + + // 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. diff --git a/src/storage/packet_buffer.rs b/src/storage/packet_buffer.rs index 5502de2..8ad7637 100644 --- a/src/storage/packet_buffer.rs +++ b/src/storage/packet_buffer.rs @@ -179,6 +179,12 @@ impl<'a, H> PacketBuffer<'a, H> { pub fn payload_capacity(&self) -> usize { self.payload_ring.capacity() } + + /// Reset the packet buffer and clear any staged. + pub(crate) fn reset(&mut self) { + self.payload_ring.clear(); + self.metadata_ring.clear(); + } } #[cfg(test)] @@ -304,4 +310,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()); + } }