work connection

This commit is contained in:
2022-09-19 23:30:35 +02:00
parent 8266ef59e0
commit 464b661bd7
24 changed files with 25 additions and 17 deletions

View File

@@ -1,7 +1,7 @@
use std::net::TcpStream;
use std::io::prelude::*;
use std::thread;
use std::sync::{Arc, Mutex};
use std::sync::{Arc, Mutex, RwLock};
mod HandShake;
@@ -11,7 +11,7 @@ pub struct Client<'a>{
//client: &'static TcpStream,
//server: &'static TcpStream,
hs: HandShake::HandShake<'a>,
run : Arc<Mutex<bool>>,
run : Arc<RwLock<bool>>,
//run: &'static bool
}
@@ -23,7 +23,7 @@ impl<'a> Client<'a> {
//client: client,
//server: server,
hs: HandShake::HandShake::new(handshake),
run: Arc::new(Mutex::new(true)),
run: Arc::new(RwLock::new(true)),
//run: &true,
}
}
@@ -34,19 +34,23 @@ impl<'a> Client<'a> {
fn join_conexions_mutex(c1: Arc<Mutex<TcpStream>>,
c2: Arc<Mutex<TcpStream>>,
run: Arc<Mutex<bool>>,
run: Arc<RwLock<bool>>,
id: i32){
let mut buf: [u8; 1000000] = [0; 1000000];
while *run.lock().unwrap() {
let mut client = c1.lock().unwrap().try_clone().unwrap();
while *run.read().unwrap() {
println!("read{}",id);
let res=c1.lock().unwrap().read(&mut buf);
let res=client.read(&mut buf);
match res {
Ok(leng) => {
println!("rx {}",leng);
if leng == 0 {
*run.write().unwrap()=false;
}
println!("tx {}",c2.lock().unwrap().write(&buf [.. leng]).unwrap());
},
//Ok(leng) => {c2.lock().unwrap().write(&buf);},
Err(_e) => {*run.lock().unwrap()=false;},
Err(_e) => {*run.write().unwrap()=false;},
}
println!("write{}",id);