Skip to content

Commit

Permalink
refactor(udp_socket): simplify send errors
Browse files Browse the repository at this point in the history
  • Loading branch information
mlegner committed Dec 5, 2023
1 parent 3eaf033 commit ff38b8e
Showing 1 changed file with 29 additions and 7 deletions.
36 changes: 29 additions & 7 deletions crates/scion/src/udp_socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use chrono::Utc;
use scion_proto::{
address::SocketAddr,
datagram::{UdpDatagram, UdpEncodeError},
packet::{ByEndpoint, EncodeError, ScionPacketRaw, ScionPacketUdp},
packet::{self, ByEndpoint, EncodeError, ScionPacketRaw, ScionPacketUdp},
path::Path,
reliable::Packet,
wire_encoding::{MaybeEncoded, WireDecode},
Expand All @@ -29,12 +29,8 @@ pub enum ConnectError {
pub enum SendError {
#[error(transparent)]
Io(#[from] std::io::Error),
#[error(transparent)]
Dispatcher(#[from] dispatcher::SendError),
#[error(transparent)]
PacketEncoding(#[from] EncodeError),
#[error(transparent)]
DatagramEncoding(#[from] UdpEncodeError),
#[error("packet is too large to be sent")]
PacketTooLarge,
#[error("path is expired")]
PathExpired,
#[error("remote address is not set")]
Expand All @@ -43,6 +39,32 @@ pub enum SendError {
NoPath,
}

impl From<dispatcher::SendError> for SendError {
fn from(value: dispatcher::SendError) -> Self {
match value {
dispatcher::SendError::Io(io) => Self::Io(io),
dispatcher::SendError::PayloadTooLarge(_) => Self::PacketTooLarge,
}
}
}

impl From<UdpEncodeError> for SendError {
fn from(value: UdpEncodeError) -> Self {
match value {
UdpEncodeError::PayloadTooLarge => Self::PacketTooLarge,
}
}
}

impl From<packet::EncodeError> for SendError {
fn from(value: packet::EncodeError) -> Self {
match value {
EncodeError::PayloadTooLarge | EncodeError::HeaderTooLarge => Self::PacketTooLarge,
EncodeError::MisalignedHeader => panic!("this should never happen"),
}
}
}

pub struct UdpSocket {
inner: Arc<UdpSocketInner>,
local_address: SocketAddr,
Expand Down

0 comments on commit ff38b8e

Please sign in to comment.