clean unused mutex

This commit is contained in:
Guillermo Roche 2022-09-30 00:58:10 +02:00
parent a698bbb14f
commit ffab5ed4a5

View File

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