Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion diode-file-bindings/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ use std::{
str::FromStr,
};

/// # Panics
///
/// Will return `Err` if ip and port cannot be parsed.
#[unsafe(no_mangle)]
#[allow(clippy::missing_safety_doc)]
pub unsafe extern "C" fn diode_new_config(
Expand Down Expand Up @@ -41,6 +44,9 @@ pub unsafe extern "C" fn diode_free_config(ptr: *mut file::Config<aux::DiodeSend
}
}

/// # Panics
///
/// Will return `Err` if reference to `config` is wrong.
#[unsafe(no_mangle)]
#[allow(clippy::missing_safety_doc)]
pub unsafe extern "C" fn diode_send_file(
Expand All @@ -58,9 +64,13 @@ pub unsafe extern "C" fn diode_send_file(
let cstr_filepath = unsafe { CStr::from_ptr(ptr_filepath) };
let rust_filepath = String::from_utf8_lossy(cstr_filepath.to_bytes()).to_string();

file::send::send_file(config, &rust_filepath).unwrap_or(0) as u32
let result: usize = file::send::send_file(config, &rust_filepath).unwrap_or(0);
u32::try_from(result).unwrap_or(0)
}

/// # Panics
///
/// Will return `Err` if reference to `config` is wrong.
#[unsafe(no_mangle)]
#[allow(clippy::missing_safety_doc)]
pub unsafe extern "C" fn diode_receive_files(
Expand Down
3 changes: 3 additions & 0 deletions src/aux/file/receive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ use std::{
path, thread,
};

/// # Errors
///
/// Will return `Err` if `output_dir` is not a directory.
pub fn receive_files(
config: &file::Config<aux::DiodeReceive>,
output_dir: &path::Path,
Expand Down
11 changes: 11 additions & 0 deletions src/aux/file/send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ use std::{
path,
};

/// # Errors
///
/// Will return `Err` if `send_file` function
/// returns an `Err`.
pub fn send_files(
config: &file::Config<aux::DiodeSend>,
files: &[String],
Expand All @@ -21,6 +25,13 @@ pub fn send_files(
Ok(())
}

/// # Errors
///
/// Will return `Err` if:
/// - `net::TcpStream::connect(socket_addr)?`
/// or
/// - `unix::net::UnixStream::connect(path)?`
/// fails.
pub fn send_file(
config: &file::Config<aux::DiodeSend>,
file_path: &String,
Expand Down
4 changes: 4 additions & 0 deletions src/aux/udp/receive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ fn receive_tcp_loop(
}
}

/// # Errors
///
/// Will return `Err` if `from_unix` `PathBuf`
/// already exists.
pub fn receive(
config: &udp::Config<aux::DiodeReceive>,
to_udp_bind: net::SocketAddr,
Expand Down
7 changes: 7 additions & 0 deletions src/aux/udp/send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ where
}
}

/// # Errors
///
/// Will return `Err` if:
/// - `net::TcpStream::connect(socket_addr)?`
/// or
/// - `unix::net::UnixStream::connect(path)?`
/// fails.
pub fn send(
config: &udp::Config<aux::DiodeSend>,
from_udp: net::SocketAddr,
Expand Down
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ mod sock_utils;
#[allow(unsafe_code)]
mod udp;

/// # Errors
///
/// Will return `Err` if `file` cannot be opened
/// or logger cannot be set (Term or file mode).
pub fn init_logger(
level_filter: log::LevelFilter,
file: Option<path::PathBuf>,
Expand Down
10 changes: 10 additions & 0 deletions src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ pub struct RaptorQ {
}

impl RaptorQ {
/// # Errors
///
/// Will return `Err` if `symbol_count`
/// or
/// `nb_repair_packets` parsing fails
pub fn new(mtu: u16, block_size: u32, repair_percentage: u32) -> Result<Self, Error> {
let mut max_packet_size = mtu - PACKET_HEADER_SIZE - RAPTORQ_HEADER_SIZE;
max_packet_size -= max_packet_size % RAPTORQ_ALIGNMENT;
Expand Down Expand Up @@ -110,18 +115,22 @@ impl RaptorQ {
})
}

#[must_use]
pub const fn block_size(&self) -> u32 {
self.transfer_length
}

#[must_use]
pub const fn min_nb_packets(&self) -> u16 {
self.symbol_count
}

#[must_use]
pub fn nb_packets(&self) -> u32 {
u32::from(self.symbol_count) + u32::from(self.nb_repair_packets)
}

#[must_use]
pub fn encode(&self, block_id: u8, data: &[u8]) -> Vec<raptorq::EncodingPacket> {
let encoder = raptorq::SourceBlockEncoder::with_encoding_plan(
block_id,
Expand All @@ -139,6 +148,7 @@ impl RaptorQ {
packets
}

#[must_use]
pub fn decode(&self, block_id: u8, packets: Vec<raptorq::EncodingPacket>) -> Option<Vec<u8>> {
let mut decoder = raptorq::SourceBlockDecoder::new(
block_id,
Expand Down
7 changes: 7 additions & 0 deletions src/receive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ where
ClientEnd: Send + Sync + Fn(C, bool),
E: Into<Error>,
{
/// # Errors
///
/// Will return `Err` if `multiplex_control` semaphore
/// cannot be created.
pub fn new(
config: Config,
raptorq: protocol::RaptorQ,
Expand Down Expand Up @@ -203,6 +207,9 @@ where
})
}

/// # Errors
///
/// Will return `Err` if scoped threads cannot spawned.
#[allow(clippy::too_many_lines)]
pub fn start<'a>(&'a self, scope: &'a thread::Scope<'a, '_>) -> Result<(), Error> {
log::info!(
Expand Down
15 changes: 13 additions & 2 deletions src/send/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ impl<C> Sender<C>
where
C: Read + AsRawFd + Send,
{
/// # Errors
///
/// Will return `Err` if `multiplex_control` semaphore
/// cannot be created.
pub fn new(config: Config, raptorq: protocol::RaptorQ) -> Result<Self, Error> {
let multiplex_control = semka::Sem::new(config.max_clients)
.ok_or(Error::Other("failed to create semaphore".into()))?;
Expand Down Expand Up @@ -151,6 +155,9 @@ where
})
}

/// # Errors
///
/// Will return `Err` if scoped threads cannot spawned.
pub fn start<'a>(&'a self, scope: &'a thread::Scope<'a, '_>) -> Result<(), Error> {
log::info!(
"max {} simultaneous clients/transfers",
Expand Down Expand Up @@ -246,14 +253,18 @@ where

Ok(())
}

/// # Errors
///
/// Will return `Err` if the `send` returns a `SendError<T>`.
pub fn new_client(&self, client: C) -> Result<(), Error> {
if let Err(e) = self.to_server.send(Some(client)) {
return Err(Error::Diode(format!("failed to enqueue client: {e}")));
}
Ok(())
}

/// # Errors
///
/// Will return `Err` if the `send` returns a `SendError<T>`.
pub fn stop(&self) -> Result<(), Error> {
if let Err(e) = self.to_server.send(None) {
return Err(Error::Diode(format!("failed to stop: {e}")));
Expand Down
Loading