Browse Source

Fixed incoming connections in IP interface

master
Alexey 3 years ago committed by ennucore
parent
commit
57397e20fd
  1. 40
      examples/test_ip_connection.rs
  2. 71
      src/interfaces/ip.rs
  3. 6
      src/lib.rs

40
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();
}

71
src/interfaces/ip.rs

@ -7,25 +7,57 @@ use crate::interface;
use crate::interface::{Interface, InterfaceRequirements, TargetingData}; use crate::interface::{Interface, InterfaceRequirements, TargetingData};
use crate::message::MessageBytes; use crate::message::MessageBytes;
use crate::res::{IFError, IFResult}; use crate::res::{IFError, IFResult};
use crate::std::io::Write; use crate::std::io::{Read, Write};
use crate::std::println; use crate::std::println;
// #[derive(Clone)] // #[derive(Clone)]
struct IPInterface { pub struct IPInterface {
id: String, pub id: String,
connections: Vec<net::TcpStream>, pub connections: Vec<net::TcpStream>,
listener: net::TcpListener, pub listener: net::TcpListener,
} }
impl Interface for IPInterface { impl Interface for IPInterface {
fn main_loop_iteration(&mut self) -> IFResult<()> { 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 { fn has_blocking_main(&self) -> bool {
false true
} }
fn id(&self) -> &str { fn id(&self) -> &str {
&*self.id &*self.id
@ -35,8 +67,8 @@ impl Interface for IPInterface {
Some(ip_string) => ip_string.parse().expect("Unable to parse address"), Some(ip_string) => ip_string.parse().expect("Unable to parse address"),
None => return Err(IFError::General(String::from("Not enough info to create connection"))) 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)) { let index = match self.connections.iter().position(|connection| connection.peer_addr().ok() == Some(addr)) {
None => { None => {
self.connections.push(net::TcpStream::connect(addr)?); self.connections.push(net::TcpStream::connect(addr)?);
@ -45,30 +77,19 @@ impl Interface for IPInterface {
Some(i) => i Some(i) => i
}; };
println!("Sending message to {:?}", self.connections[index].peer_addr().unwrap());
self.connections[index].write(message); self.connections[index].write(message);
println!("Sent message");
Ok(()) Ok(())
} }
fn receive(&mut self) -> IFResult<Option<(MessageBytes, TargetingData)>> { todo!() } fn receive(&mut self) -> IFResult<Option<(MessageBytes, TargetingData)>> { todo!() }
} }
#[test] #[test]
fn create_connection() { 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")));
}
#[cfg(test)] #[cfg(test)]
pub mod test_ip_interface {} pub mod test_ip_interface {}

6
src/lib.rs

@ -15,15 +15,13 @@ mod crypto;
mod ironforce; mod ironforce;
mod message; mod message;
mod transport; mod transport;
mod interface; pub mod interface;
mod interfaces; pub mod interfaces;
mod res; mod res;
mod tunnel; mod tunnel;
#[cfg(std)] #[cfg(std)]
use crate::interfaces::ip; use crate::interfaces::ip;
#[cfg(not(feature = "std"))]
use aboba;
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {

Loading…
Cancel
Save