diff --git a/examples/test_ip_connection.rs b/examples/test_ip_connection.rs new file mode 100644 index 0000000..9980298 --- /dev/null +++ b/examples/test_ip_connection.rs @@ -0,0 +1,40 @@ +use std::net; +use ironforce::interfaces::ip::IPInterface; +use ironforce::interface::Interface; + +fn main() { + let mut listener = match net::TcpListener::bind("0.0.0.0:60000") { + Ok(tmp) => tmp, + Err(_) => { return; } + }; + let mut interface1 = IPInterface { + id: String::from("IP interface"), + connections: vec![], + listener, + }; + + let option_listener = net::TcpListener::bind("0.0.0.0:50000"); + let mut listener= match option_listener { + Ok(tmp) => tmp, + Err(_) => { return; } + }; + let mut interface2 = IPInterface { + id: String::from("IP interface"), + connections: vec![], + listener, + }; + + let t1 = std::thread::spawn(move || { interface1.send(&[0,0,1,1], Some(String::from("0.0.0.0:50000"))); interface1}); + let t2 = std::thread::spawn(move || { interface2.main_loop_iteration(); interface2}); + let res1 = t1.join(); + match res1 { + Ok(_) => println!("Ok"), + Err(e) => println!("{:?}", e) + } + let res2 = t2.join(); + match res2 { + Ok(_) => println!("Ok"), + Err(e) => println!("{:?}", e) + } + // res1.unwrap(); +} \ No newline at end of file diff --git a/src/interfaces/ip.rs b/src/interfaces/ip.rs index 8b4fa8d..91d1841 100644 --- a/src/interfaces/ip.rs +++ b/src/interfaces/ip.rs @@ -7,25 +7,57 @@ use crate::interface; use crate::interface::{Interface, InterfaceRequirements, TargetingData}; use crate::message::MessageBytes; use crate::res::{IFError, IFResult}; -use crate::std::io::Write; +use crate::std::io::{Read, Write}; use crate::std::println; // #[derive(Clone)] -struct IPInterface { - id: String, - connections: Vec, - listener: net::TcpListener, +pub struct IPInterface { + pub id: String, + pub connections: Vec, + pub listener: net::TcpListener, } - impl Interface for IPInterface { fn main_loop_iteration(&mut self) -> IFResult<()> { - todo!() + println!("Mainloop {:?}", self.listener.local_addr()); + + + match self.listener.accept() { + Ok((stream, addr)) => {println!("New client: {:?}", addr); self.connections.push(stream)}, + Err(e) => println!("couldn't get client: {:?}", e), + } + // for stream in self.listener.incoming() { + // println!("Accepting stream..."); + // + // match stream { + // Ok(stream) => { + // self.connections.push(stream); + // println!("Stream accepted"); + // } + // Err(e) => { println!("Accepting failed"); return Err(IFError::General(String::from("Stream error"))); } + // } + // println!("End of cycle") + // } + println!("Hello from mainloop"); + for mut connection in &self.connections { + + let mut size_arr: [u8; 4] = [0, 0, 0, 0]; + connection.read(&mut size_arr); + let mut size: u32 = 0; + for i in 0..4 { + size = size * 256 + size_arr[i] as u32; + } + println!("Size: {:?}", size); + + + } + + Ok(()) } fn has_blocking_main(&self) -> bool { - false + true } fn id(&self) -> &str { &*self.id @@ -35,8 +67,8 @@ impl Interface for IPInterface { Some(ip_string) => ip_string.parse().expect("Unable to parse address"), None => return Err(IFError::General(String::from("Not enough info to create connection"))) }; - println!("Hello there"); + println!("Connecting to {:?}", addr); let index = match self.connections.iter().position(|connection| connection.peer_addr().ok() == Some(addr)) { None => { self.connections.push(net::TcpStream::connect(addr)?); @@ -45,30 +77,19 @@ impl Interface for IPInterface { Some(i) => i }; + println!("Sending message to {:?}", self.connections[index].peer_addr().unwrap()); + self.connections[index].write(message); + println!("Sent message"); + Ok(()) } fn receive(&mut self) -> IFResult> { todo!() } } #[test] -fn create_connection() { - let option_listener = net::TcpListener::bind("127.0.0.1:60000"); - let mut listener; - match option_listener { - Ok(tmp) => { listener = tmp } - Err(_) => { return; } - } - let mut interface = IPInterface { - id: String::from("IP interface"), - connections: vec![], - listener, - }; - interface.send(&[], Some(String::from("127.0.0.1:60000"))); - - -} +fn create_connection() { } #[cfg(test)] pub mod test_ip_interface {} diff --git a/src/lib.rs b/src/lib.rs index 057a0ec..7fe605d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -15,15 +15,13 @@ mod crypto; mod ironforce; mod message; mod transport; -mod interface; -mod interfaces; +pub mod interface; +pub mod interfaces; mod res; mod tunnel; #[cfg(std)] use crate::interfaces::ip; -#[cfg(not(feature = "std"))] -use aboba; #[cfg(test)] mod tests {