mirror of
https://github.com/asterinas/asterinas.git
synced 2025-06-24 01:43:22 +00:00
Use Pollee
as the socket observer
This commit is contained in:
committed by
Tate, Hongliang Tian
parent
fa76afb3a9
commit
1716f4f324
@ -1,9 +1,13 @@
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
use super::sched::PollScheduler;
|
||||
use crate::net::socket::ip::{datagram::DatagramObserver, stream::StreamObserver};
|
||||
|
||||
pub struct BigtcpExt;
|
||||
|
||||
impl aster_bigtcp::ext::Ext for BigtcpExt {
|
||||
type ScheduleNextPoll = PollScheduler;
|
||||
|
||||
type TcpEventObserver = StreamObserver;
|
||||
type UdpEventObserver = DatagramObserver;
|
||||
}
|
||||
|
@ -2,10 +2,7 @@
|
||||
|
||||
use core::sync::atomic::{AtomicBool, Ordering};
|
||||
|
||||
use aster_bigtcp::{
|
||||
socket::{SocketEventObserver, SocketEvents},
|
||||
wire::IpEndpoint,
|
||||
};
|
||||
use aster_bigtcp::wire::IpEndpoint;
|
||||
use ostd::sync::WriteIrqDisabled;
|
||||
use takeable::Takeable;
|
||||
|
||||
@ -32,8 +29,11 @@ use crate::{
|
||||
};
|
||||
|
||||
mod bound;
|
||||
mod observer;
|
||||
mod unbound;
|
||||
|
||||
pub(in crate::net) use self::observer::DatagramObserver;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct OptionSet {
|
||||
socket: SocketOptionSet,
|
||||
@ -64,6 +64,7 @@ impl Inner {
|
||||
self,
|
||||
endpoint: &IpEndpoint,
|
||||
can_reuse: bool,
|
||||
observer: DatagramObserver,
|
||||
) -> core::result::Result<BoundDatagram, (Error, Self)> {
|
||||
let unbound_datagram = match self {
|
||||
Inner::Unbound(unbound_datagram) => unbound_datagram,
|
||||
@ -75,7 +76,7 @@ impl Inner {
|
||||
}
|
||||
};
|
||||
|
||||
let bound_datagram = match unbound_datagram.bind(endpoint, can_reuse) {
|
||||
let bound_datagram = match unbound_datagram.bind(endpoint, can_reuse, observer) {
|
||||
Ok(bound_datagram) => bound_datagram,
|
||||
Err((err, unbound_datagram)) => return Err((err, Inner::Unbound(unbound_datagram))),
|
||||
};
|
||||
@ -85,26 +86,25 @@ impl Inner {
|
||||
fn bind_to_ephemeral_endpoint(
|
||||
self,
|
||||
remote_endpoint: &IpEndpoint,
|
||||
observer: DatagramObserver,
|
||||
) -> core::result::Result<BoundDatagram, (Error, Self)> {
|
||||
if let Inner::Bound(bound_datagram) = self {
|
||||
return Ok(bound_datagram);
|
||||
}
|
||||
|
||||
let endpoint = get_ephemeral_endpoint(remote_endpoint);
|
||||
self.bind(&endpoint, false)
|
||||
self.bind(&endpoint, false, observer)
|
||||
}
|
||||
}
|
||||
|
||||
impl DatagramSocket {
|
||||
pub fn new(nonblocking: bool) -> Arc<Self> {
|
||||
Arc::new_cyclic(|me| {
|
||||
let unbound_datagram = UnboundDatagram::new(me.clone() as _);
|
||||
Self {
|
||||
inner: RwLock::new(Takeable::new(Inner::Unbound(unbound_datagram))),
|
||||
nonblocking: AtomicBool::new(nonblocking),
|
||||
pollee: Pollee::new(),
|
||||
options: RwLock::new(OptionSet::new()),
|
||||
}
|
||||
let unbound_datagram = UnboundDatagram::new();
|
||||
Arc::new(Self {
|
||||
inner: RwLock::new(Takeable::new(Inner::Unbound(unbound_datagram))),
|
||||
nonblocking: AtomicBool::new(nonblocking),
|
||||
pollee: Pollee::new(),
|
||||
options: RwLock::new(OptionSet::new()),
|
||||
})
|
||||
}
|
||||
|
||||
@ -134,7 +134,10 @@ impl DatagramSocket {
|
||||
// Slow path
|
||||
let mut inner = self.inner.write();
|
||||
inner.borrow_result(|owned_inner| {
|
||||
let bound_datagram = match owned_inner.bind_to_ephemeral_endpoint(remote_endpoint) {
|
||||
let bound_datagram = match owned_inner.bind_to_ephemeral_endpoint(
|
||||
remote_endpoint,
|
||||
DatagramObserver::new(self.pollee.clone()),
|
||||
) {
|
||||
Ok(bound_datagram) => bound_datagram,
|
||||
Err((err, err_inner)) => {
|
||||
return (err_inner, Err(err));
|
||||
@ -277,7 +280,11 @@ impl Socket for DatagramSocket {
|
||||
let can_reuse = self.options.read().socket.reuse_addr();
|
||||
let mut inner = self.inner.write();
|
||||
inner.borrow_result(|owned_inner| {
|
||||
let bound_datagram = match owned_inner.bind(&endpoint, can_reuse) {
|
||||
let bound_datagram = match owned_inner.bind(
|
||||
&endpoint,
|
||||
can_reuse,
|
||||
DatagramObserver::new(self.pollee.clone()),
|
||||
) {
|
||||
Ok(bound_datagram) => bound_datagram,
|
||||
Err((err, err_inner)) => {
|
||||
return (err_inner, Err(err));
|
||||
@ -389,19 +396,3 @@ impl Socket for DatagramSocket {
|
||||
self.options.write().socket.set_option(option)
|
||||
}
|
||||
}
|
||||
|
||||
impl SocketEventObserver for DatagramSocket {
|
||||
fn on_events(&self, events: SocketEvents) {
|
||||
let mut io_events = IoEvents::empty();
|
||||
|
||||
if events.contains(SocketEvents::CAN_RECV) {
|
||||
io_events |= IoEvents::IN;
|
||||
}
|
||||
|
||||
if events.contains(SocketEvents::CAN_SEND) {
|
||||
io_events |= IoEvents::OUT;
|
||||
}
|
||||
|
||||
self.pollee.notify(io_events);
|
||||
}
|
||||
}
|
||||
|
29
kernel/src/net/socket/ip/datagram/observer.rs
Normal file
29
kernel/src/net/socket/ip/datagram/observer.rs
Normal file
@ -0,0 +1,29 @@
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
use aster_bigtcp::socket::{SocketEventObserver, SocketEvents};
|
||||
|
||||
use crate::{events::IoEvents, process::signal::Pollee};
|
||||
|
||||
pub struct DatagramObserver(Pollee);
|
||||
|
||||
impl DatagramObserver {
|
||||
pub(super) fn new(pollee: Pollee) -> Self {
|
||||
Self(pollee)
|
||||
}
|
||||
}
|
||||
|
||||
impl SocketEventObserver for DatagramObserver {
|
||||
fn on_events(&self, events: SocketEvents) {
|
||||
let mut io_events = IoEvents::empty();
|
||||
|
||||
if events.contains(SocketEvents::CAN_RECV) {
|
||||
io_events |= IoEvents::IN;
|
||||
}
|
||||
|
||||
if events.contains(SocketEvents::CAN_SEND) {
|
||||
io_events |= IoEvents::OUT;
|
||||
}
|
||||
|
||||
self.0.notify(io_events);
|
||||
}
|
||||
}
|
@ -1,13 +1,8 @@
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
use alloc::sync::Weak;
|
||||
use aster_bigtcp::{socket::UnboundUdpSocket, wire::IpEndpoint};
|
||||
|
||||
use aster_bigtcp::{
|
||||
socket::{SocketEventObserver, UnboundUdpSocket},
|
||||
wire::IpEndpoint,
|
||||
};
|
||||
|
||||
use super::bound::BoundDatagram;
|
||||
use super::{bound::BoundDatagram, DatagramObserver};
|
||||
use crate::{events::IoEvents, net::socket::ip::common::bind_socket, prelude::*};
|
||||
|
||||
pub struct UnboundDatagram {
|
||||
@ -15,9 +10,9 @@ pub struct UnboundDatagram {
|
||||
}
|
||||
|
||||
impl UnboundDatagram {
|
||||
pub fn new(observer: Weak<dyn SocketEventObserver>) -> Self {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
unbound_socket: Box::new(UnboundUdpSocket::new(observer)),
|
||||
unbound_socket: Box::new(UnboundUdpSocket::new()),
|
||||
}
|
||||
}
|
||||
|
||||
@ -25,12 +20,13 @@ impl UnboundDatagram {
|
||||
self,
|
||||
endpoint: &IpEndpoint,
|
||||
can_reuse: bool,
|
||||
observer: DatagramObserver,
|
||||
) -> core::result::Result<BoundDatagram, (Error, Self)> {
|
||||
let bound_socket = match bind_socket(
|
||||
self.unbound_socket,
|
||||
endpoint,
|
||||
can_reuse,
|
||||
|iface, socket, config| iface.bind_udp(socket, config),
|
||||
|iface, socket, config| iface.bind_udp(socket, observer, config),
|
||||
) {
|
||||
Ok(bound_socket) => bound_socket,
|
||||
Err((err, unbound_socket)) => return Err((err, Self { unbound_socket })),
|
||||
|
@ -2,9 +2,7 @@
|
||||
|
||||
mod addr;
|
||||
mod common;
|
||||
mod datagram;
|
||||
pub mod datagram;
|
||||
pub mod stream;
|
||||
|
||||
use addr::UNSPECIFIED_LOCAL_ENDPOINT;
|
||||
pub use datagram::DatagramSocket;
|
||||
pub use stream::StreamSocket;
|
||||
|
@ -1,14 +1,14 @@
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
use alloc::sync::Weak;
|
||||
use core::sync::atomic::{AtomicBool, Ordering};
|
||||
|
||||
use aster_bigtcp::{
|
||||
errors::tcp::{RecvError, SendError},
|
||||
socket::{NeedIfacePoll, SocketEventObserver, TcpStateCheck},
|
||||
socket::{NeedIfacePoll, TcpStateCheck},
|
||||
wire::IpEndpoint,
|
||||
};
|
||||
|
||||
use super::StreamObserver;
|
||||
use crate::{
|
||||
events::IoEvents,
|
||||
net::{
|
||||
@ -202,7 +202,7 @@ impl ConnectedStream {
|
||||
})
|
||||
}
|
||||
|
||||
pub(super) fn set_observer(&self, observer: Weak<dyn SocketEventObserver>) {
|
||||
pub(super) fn set_observer(&self, observer: StreamObserver) {
|
||||
self.bound_socket.set_observer(observer)
|
||||
}
|
||||
}
|
||||
|
@ -1,13 +1,8 @@
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
use alloc::sync::Weak;
|
||||
use aster_bigtcp::{socket::UnboundTcpSocket, wire::IpEndpoint};
|
||||
|
||||
use aster_bigtcp::{
|
||||
socket::{SocketEventObserver, UnboundTcpSocket},
|
||||
wire::IpEndpoint,
|
||||
};
|
||||
|
||||
use super::{connecting::ConnectingStream, listen::ListenStream};
|
||||
use super::{connecting::ConnectingStream, listen::ListenStream, StreamObserver};
|
||||
use crate::{
|
||||
events::IoEvents,
|
||||
net::{
|
||||
@ -15,6 +10,7 @@ use crate::{
|
||||
socket::ip::common::{bind_socket, get_ephemeral_endpoint},
|
||||
},
|
||||
prelude::*,
|
||||
process::signal::Pollee,
|
||||
};
|
||||
|
||||
pub enum InitStream {
|
||||
@ -23,8 +19,8 @@ pub enum InitStream {
|
||||
}
|
||||
|
||||
impl InitStream {
|
||||
pub fn new(observer: Weak<dyn SocketEventObserver>) -> Self {
|
||||
InitStream::Unbound(Box::new(UnboundTcpSocket::new(observer)))
|
||||
pub fn new() -> Self {
|
||||
InitStream::Unbound(Box::new(UnboundTcpSocket::new()))
|
||||
}
|
||||
|
||||
pub fn new_bound(bound_socket: BoundTcpSocket) -> Self {
|
||||
@ -35,6 +31,7 @@ impl InitStream {
|
||||
self,
|
||||
endpoint: &IpEndpoint,
|
||||
can_reuse: bool,
|
||||
observer: StreamObserver,
|
||||
) -> core::result::Result<BoundTcpSocket, (Error, Self)> {
|
||||
let unbound_socket = match self {
|
||||
InitStream::Unbound(unbound_socket) => unbound_socket,
|
||||
@ -49,7 +46,7 @@ impl InitStream {
|
||||
unbound_socket,
|
||||
endpoint,
|
||||
can_reuse,
|
||||
|iface, socket, config| iface.bind_tcp(socket, config),
|
||||
|iface, socket, config| iface.bind_tcp(socket, observer, config),
|
||||
) {
|
||||
Ok(bound_socket) => bound_socket,
|
||||
Err((err, unbound_socket)) => return Err((err, InitStream::Unbound(unbound_socket))),
|
||||
@ -60,25 +57,32 @@ impl InitStream {
|
||||
fn bind_to_ephemeral_endpoint(
|
||||
self,
|
||||
remote_endpoint: &IpEndpoint,
|
||||
observer: StreamObserver,
|
||||
) -> core::result::Result<BoundTcpSocket, (Error, Self)> {
|
||||
let endpoint = get_ephemeral_endpoint(remote_endpoint);
|
||||
self.bind(&endpoint, false)
|
||||
self.bind(&endpoint, false, observer)
|
||||
}
|
||||
|
||||
pub fn connect(
|
||||
self,
|
||||
remote_endpoint: &IpEndpoint,
|
||||
pollee: &Pollee,
|
||||
) -> core::result::Result<ConnectingStream, (Error, Self)> {
|
||||
let bound_socket = match self {
|
||||
InitStream::Bound(bound_socket) => bound_socket,
|
||||
InitStream::Unbound(_) => self.bind_to_ephemeral_endpoint(remote_endpoint)?,
|
||||
InitStream::Unbound(_) => self
|
||||
.bind_to_ephemeral_endpoint(remote_endpoint, StreamObserver::new(pollee.clone()))?,
|
||||
};
|
||||
|
||||
ConnectingStream::new(bound_socket, *remote_endpoint)
|
||||
.map_err(|(err, bound_socket)| (err, InitStream::Bound(bound_socket)))
|
||||
}
|
||||
|
||||
pub fn listen(self, backlog: usize) -> core::result::Result<ListenStream, (Error, Self)> {
|
||||
pub fn listen(
|
||||
self,
|
||||
backlog: usize,
|
||||
pollee: &Pollee,
|
||||
) -> core::result::Result<ListenStream, (Error, Self)> {
|
||||
let InitStream::Bound(bound_socket) = self else {
|
||||
// FIXME: The socket should be bound to INADDR_ANY (i.e., 0.0.0.0) with an ephemeral
|
||||
// port. However, INADDR_ANY is not yet supported, so we need to return an error first.
|
||||
@ -89,7 +93,7 @@ impl InitStream {
|
||||
));
|
||||
};
|
||||
|
||||
ListenStream::new(bound_socket, backlog)
|
||||
ListenStream::new(bound_socket, backlog, pollee)
|
||||
.map_err(|(err, bound_socket)| (err, InitStream::Bound(bound_socket)))
|
||||
}
|
||||
|
||||
|
@ -5,11 +5,12 @@ use aster_bigtcp::{
|
||||
};
|
||||
use ostd::sync::WriteIrqDisabled;
|
||||
|
||||
use super::connected::ConnectedStream;
|
||||
use super::{connected::ConnectedStream, StreamObserver};
|
||||
use crate::{
|
||||
events::IoEvents,
|
||||
net::iface::{BoundTcpSocket, Iface},
|
||||
prelude::*,
|
||||
process::signal::Pollee,
|
||||
};
|
||||
|
||||
pub struct ListenStream {
|
||||
@ -24,6 +25,7 @@ impl ListenStream {
|
||||
pub fn new(
|
||||
bound_socket: BoundTcpSocket,
|
||||
backlog: usize,
|
||||
pollee: &Pollee,
|
||||
) -> core::result::Result<Self, (Error, BoundTcpSocket)> {
|
||||
const SOMAXCONN: usize = 4096;
|
||||
let somaxconn = SOMAXCONN.min(backlog);
|
||||
@ -33,14 +35,14 @@ impl ListenStream {
|
||||
bound_socket,
|
||||
backlog_sockets: RwLock::new(Vec::new()),
|
||||
};
|
||||
if let Err(err) = listen_stream.fill_backlog_sockets() {
|
||||
if let Err(err) = listen_stream.fill_backlog_sockets(pollee) {
|
||||
return Err((err, listen_stream.bound_socket));
|
||||
}
|
||||
Ok(listen_stream)
|
||||
}
|
||||
|
||||
/// Append sockets listening at LocalEndPoint to support backlog
|
||||
fn fill_backlog_sockets(&self) -> Result<()> {
|
||||
fn fill_backlog_sockets(&self, pollee: &Pollee) -> Result<()> {
|
||||
let mut backlog_sockets = self.backlog_sockets.write();
|
||||
|
||||
let backlog = self.backlog;
|
||||
@ -51,14 +53,14 @@ impl ListenStream {
|
||||
}
|
||||
|
||||
for _ in current_backlog_len..backlog {
|
||||
let backlog_socket = BacklogSocket::new(&self.bound_socket)?;
|
||||
let backlog_socket = BacklogSocket::new(&self.bound_socket, pollee)?;
|
||||
backlog_sockets.push(backlog_socket);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn try_accept(&self) -> Result<ConnectedStream> {
|
||||
pub fn try_accept(&self, pollee: &Pollee) -> Result<ConnectedStream> {
|
||||
let mut backlog_sockets = self.backlog_sockets.write();
|
||||
|
||||
let index = backlog_sockets
|
||||
@ -69,7 +71,7 @@ impl ListenStream {
|
||||
})?;
|
||||
let active_backlog_socket = backlog_sockets.remove(index);
|
||||
|
||||
if let Ok(backlog_socket) = BacklogSocket::new(&self.bound_socket) {
|
||||
if let Ok(backlog_socket) = BacklogSocket::new(&self.bound_socket, pollee) {
|
||||
backlog_sockets.push(backlog_socket);
|
||||
}
|
||||
|
||||
@ -111,18 +113,22 @@ struct BacklogSocket {
|
||||
impl BacklogSocket {
|
||||
// FIXME: All of the error codes below seem to have no Linux equivalents, and I see no reason
|
||||
// why the error may occur. Perhaps it is better to call `unwrap()` directly?
|
||||
fn new(bound_socket: &BoundTcpSocket) -> Result<Self> {
|
||||
fn new(bound_socket: &BoundTcpSocket, pollee: &Pollee) -> Result<Self> {
|
||||
let local_endpoint = bound_socket.local_endpoint().ok_or(Error::with_message(
|
||||
Errno::EINVAL,
|
||||
"the socket is not bound",
|
||||
))?;
|
||||
|
||||
let unbound_socket = Box::new(UnboundTcpSocket::new(bound_socket.observer()));
|
||||
let unbound_socket = Box::new(UnboundTcpSocket::new());
|
||||
let bound_socket = {
|
||||
let iface = bound_socket.iface();
|
||||
let bind_port_config = BindPortConfig::new(local_endpoint.port, true);
|
||||
iface
|
||||
.bind_tcp(unbound_socket, bind_port_config)
|
||||
.bind_tcp(
|
||||
unbound_socket,
|
||||
StreamObserver::new(pollee.clone()),
|
||||
bind_port_config,
|
||||
)
|
||||
.map_err(|(err, _)| err)?
|
||||
};
|
||||
|
||||
|
@ -2,10 +2,7 @@
|
||||
|
||||
use core::sync::atomic::{AtomicBool, Ordering};
|
||||
|
||||
use aster_bigtcp::{
|
||||
socket::{SocketEventObserver, SocketEvents},
|
||||
wire::IpEndpoint,
|
||||
};
|
||||
use aster_bigtcp::wire::IpEndpoint;
|
||||
use connected::ConnectedStream;
|
||||
use connecting::{ConnResult, ConnectingStream};
|
||||
use init::InitStream;
|
||||
@ -40,9 +37,11 @@ mod connected;
|
||||
mod connecting;
|
||||
mod init;
|
||||
mod listen;
|
||||
mod observer;
|
||||
pub mod options;
|
||||
mod util;
|
||||
|
||||
pub(in crate::net) use self::observer::StreamObserver;
|
||||
pub use self::util::CongestionControl;
|
||||
|
||||
pub struct StreamSocket {
|
||||
@ -79,26 +78,23 @@ impl OptionSet {
|
||||
|
||||
impl StreamSocket {
|
||||
pub fn new(nonblocking: bool) -> Arc<Self> {
|
||||
Arc::new_cyclic(|me| {
|
||||
let init_stream = InitStream::new(me.clone() as _);
|
||||
Self {
|
||||
options: RwLock::new(OptionSet::new()),
|
||||
state: RwLock::new(Takeable::new(State::Init(init_stream))),
|
||||
is_nonblocking: AtomicBool::new(nonblocking),
|
||||
pollee: Pollee::new(),
|
||||
}
|
||||
let init_stream = InitStream::new();
|
||||
Arc::new(Self {
|
||||
options: RwLock::new(OptionSet::new()),
|
||||
state: RwLock::new(Takeable::new(State::Init(init_stream))),
|
||||
is_nonblocking: AtomicBool::new(nonblocking),
|
||||
pollee: Pollee::new(),
|
||||
})
|
||||
}
|
||||
|
||||
fn new_connected(connected_stream: ConnectedStream) -> Arc<Self> {
|
||||
Arc::new_cyclic(move |me| {
|
||||
connected_stream.set_observer(me.clone() as _);
|
||||
Self {
|
||||
options: RwLock::new(OptionSet::new()),
|
||||
state: RwLock::new(Takeable::new(State::Connected(connected_stream))),
|
||||
is_nonblocking: AtomicBool::new(false),
|
||||
pollee: Pollee::new(),
|
||||
}
|
||||
let pollee = Pollee::new();
|
||||
connected_stream.set_observer(StreamObserver::new(pollee.clone()));
|
||||
Arc::new(Self {
|
||||
options: RwLock::new(OptionSet::new()),
|
||||
state: RwLock::new(Takeable::new(State::Connected(connected_stream))),
|
||||
is_nonblocking: AtomicBool::new(false),
|
||||
pollee,
|
||||
})
|
||||
}
|
||||
|
||||
@ -221,7 +217,7 @@ impl StreamSocket {
|
||||
}
|
||||
};
|
||||
|
||||
let connecting_stream = match init_stream.connect(remote_endpoint) {
|
||||
let connecting_stream = match init_stream.connect(remote_endpoint, &self.pollee) {
|
||||
Ok(connecting_stream) => connecting_stream,
|
||||
Err((err, init_stream)) => {
|
||||
return (State::Init(init_stream), (Some(Err(err)), None));
|
||||
@ -276,11 +272,13 @@ impl StreamSocket {
|
||||
return_errno_with_message!(Errno::EINVAL, "the socket is not listening");
|
||||
};
|
||||
|
||||
let accepted = listen_stream.try_accept().map(|connected_stream| {
|
||||
let remote_endpoint = connected_stream.remote_endpoint();
|
||||
let accepted_socket = Self::new_connected(connected_stream);
|
||||
(accepted_socket as _, remote_endpoint.into())
|
||||
});
|
||||
let accepted = listen_stream
|
||||
.try_accept(&self.pollee)
|
||||
.map(|connected_stream| {
|
||||
let remote_endpoint = connected_stream.remote_endpoint();
|
||||
let accepted_socket = Self::new_connected(connected_stream);
|
||||
(accepted_socket as _, remote_endpoint.into())
|
||||
});
|
||||
let iface_to_poll = listen_stream.iface().clone();
|
||||
|
||||
drop(state);
|
||||
@ -451,7 +449,11 @@ impl Socket for StreamSocket {
|
||||
);
|
||||
};
|
||||
|
||||
let bound_socket = match init_stream.bind(&endpoint, can_reuse) {
|
||||
let bound_socket = match init_stream.bind(
|
||||
&endpoint,
|
||||
can_reuse,
|
||||
StreamObserver::new(self.pollee.clone()),
|
||||
) {
|
||||
Ok(bound_socket) => bound_socket,
|
||||
Err((err, init_stream)) => {
|
||||
return (State::Init(init_stream), Err(err));
|
||||
@ -492,7 +494,7 @@ impl Socket for StreamSocket {
|
||||
}
|
||||
};
|
||||
|
||||
let listen_stream = match init_stream.listen(backlog) {
|
||||
let listen_stream = match init_stream.listen(backlog, &self.pollee) {
|
||||
Ok(listen_stream) => listen_stream,
|
||||
Err((err, init_stream)) => {
|
||||
return (State::Init(init_stream), Err(err));
|
||||
@ -692,30 +694,6 @@ impl Socket for StreamSocket {
|
||||
}
|
||||
}
|
||||
|
||||
impl SocketEventObserver for StreamSocket {
|
||||
fn on_events(&self, events: SocketEvents) {
|
||||
let mut io_events = IoEvents::empty();
|
||||
|
||||
if events.contains(SocketEvents::CAN_RECV) {
|
||||
io_events |= IoEvents::IN;
|
||||
}
|
||||
|
||||
if events.contains(SocketEvents::CAN_SEND) {
|
||||
io_events |= IoEvents::OUT;
|
||||
}
|
||||
|
||||
if events.contains(SocketEvents::PEER_CLOSED) {
|
||||
io_events |= IoEvents::IN | IoEvents::RDHUP;
|
||||
}
|
||||
|
||||
if events.contains(SocketEvents::CLOSED) {
|
||||
io_events |= IoEvents::IN | IoEvents::OUT | IoEvents::RDHUP | IoEvents::HUP;
|
||||
}
|
||||
|
||||
self.pollee.notify(io_events);
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for StreamSocket {
|
||||
fn drop(&mut self) {
|
||||
let state = self.state.write().take();
|
||||
|
37
kernel/src/net/socket/ip/stream/observer.rs
Normal file
37
kernel/src/net/socket/ip/stream/observer.rs
Normal file
@ -0,0 +1,37 @@
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
use aster_bigtcp::socket::{SocketEventObserver, SocketEvents};
|
||||
|
||||
use crate::{events::IoEvents, process::signal::Pollee};
|
||||
|
||||
pub struct StreamObserver(Pollee);
|
||||
|
||||
impl StreamObserver {
|
||||
pub(super) fn new(pollee: Pollee) -> Self {
|
||||
Self(pollee)
|
||||
}
|
||||
}
|
||||
|
||||
impl SocketEventObserver for StreamObserver {
|
||||
fn on_events(&self, events: SocketEvents) {
|
||||
let mut io_events = IoEvents::empty();
|
||||
|
||||
if events.contains(SocketEvents::CAN_RECV) {
|
||||
io_events |= IoEvents::IN;
|
||||
}
|
||||
|
||||
if events.contains(SocketEvents::CAN_SEND) {
|
||||
io_events |= IoEvents::OUT;
|
||||
}
|
||||
|
||||
if events.contains(SocketEvents::PEER_CLOSED) {
|
||||
io_events |= IoEvents::IN | IoEvents::RDHUP;
|
||||
}
|
||||
|
||||
if events.contains(SocketEvents::CLOSED) {
|
||||
io_events |= IoEvents::IN | IoEvents::OUT | IoEvents::RDHUP | IoEvents::HUP;
|
||||
}
|
||||
|
||||
self.0.notify(io_events);
|
||||
}
|
||||
}
|
@ -28,6 +28,7 @@ use crate::{
|
||||
///
|
||||
/// Then, [`Pollee::poll_with`] can allow you to register a [`Poller`] to wait for certain events,
|
||||
/// or register a [`PollAdaptor`] to be notified when certain events occur.
|
||||
#[derive(Clone)]
|
||||
pub struct Pollee {
|
||||
inner: Arc<PolleeInner>,
|
||||
}
|
||||
|
@ -4,7 +4,7 @@ use super::SyscallReturn;
|
||||
use crate::{
|
||||
fs::{file_handle::FileLike, file_table::FdFlags},
|
||||
net::socket::{
|
||||
ip::{DatagramSocket, StreamSocket},
|
||||
ip::{datagram::DatagramSocket, stream::StreamSocket},
|
||||
unix::UnixStreamSocket,
|
||||
vsock::VsockStreamSocket,
|
||||
},
|
||||
|
Reference in New Issue
Block a user