use crate::bandwidth_control::BandwidthControl;
use crate::enums::BandwidthGroupType;
use crate::utility::macros::macros::{
get_function_result_number, get_function_result_pointer, get_function_result_unit,
};
use crate::utility::string_to_c_u64_str;
use crate::{BandwidthGroup, VboxError};
use vbox_raw::sys_lib::IBandwidthGroup;
impl BandwidthControl {
pub fn get_num_groups(&self) -> Result<u32, VboxError> {
get_function_result_number!(self.object, GetNumGroups, u32)
}
pub fn create_bandwidth_group(
&self,
name: &str,
group_type: BandwidthGroupType,
max_bytes_per_sec: i64,
) -> Result<(), VboxError> {
let name_ptr = string_to_c_u64_str(name)?;
let group_type: u32 = group_type.into();
get_function_result_unit!(
self.object,
CreateBandwidthGroup,
name_ptr,
group_type,
max_bytes_per_sec
)
}
pub fn delete_bandwidth_group(&self, name: &str) -> Result<(), VboxError> {
let name_ptr = string_to_c_u64_str(name)?;
get_function_result_unit!(self.object, DeleteBandwidthGroup, name_ptr)
}
pub fn get_bandwidth_group(&self, name: &str) -> Result<BandwidthGroup, VboxError> {
let name_ptr = string_to_c_u64_str(name)?;
let bandwidth_group = get_function_result_pointer!(
self.object,
GetBandwidthGroup,
*mut IBandwidthGroup,
name_ptr
)?;
Ok(BandwidthGroup::new(bandwidth_group))
}
pub fn get_all_bandwidth_groups(&self) -> Result<Vec<BandwidthGroup>, VboxError> {
let mut count = 0;
let bandwidth_groups_ptr = get_function_result_pointer!(
self.object,
GetAllBandwidthGroups,
*mut *mut IBandwidthGroup,
&mut count
)?;
let vec_bandwidth_groups_ptr =
unsafe { Vec::from_raw_parts(bandwidth_groups_ptr, count as usize, count as usize) };
let mut bandwidth_groups = Vec::new();
for bandwidth_group_ptr in vec_bandwidth_groups_ptr {
bandwidth_groups.push(BandwidthGroup::new(bandwidth_group_ptr.clone()))
}
Ok(bandwidth_groups)
}
}