Quellcode durchsuchen

Add TcpSocket::is_connected().

whitequark vor 8 Jahren
Ursprung
Commit
dc9ac44ba7
2 geänderte Dateien mit 31 neuen und 1 gelöschten Zeilen
  1. 8 0
      examples/smoltcpserver.rs
  2. 23 1
      src/socket/tcp.rs

+ 8 - 0
examples/smoltcpserver.rs

@@ -65,6 +65,7 @@ fn main() {
     let mut iface = EthernetInterface::new(device, arp_cache,
         hardware_addr, protocol_addrs, sockets);
 
+    let mut tcp_6969_connected = false;
     loop {
         match iface.poll() {
             Ok(()) => (),
@@ -97,6 +98,13 @@ fn main() {
 
         {
             let socket: &mut TcpSocket = iface.sockets()[1].as_socket();
+            if socket.is_connected() && !tcp_6969_connected {
+                debug!("tcp connected");
+            } else if !socket.is_connected() && tcp_6969_connected {
+                debug!("tcp disconnected");
+            }
+            tcp_6969_connected = socket.is_connected();
+
             if socket.can_recv() {
                 let data = {
                     let mut data = socket.recv(128).unwrap().to_owned();

+ 23 - 1
src/socket/tcp.rs

@@ -254,7 +254,7 @@ impl<'a> TcpSocket<'a> {
         Ok(())
     }
 
-    /// Return whether the connection is open.
+    /// Return whether the socket is open.
     ///
     /// This function returns true if the socket will process incoming or dispatch outgoing
     /// packets. Note that this does not mean that it is possible to send or receive data through
@@ -267,6 +267,24 @@ impl<'a> TcpSocket<'a> {
         }
     }
 
+    /// Return whether a connection is established.
+    ///
+    /// This function returns true if the socket is actively exchanging packets with
+    /// a remote endpoint. Note that this does not mean that it is possible to send or receive
+    /// data through the socket; for that, use [can_send](#method.can_send) or
+    /// [can_recv](#method.can_recv).
+    ///
+    /// If a connection is established, [abort](#method.close) will send a reset to
+    /// the remote endpoint.
+    pub fn is_connected(&self) -> bool {
+        match self.state {
+            State::Closed => false,
+            State::TimeWait => false,
+            State::Listen => false,
+            _ => true
+        }
+    }
+
     /// Return whether the transmit half of the full-duplex connection is open.
     ///
     /// This function returns true if it's possible to send data and have it arrive
@@ -491,6 +509,8 @@ impl<'a> TcpSocket<'a> {
 
             // RSTs in SYN_RECEIVED flip the socket back to the LISTEN state.
             (State::SynReceived, TcpRepr { control: TcpControl::Rst, .. }) => {
+                net_trace!("tcp:{}:{}: received RST",
+                           self.local_endpoint, self.remote_endpoint);
                 self.local_endpoint.addr = self.listen_address;
                 self.remote_endpoint     = IpEndpoint::default();
                 self.set_state(State::Listen);
@@ -499,6 +519,8 @@ impl<'a> TcpSocket<'a> {
 
             // RSTs in any other state close the socket.
             (_, TcpRepr { control: TcpControl::Rst, .. }) => {
+                net_trace!("tcp:{}:{}: received RST",
+                           self.local_endpoint, self.remote_endpoint);
                 self.local_endpoint  = IpEndpoint::default();
                 self.remote_endpoint = IpEndpoint::default();
                 self.set_state(State::Closed);