compile but not works propertly
This commit is contained in:
31
src/client/HandShake.rs
Normal file
31
src/client/HandShake.rs
Normal file
@@ -0,0 +1,31 @@
|
||||
use std::string;
|
||||
|
||||
|
||||
|
||||
pub struct HandShake<'a> {
|
||||
len_pack: u8,
|
||||
len_dom: u8,
|
||||
datagram: &'a[u8],
|
||||
host: &'a[u8],
|
||||
port: &'a[u8],
|
||||
}
|
||||
|
||||
impl<'a> HandShake<'a>{
|
||||
pub fn new(data: &[u8]) -> HandShake {
|
||||
let len_pack = data[0];
|
||||
println!("{}", len_pack);
|
||||
let len_dom = data[4];
|
||||
println!("{}", len_dom);
|
||||
HandShake {
|
||||
len_pack: len_pack,
|
||||
len_dom: len_dom,
|
||||
datagram: data.clone(),
|
||||
host: (&data[5 .. ((len_dom + 5) as usize)]).clone(),
|
||||
port: (&data[((len_pack - 2) as usize) .. ((len_pack - 1) as usize)]).clone(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn getHostName(&self) -> String {
|
||||
String::from_utf8(self.datagram[5 .. ((self.len_dom+5) as usize)].to_vec()).unwrap()
|
||||
}
|
||||
}
|
||||
90
src/client/mod.rs
Normal file
90
src/client/mod.rs
Normal 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)}
|
||||
);*/
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user