Skip to content

Commit

Permalink
attempts at fixing pipe closing on message received.
Browse files Browse the repository at this point in the history
  • Loading branch information
F0903 committed Sep 6, 2024
1 parent 82197bd commit c699e79
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 22 deletions.
7 changes: 0 additions & 7 deletions shared/src/pipe/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,3 @@ impl<M, S: HandleStreamMode> Pipe<M, S> {
self.stream.close()
}
}

impl<M, S: HandleStreamMode> Drop for Pipe<M, S> {
fn drop(&mut self) {
LOGGER.debug("Dropping pipe...");
self.close().unwrap();
}
}
6 changes: 0 additions & 6 deletions shared/src/stream/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,3 @@ impl<M: HandleStreamMode> HandleStream<M> {
Ok(())
}
}

impl<M: HandleStreamMode> Drop for HandleStream<M> {
fn drop(&mut self) {
self.close().unwrap();
}
}
5 changes: 5 additions & 0 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod config_error;
mod state_config;

use autopower_shared::logging::Logger;
pub use config_error::ConfigError;
use state_config::StateConfig;

Expand All @@ -11,6 +12,8 @@ use std::{
io::{BufReader, BufWriter, Write},
};

const LOGGER: Logger = Logger::new("power_config", "autopower");

#[derive(Serialize, Deserialize, Debug)]
pub struct PowerConfig {
wired_config: StateConfig,
Expand All @@ -34,12 +37,14 @@ impl Default for PowerConfig {

impl PowerConfig {
fn get(path: &str) -> Result<Self, ConfigError> {
LOGGER.debug("Reading power config...");
let fs = File::open(path).map_err(|_| ConfigError::CouldNotLoadOrCreate)?;
let buf = BufReader::new(fs);
serde_json::from_reader(buf).map_err(|_| ConfigError::CouldNotLoadOrCreate)
}

fn new(path: &str) -> Result<Self, ConfigError> {
LOGGER.debug("Writing new power config...");
let new_config = PowerConfig::default();
let fs = File::create(path).map_err(|_| ConfigError::CouldNotLoadOrCreate)?;
let mut buf = BufWriter::new(fs);
Expand Down
7 changes: 0 additions & 7 deletions src/notification_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,3 @@ impl NotificationProvider {
self.pipe.close()
}
}

impl Drop for NotificationProvider {
fn drop(&mut self) {
LOGGER.debug("Dropping notification provider...");
self.terminate().unwrap();
}
}
13 changes: 11 additions & 2 deletions src/services/power_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,13 @@ impl PowerService {
std::thread::spawn(move || {
match ctrl_code {
SERVICE_CONTROL_POWEREVENT => {
LOGGER.debug("Received power event.");
me.handle_power_event(data);
}
SERVICE_CONTROL_STOP => me.handle_stop(),
SERVICE_CONTROL_STOP => {
LOGGER.debug("Received stop event.");
me.handle_stop();
}
x => {
LOGGER.debug(format!("Received unknown control code: {}", x));
}
Expand Down Expand Up @@ -194,18 +198,19 @@ impl WindowsService for PowerService {
},
);

LOGGER.debug("Setting service status to start pending...");
if let Err(e) = me.set_service_status(SERVICE_START_PENDING, None, None) {
LOGGER.error(format!("Could not set service status!\n{}", e));
}

LOGGER.debug("Setting up notification provider...");
me.notification_provider = Some(match NotificationProvider::create() {
Ok(x) => x,
Err(e) => {
LOGGER.error(format!("Could not create notification provider!\n{}", e));
panic!();
}
});
LOGGER.debug("Creation of notification provider successful.");

LOGGER.debug("Registering power setting notification handling...");
if let Err(e) = RegisterPowerSettingNotification(
Expand All @@ -219,6 +224,7 @@ impl WindowsService for PowerService {
));
}

LOGGER.debug("Creating stop event...");
me.stop_event = Some(match CreateEventW(None, TRUE, FALSE, None) {
Ok(x) => x,
Err(err) => {
Expand All @@ -227,6 +233,7 @@ impl WindowsService for PowerService {
}
});

LOGGER.debug("Setting service status to running...");
if let Err(e) = me.set_service_status(
SERVICE_RUNNING,
None,
Expand All @@ -248,6 +255,8 @@ impl WindowsService for PowerService {
.unwrap()
.terminate()
.unwrap();

drop(Box::from_raw(me));
}

fn get_name() -> &'static str {
Expand Down

0 comments on commit c699e79

Please sign in to comment.