Revert to using a separate thread for tcp socket handling
parent
f2e2590c74
commit
1cc7150f5d
32
src/main.rs
32
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<tm::Notice>,
|
||||
}
|
||||
|
||||
struct BackendMessage {
|
||||
|
|
@ -162,13 +164,18 @@ struct BackendMessage {
|
|||
}
|
||||
|
||||
impl TMClient {
|
||||
fn new() -> TMClient {
|
||||
TMClient {
|
||||
}
|
||||
fn new() -> (TMClient, mpsc::Receiver<tm::Notice>) {
|
||||
let (tx, rx) = mpsc::channel();
|
||||
(TMClient {
|
||||
notices: tx,
|
||||
}, rx)
|
||||
}
|
||||
|
||||
fn process(self: &TMClient) -> Option<tm::Notice> {
|
||||
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");
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue