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 { pub(crate) reader: CraftReader, pub(crate) writer: CraftWriter, } impl CraftWrapper<(CraftReader, CraftWriter)> for CraftConnection { fn into_inner(self) -> (CraftReader, CraftWriter) { (self.reader, self.writer) } } impl CraftIo for CraftConnection { fn set_state(&mut self, next: State) { self.reader.set_state(next); self.writer.set_state(next); } fn set_compression_threshold(&mut self, threshold: Option) { self.reader.set_compression_threshold(threshold); self.writer.set_compression_threshold(threshold); } 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 CraftSyncReader for CraftConnection where CraftReader: CraftSyncReader, CraftWriter: CraftSyncWriter, { fn read_packet<'a, P>(&'a mut self) -> ReadResult<

>::Packet> where P: RawPacket<'a>, { self.reader.read_packet::

() } fn read_raw_packet<'a, P>(&'a mut self) -> ReadResult

where P: RawPacket<'a>, { self.reader.read_raw_packet::

() } } impl CraftSyncWriter for CraftConnection where CraftReader: CraftSyncReader, CraftWriter: CraftSyncWriter, { fn write_packet

(&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 CraftAsyncReader for CraftConnection where CraftReader: CraftAsyncReader, R: Send + Sync, CraftWriter: CraftAsyncWriter, W: Send + Sync, { async fn read_packet_async<'a, P>(&'a mut self) -> ReadResult<

>::Packet> where P: RawPacket<'a>, { self.reader.read_packet_async::

().await } async fn read_raw_packet_async<'a, P>(&'a mut self) -> ReadResult

where P: RawPacket<'a>, { self.reader.read_raw_packet_async::

().await } } #[cfg(any(feature = "futures-io", feature = "tokio-io"))] #[async_trait] impl CraftAsyncWriter for CraftConnection where CraftReader: CraftAsyncReader, R: Send + Sync, CraftWriter: CraftAsyncWriter, W: Send + Sync, { async fn write_packet_async

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