try to optimice with splice

This commit is contained in:
Guillermo Roche 2022-09-30 00:16:11 +02:00
parent a698bbb14f
commit 293c94e9a2
5 changed files with 76 additions and 5 deletions

8
Cargo.lock generated
View File

@ -2,6 +2,12 @@
# It is not intended for manual editing.
version = 3
[[package]]
name = "libc"
version = "0.2.133"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c0f80d65747a3e43d1596c7c5492d95d5edddaabd45a7fcdb02b95f644164966"
[[package]]
name = "linked-hash-map"
version = "0.5.6"
@ -12,7 +18,7 @@ checksum = "0717cef1bc8b636c6e1c1bbdefc09e6322da8a9321966e8928ef80d20f7f770f"
name = "minecraft_proxy"
version = "0.1.0"
dependencies = [
"linked-hash-map",
"libc",
"yaml-rust",
]

View File

@ -5,4 +5,4 @@ edition = "2021"
[dependencies]
yaml-rust = "*"
linked-hash-map = "*"
libc = "*"

View File

@ -19,6 +19,10 @@ impl Guard {
*self.cont.read().unwrap() < MAX_THREADS
}
pub fn get_connections(&self)->usize{
*self.cont.read().unwrap()
}
pub fn add_thread(&mut self,
threads: (thread::JoinHandle<()>, thread::JoinHandle<()>))-> bool {
if self.can_add() {

View File

@ -2,6 +2,12 @@ use std::net::TcpStream;
use std::io::prelude::*;
use std::thread;
use std::sync::{Arc, Mutex, RwLock};
use libc::splice;
use libc::pipe;
use libc::{SPLICE_F_MORE,SPLICE_F_MOVE};
use std::os::unix::io::AsRawFd;
use std::ptr;
use std::time::SystemTime;
use crate::protocol;
pub mod guard;
@ -31,9 +37,19 @@ impl<'a> Client<'a> {
fn join_conexions_mutex(c1: Arc<Mutex<TcpStream>>,
c2: Arc<Mutex<TcpStream>>,
run: Arc<RwLock<bool>>){
let mut buf: [u8; 100000] = [0; 100000];
let mut buf: [u8; 200] = [0; 200];
let mut client = c1.lock().unwrap().try_clone().unwrap();
let mut len2;
let mut p: [i32;2] = [0,0];
let tam = 200;
let mut max = 0;
let mut time;
let mut time2;
let mut fin = 0;
let mut fin2 = 0;
unsafe{len2 = pipe(p.as_ptr() as *mut i32);}
while *run.read().unwrap() {
time = SystemTime::now();
let res=client.read(&mut buf);
match res {
Ok(leng) => {
@ -41,15 +57,59 @@ impl<'a> Client<'a> {
*run.write().unwrap()=false;
}
match c2.lock().unwrap().write(&buf [.. leng]) {
Ok(_l) => {},
Ok(_l) => {
time2 = SystemTime::now();
fin2 = time2.duration_since(time).unwrap_or_default().as_micros();
if fin2 > fin {
fin = fin2;
}
},
Err(_e) => *run.write().unwrap()=false,
}
},
Err(_e) => *run.write().unwrap()=false,
}
}
println!("time {}", fin);
}
fn join_conexions_kernel(c1: Arc<Mutex<TcpStream>>,
c2: Arc<Mutex<TcpStream>>,
run: Arc<RwLock<bool>>){
let mut client = c1.lock().unwrap().try_clone().unwrap();
let mut len2;
let mut p: [i32;2] = [0,0];
let tam = 20000;
let mut len;
let mut time;
let mut time2;
let mut fin = 0;
let mut fin2 = 0;
unsafe{len2 = pipe(p.as_ptr() as *mut i32);}
while *run.read().unwrap() {
time = SystemTime::now();
unsafe {
len = splice(client.as_raw_fd(), ptr::null_mut(),
p[1], ptr::null_mut(), tam, SPLICE_F_MOVE);
if len <= 0 {
*run.write().unwrap()=false;
break;
}
splice(p[0], ptr::null_mut(),
c2.lock().unwrap().as_raw_fd(), ptr::null_mut(),
len as usize, SPLICE_F_MOVE | SPLICE_F_MORE);
time2 = SystemTime::now();
fin2 = time2.duration_since(time).unwrap_or_default().as_micros();
if fin2 > fin {
fin = fin2;
}
}
}
println!("time {}",fin);
}
pub fn start_proxy(&self) -> (thread::JoinHandle<()>, thread::JoinHandle<()>) {

View File

@ -16,6 +16,7 @@ fn main() {
let guard = Arc::new(RwLock::new(guard::Guard::new()));
for stream in listener.incoming() {
if guard.read().unwrap().can_add(){
println!("g {}",guard.read().unwrap().get_connections());
match stream {
Ok(stream) => {
let g = guard.clone();