mirror of
https://github.com/asterinas/asterinas.git
synced 2025-06-24 01:43:22 +00:00
Add socket hash table
This commit is contained in:
committed by
Tate, Hongliang Tian
parent
783345b90b
commit
39cc0dca26
@ -1,6 +1,7 @@
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
use aster_bigtcp::{
|
||||
errors::tcp::ConnectError,
|
||||
socket::{ConnectState, RawTcpOption, RawTcpSetOption},
|
||||
wire::IpEndpoint,
|
||||
};
|
||||
@ -30,22 +31,30 @@ impl ConnectingStream {
|
||||
option: &RawTcpOption,
|
||||
observer: StreamObserver,
|
||||
) -> core::result::Result<Self, (Error, BoundPort)> {
|
||||
// The only reason this method might fail is because we're trying to connect to an
|
||||
// unspecified address (i.e. 0.0.0.0). We currently have no support for binding to,
|
||||
// listening on, or connecting to the unspecified address.
|
||||
//
|
||||
// We assume the remote will just refuse to connect, so we return `ECONNREFUSED`.
|
||||
let tcp_conn =
|
||||
match TcpConnection::new_connect(bound_port, remote_endpoint, option, observer) {
|
||||
Ok(tcp_conn) => tcp_conn,
|
||||
Err((bound_port, ConnectError::AddressInUse)) => {
|
||||
return Err((
|
||||
Error::with_message(Errno::EADDRNOTAVAIL, "connection key conflicts"),
|
||||
bound_port,
|
||||
))
|
||||
}
|
||||
Err((bound_port, _)) => {
|
||||
// The only reason this method might go to this branch is because
|
||||
// we're trying to connect to an unspecified address (i.e. 0.0.0.0).
|
||||
// We currently have no support for binding to,
|
||||
// listening on, or connecting to the unspecified address.
|
||||
//
|
||||
// We assume the remote will just refuse to connect,
|
||||
// so we return `ECONNREFUSED`.
|
||||
return Err((
|
||||
Error::with_message(
|
||||
Errno::ECONNREFUSED,
|
||||
"connecting to an unspecified address is not supported",
|
||||
),
|
||||
bound_port,
|
||||
))
|
||||
));
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -88,7 +88,10 @@ impl InitStream {
|
||||
));
|
||||
};
|
||||
|
||||
Ok(ListenStream::new(bound_port, backlog, option, observer))
|
||||
match ListenStream::new(bound_port, backlog, option, observer) {
|
||||
Ok(listen_stream) => Ok(listen_stream),
|
||||
Err((bound_port, error)) => Err((error, Self::Bound(bound_port))),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn local_endpoint(&self) -> Option<IpEndpoint> {
|
||||
|
@ -1,6 +1,7 @@
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
use aster_bigtcp::{
|
||||
errors::tcp::ListenError,
|
||||
socket::{RawTcpOption, RawTcpSetOption},
|
||||
wire::IpEndpoint,
|
||||
};
|
||||
@ -22,18 +23,20 @@ impl ListenStream {
|
||||
backlog: usize,
|
||||
option: &RawTcpOption,
|
||||
observer: StreamObserver,
|
||||
) -> Self {
|
||||
) -> core::result::Result<Self, (BoundPort, Error)> {
|
||||
const SOMAXCONN: usize = 4096;
|
||||
let max_conn = SOMAXCONN.min(backlog);
|
||||
|
||||
let tcp_listener = match TcpListener::new_listen(bound_port, max_conn, option, observer) {
|
||||
Ok(tcp_listener) => tcp_listener,
|
||||
match TcpListener::new_listen(bound_port, max_conn, option, observer) {
|
||||
Ok(tcp_listener) => Ok(Self { tcp_listener }),
|
||||
Err((bound_port, ListenError::AddressInUse)) => Err((
|
||||
bound_port,
|
||||
Error::with_message(Errno::EADDRINUSE, "listener key conflicts"),
|
||||
)),
|
||||
Err((_, err)) => {
|
||||
unreachable!("`new_listen` fails with {:?}, which should not happen", err)
|
||||
}
|
||||
};
|
||||
|
||||
Self { tcp_listener }
|
||||
}
|
||||
}
|
||||
|
||||
pub fn try_accept(&self) -> Result<ConnectedStream> {
|
||||
|
Reference in New Issue
Block a user