Skip to content

Commit 7282f4c

Browse files
committed
make data transfer timeout configurable
1 parent 3362ec7 commit 7282f4c

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,9 @@ OPTIONS:
168168
-s, --stats-interval <SECONDS>
169169
Interval of showing data transfer statistics (0 = disabled) [default: 0]
170170
171+
-t, --timeout-secs <SECONDS>
172+
Data transfer timeout [default: 5]
173+
171174
-u, --udc <UDC>
172175
UDC Controller name
173176

src/io_uring.rs

+14-5
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ type Result<T> = std::result::Result<T, Box<dyn std::error::Error + Send + Sync>
2727

2828
const USB_ACCESSORY_PATH: &str = "/dev/usb_accessory";
2929
const BUFFER_LEN: usize = 16 * 1024;
30-
const READ_TIMEOUT: Duration = Duration::new(5, 0);
3130
const TCP_CLIENT_TIMEOUT: Duration = Duration::new(30, 0);
3231

3332
// tokio_uring::fs::File and tokio_uring::net::TcpStream are using different
@@ -69,6 +68,7 @@ async fn copy<A: Endpoint<A>, B: Endpoint<B>>(
6968
dbg_name_from: &'static str,
7069
dbg_name_to: &'static str,
7170
bytes_written: Arc<AtomicUsize>,
71+
read_timeout: Duration,
7272
) -> Result<()> {
7373
let mut buf = vec![0u8; BUFFER_LEN];
7474
loop {
@@ -77,7 +77,7 @@ async fn copy<A: Endpoint<A>, B: Endpoint<B>>(
7777
// which `Vec<u8>` implements!
7878
debug!("{}: before read", dbg_name_from);
7979
let retval = from.read(buf);
80-
let (res, buf_read) = timeout(READ_TIMEOUT, retval)
80+
let (res, buf_read) = timeout(read_timeout, retval)
8181
.await
8282
.map_err(|e| -> String { format!("{} read: {}", dbg_name_from, e) })?;
8383
// Propagate errors, see how many bytes we read
@@ -93,7 +93,7 @@ async fn copy<A: Endpoint<A>, B: Endpoint<B>>(
9393
// into the full `Vec<u8>`
9494
debug!("{}: before write", dbg_name_to);
9595
let retval = to.write(buf_read.slice(..n)).submit();
96-
let (res, buf_write) = timeout(READ_TIMEOUT, retval)
96+
let (res, buf_write) = timeout(read_timeout, retval)
9797
.await
9898
.map_err(|e| -> String { format!("{} write: {}", dbg_name_to, e) })?;
9999
let n = res?;
@@ -112,6 +112,7 @@ async fn transfer_monitor(
112112
stats_interval: Option<Duration>,
113113
usb_bytes_written: Arc<AtomicUsize>,
114114
tcp_bytes_written: Arc<AtomicUsize>,
115+
read_timeout: Duration,
115116
) -> Result<()> {
116117
let mut usb_bytes_out_last: usize = 0;
117118
let mut tcp_bytes_out_last: usize = 0;
@@ -163,7 +164,7 @@ async fn transfer_monitor(
163164
}
164165

165166
// transfer stall detection
166-
if stall_check.elapsed() > READ_TIMEOUT {
167+
if stall_check.elapsed() > read_timeout {
167168
// compute delta since last check
168169
stall_usb_bytes_last = usb_bytes_out - stall_usb_bytes_last;
169170
stall_tcp_bytes_last = tcp_bytes_out - stall_tcp_bytes_last;
@@ -194,6 +195,7 @@ pub async fn io_loop(
194195
stats_interval: Option<Duration>,
195196
need_restart: Arc<Notify>,
196197
tcp_start: Arc<Notify>,
198+
read_timeout: Duration,
197199
) -> Result<()> {
198200
info!("{} 🛰️ Starting TCP server...", NAME);
199201
let bind_addr = format!("0.0.0.0:{}", TCP_SERVER_PORT).parse().unwrap();
@@ -256,17 +258,24 @@ pub async fn io_loop(
256258
"USB",
257259
"TCP",
258260
stream_bytes.clone(),
261+
read_timeout,
259262
));
260263
let mut from_stream = tokio_uring::spawn(copy(
261264
stream.clone(),
262265
file.clone(),
263266
"TCP",
264267
"USB",
265268
file_bytes.clone(),
269+
read_timeout,
266270
));
267271

268272
// Thread for monitoring transfer
269-
let mut monitor = tokio::spawn(transfer_monitor(stats_interval, file_bytes, stream_bytes));
273+
let mut monitor = tokio::spawn(transfer_monitor(
274+
stats_interval,
275+
file_bytes,
276+
stream_bytes,
277+
read_timeout,
278+
));
270279

271280
// Stop as soon as one of them errors
272281
let res = tokio::try_join!(

src/main.rs

+6
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ struct Args {
8080
/// so that the phone can remain connected (used in special configurations)
8181
#[clap(short, long)]
8282
keepalive: bool,
83+
84+
/// Data transfer timeout
85+
#[clap(short, long, value_name = "SECONDS", default_value_t = 5)]
86+
timeout_secs: u16,
8387
}
8488

8589
#[derive(Clone)]
@@ -247,6 +251,7 @@ fn main() {
247251
Some(Duration::from_secs(args.stats_interval.into()))
248252
}
249253
};
254+
let read_timeout = Duration::from_secs(args.timeout_secs.into());
250255

251256
info!(
252257
"🛸 <b><blue>aa-proxy-rs</> is starting, build: {}, git: {}-{}",
@@ -283,6 +288,7 @@ fn main() {
283288
stats_interval,
284289
need_restart_cloned,
285290
tcp_start_cloned,
291+
read_timeout,
286292
));
287293

288294
info!(

0 commit comments

Comments
 (0)