aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/connection.rs55
-rw-r--r--src/reader.rs80
2 files changed, 123 insertions, 12 deletions
diff --git a/src/connection.rs b/src/connection.rs
index 8de42f3..f53f07d 100644
--- a/src/connection.rs
+++ b/src/connection.rs
@@ -4,7 +4,8 @@ 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(feature = "gat")]
+use mcproto_rs::protocol::PacketKind;
#[cfg(any(feature = "futures-io", feature = "tokio-io"))]
use {
crate::{reader::CraftAsyncReader, writer::CraftAsyncWriter},
@@ -16,9 +17,9 @@ pub struct CraftConnection<R, W> {
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> CraftWrapper<(R, W)> for CraftConnection<R, W> {
+ fn into_inner(self) -> (R, W) {
+ (self.reader.into_inner(), self.writer.into_inner())
}
}
@@ -47,6 +48,7 @@ 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>,
@@ -54,12 +56,29 @@ where
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>()
+ }
}
impl<R, W> CraftSyncWriter for CraftConnection<R, W>
@@ -91,6 +110,7 @@ where
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>,
@@ -98,12 +118,29 @@ where
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
+ }
}
#[cfg(any(feature = "futures-io", feature = "tokio-io"))]
@@ -129,3 +166,13 @@ where
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)
+ }
+} \ No newline at end of file
diff --git a/src/reader.rs b/src/reader.rs
index 271e0b7..5dcbd87 100644
--- a/src/reader.rs
+++ b/src/reader.rs
@@ -5,6 +5,8 @@ use crate::wrapper::{CraftIo, CraftWrapper};
#[cfg(feature = "compression")]
use flate2::{DecompressError, FlushDecompress, Status};
use mcproto_rs::protocol::{Id, PacketDirection, RawPacket, State};
+#[cfg(feature = "gat")]
+use mcproto_rs::protocol::PacketKind;
use mcproto_rs::types::VarInt;
use mcproto_rs::{Deserialize, Deserialized};
#[cfg(feature = "backtrace")]
@@ -62,6 +64,7 @@ pub type ReadResult<P> = Result<Option<P>, ReadError>;
#[cfg(any(feature = "futures-io", feature = "tokio-io"))]
#[async_trait]
pub trait CraftAsyncReader {
+ #[cfg(not(feature = "gat"))]
async fn read_packet_async<'a, P>(&'a mut self) -> ReadResult<<P as RawPacket<'a>>::Packet>
where
P: RawPacket<'a>,
@@ -69,12 +72,27 @@ pub trait CraftAsyncReader {
deserialize_raw_packet(self.read_raw_packet_async::<P>().await)
}
+ #[cfg(feature = "gat")]
+ async fn read_packet_async<P>(&mut self) -> ReadResult<<P::RawPacket<'_> as RawPacket<'_>>::Packet>
+ where
+ P: PacketKind
+ {
+ deserialize_raw_packet(self.read_raw_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>;
+
+ #[cfg(feature = "gat")]
+ async fn read_raw_packet_async<P>(&mut self) -> ReadResult<P::RawPacket<'_>>
+ where
+ P: PacketKind;
}
pub trait CraftSyncReader {
+ #[cfg(not(feature = "gat"))]
fn read_packet<'a, P>(&'a mut self) -> ReadResult<<P as RawPacket<'a>>::Packet>
where
P: RawPacket<'a>,
@@ -82,9 +100,23 @@ pub trait CraftSyncReader {
deserialize_raw_packet(self.read_raw_packet::<'a, P>())
}
+ #[cfg(feature = "gat")]
+ fn read_packet<P>(&mut self) -> ReadResult<<P::RawPacket<'_> as RawPacket>::Packet>
+ where
+ P: PacketKind
+ {
+ deserialize_raw_packet(self.read_raw_packet::<P>())
+ }
+
+ #[cfg(not(feature = "gat"))]
fn read_raw_packet<'a, P>(&'a mut self) -> ReadResult<P>
where
P: RawPacket<'a>;
+
+ #[cfg(feature = "gat")]
+ fn read_raw_packet<'a, P>(&'a mut self) -> ReadResult<P::RawPacket<'a>>
+ where
+ P: PacketKind;
}
pub struct CraftReader<R> {
@@ -150,14 +182,20 @@ impl<R> CraftSyncReader for CraftReader<R>
where
R: io::Read,
{
+ #[cfg(not(feature = "gat"))]
fn read_raw_packet<'a, P>(&'a mut self) -> ReadResult<P>
where
P: RawPacket<'a>,
{
- self.move_ready_data_to_front();
- let primary_packet_len = rr_unwrap!(self.read_packet_len_sync()).0 as usize;
- self.ensure_n_ready_sync(primary_packet_len)?;
- self.read_packet_in_buf(primary_packet_len)
+ self.read_raw_packet_inner::<P>()
+ }
+
+ #[cfg(feature = "gat")]
+ fn read_raw_packet<'a, P>(&'a mut self) -> ReadResult<P::RawPacket<'a>>
+ where
+ P: PacketKind
+ {
+ self.read_raw_packet_inner::<P::RawPacket<'a>>()
}
}
@@ -167,14 +205,20 @@ impl<R> CraftAsyncReader for CraftReader<R>
where
R: AsyncReadExact,
{
+ #[cfg(not(feature = "gat"))]
async fn read_raw_packet_async<'a, P>(&'a mut self) -> ReadResult<P>
where
P: RawPacket<'a>,
{
- self.move_ready_data_to_front();
- let primary_packet_len = rr_unwrap!(self.read_packet_len_async().await).0 as usize;
- self.ensure_n_ready_async(primary_packet_len).await?;
- self.read_packet_in_buf(primary_packet_len)
+ self.read_raw_packet_inner_async().await
+ }
+
+ #[cfg(feature = "gat")]
+ async fn read_raw_packet_async<P>(&mut self) -> ReadResult<P::RawPacket<'_>>
+ where
+ P: PacketKind,
+ {
+ self.read_raw_packet_inner_async::<P::RawPacket<'_>>().await
}
}
@@ -182,6 +226,16 @@ impl<R> CraftReader<R>
where
R: io::Read,
{
+ fn read_raw_packet_inner<'a, P>(&'a mut self) -> ReadResult<P>
+ where
+ P: RawPacket<'a>
+ {
+ self.move_ready_data_to_front();
+ let primary_packet_len = rr_unwrap!(self.read_packet_len_sync()).0 as usize;
+ self.ensure_n_ready_sync(primary_packet_len)?;
+ self.read_packet_in_buf(primary_packet_len)
+ }
+
fn read_packet_len_sync(&mut self) -> ReadResult<VarInt> {
let buf = rr_unwrap!(self.ensure_n_ready_sync(VAR_INT_BUF_SIZE));
let (v, size) = rr_unwrap!(deserialize_varint(buf));
@@ -209,6 +263,16 @@ impl<R> CraftReader<R>
where
R: AsyncReadExact,
{
+ async fn read_raw_packet_inner_async<'a, P>(&'a mut self) -> ReadResult<P>
+ where
+ P: RawPacket<'a>
+ {
+ self.move_ready_data_to_front();
+ let primary_packet_len = rr_unwrap!(self.read_packet_len_async().await).0 as usize;
+ self.ensure_n_ready_async(primary_packet_len).await?;
+ self.read_packet_in_buf(primary_packet_len)
+ }
+
async fn read_packet_len_async(&mut self) -> ReadResult<VarInt> {
self.move_ready_data_to_front();
let buf = rr_unwrap!(self.ensure_n_ready_async(VAR_INT_BUF_SIZE).await);