Skip to content

Commit

Permalink
small changes to pipe handling
Browse files Browse the repository at this point in the history
  • Loading branch information
F0903 committed Sep 6, 2024
1 parent bc52638 commit 82197bd
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 8 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ Uses virtually no CPU, as code only runs when you plug your laptop in or out of
**Note: Only Windows 11/10 is supported due to API requirements.**
**Note: Beware that antivirus might flag the service or install scripts.**

Download the files from the latest release, or the build artifacts of the latest commit.
Afterwards, copy both the autopower.exe, autopower_notification_provider.exe, and all .ps1 files from the folder to your desired location. Then simply run the install_service.ps1 script, and everything should work!
Download the files from the latest release (recommended), or the build artifacts of the latest commit (not recommended).
Afterwards, copy both the autopower.exe, autopower_notification_provider.exe, and all .ps1 files from the folder to your desired location (recommended to be a non-admin folder).
Then simply run the install_service.ps1 script, and everything should work!
After this, the service will start automatically with your PC.

## Building
Expand Down
2 changes: 1 addition & 1 deletion shared/src/pipe/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use windows::Win32::{
};

const RETRYING_DELAY: u32 = 1000;
const RETRYING_ATTEMPTS: u32 = 10;
const RETRYING_ATTEMPTS: u32 = 30;

pub struct Client;

Expand Down
4 changes: 2 additions & 2 deletions shared/src/pipe/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use std::io::{Read, Write};

pub type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;

pub const PIPE_BUFFER_SIZE: u32 = 512;
pub const PIPE_PATH_ROOT: &str = "\\\\.\\pipe\\";
pub const PIPE_NAME: &str = "AutoPowerNotificationPipe";

Expand All @@ -26,7 +25,8 @@ pub struct Pipe<M, S: HandleStreamMode> {

impl<M> Pipe<M, stream::Read> {
pub fn read_to<T: serde::de::DeserializeOwned>(&mut self) -> Result<T> {
let mut buf = [0; PIPE_BUFFER_SIZE as usize];
let mut buf = Vec::with_capacity(1024);
self.stream.read_to_end(&mut buf)?;
let count = self.read(&mut buf)?;
let obj = bincode::deserialize(&mut buf[..count])?;
Ok(obj)
Expand Down
8 changes: 5 additions & 3 deletions shared/src/pipe/server.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{Pipe, Result, PIPE_BUFFER_SIZE, PIPE_PATH_ROOT};
use super::{Pipe, Result, PIPE_PATH_ROOT};
use crate::{
stream::{HandleStream, HandleStreamMode},
util::get_last_win32_err,
Expand All @@ -16,13 +16,15 @@ use windows::Win32::{
},
};

const PIPE_BUFFER_SIZE: u32 = 1024;

pub struct Server;

impl<S: HandleStreamMode> Pipe<Server, S> {
fn get_security_descriptor() -> Result<SECURITY_DESCRIPTOR> {
let mut security_desc = SECURITY_DESCRIPTOR::default();
let p_security_desc =
PSECURITY_DESCRIPTOR(std::ptr::addr_of_mut!(security_desc) as *mut std::ffi::c_void);
PSECURITY_DESCRIPTOR((&mut security_desc as *mut SECURITY_DESCRIPTOR).cast());

unsafe {
InitializeSecurityDescriptor(p_security_desc, SECURITY_DESCRIPTOR_REVISION)?;
Expand All @@ -37,7 +39,7 @@ impl<S: HandleStreamMode> Pipe<Server, S> {
let security = SECURITY_ATTRIBUTES {
nLength: std::mem::size_of::<SECURITY_ATTRIBUTES>() as u32,
bInheritHandle: true.into(),
lpSecurityDescriptor: std::ptr::addr_of_mut!(security_desc) as *mut std::ffi::c_void,
lpSecurityDescriptor: (&mut security_desc as *mut SECURITY_DESCRIPTOR).cast(),
};

let pipe_name = to_win32_wstr(&format!("{}{}", PIPE_PATH_ROOT, name));
Expand Down

0 comments on commit 82197bd

Please sign in to comment.