|
|
|
@ -4,13 +4,10 @@ use std::thread;
|
|
|
|
|
use alloc::vec; |
|
|
|
|
use alloc::vec::Vec; |
|
|
|
|
use alloc::string::{String, ToString}; |
|
|
|
|
use core::cmp::min; |
|
|
|
|
use core::ops::RangeInclusive; |
|
|
|
|
use core::str::from_utf8; |
|
|
|
|
use core::time::Duration; |
|
|
|
|
|
|
|
|
|
use crate::interface::{Interface, InterfaceRequirements, TargetingData}; |
|
|
|
|
use crate::interfaces::ip::MessageType::PeerRequest; |
|
|
|
|
use crate::message::MessageBytes; |
|
|
|
|
use crate::res::{IFError, IFResult}; |
|
|
|
|
use crate::res::IFError::General; |
|
|
|
@ -74,7 +71,7 @@ impl Interface for IPInterface {
|
|
|
|
|
println!("New client: {:?}", addr); |
|
|
|
|
self.connections.push(stream) |
|
|
|
|
} |
|
|
|
|
Err(e) => return Err(IFError::General(String::from("No incoming connection"))), |
|
|
|
|
Err(_e) => return Err(IFError::General(String::from("No incoming connection"))), |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
println!("Hello from mainloop"); |
|
|
|
@ -87,7 +84,7 @@ impl Interface for IPInterface {
|
|
|
|
|
|
|
|
|
|
let mut message_take = connection.take(size as u64); |
|
|
|
|
let mut message: Vec<u8> = vec![]; |
|
|
|
|
message_take.read_to_end(&mut message); |
|
|
|
|
message_take.read_to_end(&mut message)?; |
|
|
|
|
|
|
|
|
|
let package = IPPackage {version, package_type, size, message}; |
|
|
|
|
self.package_queue.push(package); |
|
|
|
@ -134,7 +131,7 @@ impl Interface for IPInterface {
|
|
|
|
|
package_type: MessageType::Common, |
|
|
|
|
size: message.len() as u32, |
|
|
|
|
message: Vec::from(message), |
|
|
|
|
}); |
|
|
|
|
})?; |
|
|
|
|
println!("Sent message"); |
|
|
|
|
Ok(()) |
|
|
|
|
} |
|
|
|
@ -148,7 +145,7 @@ impl Interface for IPInterface {
|
|
|
|
|
|
|
|
|
|
impl IPInterface { |
|
|
|
|
pub fn new() -> IFResult<Self> { |
|
|
|
|
let mut listener = match create_tcp_listener() { |
|
|
|
|
let listener = match create_tcp_listener() { |
|
|
|
|
Some(listener) => listener, |
|
|
|
|
None => { |
|
|
|
|
return Err(IFError::General(String::from("Unable to open TCP listener"))); |
|
|
|
@ -194,7 +191,7 @@ fn create_tcp_listener() -> Option<net::TcpListener> {
|
|
|
|
|
for port in SOCKET_RANGE { |
|
|
|
|
match net::TcpListener::bind("127.0.0.1:".to_owned() + &port.to_string()) { |
|
|
|
|
Ok(listener) => return Some(listener), |
|
|
|
|
Err(e) => {} |
|
|
|
|
Err(_e) => {} |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
None |
|
|
|
@ -235,20 +232,19 @@ fn test_creating_connection() -> IFResult<()> {
|
|
|
|
|
let mut interface2 = IPInterface::new()?; |
|
|
|
|
|
|
|
|
|
let t1 = std::thread::spawn(move || { |
|
|
|
|
interface1.send(&message, Some(String::from("127.0.0.1:50001"))); |
|
|
|
|
interface1.send(&message, Some(String::from("127.0.0.1:50001"))).unwrap(); |
|
|
|
|
interface1 |
|
|
|
|
}); |
|
|
|
|
thread::sleep(Duration::from_millis(10)); |
|
|
|
|
let t2 = std::thread::spawn(move || { |
|
|
|
|
interface2.main_loop_iteration(); |
|
|
|
|
interface2.main_loop_iteration().unwrap(); |
|
|
|
|
interface2 |
|
|
|
|
}); |
|
|
|
|
let res1 = t1.join(); |
|
|
|
|
match res1 { |
|
|
|
|
Ok(mut res) => { |
|
|
|
|
Ok(_res) => { |
|
|
|
|
println!("Thread Ok"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
Err(e) => println!("{:?}", e) |
|
|
|
|
} |
|
|
|
@ -258,7 +254,7 @@ fn test_creating_connection() -> IFResult<()> {
|
|
|
|
|
println!("Thread Ok"); |
|
|
|
|
match res.receive() { |
|
|
|
|
Ok(tmp) => match tmp { |
|
|
|
|
Some((message, metadata)) => println!("Received {:?}", message), |
|
|
|
|
Some((message, _metadata)) => println!("Received {:?}", message), |
|
|
|
|
None => println!("None") |
|
|
|
|
} |
|
|
|
|
Err(e) => println!("{:?}", e) |
|
|
|
|