aboutsummaryrefslogtreecommitdiff
path: root/src/connection.rs
blob: c5e0cf7b3ad1f7b182bb3c14278332bb3d1368c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#[cfg(feature = "encryption")]
use crate::cfb8::CipherError;
use crate::reader::{CraftReader, CraftSyncReader, ReadResult};
use crate::wrapper::{CraftIo, CraftWrapper};
use crate::writer::{CraftSyncWriter, CraftWriter, WriteResult};
use mcproto_rs::protocol::{Packet, RawPacket, State, Id};
#[cfg(feature = "gat")]
use mcproto_rs::protocol::PacketKind;
#[cfg(any(feature = "futures-io", feature = "tokio-io"))]
use {
    crate::{reader::CraftAsyncReader, writer::CraftAsyncWriter},
    async_trait::async_trait,
};

pub struct CraftConnection<R, W> {
    pub(crate) reader: CraftReader<R>,
    pub(crate) writer: CraftWriter<W>,
}

impl<R, W> CraftWrapper<(R, W)> for CraftConnection<R, W> {
    fn into_inner(self) -> (R, W) {
        (self.reader.into_inner(), self.writer.into_inner())
    }
}

impl<R, W> CraftIo for CraftConnection<R, W> {
    fn set_state(&mut self, next: State) {
        self.reader.set_state(next);
        self.writer.set_state(next);
    }

    #[cfg(feature = "compression")]
    fn set_compression_threshold(&mut self, threshold: Option<i32>) {
        self.reader.set_compression_threshold(threshold);
        self.writer.set_compression_threshold(threshold);
    }

    #[cfg(feature = "encryption")]
    fn enable_encryption(&mut self, key: &[u8], iv: &[u8]) -> Result<(), CipherError> {
        self.reader.enable_encryption(key, iv)?;
        self.writer.enable_encryption(key, iv)?;
        Ok(())
    }

    fn set_max_packet_size(&mut self, max_size: usize) {
        self.reader.set_max_packet_size(max_size);
        self.writer.set_max_packet_size(max_size);
    }
}

impl<R, W> CraftSyncReader for CraftConnection<R, W>
where
    CraftReader<R>: CraftSyncReader,
    CraftWriter<W>: CraftSyncWriter,
{
    #[cfg(not(feature = "gat"))]
    fn read_packet<'a, P>(&'a mut self) -> ReadResult<<P as RawPacket<'a>>::Packet>
    where
        P: RawPacket<'a>,
    {
        self.reader.read_packet::<P>()
    }

    #[cfg(feature = "gat")]
    fn read_packet<P>(&mut self) -> ReadResult<<P::RawPacket<'_> as RawPacket>::Packet>
    where
        P: PacketKind
    {
        self.reader.read_packet::<P>()
    }

    #[cfg(not(feature = "gat"))]
    fn read_raw_packet<'a, P>(&'a mut self) -> ReadResult<P>
    where
        P: RawPacket<'a>,
    {
        self.reader.read_raw_packet::<P>()
    }

    #[cfg(feature = "gat")]
    fn read_raw_packet<P>(&mut self) -> ReadResult<P::RawPacket<'_>>
    where
        P: PacketKind
    {
        self.reader.read_raw_packet::<P>()
    }

    fn read_raw_untyped_packet(&mut self) -> ReadResult<(Id, &[u8])> {
        self.reader.read_raw_untyped_packet()
    }
}

impl<R, W> CraftSyncWriter for CraftConnection<R, W>
where
    CraftReader<R>: CraftSyncReader,
    CraftWriter<W>: CraftSyncWriter,
{
    fn write_packet<P>(&mut self, packet: P) -> WriteResult<()>
    where
        P: Packet,
    {
        self.writer.write_packet(packet)
    }

    fn write_raw_packet<'a, P>(&mut self, packet: P) -> WriteResult<()>
    where
        P: RawPacket<'a>,
    {
        self.writer.write_raw_packet(packet)
    }
}

#[cfg(any(feature = "futures-io", feature = "tokio-io"))]
#[async_trait]
impl<R, W> CraftAsyncReader for CraftConnection<R, W>
where
    CraftReader<R>: CraftAsyncReader,
    R: Send + Sync,
    CraftWriter<W>: CraftAsyncWriter,
    W: Send + Sync,
{
    #[cfg(not(feature = "gat"))]
    async fn read_packet_async<'a, P>(&'a mut self) -> ReadResult<<P as RawPacket<'a>>::Packet>
    where
        P: RawPacket<'a>,
    {
        self.reader.read_packet_async::<P>().await
    }

    #[cfg(feature = "gat")]
    async fn read_packet_async<P>(&mut self) -> ReadResult<<P::RawPacket<'_> as RawPacket<'_>>::Packet>
    where
        P: PacketKind
    {
        self.reader.read_packet_async::<P>().await
    }

    #[cfg(not(feature = "gat"))]
    async fn read_raw_packet_async<'a, P>(&'a mut self) -> ReadResult<P>
    where
        P: RawPacket<'a>,
    {
        self.reader.read_raw_packet_async::<P>().await
    }

    #[cfg(feature = "gat")]
    async fn read_raw_packet_async<P>(&mut self) -> ReadResult<P::RawPacket<'_>>
    where
        P: PacketKind
    {
        self.reader.read_raw_packet_async::<P>().await
    }

    async fn read_raw_untyped_packet_async(&mut self) -> ReadResult<(Id, &[u8])> {
        self.reader.read_raw_untyped_packet_async().await
    }
}

#[cfg(any(feature = "futures-io", feature = "tokio-io"))]
#[async_trait]
impl<R, W> CraftAsyncWriter for CraftConnection<R, W>
where
    CraftReader<R>: CraftAsyncReader,
    R: Send + Sync,
    CraftWriter<W>: CraftAsyncWriter,
    W: Send + Sync,
{
    async fn write_packet_async<P>(&mut self, packet: P) -> WriteResult<()>
    where
        P: Packet + Send + Sync,
    {
        self.writer.write_packet_async(packet).await
    }

    async fn write_raw_packet_async<'a, P>(&mut self, packet: P) -> WriteResult<()>
    where
        P: RawPacket<'a> + Send + Sync,
    {
        self.writer.write_raw_packet_async(packet).await
    }
}

impl<R, W> CraftConnection<R, W> {
    pub fn into_split(self) -> (CraftReader<R>, CraftWriter<W>) {
        (self.reader, self.writer)
    }

    pub fn split(&mut self) -> (&mut CraftReader<R>, &mut CraftWriter<W>) {
        (&mut self.reader, &mut self.writer)
    }
}