From a3927e752c55dbc70d135ff97cf809eb356d60a4 Mon Sep 17 00:00:00 2001 From: Joey Sacchini Date: Sat, 9 Jan 2021 14:36:46 -0500 Subject: add a readme and cleanup some of the wrapper methods in CraftConnection --- src/reader.rs | 31 ------------------------ src/tcp.rs | 78 +++++++++++++++++++++++++++++++++++++++++++---------------- 2 files changed, 57 insertions(+), 52 deletions(-) (limited to 'src') diff --git a/src/reader.rs b/src/reader.rs index b3bb2dd..1439a17 100644 --- a/src/reader.rs +++ b/src/reader.rs @@ -400,37 +400,6 @@ where } } -#[cfg(any(feature = "futures-io", feature = "tokio-io"))] -pub trait IntoBufferedAsyncRead { - type Target: AsyncReadExact; - - fn into_buffered(self, capacity: usize) -> Self::Target; -} - -#[cfg(all(feature = "futures-io", not(feature = "tokio-io")))] -impl IntoBufferedAsyncRead for R -where - R: futures::io::AsyncRead + Send + Sync + Unpin, -{ - type Target = futures::io::BufReader; - - fn into_buffered(self, capacity: usize) -> Self::Target { - futures::io::BufReader::with_capacity(capacity, self) - } -} - -#[cfg(feature = "tokio-io")] -impl IntoBufferedAsyncRead for R -where - R: tokio::io::AsyncRead + Send + Sync + Unpin, -{ - type Target = tokio::io::BufReader; - - fn into_buffered(self, capacity: usize) -> Self::Target { - tokio::io::BufReader::with_capacity(capacity, self) - } -} - #[cfg(any(feature = "futures-io", feature = "tokio-io"))] #[async_trait] pub trait AsyncReadExact: Unpin + Sync + Send { diff --git a/src/tcp.rs b/src/tcp.rs index bd88885..f16a3c3 100644 --- a/src/tcp.rs +++ b/src/tcp.rs @@ -6,14 +6,30 @@ use std::io::BufReader as StdBufReader; use std::net::TcpStream; #[cfg(any(feature = "futures-io", feature = "tokio-io"))] -use crate::{CraftAsyncReader, CraftAsyncWriter, IntoBufferedAsyncRead}; +use crate::{CraftAsyncReader, CraftAsyncWriter}; + +#[cfg(feature = "tokio-io")] +use tokio::{ + net::{ + TcpStream as TokioTcpStream, + tcp::{ + OwnedReadHalf as TokioReadHalf, + OwnedWriteHalf as TokioWriteHalf, + }, + ToSocketAddrs as TokioToSocketAddrs, + }, + io::{ + BufReader as TokioBufReader, + Error as TokioIoError, + }, +}; pub const BUF_SIZE: usize = 8192; pub type CraftTcpConnection = CraftConnection, TcpStream>; -impl CraftConnection, TcpStream> { - pub fn connect_server_std(to: String) -> Result { +impl CraftTcpConnection { + pub fn connect_server_std(to: A) -> Result where A: std::net::ToSocketAddrs { Self::from_std(TcpStream::connect(to)?, PacketDirection::ClientBound) } @@ -43,32 +59,52 @@ impl CraftConnection, TcpStream> { } } -#[cfg(any(feature = "futures-io", feature = "tokio-io"))] -impl CraftConnection -where - CraftReader: CraftAsyncReader, - CraftWriter: CraftAsyncWriter, -{ - pub fn from_unbuffered_async(tuple: (U, W), read_direction: PacketDirection) -> Self +#[cfg(feature = "tokio-io")] +pub type CraftTokioConnection = CraftConnection, TokioWriteHalf>; + +#[cfg(feature = "tokio-io")] +impl CraftTokioConnection { + pub async fn connect_server_tokio( + to: A + ) -> Result where - U: IntoBufferedAsyncRead, + A: TokioToSocketAddrs { - Self::from_unbuffered_async_with_state(tuple, read_direction, State::Handshaking) + let conn = TokioTcpStream::connect(to).await?; + conn.set_nodelay(true)?; + let (reader, writer) = conn.into_split(); + let reader = TokioBufReader::with_capacity(BUF_SIZE, reader); + Ok(Self::from_async((reader, writer), PacketDirection::ClientBound)) } +} - pub fn from_unbuffered_async_with_state( - tuple: (U, W), - read_direction: PacketDirection, - state: State, - ) -> Self +#[cfg(feature = "tokio-io")] +pub type CraftUnbufferedTokioConnection = CraftConnection; + +#[cfg(feature = "tokio-io")] +impl CraftUnbufferedTokioConnection { + pub async fn connect_server_tokio_unbuffered( + to: A + ) -> Result where - U: IntoBufferedAsyncRead, + A: TokioToSocketAddrs { - let (ru, writer) = tuple; - let reader = ru.into_buffered(BUF_SIZE); - Self::from_async_with_state((reader, writer), read_direction, state) + let conn = TokioTcpStream::connect(to).await?; + conn.set_nodelay(true)?; + + Ok(Self::from_async( + conn.into_split(), + PacketDirection::ClientBound, + )) } +} +#[cfg(any(feature = "futures-io", feature = "tokio-io"))] +impl CraftConnection +where + CraftReader: CraftAsyncReader, + CraftWriter: CraftAsyncWriter, +{ pub fn from_async(tuple: (R, W), read_direction: PacketDirection) -> Self { Self::from_async_with_state(tuple, read_direction, State::Handshaking) } -- cgit