Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split tasks - No more blocking the event loop #57

Merged
merged 11 commits into from
Dec 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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