Skip to content

Commit

Permalink
Make tcp socket creation non blocking (#1509)
Browse files Browse the repository at this point in the history
  • Loading branch information
AzazKamaz authored Apr 21, 2024
1 parent 1ff42d2 commit 27832ea
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions crates/shadowsocks-service/src/local/tun/tcp.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{
collections::HashMap,
future::Future,
io::{self, ErrorKind},
mem,
net::{IpAddr, SocketAddr},
Expand Down Expand Up @@ -113,12 +114,12 @@ impl Drop for TcpConnection {
}

impl TcpConnection {
async fn new(
fn new(
socket: TcpSocket<'static>,
socket_creation_tx: &mpsc::UnboundedSender<TcpSocketCreation>,
manager_notify: Arc<ManagerNotify>,
tcp_opts: &TcpSocketOpts,
) -> TcpConnection {
) -> impl Future<Output = TcpConnection> {
let send_buffer_size = tcp_opts.send_buffer_size.unwrap_or(DEFAULT_TCP_SEND_BUFFER_SIZE);
let recv_buffer_size = tcp_opts.recv_buffer_size.unwrap_or(DEFAULT_TCP_RECV_BUFFER_SIZE);

Expand All @@ -136,11 +137,13 @@ impl TcpConnection {
socket,
socket_created_tx: tx,
});
// waiting socket add to SocketSet
let _ = rx.await;
TcpConnection {
control,
manager_notify,
async move {
// waiting socket add to SocketSet
let _ = rx.await;
TcpConnection {
control,
manager_notify,
}
}
}
}
Expand Down Expand Up @@ -524,13 +527,13 @@ impl TcpTun {
&self.manager_socket_creation_tx,
self.manager_notify.clone(),
&accept_opts.tcp,
)
.await;
);

// establish a tunnel
let context = self.context.clone();
let balancer = self.balancer.clone();
tokio::spawn(async move {
let connection = connection.await;
if let Err(err) = handle_redir_client(context, balancer, connection, src_addr, dst_addr).await {
error!("TCP tunnel failure, {} <-> {}, error: {}", src_addr, dst_addr, err);
}
Expand Down

0 comments on commit 27832ea

Please sign in to comment.