mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-18 08:52:45 +00:00
refactor TcpClose; fix RxWait
TcpClose should not immediately remove the socket. It has to wait until the FIN comes. Add a fin waiter state. Also rewrite to use "retain". Woo. I'm starting to get the hang of Rust closures. Only took a couple years. RxWait gets stuck in CLOSE_WAIT state. It's a state where you "could" transmit, but you can't receive anymore. In this case respond with a 0-length receive? That seems to be what leads to the right behavior. I would have thought you'd want to respond with a timeout error but that doesn't match what's in the test bench.
This commit is contained in:
parent
4992d4ebff
commit
6045c75cf0
1 changed files with 50 additions and 27 deletions
|
|
@ -103,6 +103,7 @@ pub struct PingConnection {
|
||||||
retop: usize,
|
retop: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
struct WaitingSocket {
|
struct WaitingSocket {
|
||||||
env: xous::MessageEnvelope,
|
env: xous::MessageEnvelope,
|
||||||
handle: SocketHandle,
|
handle: SocketHandle,
|
||||||
|
|
@ -239,9 +240,12 @@ fn main() -> ! {
|
||||||
// is being accumulated.
|
// is being accumulated.
|
||||||
let mut tcp_tx_waiting: Vec<Option<WaitingSocket>> = Vec::new();
|
let mut tcp_tx_waiting: Vec<Option<WaitingSocket>> = Vec::new();
|
||||||
|
|
||||||
// socket handles waiting for writes to flush on close
|
// socket handles waiting for writes to flush on close (transitions to sending FIN)
|
||||||
let mut tcp_tx_closing: Vec<(SocketHandle, xous::MessageSender)> = Vec::new();
|
let mut tcp_tx_closing: Vec<(SocketHandle, xous::MessageSender)> = Vec::new();
|
||||||
|
|
||||||
|
// socket handles waiting to enter the closed state
|
||||||
|
let mut tcp_tx_wait_fin: Vec<(SocketHandle, xous::MessageSender)> = Vec::new();
|
||||||
|
|
||||||
// When a client issues a Connect request, it will get placed here while the connection is
|
// When a client issues a Connect request, it will get placed here while the connection is
|
||||||
// being established.
|
// being established.
|
||||||
let mut tcp_connect_waiting: Vec<
|
let mut tcp_connect_waiting: Vec<
|
||||||
|
|
@ -660,7 +664,6 @@ fn main() -> ! {
|
||||||
}
|
}
|
||||||
|
|
||||||
Some(Opcode::StdTcpClose) => {
|
Some(Opcode::StdTcpClose) => {
|
||||||
log::debug!("StdTcpClose");
|
|
||||||
let pid = msg.sender.pid();
|
let pid = msg.sender.pid();
|
||||||
let connection_idx = msg.body.id() >> 16;
|
let connection_idx = msg.body.id() >> 16;
|
||||||
let handle = if let Some(connection) = process_sockets
|
let handle = if let Some(connection) = process_sockets
|
||||||
|
|
@ -678,20 +681,16 @@ fn main() -> ! {
|
||||||
respond_with_error(msg, NetError::Invalid);
|
respond_with_error(msg, NetError::Invalid);
|
||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
|
let socket = iface.get_socket::<TcpSocket>(handle);
|
||||||
|
log::debug!("StdTcpClose {:?}", socket.local_endpoint());
|
||||||
if !std_tcp_can_close(&tcp_tx_waiting, handle) {
|
if !std_tcp_can_close(&tcp_tx_waiting, handle) {
|
||||||
log::trace!("def"); // these are short because the extra delay of a long message affects the computation
|
log::trace!("def"); // these are short because the extra delay of a long message affects the computation
|
||||||
tcp_tx_closing.push((handle, msg.sender));
|
tcp_tx_closing.push((handle, msg.sender));
|
||||||
} else {
|
} else {
|
||||||
let socket = iface.get_socket::<TcpSocket>(handle);
|
|
||||||
if socket.may_send() && socket.send_queue() == 0 {
|
if socket.may_send() && socket.send_queue() == 0 {
|
||||||
log::trace!("imm");
|
log::trace!("imm");
|
||||||
socket.close();
|
socket.close();
|
||||||
iface.remove_socket(handle);
|
tcp_tx_wait_fin.push((handle, msg.sender));
|
||||||
if let Some(response) = msg.body.memory_message_mut() {
|
|
||||||
response.buf.as_slice_mut::<u8>()[0] = 0;
|
|
||||||
} else if !msg.body.has_memory() && msg.body.is_blocking() {
|
|
||||||
xous::return_scalar(msg.sender, 0).ok();
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
log::trace!("def2");
|
log::trace!("def2");
|
||||||
tcp_tx_closing.push((handle, msg.sender));
|
tcp_tx_closing.push((handle, msg.sender));
|
||||||
|
|
@ -1309,6 +1308,7 @@ fn main() -> ! {
|
||||||
&mut None => continue,
|
&mut None => continue,
|
||||||
Some(s) => {
|
Some(s) => {
|
||||||
socket = iface.get_socket::<TcpSocket>(s.handle);
|
socket = iface.get_socket::<TcpSocket>(s.handle);
|
||||||
|
log::debug!("rx_state: {:?} {:?}", socket.state(), socket.local_endpoint());
|
||||||
if !socket.can_recv() {
|
if !socket.can_recv() {
|
||||||
if let Some(trigger) = s.expiry {
|
if let Some(trigger) = s.expiry {
|
||||||
log::debug!("rxrcv {:?}", trigger.get());
|
log::debug!("rxrcv {:?}", trigger.get());
|
||||||
|
|
@ -1317,6 +1317,8 @@ fn main() -> ! {
|
||||||
} else {
|
} else {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
} else if socket.state() == smoltcp::socket::TcpState::CloseWait {
|
||||||
|
// stop waiting if we're in CloseWait, as we don't plan to transmit
|
||||||
} else {
|
} else {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
@ -1328,9 +1330,18 @@ fn main() -> ! {
|
||||||
|
|
||||||
// If it can't receive, then the only explanation was that it timed out
|
// If it can't receive, then the only explanation was that it timed out
|
||||||
if !socket.can_recv() {
|
if !socket.can_recv() {
|
||||||
log::debug!("rxrcv timed out");
|
if socket.state() == smoltcp::socket::TcpState::CloseWait {
|
||||||
respond_with_error(env, NetError::TimedOut);
|
log::debug!("rxrcv connection closed");
|
||||||
continue;
|
let body = env.body.memory_message_mut().unwrap();
|
||||||
|
log::debug!("rxrcv of {}", 0);
|
||||||
|
body.valid = xous::MemorySize::new(0);
|
||||||
|
body.offset = xous::MemoryAddress::new(1);
|
||||||
|
continue;
|
||||||
|
} else {
|
||||||
|
log::debug!("rxrcv timed out");
|
||||||
|
respond_with_error(env, NetError::TimedOut);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let body = env.body.memory_message_mut().unwrap();
|
let body = env.body.memory_message_mut().unwrap();
|
||||||
|
|
@ -1512,24 +1523,36 @@ fn main() -> ! {
|
||||||
}
|
}
|
||||||
|
|
||||||
log::trace!("pump: tcp close");
|
log::trace!("pump: tcp close");
|
||||||
if tcp_tx_closing.len() > 0 {
|
tcp_tx_closing.retain(|(handle, sender)| {
|
||||||
let mut closed = Vec::<usize>::new();
|
if std_tcp_can_close(&tcp_tx_waiting, *handle) {
|
||||||
for (index, &(handle, sender)) in tcp_tx_closing.iter().enumerate() {
|
let socket = iface.get_socket::<TcpSocket>(*handle);
|
||||||
if std_tcp_can_close(&tcp_tx_waiting, handle) {
|
log::trace!("may_send: {}, send_queue: {}", socket.may_send(), socket.send_queue());
|
||||||
let socket = iface.get_socket::<TcpSocket>(handle);
|
// different condition than the previous wait -- here we opportunistically close
|
||||||
if socket.may_send() && socket.send_queue() == 0 {
|
// when either condition is met.
|
||||||
socket.close();
|
if !socket.may_send() || socket.send_queue() == 0 {
|
||||||
iface.remove_socket(handle);
|
socket.close();
|
||||||
log::debug!("deferred close, socket is not active");
|
tcp_tx_wait_fin.push((*handle, *sender));
|
||||||
xous::return_scalar(sender, 0).ok();
|
false
|
||||||
closed.push(index);
|
} else {
|
||||||
}
|
true
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
true
|
||||||
}
|
}
|
||||||
for &index in closed.iter().rev() {
|
});
|
||||||
tcp_tx_closing.remove(index);
|
|
||||||
|
tcp_tx_wait_fin.retain(|(handle, sender)| {
|
||||||
|
let socket = iface.get_socket::<TcpSocket>(*handle);
|
||||||
|
if !socket.is_open() {
|
||||||
|
log::debug!("socket closed {:?}", socket.local_endpoint());
|
||||||
|
iface.remove_socket(*handle);
|
||||||
|
xous::return_scalar(*sender, 0).ok();
|
||||||
|
false
|
||||||
|
} else {
|
||||||
|
log::debug!("socket waiting to close: {:?} {:?}->{:?}", socket.state(), socket.local_endpoint(), socket.remote_endpoint());
|
||||||
|
true
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
|
|
||||||
// this block contains the ICMP Rx handler. Tx is initiated by an incoming message to the Net crate.
|
// this block contains the ICMP Rx handler. Tx is initiated by an incoming message to the Net crate.
|
||||||
log::trace!("pump: icmp");
|
log::trace!("pump: icmp");
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue