add conexion number control
This commit is contained in:
		
							parent
							
								
									aeb69bab26
								
							
						
					
					
						commit
						230d5d1c82
					
				
							
								
								
									
										44
									
								
								src/client/guard.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										44
									
								
								src/client/guard.rs
									
									
									
									
									
										Normal 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; | ||||||
|  | 
 | ||||||
|  |     } | ||||||
|  | } | ||||||
| @ -4,6 +4,7 @@ use std::thread; | |||||||
| use std::sync::{Arc, Mutex, RwLock}; | use std::sync::{Arc, Mutex, RwLock}; | ||||||
| 
 | 
 | ||||||
| use crate::protocol; | use crate::protocol; | ||||||
|  | pub mod guard; | ||||||
| 
 | 
 | ||||||
| pub struct Client<'a>{ | pub struct Client<'a>{ | ||||||
|     client: Arc<Mutex<TcpStream>>, |     client: Arc<Mutex<TcpStream>>, | ||||||
| @ -17,11 +18,8 @@ impl<'a> Client<'a> { | |||||||
|         Client { |         Client { | ||||||
|             client: Arc::new(Mutex::new(client)), |             client: Arc::new(Mutex::new(client)), | ||||||
|             server: Arc::new(Mutex::new(server)), |             server: Arc::new(Mutex::new(server)), | ||||||
|             //client: client,
 |  | ||||||
|             //server: server,
 |  | ||||||
|             hs: handshake, |             hs: handshake, | ||||||
|             run: Arc::new(RwLock::new(true)), |             run: Arc::new(RwLock::new(true)), | ||||||
|             //run: &true,
 |  | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| @ -32,7 +30,7 @@ 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<RwLock<bool>>){ |             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(); |         let mut client = c1.lock().unwrap().try_clone().unwrap(); | ||||||
|         while *run.read().unwrap() { |         while *run.read().unwrap() { | ||||||
|             let res=client.read(&mut buf); |             let res=client.read(&mut buf); | ||||||
|  | |||||||
| @ -11,8 +11,8 @@ pub struct Servers{ | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| impl Servers { | impl Servers { | ||||||
|     pub fn new() -> Servers { |     pub fn new() -> Self { | ||||||
|         Servers{ |         Self{ | ||||||
|             l_servers: Self::get_servers(), |             l_servers: Self::get_servers(), | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| @ -23,15 +23,13 @@ impl Servers { | |||||||
|         let mut ret = HashMap::new(); |         let mut ret = HashMap::new(); | ||||||
|         f.read_to_string(&mut s).unwrap(); |         f.read_to_string(&mut s).unwrap(); | ||||||
|         let docs = yaml::YamlLoader::load_from_str(&s).unwrap(); |         let docs = yaml::YamlLoader::load_from_str(&s).unwrap(); | ||||||
|         let docs2; |         let docs2 = match &docs[0]["servers"] { | ||||||
|         match &docs[0]["servers"] { |             yaml::Yaml::Hash(ref h) => h, | ||||||
|             yaml::Yaml::Hash(ref h) => docs2 = h, |  | ||||||
|             _ => return ret, |             _ => return ret, | ||||||
|         } |         }; | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|         for (k, v) in docs2{ |         for (k, v) in docs2{ | ||||||
|             //println!("{}",String::from(doc.as_str().unwrap()));
 |  | ||||||
|             ret.insert(String::from(k.as_str().unwrap()), |             ret.insert(String::from(k.as_str().unwrap()), | ||||||
|                     String::from(v.as_str().unwrap())); |                     String::from(v.as_str().unwrap())); | ||||||
|         } |         } | ||||||
|  | |||||||
							
								
								
									
										46
									
								
								src/main.rs
									
									
									
									
									
								
							
							
						
						
									
										46
									
								
								src/main.rs
									
									
									
									
									
								
							| @ -1,5 +1,7 @@ | |||||||
| use std::net::{TcpListener, TcpStream}; | use std::net::{TcpListener, TcpStream}; | ||||||
| use std::io::prelude::*; | use std::io::prelude::*; | ||||||
|  | use crate::client::guard; | ||||||
|  | 
 | ||||||
| mod client; | mod client; | ||||||
| mod conf; | mod conf; | ||||||
| mod protocol; | mod protocol; | ||||||
| @ -8,30 +10,32 @@ 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; 256] = [1; 256]; |     let mut buf: [u8; 256] = [1; 256]; | ||||||
|     let servers = conf::Servers::new(); |     let servers = conf::Servers::new(); | ||||||
| 
 |     let mut guard: guard::Guard = guard::Guard::new(); | ||||||
|     for stream in listener.incoming() { |     for stream in listener.incoming() { | ||||||
|         match stream { |         if guard.can_add(){ | ||||||
|             Ok(mut stream) => { |             match stream { | ||||||
|                 println!("Go!"); |                 Ok(mut stream) => { | ||||||
|                 let leng = stream.read(&mut buf).unwrap(); |                     let leng = stream.read(&mut buf).unwrap(); | ||||||
|                 let mut hs = protocol::HandShake::new(&mut buf[.. leng]); |                     let mut hs = protocol::HandShake::new(&mut buf[.. leng]); | ||||||
|                 if hs.get_raw()[0] < 200 { //Filtra los ping, solo controlamos los handshakes
 |                     if hs.get_raw()[0] < 200 { //Filtra los ping, solo controlamos los handshakes
 | ||||||
|                     match servers.get_server(&hs.getHostName()) { |                         match servers.get_server(&hs.getHostName()) { | ||||||
|                         Some(s) => { |                             Some(s) => { | ||||||
|                             hs.replace_port(s.1); |                                 hs.replace_port(s.1); | ||||||
|                             let mut sstream = TcpStream::connect(s.0 + ":" + &s.1.to_string()).unwrap(); |                                 let mut sstream = TcpStream::connect(s.0 + ":" + &s.1.to_string()).unwrap(); | ||||||
|                             println!("{}",hs.get_port()); |                                 let p_id = sstream.local_addr().unwrap().port(); | ||||||
|                             sstream.write(hs.get_raw()); |                                 println!("port4: {}",sstream.peer_addr().unwrap().port()); | ||||||
|                             let c1 = client::Client::new(stream,sstream, hs); |                                 sstream.write(hs.get_raw()); | ||||||
|                             c1.start_proxy(); |                                 let c1 = client::Client::new(stream,sstream, hs); | ||||||
|                         }, |                                 guard.add_thread(c1.start_proxy()); | ||||||
|                         None => println!("No server found for{}", hs.getHostName()) |                             }, | ||||||
|  |                             None => println!("No server found for{}", hs.getHostName()) | ||||||
|  |                         } | ||||||
|  | 
 | ||||||
|                     } |                     } | ||||||
|  |                 }, | ||||||
| 
 | 
 | ||||||
|                 } |                 Err(_e) => println!("{}",_e), | ||||||
|             }, |             } | ||||||
| 
 |  | ||||||
|             Err(_e) => println!("{}",_e), |  | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | |||||||
| @ -2,6 +2,7 @@ minecraft_proxy_sources = [ | |||||||
|   cargo_sources, |   cargo_sources, | ||||||
|   'main.rs', |   'main.rs', | ||||||
|   'client/mod.rs', |   'client/mod.rs', | ||||||
|  |   'client/guard.rs', | ||||||
|   'protocol/mod.rs', |   'protocol/mod.rs', | ||||||
|   'conf/mod.rs', |   'conf/mod.rs', | ||||||
| ] | ] | ||||||
|  | |||||||
| @ -8,10 +8,10 @@ pub struct HandShake<'a> { | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| impl<'a> 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_pack = data[0]; | ||||||
|         let len_dom = data[4]; |         let len_dom = data[4]; | ||||||
|         HandShake { |         Self { | ||||||
|             len_pack: len_pack, |             len_pack: len_pack, | ||||||
|             len_dom: len_dom, |             len_dom: len_dom, | ||||||
|             datagram: data, |             datagram: data, | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user