add conexion number control

This commit is contained in:
Guillermo Roche 2022-09-24 14:26:07 +02:00
parent aeb69bab26
commit 230d5d1c82
6 changed files with 79 additions and 34 deletions

44
src/client/guard.rs Normal file
View File

@ -0,0 +1,44 @@
use std::collections::HashMap;
use std::sync::{Arc, RwLock};
use std::thread;
const MAX_THREADS : usize = 200;
pub struct Guard {
cont: Arc<RwLock<usize>>,
}
impl Guard {
pub fn new() -> Self {
Self{
//threads: HashMap::new(),
cont: Arc::new(RwLock::new(0)),
}
}
pub fn can_add(&self) -> bool {
*self.cont.read().unwrap() < MAX_THREADS
}
pub fn add_thread(&mut self,
threads: (thread::JoinHandle<()>, thread::JoinHandle<()>))-> bool {
if self.can_add() {
//self.threads.insert(id, threads);
*self.cont.write().unwrap() += 1;
let cont: Arc<RwLock<usize>> = self.cont.clone();
thread::spawn(move || {Self::whatch_client(cont, threads)});
return true;
} else {
false
}
}
fn whatch_client(cont: Arc<RwLock<usize>>,
threads: (thread::JoinHandle<()>, thread::JoinHandle<()>)){
threads.0.join();
threads.1.join();
println!("Cliente muerto {}", *cont.read().unwrap());
*cont.write().unwrap() -= 1;
}
}

View File

@ -4,6 +4,7 @@ use std::thread;
use std::sync::{Arc, Mutex, RwLock};
use crate::protocol;
pub mod guard;
pub struct Client<'a>{
client: Arc<Mutex<TcpStream>>,
@ -17,11 +18,8 @@ impl<'a> Client<'a> {
Client {
client: Arc::new(Mutex::new(client)),
server: Arc::new(Mutex::new(server)),
//client: client,
//server: server,
hs: handshake,
run: Arc::new(RwLock::new(true)),
//run: &true,
}
}
@ -32,7 +30,7 @@ impl<'a> Client<'a> {
fn join_conexions_mutex(c1: Arc<Mutex<TcpStream>>,
c2: Arc<Mutex<TcpStream>>,
run: Arc<RwLock<bool>>){
let mut buf: [u8; 1000000] = [0; 1000000];
let mut buf: [u8; 100000] = [0; 100000];
let mut client = c1.lock().unwrap().try_clone().unwrap();
while *run.read().unwrap() {
let res=client.read(&mut buf);

View File

@ -11,8 +11,8 @@ pub struct Servers{
}
impl Servers {
pub fn new() -> Servers {
Servers{
pub fn new() -> Self {
Self{
l_servers: Self::get_servers(),
}
}
@ -23,15 +23,13 @@ impl Servers {
let mut ret = HashMap::new();
f.read_to_string(&mut s).unwrap();
let docs = yaml::YamlLoader::load_from_str(&s).unwrap();
let docs2;
match &docs[0]["servers"] {
yaml::Yaml::Hash(ref h) => docs2 = h,
let docs2 = match &docs[0]["servers"] {
yaml::Yaml::Hash(ref h) => h,
_ => return ret,
}
};
for (k, v) in docs2{
//println!("{}",String::from(doc.as_str().unwrap()));
ret.insert(String::from(k.as_str().unwrap()),
String::from(v.as_str().unwrap()));
}

View File

@ -1,5 +1,7 @@
use std::net::{TcpListener, TcpStream};
use std::io::prelude::*;
use crate::client::guard;
mod client;
mod conf;
mod protocol;
@ -8,11 +10,11 @@ fn main() {
let listener = TcpListener::bind("127.0.0.1:25567").unwrap();
let mut buf: [u8; 256] = [1; 256];
let servers = conf::Servers::new();
let mut guard: guard::Guard = guard::Guard::new();
for stream in listener.incoming() {
if guard.can_add(){
match stream {
Ok(mut stream) => {
println!("Go!");
let leng = stream.read(&mut buf).unwrap();
let mut hs = protocol::HandShake::new(&mut buf[.. leng]);
if hs.get_raw()[0] < 200 { //Filtra los ping, solo controlamos los handshakes
@ -20,10 +22,11 @@ fn main() {
Some(s) => {
hs.replace_port(s.1);
let mut sstream = TcpStream::connect(s.0 + ":" + &s.1.to_string()).unwrap();
println!("{}",hs.get_port());
let p_id = sstream.local_addr().unwrap().port();
println!("port4: {}",sstream.peer_addr().unwrap().port());
sstream.write(hs.get_raw());
let c1 = client::Client::new(stream,sstream, hs);
c1.start_proxy();
guard.add_thread(c1.start_proxy());
},
None => println!("No server found for{}", hs.getHostName())
}
@ -35,4 +38,5 @@ fn main() {
}
}
}
}

View File

@ -2,6 +2,7 @@ minecraft_proxy_sources = [
cargo_sources,
'main.rs',
'client/mod.rs',
'client/guard.rs',
'protocol/mod.rs',
'conf/mod.rs',
]

View File

@ -8,10 +8,10 @@ pub struct HandShake<'a> {
}
impl<'a> HandShake<'a>{
pub fn new(data: &'a mut[u8]) -> HandShake {
pub fn new(data: &'a mut[u8]) -> Self {
let len_pack = data[0];
let len_dom = data[4];
HandShake {
Self {
len_pack: len_pack,
len_dom: len_dom,
datagram: data,