use std::net::TcpStream; use std::io::prelude::*; use std::thread; use std::sync::{Arc, RwLock}; use crate::protocol; pub mod guard; pub struct Client<'a>{ client: TcpStream, server:TcpStream, hs: protocol::HandShake<'a>, run : Arc>, } impl<'a> Client<'a> { pub fn new(client: TcpStream, server: TcpStream, handshake: protocol::HandShake) -> Client{ Client { client: client, server: server, hs: handshake, run: Arc::new(RwLock::new(true)), //threads: None, } } fn join_conexions(mut origin: TcpStream, mut dest: TcpStream, run: Arc>){ let mut buf: [u8; 200] = [0; 200]; while *run.read().unwrap() { let res=origin.read(&mut buf); match res { Ok(leng) => { if leng == 0 { *run.write().unwrap()=false; } match dest.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.try_clone().unwrap(); let s1 = self.server.try_clone().unwrap(); let r1 = self.run.clone(); let handler1 = thread::spawn( || { Self::join_conexions(s1, c1, r1)} ); let c2 = self.client.try_clone().unwrap(); let s2 = self.server.try_clone().unwrap(); let r2 = self.run.clone(); let handler2 = thread::spawn( || { Self::join_conexions(c2, s2, r2)} ); return (handler1, handler2); } }