From 8efdc7b5ab277a7773cc30ba8d7a844e9df857ce Mon Sep 17 00:00:00 2001 From: Noah Metz Date: Sat, 20 Jan 2024 18:05:16 -0700 Subject: [PATCH] Added incrementing sequence number --- src/main.rs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index 6dda60f..9068fdc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -322,6 +322,7 @@ struct TMClient { uuid: [u8; 16], client_name: [u8; 32], password: String, + last_seq_num: u64, } struct TMConnection { @@ -359,6 +360,7 @@ impl TMClient { uuid, client_name, password, + last_seq_num: 0xFFFFFFFFFFFFFFFF, }, TMConnection{ stream: stream_arc, @@ -367,7 +369,7 @@ impl TMClient { },); } - fn process(self: &TMClient) { + fn process(self: &mut TMClient) { let mut incoming = [0; 2048]; let mut stream = self.stream.lock().unwrap(); @@ -376,6 +378,7 @@ impl TMClient { let data = incoming[0..read].to_vec(); match BackendPacket::from_bytes(data) { Some(packet) => { + self.last_seq_num = packet.seq_num; match packet.msg_type { 2 => { match ConnectMsg::from_bytes(packet.data) { @@ -383,10 +386,13 @@ impl TMClient { println!("Welcome msg: {:?}", welcome_msg); if welcome_msg.pw_valid == 0 { let connect_response = ConnectMsg::from_welcome(welcome_msg, &self.password, self.uuid, self.client_name); - let response = BackendPacket::new(packet.header, packet.timestamp, packet.msg_type, packet.seq_num, connect_response.as_bytes()); + let response = BackendPacket::new(packet.header, packet.timestamp, packet.msg_type, self.last_seq_num+1, connect_response.as_bytes()); match stream.write(&response.as_bytes()) { Err(error) => println!("Send error: {:?}", error), - Ok(sent) => println!("Sent {} bytes", sent), + Ok(sent) => { + println!("Sent {} bytes", sent); + self.last_seq_num += 1; + }, } } else { println!("Connected to TM backend!"); @@ -563,7 +569,7 @@ fn main() { let running = true; let uuid = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]; let client_name: [u8;32] = [b'r', b'u', b's', b't', b'-', b'b', b'r', b'i', b'd', b'g', b'e', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; - let (tm_client, tm_connection) = TMClient::new(uuid, client_name, String::from("")); + let (mut tm_client, tm_connection) = TMClient::new(uuid, client_name, String::from("")); let tm_thread = thread::spawn(move || while running { tm_client.process();