#[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 { pub(crate) reader: CraftReader, pub(crate) writer: CraftWriter, } impl CraftWrapper<(R, W)> for CraftConnection { fn into_inner(self) -> (R, W) { (self.reader.into_inner(), self.writer.into_inner()) } } impl CraftIo for CraftConnection { 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) { 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); } fn ensure_buf_capacity(&mut self, capacity: usize) { self.reader.ensure_buf_capacity(capacity); self.writer.ensure_buf_capacity(capacity); } #[cfg(feature = "compression")] fn ensure_compression_buf_capacity(&mut self, capacity: usize) { self.reader.ensure_buf_capacity(capacity); self.writer.ensure_buf_capacity(capacity); } } impl CraftSyncReader for CraftConnection where CraftReader: CraftSyncReader, CraftWriter: CraftSyncWriter, { #[cfg(not(feature = "gat"))] fn read_packet<'a, P>(&'a mut self) -> ReadResult<

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

() } #[cfg(feature = "gat")] fn read_packet

(&mut self) -> ReadResult< as RawPacket>::Packet> where P: PacketKind { self.reader.read_packet::

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

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

() } #[cfg(feature = "gat")] fn read_raw_packet

(&mut self) -> ReadResult> where P: PacketKind { self.reader.read_raw_packet::

() } fn read_raw_untyped_packet(&mut self) -> ReadResult<(Id, &[u8])> { self.reader.read_raw_untyped_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, { #[cfg(not(feature = "gat"))] async fn read_packet_async<'a, P>(&'a mut self) -> ReadResult<

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

().await } #[cfg(feature = "gat")] async fn read_packet_async

(&mut self) -> ReadResult< as RawPacket<'_>>::Packet> where P: PacketKind { self.reader.read_packet_async::

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

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

().await } #[cfg(feature = "gat")] async fn read_raw_packet_async

(&mut self) -> ReadResult> where P: PacketKind { self.reader.read_raw_packet_async::

().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 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 } } impl CraftConnection { pub fn into_split(self) -> (CraftReader, CraftWriter) { (self.reader, self.writer) } pub fn split(&mut self) -> (&mut CraftReader, &mut CraftWriter) { (&mut self.reader, &mut self.writer) } }