craftio_rs/wrapper.rs
1#[cfg(feature = "encryption")]
2use crate::cfb8::CipherError;
3use mcproto_rs::protocol::State;
4
5///
6/// Indicates that a type provided by this crate is wrapping some inner value of type `I`, which can
7/// be unwrapped by calling the `into_inner` function.
8///
9pub trait CraftWrapper<I> {
10 ///
11 /// Unwraps the wrapped value of type `I`, and drops the wrapper type
12 ///
13 fn into_inner(self) -> I;
14}
15
16///
17/// Trait for stateful connection types, such as the `CraftReader<R>` or `CraftWriter<W>` or combo
18/// type `CraftConnection<R, W>`.
19///
20/// Allows for control over protocol state, compression threshold, and encryption if those features
21/// are enabled.
22///
23pub trait CraftIo {
24 ///
25 /// Changes the current connection state. For readers, this changes how numeric packet IDs are
26 /// interpreted. For writers, this will change the packets that can be written without a panic.
27 ///
28 fn set_state(&mut self, next: State);
29
30
31 ///
32 /// Modifies the compression configuration. If a value of `None` is provided, then compression is
33 /// disabled. If a value of `Some` is provided, then the threshold is set to that value.
34 ///
35 /// If a 0 or negative value is provided in a `Some` variant, then it is the same as calling
36 /// this function with the `None` variant
37 ///
38 #[cfg(feature = "compression")]
39 fn set_compression_threshold(&mut self, threshold: Option<i32>);
40
41 ///
42 /// Modifies the encryption configuration. This function should only be called once, and can only
43 /// be used to enable encryption.
44 ///
45 /// If encryption is already enabled or the arguments are not valid for the cipher, then an
46 /// error is returned and nothing in the underlying state is changed.
47 ///
48 #[cfg(feature = "encryption")]
49 fn enable_encryption(&mut self, key: &[u8], iv: &[u8]) -> Result<(), CipherError>;
50
51 ///
52 /// Sets the max packet size which this I/O wrapper will decode or transmit.
53 ///
54 /// This limit is meant to be used to ensure connections never allocate gigantic buffers.
55 /// Therefore, the limitation applies to the representation of packet in memory. This means
56 /// that a reader cannot read a compressed packet above this threshold, nor can it decompress
57 /// to a packet which is above this threshold. A writer cannot write a packet which exceeds
58 /// this size (when serialized) even if compression is enabled.
59 ///
60 /// todo split the compressed vs not compressed limits?
61 ///
62 fn set_max_packet_size(&mut self, max_size: usize);
63
64 fn ensure_buf_capacity(&mut self, capacity: usize);
65
66 #[cfg(feature = "compression")]
67 fn ensure_compression_buf_capacity(&mut self, capacity: usize);
68}