mirror of
https://github.com/tun2proxy/tun2proxy.git
synced 2025-04-23 15:29:10 +00:00
read_data_from_tcp_stream for callback
This commit is contained in:
parent
03aa70f3c2
commit
05cb35fabb
1 changed files with 17 additions and 7 deletions
|
@ -567,7 +567,11 @@ impl<'a> TunToProxy<'a> {
|
|||
assert!(state.dns_over_tcp_expiry.is_some());
|
||||
state.dns_over_tcp_expiry = Some(Self::common_udp_life_timeout());
|
||||
|
||||
let vecbuf = Self::read_data_from_tcp_stream(&mut state.mio_stream)?;
|
||||
let mut vecbuf = vec![];
|
||||
Self::read_data_from_tcp_stream(&mut state.mio_stream, |data| {
|
||||
vecbuf.extend_from_slice(data);
|
||||
Ok(())
|
||||
})?;
|
||||
|
||||
let data_event = IncomingDataEvent {
|
||||
direction: IncomingDirection::FromServer,
|
||||
|
@ -1029,7 +1033,11 @@ impl<'a> TunToProxy<'a> {
|
|||
let state = self.connection_map.get_mut(&conn_info).ok_or(e)?;
|
||||
|
||||
// TODO: Move this reading process to its own function.
|
||||
let vecbuf = Self::read_data_from_tcp_stream(&mut state.mio_stream)?;
|
||||
let mut vecbuf = vec![];
|
||||
Self::read_data_from_tcp_stream(&mut state.mio_stream, |data| {
|
||||
vecbuf.extend_from_slice(data);
|
||||
Ok(())
|
||||
})?;
|
||||
|
||||
let data_event = IncomingDataEvent {
|
||||
direction: IncomingDirection::FromServer,
|
||||
|
@ -1095,17 +1103,19 @@ impl<'a> TunToProxy<'a> {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn read_data_from_tcp_stream(stream: &mut TcpStream) -> Result<Vec<u8>> {
|
||||
let mut vecbuf = Vec::<u8>::new();
|
||||
fn read_data_from_tcp_stream<F>(stream: &mut TcpStream, mut callback: F) -> Result<()>
|
||||
where
|
||||
F: FnMut(&mut [u8]) -> Result<()>,
|
||||
{
|
||||
let mut tmp: [u8; 4096] = [0_u8; 4096];
|
||||
loop {
|
||||
let mut tmp: [u8; 4096] = [0_u8; 4096];
|
||||
match stream.read(&mut tmp) {
|
||||
Ok(0) => {
|
||||
// The tcp connection closed
|
||||
break;
|
||||
}
|
||||
Ok(read_result) => {
|
||||
vecbuf.extend_from_slice(&tmp[0..read_result]);
|
||||
callback(&mut tmp[0..read_result])?;
|
||||
}
|
||||
Err(error) => {
|
||||
if error.kind() == std::io::ErrorKind::WouldBlock {
|
||||
|
@ -1120,7 +1130,7 @@ impl<'a> TunToProxy<'a> {
|
|||
}
|
||||
};
|
||||
}
|
||||
Ok(vecbuf)
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(any(target_os = "linux", target_os = "macos"))]
|
||||
|
|
Loading…
Add table
Reference in a new issue