M src/client.rs => src/client.rs +7 -8
@@ 145,15 145,14 @@ impl Client {
}
async fn save_data_to_file(&mut self, message: [u8; protocol::MTU], size: usize) {
- if size < protocol::MTU {
- match self.file.lock().await.write_all(&message[..size]) {
- Ok(v) => v,
- Err(e) => eprint!("Encountered an error while writing: {}", e),
- };
+ if protocol::parse_end(message, size) {
self.state = protocol::ClientState::EndConn;
self.end_connection();
- } else {
- match self.file.lock().await.write_all(&message) {
+ return;
+ }
+ let (offset, data) = protocol::parse_data_packet(message, size);
+ {
+ match self.file.lock().await.write_all(&data) {
Ok(v) => v,
Err(e) => eprint!("Encountered an error while writing: {}", e),
};
@@ 161,12 160,12 @@ impl Client {
"Sending server msg that we have received offset {}",
self.lastpacket
);
- self.lastpacket += protocol::MTU;
protocol::send(
&self.socket,
&protocol::last_received_packet(self.lastpacket),
)
.await;
+ self.lastpacket += protocol::RAW_MTU;
}
}
}
M src/protocol.rs => src/protocol.rs +1 -0
@@ 95,6 95,7 @@ pub async fn get_external_and_nat(socket: Arc<UdpSocket>) {
//some constants defined for convenience
pub const MTU: usize = 1280;
+pub const RAW_MTU: usize = 1000;
//important messages implemented as constants
pub const ACK: [u8; 3] = *b"ACK";
M src/server.rs => src/server.rs +6 -4
@@ 163,21 163,23 @@ impl Server {
amt: usize,
) {
let offset = protocol::parse_last_received(message, amt);
- if offset + protocol::MTU < self.data.len() {
- //send MTU size chunk
+ if offset + protocol::RAW_MTU < self.data.len() {
+ //send chunk
println!("Sending a chunk...");
+ let packet = protocol::data_packet(offset, &self.data[offset..offset+protocol::RAW_MTU].to_vec());
protocol::send_to(
&self.socket,
src,
- &self.data[offset..offset + protocol::MTU].to_vec(),
+ &packet,
)
.await;
} else {
//send remaining data and end connection
+ let packet = protocol::data_packet(offset, &self.data[offset..self.data.len()].to_vec());
protocol::send_to(
&self.socket,
src,
- &self.data[offset..self.data.len()].to_vec(),
+ &packet,
)
.await;
println!("File sent completely");