aboutsummaryrefslogtreecommitdiff
path: root/src/connection.rs
blob: 8de42f362580c603ffba56278b61b2d5d5eb7848 (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
#[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};

#[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<(CraftReader<R>, CraftWriter<W>)> for CraftConnection<R, W> {
    fn into_inner(self) -> (CraftReader<R>, CraftWriter<W>) {
        (self.reader, self.writer)
    }
}

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(())
    }
}

impl<R, W> CraftSyncReader for CraftConnection<R, W>
where
    CraftReader<R>: CraftSyncReader,
    CraftWriter<W>: CraftSyncWriter,
{
    fn read_packet<'a, P>(&'a mut self) -> ReadResult<<P as RawPacket<'a>>::Packet>
    where
        P: RawPacket<'a>,
    {
        self.reader.read_packet::<P>()
    }

    fn read_raw_packet<'a, P>(&'a mut self) -> ReadResult<P>
    where
        P: RawPacket<'a>,
    {
        self.reader.read_raw_packet::<P>()
    }
}

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,
{
    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
    }

    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(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
    }
}