Skip to main content

mctx_core/
error.rs

1use std::{io, net::Ipv4Addr};
2use thiserror::Error;
3
4/// Errors returned by the multicast sender core.
5#[derive(Debug, Error)]
6pub enum MctxError {
7    /// The configured destination port is invalid.
8    #[error("MCTX: invalid destination port")]
9    InvalidDestinationPort,
10
11    /// The configured group address is not a valid multicast IPv4 address.
12    #[error("MCTX: group must be a multicast IPv4 address")]
13    InvalidMulticastGroup,
14
15    /// The configured source port is invalid.
16    #[error("MCTX: invalid source port")]
17    InvalidSourcePort,
18
19    /// The configured source IPv4 address is invalid.
20    #[error("MCTX: invalid source address")]
21    InvalidSourceAddress,
22
23    /// The configured multicast interface address is invalid.
24    #[error("MCTX: invalid interface address")]
25    InvalidInterfaceAddress,
26
27    /// A publication with the same configuration already exists.
28    #[error("MCTX: publication already exists")]
29    DuplicatePublication,
30
31    /// No publication with the requested ID exists.
32    #[error("MCTX: publication not found")]
33    PublicationNotFound,
34
35    /// Creating the UDP socket failed.
36    #[error("MCTX: failed to create UDP socket: {0}")]
37    SocketCreateFailed(io::Error),
38
39    /// Setting a socket option failed.
40    #[error("MCTX: failed to set socket option: {0}")]
41    SocketOptionFailed(io::Error),
42
43    /// Binding the UDP socket failed.
44    #[error("MCTX: failed to bind UDP socket: {0}")]
45    SocketBindFailed(io::Error),
46
47    /// Connecting the UDP socket failed.
48    #[error("MCTX: failed to connect UDP socket: {0}")]
49    SocketConnectFailed(io::Error),
50
51    /// Reading the local address from a socket failed.
52    #[error("MCTX: failed to read local address from socket: {0}")]
53    SocketLocalAddrFailed(io::Error),
54
55    /// The provided existing socket does not match the current IPv4-only send model.
56    #[error("MCTX: existing socket must be an IPv4 UDP socket")]
57    ExistingSocketMustBeIpv4,
58
59    /// The provided existing socket is bound to a different UDP port than requested.
60    #[error("MCTX: existing socket is bound to UDP port {actual}, expected {expected}")]
61    ExistingSocketPortMismatch { expected: u16, actual: u16 },
62
63    /// The provided existing socket is bound to a different local IPv4 address than requested.
64    #[error("MCTX: existing socket is bound to local IPv4 address {actual}, expected {expected}")]
65    ExistingSocketAddressMismatch {
66        expected: Ipv4Addr,
67        actual: Ipv4Addr,
68    },
69
70    /// Sending a packet failed.
71    #[error("MCTX: send failed: {0}")]
72    SendFailed(io::Error),
73}
74
75impl MctxError {
76    #[cfg(feature = "tokio")]
77    pub(crate) fn is_would_block(&self) -> bool {
78        matches!(self, Self::SendFailed(error) if error.kind() == io::ErrorKind::WouldBlock)
79    }
80}