Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Itsusinn committed Dec 21, 2024
1 parent 3632d0d commit cf4b401
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 50 deletions.
3 changes: 2 additions & 1 deletion tuic-server/src/connection/handle_task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl Connection {
let target_addr = conn.addr().to_string();

info!(
"[{id:#010x}] [{addr}] [{user}] [TCP] {target_addr}",
"[{id:#010x}] [{addr}] [{user}] [TCP] {target_addr} ",
id = self.id(),
addr = self.inner.remote_address(),
user = self.auth,
Expand Down Expand Up @@ -63,6 +63,7 @@ impl Connection {
// a <- b rx
let (tx, rx, err) =
exchange_tcp(&mut conn, &mut stream, self.ctx.cfg.stream_timeout).await;
// let (tx, rx) = tokio::io::copy_bidirectional(&mut conn, &mut stream).await?;
_ = conn.reset(ERROR_CODE);
_ = stream.shutdown().await;

Expand Down
2 changes: 1 addition & 1 deletion tuic-server/src/connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ mod handle_stream;
mod handle_task;
mod udp_session;

pub const ERROR_CODE: VarInt = VarInt::from_u32(0);
pub const ERROR_CODE: VarInt = VarInt::from_u32(6000);
pub const INIT_CONCURRENT_STREAMS: u32 = 32;

#[derive(Clone)]
Expand Down
103 changes: 55 additions & 48 deletions tuic-server/src/io.rs
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)
}

0 comments on commit cf4b401

Please sign in to comment.