Skip to content

Commit

Permalink
Split tasks - event loop now properly asynchronous (#57)
Browse files Browse the repository at this point in the history
DNS, etc. does no longer block the event loop
  • Loading branch information
feschber authored Dec 23, 2023
1 parent 1cefa38 commit cdd3a3b
Show file tree
Hide file tree
Showing 4 changed files with 621 additions and 367 deletions.
10 changes: 8 additions & 2 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ impl Display for Position {
pub struct Client {
/// hostname of this client
pub hostname: Option<String>,
/// fix ips, determined by the user
pub fix_ips: Vec<IpAddr>,
/// unique handle to refer to the client.
/// This way any event consumer / producer backend does not
/// need to know anything about a client other than its handle.
Expand Down Expand Up @@ -105,7 +107,7 @@ impl ClientManager {
pub fn add_client(
&mut self,
hostname: Option<String>,
addrs: HashSet<IpAddr>,
ips: HashSet<IpAddr>,
port: u16,
pos: Position,
) -> ClientHandle {
Expand All @@ -115,12 +117,16 @@ impl ClientManager {
// we dont know, which IP is initially active
let active_addr = None;

// store fix ip addresses
let fix_ips = ips.iter().cloned().collect();

// map ip addresses to socket addresses
let addrs = HashSet::from_iter(addrs.into_iter().map(|ip| SocketAddr::new(ip, port)));
let addrs = HashSet::from_iter(ips.into_iter().map(|ip| SocketAddr::new(ip, port)));

// store the client
let client = Client {
hostname,
fix_ips,
handle,
active_addr,
addrs,
Expand Down
2 changes: 1 addition & 1 deletion src/dns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{error::Error, net::IpAddr};

use trust_dns_resolver::TokioAsyncResolver;

pub(crate) struct DnsResolver {
pub struct DnsResolver {
resolver: TokioAsyncResolver,
}
impl DnsResolver {
Expand Down
30 changes: 4 additions & 26 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,9 @@ use anyhow::Result;
use std::process::{self, Child, Command};

use env_logger::Env;
use lan_mouse::{
config::Config,
consumer,
frontend::{self, FrontendListener},
producer,
server::Server,
};
use lan_mouse::{config::Config, frontend, server::Server};

use tokio::{join, task::LocalSet};
use tokio::task::LocalSet;

pub fn main() {
// init logging
Expand Down Expand Up @@ -58,26 +52,10 @@ fn run_service(config: &Config) -> Result<()> {

// run async event loop
runtime.block_on(LocalSet::new().run_until(async {
// create frontend communication adapter
let frontend_adapter = match FrontendListener::new().await {
Some(Err(e)) => return Err(e),
Some(Ok(f)) => f,
None => {
// none means some other instance is already running
log::info!("service already running, exiting");
return anyhow::Ok(());
}
};

// create event producer and consumer
let (producer, consumer) = join!(producer::create(), consumer::create());

// create server
let mut event_server = Server::new(config, frontend_adapter, consumer, producer).await?;
// run main loop
log::info!("Press Ctrl+Alt+Shift+Super to release the mouse");
Server::run(config).await?;

// run event loop
event_server.run().await?;
log::debug!("service exiting");
anyhow::Ok(())
}))?;
Expand Down
Loading

0 comments on commit cdd3a3b

Please sign in to comment.