fix: keys and clicks not counting correctly
This commit is contained in:
parent
cff4c435d2
commit
c7c2a10f59
1 changed files with 12 additions and 14 deletions
|
|
@ -1,6 +1,9 @@
|
|||
use std::{
|
||||
net::TcpStream,
|
||||
sync::{Arc, Mutex},
|
||||
sync::{
|
||||
atomic::{AtomicU16, Ordering},
|
||||
Arc,
|
||||
},
|
||||
thread,
|
||||
time::Duration,
|
||||
};
|
||||
|
|
@ -14,22 +17,20 @@ use crate::websocket;
|
|||
pub fn start_sending(socket: &mut WebSocket<MaybeTlsStream<TcpStream>>) {
|
||||
let mut sys = System::new();
|
||||
|
||||
let key_counter = Arc::new(Mutex::new(0_u16));
|
||||
let click_counter = Arc::new(Mutex::new(0_u16));
|
||||
let key_counter = Arc::new(AtomicU16::new(0));
|
||||
let click_counter = Arc::new(AtomicU16::new(0));
|
||||
|
||||
let key_counter_clone = Arc::clone(&key_counter);
|
||||
let click_counter_clone = Arc::clone(&click_counter);
|
||||
|
||||
thread::spawn(move || {
|
||||
KeybdKey::bind_all(move |_| {
|
||||
let mut count = key_counter_clone.lock().unwrap();
|
||||
*count += 1;
|
||||
key_counter_clone.fetch_add(1, Ordering::SeqCst);
|
||||
});
|
||||
|
||||
MouseButton::bind_all(move |event| match event {
|
||||
MouseButton::LeftButton | MouseButton::RightButton => {
|
||||
let mut count = click_counter_clone.lock().unwrap();
|
||||
*count += 1;
|
||||
click_counter_clone.fetch_add(1, Ordering::SeqCst);
|
||||
}
|
||||
_ => {}
|
||||
});
|
||||
|
|
@ -47,17 +48,14 @@ pub fn start_sending(socket: &mut WebSocket<MaybeTlsStream<TcpStream>>) {
|
|||
let used_memory = sys.used_memory();
|
||||
let memory_usage = ((used_memory as f64) / (total_memory as f64) * 100.0).floor() as u8;
|
||||
|
||||
let mut key_guard = key_counter.lock().unwrap();
|
||||
let key_presses = *key_guard;
|
||||
|
||||
let mut click_guard = click_counter.lock().unwrap();
|
||||
let clicks = *click_guard;
|
||||
let key_presses = key_counter.load(Ordering::SeqCst);
|
||||
let clicks = click_counter.load(Ordering::SeqCst);
|
||||
|
||||
websocket::send(socket, cpu_usage, memory_usage, key_presses, clicks);
|
||||
|
||||
// Reset counters after sending
|
||||
*key_guard = 0;
|
||||
*click_guard = 0;
|
||||
key_counter.store(0, Ordering::SeqCst);
|
||||
click_counter.store(0, Ordering::SeqCst);
|
||||
|
||||
thread::sleep(Duration::from_secs(60));
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue