torrust_tracker/shared/bit_torrent/tracker/udp/
mod.rs

1use std::net::SocketAddr;
2use std::sync::Arc;
3
4use aquatic_udp_protocol::Request;
5use thiserror::Error;
6use torrust_tracker_located_error::DynError;
7
8pub mod client;
9
10/// The maximum number of bytes in a UDP packet.
11pub const MAX_PACKET_SIZE: usize = 1496;
12/// A magic 64-bit integer constant defined in the protocol that is used to
13/// identify the protocol.
14pub const PROTOCOL_ID: i64 = 0x0417_2710_1980;
15
16#[derive(Debug, Clone, Error)]
17pub enum Error {
18    #[error("Timeout while waiting for socket to bind: {addr:?}")]
19    TimeoutWhileBindingToSocket { addr: SocketAddr },
20
21    #[error("Failed to bind to socket: {addr:?}, with error: {err:?}")]
22    UnableToBindToSocket { err: Arc<std::io::Error>, addr: SocketAddr },
23
24    #[error("Timeout while waiting for connection to remote: {remote_addr:?}")]
25    TimeoutWhileConnectingToRemote { remote_addr: SocketAddr },
26
27    #[error("Failed to connect to remote: {remote_addr:?}, with error: {err:?}")]
28    UnableToConnectToRemote {
29        err: Arc<std::io::Error>,
30        remote_addr: SocketAddr,
31    },
32
33    #[error("Timeout while waiting for the socket to become writable.")]
34    TimeoutWaitForWriteableSocket,
35
36    #[error("Failed to get writable socket: {err:?}")]
37    UnableToGetWritableSocket { err: Arc<std::io::Error> },
38
39    #[error("Timeout while trying to send data: {data:?}")]
40    TimeoutWhileSendingData { data: Vec<u8> },
41
42    #[error("Failed to send data: {data:?}, with error: {err:?}")]
43    UnableToSendData { err: Arc<std::io::Error>, data: Vec<u8> },
44
45    #[error("Timeout while waiting for the socket to become readable.")]
46    TimeoutWaitForReadableSocket,
47
48    #[error("Failed to get readable socket: {err:?}")]
49    UnableToGetReadableSocket { err: Arc<std::io::Error> },
50
51    #[error("Timeout while trying to receive data.")]
52    TimeoutWhileReceivingData,
53
54    #[error("Failed to receive data: {err:?}")]
55    UnableToReceivingData { err: Arc<std::io::Error> },
56
57    #[error("Failed to get data from request: {request:?}, with error: {err:?}")]
58    UnableToWriteDataFromRequest { err: Arc<std::io::Error>, request: Request },
59
60    #[error("Failed to parse response: {response:?}, with error: {err:?}")]
61    UnableToParseResponse { err: Arc<std::io::Error>, response: Vec<u8> },
62}
63
64impl From<Error> for DynError {
65    fn from(e: Error) -> Self {
66        Arc::new(Box::new(e))
67    }
68}