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 {
/// 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.
@@ -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 {
@@ -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,

View File

@@ -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 {

View File

@@ -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
@@ -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(())
}))?;

File diff suppressed because it is too large Load Diff