|
3 | 3 |
|
4 | 4 | //! For build and run instructions, see README.md
|
5 | 5 | //!
|
6 |
| -//! This example starts a TCP listening server that should transmit `Hello` to |
7 |
| -//! any connecting client, and then close the connection. |
| 6 | +//! This example starts a TCP listening server at the address 10.0.0.1/24, on port 80, that |
| 7 | +//! should transmit `Hello` to any connecting client, and then close the connection. |
8 | 8 |
|
9 | 9 | use defmt_rtt as _;
|
10 | 10 | use panic_probe as _;
|
@@ -78,33 +78,40 @@ fn main() -> ! {
|
78 | 78 | );
|
79 | 79 | let server_handle = iface.add_socket(server_socket);
|
80 | 80 |
|
81 |
| - defmt::info!("Ready, listening at {}", ip_addr); |
82 | 81 | loop {
|
83 | 82 | let time: u64 = cortex_m::interrupt::free(|cs| *TIME.borrow(cs).borrow());
|
84 | 83 | cortex_m::interrupt::free(|cs| {
|
85 | 84 | let mut eth_pending = ETH_PENDING.borrow(cs).borrow_mut();
|
86 | 85 | *eth_pending = false;
|
87 | 86 | });
|
88 |
| - match iface.poll(Instant::from_millis(time as i64)) { |
89 |
| - Ok(true) => { |
90 |
| - let socket = iface.get_socket::<TcpSocket>(server_handle); |
91 |
| - if !socket.is_open() { |
92 |
| - if let Err(e) = socket.listen(80) { |
93 |
| - defmt::error!("TCP listen error: {:?}", e) |
94 |
| - } |
95 |
| - } |
96 | 87 |
|
97 |
| - if socket.can_send() { |
98 |
| - if let Err(e) = socket.send_slice(b"hello\n").map(|_| socket.close()) { |
99 |
| - defmt::info!("TCP send error: {:?}", e); |
| 88 | + iface.poll(Instant::from_millis(time as i64)).ok(); |
| 89 | + |
| 90 | + let socket = iface.get_socket::<TcpSocket>(server_handle); |
| 91 | + |
| 92 | + if !socket.is_listening() && !socket.is_open() { |
| 93 | + socket.abort(); |
| 94 | + if let Err(e) = socket.listen(80) { |
| 95 | + defmt::error!("TCP listen error: {:?}", e) |
| 96 | + } else { |
| 97 | + defmt::info!("Listening at {}:80...", ip_addr); |
| 98 | + } |
| 99 | + } else { |
| 100 | + match socket.send_slice(b"hello\n") { |
| 101 | + Ok(_) => { |
| 102 | + while iface.get_socket::<TcpSocket>(server_handle).send_queue() != 0 { |
| 103 | + // Poll to get the message out of the door |
| 104 | + iface.poll(Instant::from_millis(time as i64 + 1)).ok(); |
100 | 105 | }
|
| 106 | + |
| 107 | + // Abort the connection |
| 108 | + let socket = iface.get_socket::<TcpSocket>(server_handle); |
| 109 | + socket.abort(); |
| 110 | + defmt::info!("Transmitted hello! Closing socket..."); |
| 111 | + |
| 112 | + iface.poll(Instant::from_millis(time as i64 + 1)).ok(); |
101 | 113 | }
|
102 |
| - } |
103 |
| - Ok(false) => {} |
104 |
| - Err(e) => |
105 |
| - // Ignore malformed packets |
106 |
| - { |
107 |
| - defmt::info!("Error: {:?}", e); |
| 114 | + Err(_) => {} |
108 | 115 | }
|
109 | 116 | }
|
110 | 117 | }
|
|
0 commit comments