work connection

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

View File

@ -4,7 +4,7 @@ mod client;
fn main() { fn main() {
let listener = TcpListener::bind("127.0.0.1:25567").unwrap(); let listener = TcpListener::bind("127.0.0.1:25567").unwrap();
let mut buf: [u8; 128] = [1; 128]; let mut buf: [u8; 256] = [1; 256];
for stream in listener.incoming() { for stream in listener.incoming() {
//stream.unwrap().write(buf); //stream.unwrap().write(buf);
match stream { match stream {
@ -15,13 +15,17 @@ fn main() {
stream.flush();*/ stream.flush();*/
println!("Go!"); println!("Go!");
let leng = stream.read(&mut buf).unwrap(); let leng = stream.read(&mut buf).unwrap();
println!("{}", leng);
if buf[0] < 200 {
buf[(buf[0]-1) as usize] += 2; buf[(buf[0]-1) as usize] += 2;
let mut sstream = TcpStream::connect("127.0.0.1:25565").unwrap(); let mut sstream = TcpStream::connect("127.0.0.1:25565").unwrap();
sstream.write(&buf[.. leng]); sstream.write(&buf[.. leng]);
let c1 = client::Client::new(stream,sstream, &buf); let c1 = client::Client::new(stream,sstream, &buf);
c1.to_string(); c1.to_string();
c1.start_proxy().0.join(); c1.start_proxy();
c1.start_proxy().1.join(); }
//c1.start_proxy().0.join();
//c1.start_proxy().1.join();
}, },
Err(_e) => println!("{}",_e), Err(_e) => println!("{}",_e),

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long