forked from EAimTY/tuic
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
58 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,75 @@ | ||
use std::time::Duration; | ||
|
||
use tokio::io::{AsyncReadExt, AsyncWriteExt}; | ||
// use tokio::io::{AsyncReadExt, AsyncWriteExt}; | ||
|
||
const BUFFER_SIZE: usize = 8 * 1024; | ||
// const BUFFER_SIZE: usize = 8 * 1024; | ||
|
||
#[allow(dead_code)] | ||
pub async fn exchange_tcp( | ||
a: &mut tuic_quinn::Connect, | ||
b: &mut tokio::net::TcpStream, | ||
timeout: Duration, | ||
) -> (usize, usize, Option<eyre::Error>) { | ||
let mut a2b = [0u8; BUFFER_SIZE]; | ||
let mut b2a = [0u8; BUFFER_SIZE]; | ||
// let mut a2b = [0u8; BUFFER_SIZE]; | ||
// let mut b2a = [0u8; BUFFER_SIZE]; | ||
|
||
let mut a2b_num = 0; | ||
let mut b2a_num = 0; | ||
// let mut a2b_num = 0; | ||
// let mut b2a_num = 0; | ||
|
||
let a2b_num = 0; | ||
let b2a_num = 0; | ||
|
||
let mut last_err = None; | ||
let mut timeout = tokio::time::interval(timeout); | ||
timeout.reset(); | ||
loop { | ||
tokio::select! { | ||
_ = timeout.tick() => { | ||
last_err = Some(eyre::eyre!("TCP stream timeout")); | ||
break; | ||
}, | ||
if let Err(err) = tokio::io::copy_bidirectional(a,b).await { | ||
last_err = Some(err.into()); | ||
} | ||
// loop { | ||
// tokio::select! { | ||
// _ = timeout.tick() => { | ||
// last_err = Some(eyre::eyre!("TCP stream timeout")); | ||
// break; | ||
// }, | ||
|
||
a2b_res = a.recv.read(&mut a2b) => match a2b_res { | ||
Ok(Some(num)) => { | ||
a2b_num += num; | ||
if let Err(err) = b.write(&a2b).await { | ||
last_err = Some(err.into()); | ||
break; | ||
} | ||
}, | ||
// EOF | ||
Ok(None) => { | ||
break; | ||
}, | ||
Err(err) => { | ||
last_err = Some(err.into()); | ||
break; | ||
} | ||
}, | ||
// a2b_res = a.recv.read(&mut a2b) => match a2b_res { | ||
// Ok(Some(num)) => { | ||
// a2b_num += num; | ||
// if let Err(err) = b.write(&a2b).await { | ||
// last_err = Some(err.into()); | ||
// break; | ||
// } | ||
// }, | ||
// // EOF | ||
// Ok(None) => { | ||
// break; | ||
// }, | ||
// Err(err) => { | ||
// last_err = Some(err.into()); | ||
// break; | ||
// } | ||
// }, | ||
|
||
b2a_res = b.read(&mut b2a) => match b2a_res { | ||
Ok(num) => { | ||
// EOF | ||
if num == 0 { | ||
break; | ||
} | ||
b2a_num += num; | ||
if let Err(err) = a.send.write(&b2a).await { | ||
last_err = Some(err.into()); | ||
break; | ||
} | ||
}, | ||
Err(err) => { | ||
last_err = Some(err.into()); | ||
break; | ||
}, | ||
} | ||
// b2a_res = b.read(&mut b2a) => match b2a_res { | ||
// Ok(num) => { | ||
// // EOF | ||
// if num == 0 { | ||
// break; | ||
// } | ||
// b2a_num += num; | ||
// if let Err(err) = a.send.write(&b2a).await { | ||
// last_err = Some(err.into()); | ||
// break; | ||
// } | ||
// }, | ||
// Err(err) => { | ||
// last_err = Some(err.into()); | ||
// break; | ||
// }, | ||
// } | ||
|
||
} | ||
} | ||
// } | ||
// } | ||
|
||
(a2b_num, b2a_num, last_err) | ||
} |