|
@@ -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();
|
|
|
+
|
|
|
+ // 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 {
|
|
@@ -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());
|
|
|
+ }
|
|
|
}
|