start type socket parametrization

This commit is contained in:
Guillermo Roche 2022-12-19 23:56:34 +01:00
parent 6530d7b64a
commit e0a9fe3678
5 changed files with 69 additions and 10 deletions

View File

@ -8,6 +8,7 @@ minecraft_proxy_sources = [
'server_conf/mod.rs',
'server_conf/server.rs',
'server_conf/conexion.rs',
'server_conf/listener.rs',
'server_proxy.rs',
]

View File

@ -1,5 +1,3 @@
use std::net::{TcpListener, TcpStream};
use std::os::unix::net::{UnixStream};
use std::io::prelude::*;
use crate::conf;
use std::sync::{Arc, RwLock};
@ -14,16 +12,15 @@ const RE_MOD: u8 = 1;
const RE_OK: u8 = 0;
const RE_KO: u8 = 1;
pub struct Conexion {
pub struct Conexion<TSocket: Read + Write> {
conf: Arc<RwLock<conf::Config>>,
stream: UnixStream,
stream: TSocket,
buf:[u8; 256],
exit: bool,
}
impl Conexion {
pub fn new(conf: Arc<RwLock<conf::Config>>, stream: UnixStream) -> Self {
impl<TSocket: Read + Write> Conexion<TSocket> {
pub fn new(conf: Arc<RwLock<conf::Config>>, stream: TSocket) -> Self {
Self{
conf: conf,
stream: stream,

View File

@ -0,0 +1,60 @@
use std::net::TcpListener;
use std::os::unix::net::UnixListener;
use std::io::Error;
use std::io::prelude::*;
pub const TCP_LIS : u8 = 0;
pub const UNIX_LIS : u8 = 1;
trait NewTrait: std::io::Read + std::io::Write {}
pub struct GenericListener {
tcp_lis : Option<TcpListener>,
unix_lis : Option<UnixListener>,
type_lis : u8,
}
impl GenericListener {
fn new_tcp(listener: TcpListener) -> Self{
GenericListener{
tcp_lis: Some(listener),
unix_lis: None,
type_lis: TCP_LIS,
}
}
fn new_unix(listener: UnixListener) -> Self{
GenericListener{
tcp_lis: None,
unix_lis: Some(listener),
type_lis: UNIX_LIS,
}
}
pub fn bind(address: String, type_lis: u8) -> Result<Self, String>{
let ret = match type_lis {
TCP_LIS => Self::new_tcp(TcpListener::bind(address).unwrap()),
UNIX_LIS => Self::new_unix(UnixListener::bind(address).unwrap()),
_ => return Err("No valid option".to_string()),
};
Ok(ret)
}
/*pub fn incoming(&self) -> dyn Iterator<Item = Result<TcpStream, Error>> {
match self.type_lis {
TCP_LIS => self.tcp_lis.unwrap().incoming(),
UNIX_LIS => self.unix_lis.unwrap().incoming(),
}
}*/
/*pub fn accept(&self) -> Result<dyn NewTrait, Error> {
match self.type_lis {
TCP_LIS => self.tcp_lis.unwrap().accept(),
UNIX_LIS => self.unix_lis.unwrap().accept(),
}
}*/
}

View File

@ -1,2 +1,3 @@
pub mod server;
pub mod conexion;
pub mod listener;

View File

@ -1,10 +1,10 @@
use std::net::{TcpListener, TcpStream};
use std::os::unix::net::{UnixListener};
use std::net::TcpListener;
use std::os::unix::net::UnixListener;
use crate::conf;
use crate::server_conf::conexion::Conexion;
use crate::server_conf::listener::GenericListener;
use std::thread;
use std::sync::{Arc, RwLock};
use std::path::{Path, PathBuf};
pub struct ConfSer{
path: String,