From 9f78b3f4a7229033a3f5acaad6c8fffbe24e3e75 Mon Sep 17 00:00:00 2001 From: Honbra Date: Mon, 14 Nov 2022 20:25:59 +0100 Subject: Military-grade server implementation (#40) * Military-grade server implementation * Add doc comments --- azalea-protocol/src/connect.rs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/azalea-protocol/src/connect.rs b/azalea-protocol/src/connect.rs index 37df0f08..00685d3c 100755 --- a/azalea-protocol/src/connect.rs +++ b/azalea-protocol/src/connect.rs @@ -302,7 +302,8 @@ where R1: ProtocolPacket + Debug, W1: ProtocolPacket + Debug, { - fn from(connection: Connection) -> Connection + /// Creates a `Connection` of a type from a `Connection` of another type. Useful for servers or custom packets. + pub fn from(connection: Connection) -> Connection where R2: ProtocolPacket + Debug, W2: ProtocolPacket + Debug, @@ -323,4 +324,25 @@ where }, } } + + /// Convert an existing `TcpStream` into a `Connection`. Useful for servers. + pub fn wrap(stream: TcpStream) -> Connection { + let (read_stream, write_stream) = stream.into_split(); + + Connection { + reader: ReadConnection { + read_stream, + buffer: BytesMut::new(), + compression_threshold: None, + dec_cipher: None, + _reading: PhantomData, + }, + writer: WriteConnection { + write_stream, + compression_threshold: None, + enc_cipher: None, + _writing: PhantomData, + }, + } + } } -- cgit v1.2.3