Skip to content

Commit

Permalink
refactored code
Browse files Browse the repository at this point in the history
  • Loading branch information
Anastasiia Romaniuk committed Apr 8, 2021
1 parent 0e0d1fe commit ab78a7b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 20 deletions.
33 changes: 18 additions & 15 deletions devolutions-gateway/src/interceptor/pcap_recording.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ use slog_scope::debug;
use std::{
net::SocketAddr,
sync::{Arc, Condvar, Mutex},
thread,
time::Duration,
};

#[derive(Debug, Clone, Copy)]
enum State {
#[derive(Debug)]
enum RecordingState {
Update,
Finish,
}
Expand All @@ -18,7 +20,7 @@ pub struct PcapRecordingInterceptor {
client_info: Arc<Mutex<PeerInfo>>,
packets_parser: Arc<Mutex<Option<PacketsParser>>>,
recorder: Arc<Mutex<Option<Recorder>>>,
condition_timeout: Arc<(Mutex<State>, Condvar)>,
condition_timeout: Arc<(Mutex<RecordingState>, Condvar)>,
}

impl PcapRecordingInterceptor {
Expand All @@ -35,34 +37,33 @@ impl PcapRecordingInterceptor {
client_info: Arc::new(Mutex::new(PeerInfo::new(client_addr))),
packets_parser: Arc::new(Mutex::new(PLUGIN_MANAGER.lock().unwrap().get_parsing_packets_plugin())),
recorder: Arc::new(Mutex::new(recording_plugin)),
condition_timeout: Arc::new((Mutex::new(State::Update), Condvar::new())),
condition_timeout: Arc::new((Mutex::new(RecordingState::Update), Condvar::new())),
};

let recorder = interceptor.recorder.clone();
let condition_timeout = interceptor.condition_timeout.clone();
std::thread::spawn(move || loop {
let mut timeout: u32 = 0;
thread::spawn(move || loop {
let mut timeout = 0;

{
if let Some(recorder) = recorder.lock().unwrap().as_ref() {
timeout = recorder.get_timeout();
}
}

let (state, cond_var) = &*condition_timeout;
let result = cond_var
.wait_timeout(state.lock().unwrap(), std::time::Duration::from_millis(timeout as u64));
let (state, cond_var) = condition_timeout.as_ref();
let result = cond_var.wait_timeout(state.lock().unwrap(), Duration::from_millis(timeout as u64));

match result {
Ok((state_result, timeout_result)) => match *state_result {
State::Update => {
RecordingState::Update => {
if timeout_result.timed_out() {
if let Some(recorder) = recorder.lock().unwrap().as_ref() {
recorder.timeout();
}
}
}
State::Finish => break,
RecordingState::Finish => break,
},
Err(e) => {
slog_scope::error!("Wait timeout failed with error! {}", e);
Expand All @@ -89,9 +90,10 @@ impl PacketInterceptor for PcapRecordingInterceptor {
let is_from_server = source_addr.unwrap() == server_info.addr;

if is_from_server {
let (state, cond_var) = &*self.condition_timeout.clone();
let condition_timeout = self.condition_timeout.clone();
let (state, cond_var) = condition_timeout.as_ref();
let mut pending = state.lock().unwrap();
*pending = State::Update;
*pending = RecordingState::Update;
cond_var.notify_one();
}

Expand Down Expand Up @@ -132,9 +134,10 @@ impl PacketInterceptor for PcapRecordingInterceptor {

impl Drop for PcapRecordingInterceptor {
fn drop(&mut self) {
let (state, cond_var) = &*self.condition_timeout.clone();
let condition_timeout = self.condition_timeout.clone();
let (state, cond_var) = condition_timeout.as_ref();
let mut pending = state.lock().unwrap();
*pending = State::Finish;
*pending = RecordingState::Finish;
cond_var.notify_one();
}
}
22 changes: 17 additions & 5 deletions devolutions-gateway/src/plugin_manager/recording.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@ use crate::plugin_manager::packets_parsing::ImageUpdate;
use crate::utils::into_other_io_error;
use dlopen::symbor::{Library, SymBorApi, Symbol};
use dlopen_derive::SymBorApi;
use std::{ffi::CString, io::Error, mem::transmute, os::raw::c_char, sync::Arc};
use std::{
ffi::CString,
io::Error,
mem::transmute,
os::raw::c_char,
path::{Path, PathBuf},
sync::Arc,
};

pub type RecordingContext = usize;
const MAX_PATH_LEN: u32 = 512;
const MAX_PATH_LEN: usize = 512;

#[allow(non_snake_case)]
#[derive(SymBorApi)]
Expand Down Expand Up @@ -101,13 +108,18 @@ impl Recorder {
unsafe { (self.api.NowRecording_GetTimeout)(self.ctx) }
}

pub fn get_filepath(&self) -> String {
pub fn get_filepath(&self) -> Option<PathBuf> {
let mut path_array = [0i8; MAX_PATH_LEN];
unsafe {
(self.api.NowRecording_GetPath)(self.ctx, path_array.as_mut_ptr());
}
return String::from_utf8(path_array.iter().map(|element| *element as u8).collect())
.map_or("".to_string(), |path| path);

let str_path = String::from_utf8(path_array.iter().map(|element| *element as u8).collect());
if let Ok(path) = str_path {
Some(Path::new(path.as_str()).to_path_buf())
} else {
None
}
}
}

Expand Down

0 comments on commit ab78a7b

Please sign in to comment.