Split tasks - event loop now properly asynchronous (#57)

DNS, etc. does no longer block the event loop
This commit is contained in:
Ferdinand Schober
2023-12-23 14:46:38 +01:00
committed by GitHub
parent 1cefa38543
commit cdd3a3b818
4 changed files with 639 additions and 385 deletions

View File

@@ -51,6 +51,8 @@ impl Display for Position {
pub struct Client { pub struct Client {
/// hostname of this client /// hostname of this client
pub hostname: Option<String>, pub hostname: Option<String>,
/// fix ips, determined by the user
pub fix_ips: Vec<IpAddr>,
/// unique handle to refer to the client. /// unique handle to refer to the client.
/// This way any event consumer / producer backend does not /// This way any event consumer / producer backend does not
/// need to know anything about a client other than its handle. /// need to know anything about a client other than its handle.
@@ -105,7 +107,7 @@ impl ClientManager {
pub fn add_client( pub fn add_client(
&mut self, &mut self,
hostname: Option<String>, hostname: Option<String>,
addrs: HashSet<IpAddr>, ips: HashSet<IpAddr>,
port: u16, port: u16,
pos: Position, pos: Position,
) -> ClientHandle { ) -> ClientHandle {
@@ -115,12 +117,16 @@ impl ClientManager {
// we dont know, which IP is initially active // we dont know, which IP is initially active
let active_addr = None; let active_addr = None;
// store fix ip addresses
let fix_ips = ips.iter().cloned().collect();
// map ip addresses to socket addresses // 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 // store the client
let client = Client { let client = Client {
hostname, hostname,
fix_ips,
handle, handle,
active_addr, active_addr,
addrs, addrs,

View File

@@ -3,7 +3,7 @@ use std::{error::Error, net::IpAddr};
use trust_dns_resolver::TokioAsyncResolver; use trust_dns_resolver::TokioAsyncResolver;
pub(crate) struct DnsResolver { pub struct DnsResolver {
resolver: TokioAsyncResolver, resolver: TokioAsyncResolver,
} }
impl DnsResolver { impl DnsResolver {

View File

@@ -2,15 +2,9 @@ use anyhow::Result;
use std::process::{self, Child, Command}; use std::process::{self, Child, Command};
use env_logger::Env; use env_logger::Env;
use lan_mouse::{ use lan_mouse::{config::Config, frontend, server::Server};
config::Config,
consumer,
frontend::{self, FrontendListener},
producer,
server::Server,
};
use tokio::{join, task::LocalSet}; use tokio::task::LocalSet;
pub fn main() { pub fn main() {
// init logging // init logging
@@ -58,26 +52,10 @@ fn run_service(config: &Config) -> Result<()> {
// run async event loop // run async event loop
runtime.block_on(LocalSet::new().run_until(async { runtime.block_on(LocalSet::new().run_until(async {
// create frontend communication adapter // run main loop
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?;
log::info!("Press Ctrl+Alt+Shift+Super to release the mouse"); 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"); log::debug!("service exiting");
anyhow::Ok(()) anyhow::Ok(())
}))?; }))?;

File diff suppressed because it is too large Load Diff