net: add option not to open sock when it is polled

- fix a bug, which mcu can get stuck in State::HardReset indefinitely
This commit is contained in:
linuswck 2024-09-11 12:08:37 +08:00
parent 76f7875e3a
commit a512614de5
2 changed files with 14 additions and 12 deletions

View File

@ -167,7 +167,7 @@ fn main() -> ! {
} }
net::net::for_each(|mut socket, id| { net::net::for_each(|mut socket, id| {
if net::net::eth_is_socket_active(socket) && net::net::eth_is_socket_connected(socket) { if net::net::eth_is_socket_active(socket, true) && net::net::eth_is_socket_connected(socket) {
if active_report[id] { if active_report[id] {
if net::net::eth_can_sock_send(socket) { if net::net::eth_can_sock_send(socket) {
net::cmd_handler::send_status_report( net::cmd_handler::send_status_report(
@ -191,7 +191,7 @@ fn main() -> ! {
let mut oldest_connected_sock_id: usize = 0; let mut oldest_connected_sock_id: usize = 0;
net::net::for_each(|mut socket, id| { net::net::for_each(|mut socket, id| {
if net::net::eth_is_socket_active(socket) && net::net::eth_is_socket_connected(socket) { if net::net::eth_is_socket_active(socket, true) && net::net::eth_is_socket_connected(socket) {
if net::net::eth_can_sock_recv(socket) && net::net::eth_can_sock_send(socket) { if net::net::eth_can_sock_recv(socket) && net::net::eth_can_sock_send(socket) {
let bytes = net::net::eth_recv(eth_data_buffer, socket); let bytes = net::net::eth_recv(eth_data_buffer, socket);
if bytes != 0 { if bytes != 0 {
@ -289,7 +289,7 @@ fn main() -> ! {
laser.power_down(); laser.power_down();
thermostat.power_down(); thermostat.power_down();
net::net::for_each(|mut socket, _| { net::net::for_each(|mut socket, _| {
if net::net::eth_is_socket_active(socket) { if net::net::eth_is_socket_active(socket, false) {
net::cmd_handler::send_response( net::cmd_handler::send_response(
eth_data_buffer, eth_data_buffer,
net::cmd_handler::ResponseEnum::HardReset, net::cmd_handler::ResponseEnum::HardReset,
@ -308,7 +308,7 @@ fn main() -> ! {
laser.power_down(); laser.power_down();
thermostat.power_down(); thermostat.power_down();
net::net::for_each(|mut socket, _| { net::net::for_each(|mut socket, _| {
if net::net::eth_is_socket_active(socket) { if net::net::eth_is_socket_active(socket, false) {
net::cmd_handler::send_response( net::cmd_handler::send_response(
eth_data_buffer, eth_data_buffer,
net::cmd_handler::ResponseEnum::Dfu, net::cmd_handler::ResponseEnum::Dfu,
@ -326,7 +326,7 @@ fn main() -> ! {
thermostat.power_down(); thermostat.power_down();
let mut any_socket_alive = false; let mut any_socket_alive = false;
net::net::for_each(|socket, _| { net::net::for_each(|socket, _| {
if net::net::eth_is_socket_active(socket) { if net::net::eth_is_socket_active(socket, false) {
net::net::eth_close_socket(socket); net::net::eth_close_socket(socket);
any_socket_alive = true; any_socket_alive = true;
} }

View File

@ -268,14 +268,16 @@ impl ServerHandle {
socket.state() == State::Established socket.state() == State::Established
} }
pub fn poll_socket_status(&mut self, socket_handles: SocketHandle) -> bool { pub fn poll_socket_status(&mut self, socket_handles: SocketHandle, reopen_sock: bool) -> bool {
let socket = self.socket_set.get_mut::<Socket>(socket_handles); let socket = self.socket_set.get_mut::<Socket>(socket_handles);
if !socket.is_listening() && !socket.is_open() || socket.state() == State::CloseWait { if !socket.is_listening() && !socket.is_open() || socket.state() == State::CloseWait {
if reopen_sock {
socket.abort(); socket.abort();
socket.listen(self.socket_addr).ok(); socket.listen(self.socket_addr).ok();
info!("Disconnected... Reopening listening socket."); info!("Disconnected... Reopening listening socket.");
}
return false; return false;
} else if socket.state() == State::Closed || socket.state() == State::Closing { } else if socket.state() == State::Closed || socket.state() == State::Closing || socket.is_listening() {
return false; return false;
} }
return true; return true;
@ -485,10 +487,10 @@ pub fn eth_is_socket_connected(socket_handles: SocketHandle) -> bool {
} }
} }
pub fn eth_is_socket_active(socket_handles: SocketHandle) -> bool { pub fn eth_is_socket_active(socket_handles: SocketHandle, reopen_sock: bool) -> bool {
unsafe { unsafe {
if let Some(ref mut server_handle) = SERVER_HANDLE { if let Some(ref mut server_handle) = SERVER_HANDLE {
server_handle.poll_socket_status(socket_handles) server_handle.poll_socket_status(socket_handles, reopen_sock)
} else { } else {
panic!("eth_is_socket_active is called before init"); panic!("eth_is_socket_active is called before init");
} }