浏览代码

Try to get TCP state query methods into a saner state.

whitequark 8 年之前
父节点
当前提交
b860505775
共有 2 个文件被更改,包括 14 次插入6 次删除
  1. 4 4
      examples/server.rs
  2. 10 2
      src/socket/tcp.rs

+ 4 - 4
examples/server.rs

@@ -90,7 +90,7 @@ fn main() {
     let tcp1_handle = sockets.add(tcp1_socket);
     let tcp2_handle = sockets.add(tcp2_socket);
 
-    let mut tcp_6969_connected = false;
+    let mut tcp_6969_active = false;
     loop {
         // udp:6969: respond "yo dawg"
         {
@@ -139,12 +139,12 @@ fn main() {
                 socket.listen(6970).unwrap()
             }
 
-            if socket.is_connected() && !tcp_6969_connected {
+            if socket.is_active() && !tcp_6969_active {
                 debug!("tcp:6970 connected");
-            } else if !socket.is_connected() && tcp_6969_connected {
+            } else if !socket.is_active() && tcp_6969_active {
                 debug!("tcp:6970 disconnected");
             }
-            tcp_6969_connected = socket.is_connected();
+            tcp_6969_active = socket.is_active();
 
             if socket.may_recv() {
                 let data = {

+ 10 - 2
src/socket/tcp.rs

@@ -344,7 +344,15 @@ impl<'a> TcpSocket<'a> {
         }
     }
 
-    /// Return whether a connection is established.
+    /// Return whether the socket is passively listening for incoming connections.
+    pub fn is_listening(&self) -> bool {
+        match self.state {
+            State::Listen => true,
+            _ => false
+        }
+    }
+
+    /// Return whether a connection is active.
     ///
     /// 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
@@ -353,7 +361,7 @@ impl<'a> TcpSocket<'a> {
     ///
     /// If a connection is established, [abort](#method.close) will send a reset to
     /// the remote endpoint.
-    pub fn is_connected(&self) -> bool {
+    pub fn is_active(&self) -> bool {
         match self.state {
             State::Closed => false,
             State::TimeWait => false,