use std::net::TcpStream; use std::io::prelude::*; use std::thread; use std::sync::{Arc, Mutex, RwLock}; use crate::protocol; pub mod guard; pub struct Client<'a>{ client: Arc>, server:Arc>, hs: protocol::HandShake<'a>, run : Arc>, } impl<'a> Client<'a> { pub fn new(client: TcpStream, server: TcpStream, handshake: protocol::HandShake) -> Client{ Client { client: Arc::new(Mutex::new(client)), server: Arc::new(Mutex::new(server)), hs: handshake, run: Arc::new(RwLock::new(true)), //threads: None, } } pub fn to_string(&self){ println!("len_pack {}", self.hs.get_host_name()); } fn join_conexions_mutex(c1: Arc>, c2: Arc>, run: Arc>){ let mut buf: [u8; 100000] = [0; 100000]; let mut client = c1.lock().unwrap().try_clone().unwrap(); while *run.read().unwrap() { let res=client.read(&mut buf); match res { Ok(leng) => { if leng == 0 { *run.write().unwrap()=false; } match c2.lock().unwrap().write(&buf [.. leng]) { Ok(_l) => {}, Err(_e) => *run.write().unwrap()=false, } }, Err(_e) => *run.write().unwrap()=false, } } } pub fn start_proxy(&self) -> (thread::JoinHandle<()>, thread::JoinHandle<()>) { let c1 = self.client.clone(); let s1 = self.server.clone(); let r1 = self.run.clone(); let handler1 = thread::spawn( || { Self::join_conexions_mutex(s1, c1, r1)} ); let c2 = self.client.clone(); let s2 = self.server.clone(); let r2 = self.run.clone(); let handler2 = thread::spawn( || { Self::join_conexions_mutex(c2, s2, r2)} ); return (handler1, handler2); } }