Skip to content

Commit 27832ea

Browse files
authored
Make tcp socket creation non blocking (#1509)
1 parent 1ff42d2 commit 27832ea

File tree

1 file changed

+12
-9
lines changed
  • crates/shadowsocks-service/src/local/tun

1 file changed

+12
-9
lines changed

crates/shadowsocks-service/src/local/tun/tcp.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::{
22
collections::HashMap,
3+
future::Future,
34
io::{self, ErrorKind},
45
mem,
56
net::{IpAddr, SocketAddr},
@@ -113,12 +114,12 @@ impl Drop for TcpConnection {
113114
}
114115

115116
impl TcpConnection {
116-
async fn new(
117+
fn new(
117118
socket: TcpSocket<'static>,
118119
socket_creation_tx: &mpsc::UnboundedSender<TcpSocketCreation>,
119120
manager_notify: Arc<ManagerNotify>,
120121
tcp_opts: &TcpSocketOpts,
121-
) -> TcpConnection {
122+
) -> impl Future<Output = TcpConnection> {
122123
let send_buffer_size = tcp_opts.send_buffer_size.unwrap_or(DEFAULT_TCP_SEND_BUFFER_SIZE);
123124
let recv_buffer_size = tcp_opts.recv_buffer_size.unwrap_or(DEFAULT_TCP_RECV_BUFFER_SIZE);
124125

@@ -136,11 +137,13 @@ impl TcpConnection {
136137
socket,
137138
socket_created_tx: tx,
138139
});
139-
// waiting socket add to SocketSet
140-
let _ = rx.await;
141-
TcpConnection {
142-
control,
143-
manager_notify,
140+
async move {
141+
// waiting socket add to SocketSet
142+
let _ = rx.await;
143+
TcpConnection {
144+
control,
145+
manager_notify,
146+
}
144147
}
145148
}
146149
}
@@ -524,13 +527,13 @@ impl TcpTun {
524527
&self.manager_socket_creation_tx,
525528
self.manager_notify.clone(),
526529
&accept_opts.tcp,
527-
)
528-
.await;
530+
);
529531

530532
// establish a tunnel
531533
let context = self.context.clone();
532534
let balancer = self.balancer.clone();
533535
tokio::spawn(async move {
536+
let connection = connection.await;
534537
if let Err(err) = handle_redir_client(context, balancer, connection, src_addr, dst_addr).await {
535538
error!("TCP tunnel failure, {} <-> {}, error: {}", src_addr, dst_addr, err);
536539
}

0 commit comments

Comments
 (0)