diff --git a/services/net/src/api.rs b/services/net/src/api.rs index 128010153..217fc8537 100644 --- a/services/net/src/api.rs +++ b/services/net/src/api.rs @@ -146,6 +146,8 @@ pub(crate) enum Opcode { /// Receives data from the specified TCP Connection. The TCP connection number is /// passed in the upper 16 bits of the opcode, and the number of received bytes /// is returned as part of the `Valid` parameter. This is not blocking. + /// Unfortunately, it is *valid* to receive zero bytes, but the xous::Message parameter does not allow it. + /// Thus the value of u32::MAX is mapped to "zero" bytes received. StdTcpPeek = 32, /// Receives data from the specified TCP Connection. @@ -154,6 +156,8 @@ pub(crate) enum Opcode { /// - The return buffer is the `buf` parameter /// - The read timeout is passed as the `offset` parameter. /// - The number of received bytes is returned as part of the `valid` parameter. + /// Unfortunately, it is *valid* to receive zero bytes, but the xous::Message parameter does not allow it. + /// Thus the value of u32::MAX is mapped to "zero" bytes received. StdTcpRx = 33, /// Close the TCP connection. The connection ID is specified in the upper 16 bits @@ -347,6 +351,8 @@ pub(crate) enum Opcode { /// /// - The `valid` and `offset` parameters are not used. StdTcpAccept = 45, + + StdTcpStreamShutdown = 46, } #[derive(Debug, Archive, Serialize, Deserialize, Copy, Clone, Default)] diff --git a/services/net/src/main.rs b/services/net/src/main.rs index 149f526ed..8662daf3b 100644 --- a/services/net/src/main.rs +++ b/services/net/src/main.rs @@ -523,6 +523,13 @@ fn xmain() -> ! { Some(Opcode::StdTcpPeek) => { let pid = msg.sender.pid(); std_tcp_peek(msg, &mut sockets, process_sockets.entry(pid).or_default()); + xous::try_send_message( + net_conn, + Message::new_scalar( + Opcode::NetPump.to_usize().unwrap(), + 0, 0, 0, 0 + ), + ).ok(); } Some(Opcode::StdTcpRx) => { @@ -534,6 +541,13 @@ fn xmain() -> ! { &mut tcp_rx_waiting, process_sockets.entry(pid).or_default(), ); + xous::try_send_message( + net_conn, + Message::new_scalar( + Opcode::NetPump.to_usize().unwrap(), + 0, 0, 0, 0 + ), + ).ok(); } Some(Opcode::StdTcpClose) => { @@ -563,6 +577,102 @@ fn xmain() -> ! { } } + Some(Opcode::StdTcpStreamShutdown) => { + // Only work with blockingscalar messages + if !msg.body.is_blocking() || msg.body.has_memory() { + respond_with_error(msg, NetError::LibraryError); + continue; + } + + let pid = msg.sender.pid(); + let connection_idx = msg.body.id() >> 16; + let shutdown_code = msg.body.scalar_message().unwrap().arg1; + if let Some(Some(connection)) = process_sockets + .entry(pid) + .or_default() + .get(connection_idx) + { + if (shutdown_code & 1) != 0 { // read shutdown + // search for the handle in the rxwaiting set + for rx_waiter in tcp_rx_waiting.iter_mut() { + let WaitingSocket { + env: mut msg, + handle, + expiry: _, + } = match rx_waiter { + &mut None => continue, + Some(s) => { + if s.handle == *connection { + rx_waiter.take().unwrap() // removes the message from the waiting queue + } else { + continue + } + } + }; + // if we got here, we found a message that needs to be aborted + log::info!("TcpShutdown: aborting rx waiting handle: {:?}", handle); + match msg.body.memory_message_mut() { + Some(body) => { + // u32::MAX indicates a zero-length receive + body.valid = xous::MemorySize::new(u32::MAX as usize); + }, + None => { + respond_with_error(msg, NetError::LibraryError); + } + } + // in theory, there should be no more matching handles as they should be all unique, so we can abort the search. + break; + } + } + if (shutdown_code & 1) != 0 { // write shutdown + // search for the handle in the txwaiting set + for tx_waiter in tcp_tx_waiting.iter_mut() { + let WaitingSocket { + env: mut msg, + handle, + expiry: _, + } = match tx_waiter { + &mut None => continue, + Some(s) => { + if s.handle == *connection { + tx_waiter.take().unwrap() // removes the message from the waiting queue + } else { + continue + } + } + }; + // if we got here, we found a message that needs to be aborted + log::info!("TcpShutdown: aborting tx waiting handle: {:?}", handle); + match msg.body.memory_message_mut() { + Some(body) => { + // u32::MAX indicates a zero-length receive + body.valid = xous::MemorySize::new(u32::MAX as usize); + let response_data = body.buf.as_slice_mut::(); + response_data[0] = 0; + response_data[1] = 0; + }, + None => { + respond_with_error(msg, NetError::LibraryError); + } + } + // in theory, there should be no more matching handles as they should be all unique, so we can abort the search. + break; + } + } + } + + // unblock the sender + xous::return_scalar(msg.sender, 1).ok(); + // pump the rx to process any shutdowns + xous::try_send_message( + net_conn, + Message::new_scalar( + Opcode::NetPump.to_usize().unwrap(), + 0, 0, 0, 0 + ), + ).ok(); + } + Some(Opcode::StdTcpListen) => { let pid = msg.sender.pid();