Revert to using a separate thread for tcp socket handling

master
noah metz 2024-01-18 20:29:15 -07:00
parent f2e2590c74
commit 1cc7150f5d
1 changed files with 22 additions and 10 deletions

@ -2,6 +2,7 @@ use rumqttc::{MqttOptions, Client, QoS, LastWill};
use bytes::Bytes; use bytes::Bytes;
use std::time::Duration; use std::time::Duration;
use std::thread; use std::thread;
use std::sync::mpsc;
use std::collections::hash_map::HashMap; use std::collections::hash_map::HashMap;
use prost::Message; use prost::Message;
use std::io::Cursor; use std::io::Cursor;
@ -155,6 +156,7 @@ struct Match {
} }
struct TMClient { struct TMClient {
notices: mpsc::Sender<tm::Notice>,
} }
struct BackendMessage { struct BackendMessage {
@ -162,13 +164,18 @@ struct BackendMessage {
} }
impl TMClient { impl TMClient {
fn new() -> TMClient { fn new() -> (TMClient, mpsc::Receiver<tm::Notice>) {
TMClient { let (tx, rx) = mpsc::channel();
} (TMClient {
notices: tx,
}, rx)
} }
fn process(self: &TMClient) -> Option<tm::Notice> { fn process(self: &TMClient) {
Some(tm::Notice::default()) match self.notices.send(tm::Notice::default()) {
Ok(_) => println!("Received notice"),
Err(error) => println!("Recv error {}", error),
}
} }
// TODO: make send functionality // TODO: make send functionality
@ -314,12 +321,16 @@ fn main() {
); );
let running = true; 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 { while running {
match tm_client.process() { match tm_notices.recv() {
Some(notice) => { Ok(notice) => {
let callback = callbacks.get(&notice.id()); let callback = callbacks.get(&notice.id());
match callback { match callback {
None => { 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"); mqtt_recv_thread.join().expect("Failed to join mqtt thread");
tm_thread.join().expect("Failed to join tm connection thread");
} }