From a75dccefd966560793e9776bc44d09fa22733a43 Mon Sep 17 00:00:00 2001 From: Joey Sacchini Date: Wed, 2 Dec 2020 19:28:56 -0500 Subject: init commit --- src/tcp.rs | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 src/tcp.rs (limited to 'src/tcp.rs') diff --git a/src/tcp.rs b/src/tcp.rs new file mode 100644 index 0000000..e2ead90 --- /dev/null +++ b/src/tcp.rs @@ -0,0 +1,69 @@ +use crate::connection::CraftConnection; +use crate::reader::CraftReader; +use crate::writer::CraftWriter; +use mcproto_rs::protocol::{PacketDirection, State}; +use std::convert::TryFrom; +use std::io::BufReader as StdBufReader; +use std::net::TcpStream; + +#[cfg(feature = "async")] +use futures::io::{AsyncRead, AsyncWrite, BufReader as AsyncBufReader}; + +pub const BUF_SIZE: usize = 8192; + +pub type CraftTcpConnection = CraftConnection, TcpStream>; + +impl CraftConnection, TcpStream> { + pub fn connect_server_std(to: String) -> Result { + Self::from_std(TcpStream::connect(to)?, PacketDirection::ClientBound) + } + + pub fn wrap_client_stream_std(stream: TcpStream) -> Result { + Self::from_std(stream, PacketDirection::ServerBound) + } + + pub fn from_std( + s1: TcpStream, + read_direction: PacketDirection, + ) -> Result { + Self::from_std_with_state(s1, read_direction, State::Handshaking) + } + + pub fn from_std_with_state( + s1: TcpStream, + read_direction: PacketDirection, + state: State, + ) -> Result { + let write = s1.try_clone()?; + let read = StdBufReader::with_capacity(BUF_SIZE, s1); + + Ok(Self { + reader: CraftReader::wrap_with_state(read, read_direction, state), + writer: CraftWriter::wrap_with_state(write, read_direction.opposite(), state), + }) + } +} + +#[cfg(feature = "async")] +impl CraftConnection, W> +where + R: AsyncRead + Send + Sync + Unpin, + W: AsyncWrite + Send + Sync + Unpin, +{ + pub fn from_async(tuple: (R, W), read_direction: PacketDirection) -> Self { + Self::from_async_with_state(tuple, read_direction, State::Handshaking) + } + + pub fn from_async_with_state( + tuple: (R, W), + read_direction: PacketDirection, + state: State, + ) -> Self { + let (reader, writer) = tuple; + let reader = AsyncBufReader::with_capacity(BUF_SIZE, reader); + Self { + reader: CraftReader::wrap_with_state(reader, read_direction, state), + writer: CraftWriter::wrap_with_state(writer, read_direction.opposite(), state), + } + } +} -- cgit