ソースを参照

Add a TCP data source endpoint to the server example.

whitequark 7 年 前
コミット
49ed4ae5f3
2 ファイル変更30 行追加4 行削除
  1. 6 4
      README.md
  2. 24 0
      examples/server.rs

+ 6 - 4
README.md

@@ -201,12 +201,14 @@ It responds to:
   * pings (`ping 192.168.69.1`);
   * UDP packets on port 6969 (`socat stdio udp4-connect:192.168.69.1:6969 <<<"abcdefg"`),
     where it will respond "hello" to any incoming packet;
-  * TCP packets on port 6969 (`socat stdio tcp4-connect:192.168.69.1:6969`),
+  * TCP connections on port 6969 (`socat stdio tcp4-connect:192.168.69.1:6969`),
     where it will respond "hello" to any incoming connection and immediately close it;
-  * TCP packets on port 6970 (`socat stdio tcp4-connect:192.168.69.1:6970 <<<"abcdefg"`),
+  * TCP connections on port 6970 (`socat stdio tcp4-connect:192.168.69.1:6970 <<<"abcdefg"`),
     where it will respond with reversed chunks of the input indefinitely.
-  * TCP packets on port 6971 (`cat /dev/urandom | socat stdio tcp4-connect:192.168.69.1:6971`),
-    which will be ignored.
+  * TCP connections on port 6971 (`socat stdio tcp4-connect:192.168.69.1:6971 </dev/urandom`),
+    which will sink data.
+  * TCP connections on port 6972 (`socat stdio tcp4-connect:192.168.69.1:6972 >/dev/null`),
+    which will source data.
 
 Except for the socket on port 6971. the buffers are only 64 bytes long, for convenience
 of testing resource exhaustion conditions.

+ 24 - 0
examples/server.rs

@@ -49,6 +49,10 @@ fn main() {
     let tcp3_tx_buffer = TcpSocketBuffer::new(vec![0; 65535]);
     let tcp3_socket = TcpSocket::new(tcp3_rx_buffer, tcp3_tx_buffer);
 
+    let tcp4_rx_buffer = TcpSocketBuffer::new(vec![0; 65535]);
+    let tcp4_tx_buffer = TcpSocketBuffer::new(vec![0; 65535]);
+    let tcp4_socket = TcpSocket::new(tcp4_rx_buffer, tcp4_tx_buffer);
+
     let hardware_addr  = EthernetAddress([0x02, 0x00, 0x00, 0x00, 0x00, 0x01]);
     let protocol_addrs = [IpAddress::v4(192, 168, 69, 1)];
     let mut iface      = EthernetInterface::new(
@@ -60,6 +64,7 @@ fn main() {
     let tcp1_handle = sockets.add(tcp1_socket);
     let tcp2_handle = sockets.add(tcp2_socket);
     let tcp3_handle = sockets.add(tcp3_socket);
+    let tcp4_handle = sockets.add(tcp4_socket);
 
     let mut tcp_6970_active = false;
     loop {
@@ -156,6 +161,25 @@ fn main() {
             }
         }
 
+        // tcp:6972: fountain
+        {
+            let socket: &mut TcpSocket = sockets.get_mut(tcp4_handle).as_socket();
+            if !socket.is_open() {
+                socket.listen(6972).unwrap()
+            }
+
+            if socket.may_send() {
+                if let Ok(data) = socket.send(65535) {
+                    if data.len() > 0 {
+                        debug!("tcp:6972 send {:?} octets", data.len());
+                        for (i, b) in data.iter_mut().enumerate() {
+                            *b = (i % 256) as u8;
+                        }
+                    }
+                }
+            }
+        }
+
         let timestamp = utils::millis_since(startup_time);
         let poll_at = iface.poll(&mut sockets, timestamp).expect("poll error");
         phy_wait(fd, poll_at).expect("wait error");