compile but not works propertly

This commit is contained in:
2022-09-19 22:22:09 +02:00
commit 8266ef59e0
36 changed files with 938 additions and 0 deletions

90
src/client/mod.rs Normal file
View File

@@ -0,0 +1,90 @@
use std::net::TcpStream;
use std::io::prelude::*;
use std::thread;
use std::sync::{Arc, Mutex};
mod HandShake;
pub struct Client<'a>{
client: Arc<Mutex<TcpStream>>,
server:Arc<Mutex<TcpStream>>,
//client: &'static TcpStream,
//server: &'static TcpStream,
hs: HandShake::HandShake<'a>,
run : Arc<Mutex<bool>>,
//run: &'static bool
}
impl<'a> Client<'a> {
pub fn new(client: TcpStream, server: TcpStream, handshake: &[u8]) -> Client{
Client {
client: Arc::new(Mutex::new(client)),
server: Arc::new(Mutex::new(server)),
//client: client,
//server: server,
hs: HandShake::HandShake::new(handshake),
run: Arc::new(Mutex::new(true)),
//run: &true,
}
}
pub fn to_string(&self){
println!("len_pack {}", self.hs.getHostName());
}
fn join_conexions_mutex(c1: Arc<Mutex<TcpStream>>,
c2: Arc<Mutex<TcpStream>>,
run: Arc<Mutex<bool>>,
id: i32){
let mut buf: [u8; 1000000] = [0; 1000000];
while *run.lock().unwrap() {
println!("read{}",id);
let res=c1.lock().unwrap().read(&mut buf);
match res {
Ok(leng) => {
println!("rx {}",leng);
println!("tx {}",c2.lock().unwrap().write(&buf [.. leng]).unwrap());
},
//Ok(leng) => {c2.lock().unwrap().write(&buf);},
Err(_e) => {*run.lock().unwrap()=false;},
}
println!("write{}",id);
}
}
pub fn start_proxy(&self) -> (thread::JoinHandle<()>, thread::JoinHandle<()>) {
//let c = client.client;
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,1)}
);
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,2)}
);
return (handler1, handler2);
/*let handler = thread::spawn( || {
Self::join_conexions(&mut client.client,
&mut client.server,
&mut client.run)}
);
let handler2 = thread::spawn( || {
Self::join_conexions(&mut client.client,
&mut client.server,
&mut client.run)}
);*/
}
}