From 1cc7150f5d00f4bd5f3fa4deafec5c9bad7ea180 Mon Sep 17 00:00:00 2001 From: Noah Metz Date: Thu, 18 Jan 2024 20:29:15 -0700 Subject: [PATCH] Revert to using a separate thread for tcp socket handling --- src/main.rs | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/src/main.rs b/src/main.rs index dd57528..27468d4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ use rumqttc::{MqttOptions, Client, QoS, LastWill}; use bytes::Bytes; use std::time::Duration; use std::thread; +use std::sync::mpsc; use std::collections::hash_map::HashMap; use prost::Message; use std::io::Cursor; @@ -155,6 +156,7 @@ struct Match { } struct TMClient { + notices: mpsc::Sender, } struct BackendMessage { @@ -162,13 +164,18 @@ struct BackendMessage { } impl TMClient { - fn new() -> TMClient { - TMClient { - } + fn new() -> (TMClient, mpsc::Receiver) { + let (tx, rx) = mpsc::channel(); + (TMClient { + notices: tx, + }, rx) } - fn process(self: &TMClient) -> Option { - Some(tm::Notice::default()) + fn process(self: &TMClient) { + match self.notices.send(tm::Notice::default()) { + Ok(_) => println!("Received notice"), + Err(error) => println!("Recv error {}", error), + } } // TODO: make send functionality @@ -314,12 +321,16 @@ fn main() { ); let running = true; - let tm_client = TMClient::new(); + let (tm_client, tm_notices) = TMClient::new(); + let tm_thread = thread::spawn(move || + while running { + tm_client.process(); + } + ); - // Run the callback loop in the main thread while running { - match tm_client.process() { - Some(notice) => { + match tm_notices.recv() { + Ok(notice) => { let callback = callbacks.get(¬ice.id()); match callback { None => { @@ -341,10 +352,11 @@ fn main() { }, } }, - None => {}, + Err(error) => println!("Notice recv error: {}", error), } } mqtt_recv_thread.join().expect("Failed to join mqtt thread"); + tm_thread.join().expect("Failed to join tm connection thread"); }