Added incrementing sequence number

master
noah metz 2024-01-20 18:05:16 -07:00
parent 452dd2986d
commit 8efdc7b5ab
1 changed files with 10 additions and 4 deletions

@ -322,6 +322,7 @@ struct TMClient {
uuid: [u8; 16], uuid: [u8; 16],
client_name: [u8; 32], client_name: [u8; 32],
password: String, password: String,
last_seq_num: u64,
} }
struct TMConnection { struct TMConnection {
@ -359,6 +360,7 @@ impl TMClient {
uuid, uuid,
client_name, client_name,
password, password,
last_seq_num: 0xFFFFFFFFFFFFFFFF,
}, },
TMConnection{ TMConnection{
stream: stream_arc, 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 incoming = [0; 2048];
let mut stream = self.stream.lock().unwrap(); let mut stream = self.stream.lock().unwrap();
@ -376,6 +378,7 @@ impl TMClient {
let data = incoming[0..read].to_vec(); let data = incoming[0..read].to_vec();
match BackendPacket::from_bytes(data) { match BackendPacket::from_bytes(data) {
Some(packet) => { Some(packet) => {
self.last_seq_num = packet.seq_num;
match packet.msg_type { match packet.msg_type {
2 => { 2 => {
match ConnectMsg::from_bytes(packet.data) { match ConnectMsg::from_bytes(packet.data) {
@ -383,10 +386,13 @@ impl TMClient {
println!("Welcome msg: {:?}", welcome_msg); println!("Welcome msg: {:?}", welcome_msg);
if welcome_msg.pw_valid == 0 { if welcome_msg.pw_valid == 0 {
let connect_response = ConnectMsg::from_welcome(welcome_msg, &self.password, self.uuid, self.client_name); 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()) { match stream.write(&response.as_bytes()) {
Err(error) => println!("Send error: {:?}", error), Err(error) => println!("Send error: {:?}", error),
Ok(sent) => println!("Sent {} bytes", sent), Ok(sent) => {
println!("Sent {} bytes", sent);
self.last_seq_num += 1;
},
} }
} else { } else {
println!("Connected to TM backend!"); println!("Connected to TM backend!");
@ -563,7 +569,7 @@ fn main() {
let running = true; let running = true;
let uuid = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]; 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 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 || let tm_thread = thread::spawn(move ||
while running { while running {
tm_client.process(); tm_client.process();