diff --git a/rust/server/src/main.rs b/rust/server/src/main.rs index a788bd0..eddf565 100644 --- a/rust/server/src/main.rs +++ b/rust/server/src/main.rs @@ -45,7 +45,7 @@ fn main() { println!("UDP Server up and running on port {}", local_addr.port()); - let _options = options().with_big_endian().allow_trailing_bytes().with_fixint_encoding(); + let options = options().with_big_endian().allow_trailing_bytes().with_fixint_encoding(); let hash_request_regex = Regex::new(r"[a-zA-Z0-9.-_ ]+:").unwrap(); @@ -58,7 +58,9 @@ fn main() { let filled_buffer = &buffer;//[..received_bytes]; let request_packet = bincode::deserialize::(filled_buffer).unwrap(); - let request: String = request_packet.payload.iter().map(|&c| c as char).collect(); + let mut request: String = request_packet.payload.iter().map(|&c| c as char).collect(); + request.retain(|c| c != '\0'); + println!("Received request: {}", request); let req = request.as_str(); @@ -74,9 +76,10 @@ fn main() { println!("Received file request"); - let file_result = std::fs::File::open("files/".to_string() + req); + let file_result = std::fs::File::open("./files/".to_string() + req); if file_result.is_err() { + println!("{}",file_result.err().unwrap().to_string()); println!("File not found"); continue; } @@ -90,13 +93,15 @@ fn main() { let file_length = file.metadata().unwrap().len(); println!("file length: {}", file_length); + let packet_numbers = f64::ceil(file_length as f64/MAX_PAYLOAD as f64) as u32; + let packet_information = PacketInformation { - packet_numbers: (file_length/MAX_PAYLOAD as u64) as u32, + packet_numbers, last_packet_size: (file_length%MAX_PAYLOAD as u64) as u16, response_filename_checksum: request_packet.payload.iter().fold(0u64, |acc, &x| acc.wrapping_add(x as u64)), }; - socket.send_to(bincode::serialize(&packet_information).unwrap().as_slice(), remote_addr).expect("Failed to send packet information"); + socket.send_to(options.serialize(&packet_information).unwrap().as_slice(), remote_addr).expect("Failed to send packet information"); let mut sha512_hasher = Sha512::new(); @@ -113,11 +118,11 @@ fn main() { let mut packet = Packet { packet_number, payload_hash: [0u8; 16], - payload: file_buffer[..bytes_read].try_into().unwrap(), + payload: [0u8; MAX_PAYLOAD_U], }; packet.payload_hash.copy_from_slice(&result[..16]); packet.payload[..bytes_read].copy_from_slice(&file_buffer[..bytes_read]); - let packet_bytes = bincode::serialize(&packet).unwrap(); + let packet_bytes = options.serialize(&packet).unwrap(); socket.send_to(&packet_bytes, remote_addr).expect("Failed to send packet"); packet_number += 1;