Procházet zdrojové kódy

Updating UDP close to clear RX/TX buffers

Ryan Summers před 3 roky
rodič
revize
4749c328b1
2 změnil soubory, kde provedl 31 přidání a 0 odebrání
  1. 11 0
      src/socket/udp.rs
  2. 20 0
      src/storage/packet_buffer.rs

+ 11 - 0
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.

+ 20 - 0
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());
+    }
 }