mirror of
https://github.com/asterinas/asterinas.git
synced 2025-06-20 04:56:32 +00:00
Simplify the TCP state check
This commit is contained in:
committed by
Tate, Hongliang Tian
parent
9edee83ef1
commit
eef56c770b
@ -6,7 +6,7 @@ mod tcp_listen;
|
||||
mod udp;
|
||||
|
||||
pub use common::NeedIfacePoll;
|
||||
pub use tcp_conn::{ConnectState, TcpConnection};
|
||||
pub use tcp_conn::{ConnectState, RawTcpSocketExt, TcpConnection};
|
||||
pub(crate) use tcp_conn::{TcpConnectionBg, TcpProcessResult};
|
||||
pub use tcp_listen::TcpListener;
|
||||
pub(crate) use tcp_listen::TcpListenerBg;
|
||||
|
@ -23,8 +23,7 @@ use crate::{
|
||||
socket::{
|
||||
event::SocketEvents,
|
||||
option::{RawTcpOption, RawTcpSetOption},
|
||||
unbound::new_tcp_socket,
|
||||
RawTcpSocket, TcpStateCheck,
|
||||
unbound::{new_tcp_socket, RawTcpSocket},
|
||||
},
|
||||
socket_table::ConnectionKey,
|
||||
};
|
||||
@ -37,7 +36,7 @@ pub struct TcpConnectionInner<E: Ext> {
|
||||
connection_key: ConnectionKey,
|
||||
}
|
||||
|
||||
pub(super) struct RawTcpSocketExt<E: Ext> {
|
||||
pub struct RawTcpSocketExt<E: Ext> {
|
||||
socket: Box<RawTcpSocket>,
|
||||
pub(super) listener: Option<Arc<TcpListenerBg<E>>>,
|
||||
has_connected: bool,
|
||||
@ -57,6 +56,25 @@ impl<E: Ext> DerefMut for RawTcpSocketExt<E> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: Ext> RawTcpSocketExt<E> {
|
||||
/// Checks if the socket may receive any new data.
|
||||
///
|
||||
/// This is similar to [`RawTcpSocket::may_recv`]. However, this method checks if there can be
|
||||
/// _new_ data. In other words, if there is already buffered data in the socket,
|
||||
/// [`RawTcpSocket::may_recv`] will always return true since it is possible to receive the
|
||||
/// buffered data, but this method may return false if the peer has closed its sending half (so
|
||||
/// no new data can come in).
|
||||
pub fn may_recv_new(&self) -> bool {
|
||||
// See also the implementation of `RawTcpSocket::may_recv`.
|
||||
match self.state() {
|
||||
State::Established => true,
|
||||
// Our sending half is closed, but the peer's sending half is still active.
|
||||
State::FinWait1 | State::FinWait2 => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
define_boolean_value!(
|
||||
/// Whether the TCP connection became dead.
|
||||
TcpConnBecameDead
|
||||
@ -67,7 +85,9 @@ impl<E: Ext> RawTcpSocketExt<E> {
|
||||
&mut self,
|
||||
this: &Arc<TcpConnectionBg<E>>,
|
||||
) -> (SocketEvents, TcpConnBecameDead) {
|
||||
if self.may_send() && !self.has_connected {
|
||||
let may_send = self.may_send();
|
||||
|
||||
if may_send && !self.has_connected {
|
||||
self.has_connected = true;
|
||||
|
||||
if let Some(ref listener) = self.listener {
|
||||
@ -81,13 +101,13 @@ impl<E: Ext> RawTcpSocketExt<E> {
|
||||
|
||||
let became_dead = self.check_dead(this);
|
||||
|
||||
let events = if self.is_peer_closed() {
|
||||
SocketEvents::PEER_CLOSED
|
||||
} else if self.is_closed() {
|
||||
SocketEvents::CLOSED
|
||||
} else {
|
||||
SocketEvents::empty()
|
||||
};
|
||||
let mut events = SocketEvents::empty();
|
||||
if !self.may_recv_new() {
|
||||
events |= SocketEvents::CLOSED_RECV;
|
||||
}
|
||||
if !may_send {
|
||||
events |= SocketEvents::CLOSED_SEND;
|
||||
}
|
||||
|
||||
(events, became_dead)
|
||||
}
|
||||
@ -317,7 +337,7 @@ impl<E: Ext> TcpConnection<E> {
|
||||
|
||||
socket.listener = None;
|
||||
|
||||
if socket.is_closed() {
|
||||
if socket.state() == State::Closed {
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -333,7 +353,7 @@ impl<E: Ext> TcpConnection<E> {
|
||||
// polling time.
|
||||
pub fn raw_with<F, R>(&self, f: F) -> R
|
||||
where
|
||||
F: FnOnce(&RawTcpSocket) -> R,
|
||||
F: FnOnce(&RawTcpSocketExt<E>) -> R,
|
||||
{
|
||||
let socket = self.0.inner.lock();
|
||||
f(&socket)
|
||||
|
@ -20,8 +20,7 @@ use crate::{
|
||||
iface::{BindPortConfig, BoundPort},
|
||||
socket::{
|
||||
option::{RawTcpOption, RawTcpSetOption},
|
||||
unbound::new_tcp_socket,
|
||||
RawTcpSocket,
|
||||
unbound::{new_tcp_socket, RawTcpSocket},
|
||||
},
|
||||
socket_table::{ConnectionKey, ListenerKey},
|
||||
};
|
||||
|
@ -15,7 +15,9 @@ bitflags::bitflags! {
|
||||
pub struct SocketEvents: u8 {
|
||||
const CAN_RECV = 1;
|
||||
const CAN_SEND = 2;
|
||||
const PEER_CLOSED = 4;
|
||||
const CLOSED = 8;
|
||||
/// Receiving new data isn't possible anymore.
|
||||
const CLOSED_RECV = 4;
|
||||
/// Sending data isn't possible anymore.
|
||||
const CLOSED_SEND = 8;
|
||||
}
|
||||
}
|
||||
|
@ -3,15 +3,14 @@
|
||||
mod bound;
|
||||
mod event;
|
||||
mod option;
|
||||
mod state;
|
||||
mod unbound;
|
||||
|
||||
pub use bound::{ConnectState, NeedIfacePoll, TcpConnection, TcpListener, UdpSocket};
|
||||
pub use bound::{
|
||||
ConnectState, NeedIfacePoll, RawTcpSocketExt, TcpConnection, TcpListener, UdpSocket,
|
||||
};
|
||||
pub(crate) use bound::{TcpConnectionBg, TcpListenerBg, TcpProcessResult, UdpSocketBg};
|
||||
pub use event::{SocketEventObserver, SocketEvents};
|
||||
pub use option::{RawTcpOption, RawTcpSetOption};
|
||||
pub use state::TcpStateCheck;
|
||||
pub use unbound::{TCP_RECV_BUF_LEN, TCP_SEND_BUF_LEN, UDP_RECV_PAYLOAD_LEN, UDP_SEND_PAYLOAD_LEN};
|
||||
|
||||
pub type RawTcpSocket = smoltcp::socket::tcp::Socket<'static>;
|
||||
pub type RawUdpSocket = smoltcp::socket::udp::Socket<'static>;
|
||||
pub use unbound::{
|
||||
RawUdpSocket, TCP_RECV_BUF_LEN, TCP_SEND_BUF_LEN, UDP_RECV_PAYLOAD_LEN, UDP_SEND_PAYLOAD_LEN,
|
||||
};
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
use smoltcp::time::Duration;
|
||||
|
||||
use super::{NeedIfacePoll, RawTcpSocket};
|
||||
use super::{unbound::RawTcpSocket, NeedIfacePoll};
|
||||
|
||||
/// A trait defines setting socket options on a raw socket.
|
||||
pub trait RawTcpSetOption {
|
||||
|
@ -1,31 +0,0 @@
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
pub use smoltcp::socket::tcp::State as TcpState;
|
||||
|
||||
use super::RawTcpSocket;
|
||||
|
||||
pub trait TcpStateCheck {
|
||||
/// Checks if the peer socket has closed its sending side.
|
||||
///
|
||||
/// If the sending side of this socket is also closed, this method will return `false`.
|
||||
/// In such cases, you should verify using [`is_closed`].
|
||||
fn is_peer_closed(&self) -> bool;
|
||||
|
||||
/// Checks if the socket is fully closed.
|
||||
///
|
||||
/// This function returns `true` if both this socket and the peer have closed their sending sides.
|
||||
///
|
||||
/// This TCP state corresponds to the `Normal Close Sequence` and `Simultaneous Close Sequence`
|
||||
/// as outlined in RFC793 (https://datatracker.ietf.org/doc/html/rfc793#page-39).
|
||||
fn is_closed(&self) -> bool;
|
||||
}
|
||||
|
||||
impl TcpStateCheck for RawTcpSocket {
|
||||
fn is_peer_closed(&self) -> bool {
|
||||
self.state() == TcpState::CloseWait
|
||||
}
|
||||
|
||||
fn is_closed(&self) -> bool {
|
||||
!self.is_open() || self.state() == TcpState::Closing || self.state() == TcpState::LastAck
|
||||
}
|
||||
}
|
@ -2,7 +2,8 @@
|
||||
|
||||
use alloc::{boxed::Box, vec};
|
||||
|
||||
use super::{RawTcpSocket, RawUdpSocket};
|
||||
pub(super) type RawTcpSocket = smoltcp::socket::tcp::Socket<'static>;
|
||||
pub type RawUdpSocket = smoltcp::socket::udp::Socket<'static>;
|
||||
|
||||
pub(super) fn new_tcp_socket() -> Box<RawTcpSocket> {
|
||||
let raw_tcp_socket = {
|
||||
|
Reference in New Issue
Block a user