lt_rs/alerts/
types.rs

1#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
2#[cfg_attr(feature = "safe_enums", derive(num_enum::FromPrimitive))]
3#[repr(u8)]
4pub enum Event {
5    None = 0,
6    Completed,
7    Started,
8    Stopped,
9    Paused,
10    #[cfg(feature = "safe_enums")]
11    #[num_enum(default)]
12    Unknown,
13}
14
15pub use crate::ffi::ffi::PieceIndex;
16
17impl PieceIndex {
18    pub fn new(value: i32) -> Self {
19        Self { inner: value }
20    }
21
22    pub fn to_inner(self) -> i32 {
23        self.inner
24    }
25}
26
27#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
28#[cfg_attr(feature = "safe_enums", derive(num_enum::FromPrimitive))]
29#[repr(u8)]
30pub enum SocketType {
31    Tcp = 0,
32    Socks5,
33    Http,
34    Utp,
35    I2p,
36    TcpSsl,
37    Socks5Ssl,
38    HttpSsl,
39    UtpSsl,
40    #[cfg(feature = "safe_enums")]
41    #[num_enum(default)]
42    Unknown,
43}
44
45#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
46#[cfg_attr(feature = "safe_enums", derive(num_enum::FromPrimitive))]
47#[repr(u8)]
48pub enum ConnectionType {
49    BitTorrent = 0,
50    UrlSeed,
51    HttpSeed,
52    #[cfg(feature = "safe_enums")]
53    #[num_enum(default)]
54    Unknown,
55}
56
57#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
58#[cfg_attr(feature = "safe_enums", derive(num_enum::FromPrimitive))]
59#[repr(u8)]
60pub enum Direction {
61    In = 0,
62    Out,
63    #[cfg(feature = "safe_enums")]
64    #[num_enum(default)]
65    Unknown,
66}
67
68/// These are all the reasons to disconnect a peer
69/// all reasons caused by the peer sending unexpected data
70/// are 256 and up.
71#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
72#[cfg_attr(feature = "safe_enums", derive(num_enum::FromPrimitive))]
73#[repr(u16)]
74pub enum CloseReason {
75    /// No reason specified. Generic close.
76    None = 0,
77    /// We're already connected to
78    DuplicatePeerId,
79    /// This torrent has been removed, paused or stopped from this client.
80    TorrentRemoved,
81    /// Client failed to allocate necessary memory for this peer connection
82    NoMemory,
83    /// The source port of this peer is blocked
84    PortBlocked,
85    /// The source IP has been blocked
86    Blocked,
87    /// Both ends of the connection are upload-only. staying connected would
88    /// be redundant
89    UploadToUpload,
90    /// Connection was closed because the other end is upload only and does
91    /// not have any pieces we're interested in
92    NotInterestedUploadOnly,
93    /// Peer connection timed out (generic timeout)
94    Timeout,
95    /// The peers have not been interested in each other for a very long time.
96    /// disconnect
97    TimedOutInterest,
98    /// The peer has not sent any message in a long time.
99    TimedOutActivity,
100    /// The peer did not complete the handshake in too long
101    TimedOutHandshake,
102    /// The peer sent an interested message, but did not send a request
103    /// after a very long time after being unchoked.
104    TimedOutRequest,
105    /// The encryption mode is blocked
106    ProtocolBlocked,
107    /// The peer was disconnected in the hopes of finding a better peer
108    /// in the swarm
109    PeerChurn,
110    /// We have too many peers connected
111    TooManyConnections,
112    /// We have too many file-descriptors open
113    TooManyFiles,
114
115    /// The encryption handshake failed
116    EncryptionError = 256,
117    /// The info hash sent as part of the handshake was not what we expected
118    InvalidInfoHash,
119    SelfConnection,
120    /// The metadata received matched the info-hash, but failed to parse.
121    /// this is either someone finding a SHA1 collision, or the author of
122    /// the magnet link creating it from an invalid torrent
123    InvalidMetadata,
124    /// The advertised metadata size
125    MetadataTooBig,
126
127    /// Invalid bittorrent messages
128    MessageTooBig,
129    InvalidMessageId,
130    InvalidMessage,
131    InvalidPieceMessage,
132    InvalidHaveMessage,
133    InvalidBitfieldMessage,
134    InvalidChokeMessage,
135    InvalidUnchokeMessage,
136    InvalidInterestedMessage,
137    InvalidNotInterestedMessage,
138    InvalidRequestMessage,
139    InvalidRejectMessage,
140    InvalidAllowFastMessage,
141    InvalidExtendedMessage,
142    InvalidCancelMessage,
143    InvalidDhtPortMessage,
144    InvalidSuggestMessage,
145    InvalidHaveAllMessage,
146    InvalidDontHaveMessage,
147    InvalidHaveNoneMessage,
148    InvalidPexMessage,
149    InvalidMetadataRequestMessage,
150    InvalidMetadataMessage,
151    InvalidMetadataOffset,
152
153    /// The peer sent a request while being choked
154    RequestWhenChoked,
155
156    /// The peer sent corrupt data
157    CorruptPieces,
158
159    PexMessageTooBig,
160    PexTooFrequent,
161
162    #[cfg(feature = "safe_enums")]
163    #[num_enum(default)]
164    Unknown,
165}
166
167impl CloseReason {
168    pub(crate) fn from_u16(value: u16) -> Self {
169        cfg_if::cfg_if! {
170            if #[cfg(feature = "safe_enums")] {
171                value.into()
172            } else {
173                unsafe { std::mem::transmute(value) }
174            }
175        }
176    }
177}
178
179pub use crate::ffi::ffi::PeerRequest;
180impl PartialEq for PeerRequest {
181    fn eq(&self, other: &Self) -> bool {
182        self.piece == other.piece && self.start == other.start && self.length == other.length
183    }
184}
185
186impl Direction {
187    pub(crate) fn from_u8(value: u8) -> Self {
188        cfg_if::cfg_if! {
189            if #[cfg(feature = "safe_enums")] {
190                value.into()
191            } else {
192                unsafe { std::mem::transmute(value) }
193            }
194        }
195    }
196}
197
198impl ConnectionType {
199    pub(crate) fn from_u8(value: u8) -> Self {
200        cfg_if::cfg_if! {
201            if #[cfg(feature = "safe_enums")] {
202                value.into()
203            } else {
204                unsafe { std::mem::transmute(value) }
205            }
206        }
207    }
208}
209
210impl SocketType {
211    pub(crate) fn from_u8(value: u8) -> Self {
212        cfg_if::cfg_if! {
213            if #[cfg(feature = "safe_enums")] {
214                value.into()
215            } else {
216                unsafe { std::mem::transmute(value) }
217            }
218        }
219    }
220}
221
222impl Event {
223    pub(crate) fn from_u8(value: u8) -> Self {
224        cfg_if::cfg_if! {
225            if #[cfg(feature = "safe_enums")] {
226                value.into()
227            } else {
228                unsafe { std::mem::transmute(value) }
229            }
230        }
231    }
232}