use std::{error::Error, fmt, net::SocketAddr, time::Duration};
use crate::{ClientError, ClientEvent, ClientTimestamp};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SendReceipt {
pub seq: u32,
pub remote: SocketAddr,
pub sent_at: ClientTimestamp,
pub bytes: usize,
pub send_call: Duration,
}
impl From<SendReceipt> for ClientEvent {
fn from(receipt: SendReceipt) -> Self {
Self::EchoSent {
seq: receipt.seq,
remote: receipt.remote,
scheduled_at: None,
sent_at: receipt.sent_at,
bytes: receipt.bytes,
send_call: receipt.send_call,
timer_error: None,
}
}
}
#[derive(Debug)]
pub enum SendProbeError {
NotCommitted(ClientError),
AfterCommit {
receipt: SendReceipt,
source: Box<ClientError>,
},
}
impl fmt::Display for SendProbeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::NotCommitted(source) => write!(f, "probe send was not committed: {source}"),
Self::AfterCommit { source, .. } => {
write!(
f,
"probe send was committed, but post-send processing failed: {source}"
)
}
}
}
}
impl Error for SendProbeError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
Some(match self {
Self::NotCommitted(source) => source,
Self::AfterCommit { source, .. } => source.as_ref(),
})
}
}