Skip to content

Commit

Permalink
added some checks to reduce performance penalties when services are n…
Browse files Browse the repository at this point in the history
…ot available
  • Loading branch information
greaka committed Jul 4, 2019
1 parent dc4f32a commit 35addd0
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "arcdps-bhud"
version = "0.2.0"
version = "0.2.2"
authors = ["Greaka <[email protected]>"]
edition = "2018"

Expand Down
2 changes: 1 addition & 1 deletion src/arcdps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use winapi::shared::{minwindef::LPVOID, ntdef::PCCHAR};
static mut ARCDPS: LPVOID = null::<isize>() as LPVOID;

pub fn gen_arcdps() -> LPVOID {
let arcdps = arcdps_bindings::arcdps_exports::new(0x0002_0804, "BHUDrender", "0.1")
let arcdps = arcdps_bindings::arcdps_exports::new(0x0002_0804, "BHUDrender", env!("CARGO_PKG_VERSION"))
.imgui(imgui as arcdps_bindings::SafeImguiCallback)
.combat(combat as arcdps_bindings::SafeCombatCallback);

Expand Down
5 changes: 3 additions & 2 deletions src/worker/device/mod.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
mod worker;

use std::collections::HashMap;
use std::sync::atomic::AtomicBool;
use std::sync::mpsc::Receiver;
use std::sync::Mutex;
use std::sync::{Arc, Mutex};
pub use worker::ChannelType;
use worker::Device;

static mut DEVICE: Option<Mutex<HashMap<&'static str, Device>>> = None;

pub fn gen_device<A>(device_name: &'static str, action: A)
where
A: Fn(Receiver<ChannelType>) + Sized + Send + 'static,
A: Fn(Arc<AtomicBool>, Receiver<ChannelType>) + Sized + Send + 'static,
{
let device = Device::new(action);
unsafe {
Expand Down
25 changes: 19 additions & 6 deletions src/worker/device/worker.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,38 @@
use std::sync::mpsc;
use std::sync::mpsc::Receiver;
use std::sync::atomic::Ordering::Acquire;
use std::sync::{
atomic::AtomicBool,
mpsc::{self, Receiver},
Arc,
};

pub type ChannelType = Vec<u8>;

pub struct Device {
active: Arc<AtomicBool>,
sender: mpsc::Sender<ChannelType>,
}

impl Device {
pub fn new<A>(action: A) -> Device
where
A: Fn(Receiver<ChannelType>) + Sized + Send + 'static,
A: Fn(Arc<AtomicBool>, Receiver<ChannelType>) + Sized + Send + 'static,
{
let (tx, rx) = mpsc::channel();
let device = Device { sender: tx };
std::thread::spawn(move || action(rx));
let device = Device {
active: Arc::new(AtomicBool::new(false)),
sender: tx,
};
let active = device.active.clone();
std::thread::spawn(move || action(active, rx));

device
}

pub fn send(&self, func: ChannelType) -> Result<(), mpsc::SendError<ChannelType>> {
self.sender.send(func)
if self.active.load(Acquire) {
self.sender.send(func)
} else {
Ok(())
}
}
}
23 changes: 20 additions & 3 deletions src/worker/log.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,37 @@
use super::device::*;
use std::fs::OpenOptions;
use std::io::Write;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering::{Acquire, Release};
use std::sync::mpsc::Receiver;
use std::sync::Arc;
use std::time::Duration;

static NAME: &'static str = "log";
static FILE_PATH: &'static str = "addons/bhud";
static FILE_NAME: &'static str = "addons/bhud/errors.log";

pub fn new() {
let action = |rx: Receiver<ChannelType>| {
std::fs::create_dir_all(FILE_PATH);
let action = |active: Arc<AtomicBool>, rx: Receiver<ChannelType>| {
let _ = std::fs::create_dir_all(FILE_PATH);
let mut file_res = OpenOptions::new().append(true).create(true).open(FILE_NAME);
let duration = Duration::new(1, 0);
loop {
let content = rx.recv().unwrap();
let content = if active.load(Acquire) {
rx.recv().unwrap()
} else {
std::thread::sleep(duration);
file_res = OpenOptions::new().append(true).create(true).open(FILE_NAME);
if file_res.is_ok() {
active.store(true, Release);
}
continue;
};
if file_res.is_err() {
file_res = OpenOptions::new().append(true).create(true).open(FILE_NAME);
}
if let Ok(file) = &mut file_res {
active.store(true, Release);
let res = file.write(content.as_ref());
match res {
Ok(0) => {
Expand All @@ -27,6 +42,8 @@ pub fn new() {
}
_ => {}
}
} else {
active.store(false, Release);
}
}
};
Expand Down
21 changes: 19 additions & 2 deletions src/worker/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,35 @@ use super::device::*;
use super::log;
use std::io::Write;
use std::net::TcpStream;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering::{Acquire, Release};
use std::sync::mpsc::Receiver;
use std::sync::Arc;
use std::time::Duration;

static NAME: &'static str = "tcp";
static CONNECTION_STRING: &'static str = "127.0.0.1:8214";

pub fn new() {
let action = |rx: Receiver<ChannelType>| {
let action = |active: Arc<AtomicBool>, rx: Receiver<ChannelType>| {
let mut stream = TcpStream::connect(CONNECTION_STRING);
let duration = Duration::new(1, 0);
loop {
let data_to_send = rx.recv().unwrap();
let data_to_send = if active.load(Acquire) {
rx.recv().unwrap()
} else {
std::thread::sleep(duration);
stream = TcpStream::connect(CONNECTION_STRING);
if stream.is_ok() {
active.store(true, Release);
}
continue;
};
if stream.is_err() {
stream = TcpStream::connect(CONNECTION_STRING);
}
if let Ok(tcp_stream) = &mut stream {
active.store(true, Release);
let bytes = build_array(data_to_send.as_ref());
let res = tcp_stream.write(bytes.as_ref());
match res {
Expand All @@ -29,6 +44,8 @@ pub fn new() {
}
_ => {}
}
} else {
active.store(false, Release);
}
}
};
Expand Down

0 comments on commit 35addd0

Please sign in to comment.