|
|
|
@ -1,9 +1,7 @@
|
|
|
|
|
use std::net; |
|
|
|
|
use alloc::vec; |
|
|
|
|
use alloc::vec::Vec; |
|
|
|
|
use alloc::string::String; |
|
|
|
|
|
|
|
|
|
use crate::interface; |
|
|
|
|
use crate::interface::{Interface, InterfaceRequirements, TargetingData}; |
|
|
|
|
use crate::message::MessageBytes; |
|
|
|
|
use crate::res::{IFError, IFResult}; |
|
|
|
@ -18,6 +16,8 @@ pub struct IPInterface {
|
|
|
|
|
pub listener: net::TcpListener, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
impl InterfaceRequirements for IPInterface {} |
|
|
|
|
|
|
|
|
|
impl Interface for IPInterface { |
|
|
|
|
fn main_loop_iteration(&mut self) -> IFResult<()> { |
|
|
|
|
println!("Mainloop {:?}", self.listener.local_addr()); |
|
|
|
@ -43,10 +43,10 @@ impl Interface for IPInterface {
|
|
|
|
|
for mut connection in &self.connections { |
|
|
|
|
|
|
|
|
|
let mut size_arr: [u8; 4] = [0, 0, 0, 0]; |
|
|
|
|
connection.read(&mut size_arr); |
|
|
|
|
connection.read_exact(&mut size_arr)?; |
|
|
|
|
let mut size: u32 = 0; |
|
|
|
|
for i in 0..4 { |
|
|
|
|
size = size * 256 + size_arr[i] as u32; |
|
|
|
|
for size_byte in &size_arr { |
|
|
|
|
size = size * 256 + *size_byte as u32; |
|
|
|
|
} |
|
|
|
|
println!("Size: {:?}", size); |
|
|
|
|
|
|
|
|
@ -63,7 +63,7 @@ impl Interface for IPInterface {
|
|
|
|
|
&*self.id |
|
|
|
|
} |
|
|
|
|
fn send(&mut self, message: &[u8], interface_data: Option<TargetingData>) -> IFResult<()> { |
|
|
|
|
let mut addr: net::SocketAddr = match interface_data { |
|
|
|
|
let addr: net::SocketAddr = match interface_data { |
|
|
|
|
Some(ip_string) => ip_string.parse().expect("Unable to parse address"), |
|
|
|
|
None => return Err(IFError::General(String::from("Not enough info to create connection"))) |
|
|
|
|
}; |
|
|
|
@ -79,7 +79,7 @@ impl Interface for IPInterface {
|
|
|
|
|
|
|
|
|
|
println!("Sending message to {:?}", self.connections[index].peer_addr().unwrap()); |
|
|
|
|
|
|
|
|
|
self.connections[index].write(message); |
|
|
|
|
self.connections[index].write_all(message)?; |
|
|
|
|
|
|
|
|
|
println!("Sent message"); |
|
|
|
|
|
|
|
|
|