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