diff options
| author | Ubuntu <github@matdoes.dev> | 2022-11-11 21:04:18 +0000 |
|---|---|---|
| committer | Ubuntu <github@matdoes.dev> | 2022-11-11 21:04:18 +0000 |
| commit | 4a1ec068fc13393e249853403085be6958a807a4 (patch) | |
| tree | bdaf6de36f1682eef0839122af1ba9037fa95165 /azalea-protocol | |
| parent | dc62ada865db6534e7c4a5a958d82c44e42bf089 (diff) | |
| download | azalea-drasl-4a1ec068fc13393e249853403085be6958a807a4.tar.xz | |
improved docs
Diffstat (limited to 'azalea-protocol')
| -rw-r--r-- | azalea-protocol/src/connect.rs | 5 | ||||
| -rwxr-xr-x | azalea-protocol/src/lib.rs | 11 | ||||
| -rw-r--r-- | azalea-protocol/src/read.rs | 10 | ||||
| -rwxr-xr-x | azalea-protocol/src/resolver.rs | 2 | ||||
| -rwxr-xr-x | azalea-protocol/src/write.rs | 9 |
5 files changed, 23 insertions, 14 deletions
diff --git a/azalea-protocol/src/connect.rs b/azalea-protocol/src/connect.rs index f67d0e4a..d14d3424 100644 --- a/azalea-protocol/src/connect.rs +++ b/azalea-protocol/src/connect.rs @@ -1,4 +1,4 @@ -//! Create connections that communicate with a remote server or client. +//! Connect to remote servers/clients. use crate::packets::game::{ClientboundGamePacket, ServerboundGamePacket}; use crate::packets::handshake::{ClientboundHandshakePacket, ServerboundHandshakePacket}; @@ -11,7 +11,7 @@ use crate::write::write_packet; use azalea_auth::sessionserver::SessionServerError; use azalea_crypto::{Aes128CfbDec, Aes128CfbEnc}; use bytes::BytesMut; -use log::{info, error}; +use log::{error, info}; use std::fmt::Debug; use std::marker::PhantomData; use std::net::SocketAddr; @@ -116,6 +116,7 @@ impl<R> ReadConnection<R> where R: ProtocolPacket + Debug, { + /// Read a packet from the stream. pub async fn read(&mut self) -> Result<R, ReadPacketError> { read_packet::<R, _>( &mut self.read_stream, diff --git a/azalea-protocol/src/lib.rs b/azalea-protocol/src/lib.rs index 84306142..0fae75b1 100755 --- a/azalea-protocol/src/lib.rs +++ b/azalea-protocol/src/lib.rs @@ -6,6 +6,8 @@ //! //! [`azalea`]: https://crates.io/crates/azalea //! [`azalea_client`]: https://crates.io/crates/azalea-client +//! +//! See [`crate::connect::Connection`] for an example. // these two are necessary for thiserror backtraces #![feature(error_generic_member_access)] @@ -57,15 +59,6 @@ impl<'a> TryFrom<&'a str> for ServerAddress { } } -#[cfg(feature = "connecting")] -pub async fn connect(address: ServerAddress) -> Result<(), Box<dyn std::error::Error>> { - use log::debug; - - let resolved_address = resolver::resolve_address(&address).await; - debug!("Resolved address: {:?}", resolved_address); - Ok(()) -} - #[cfg(test)] mod tests { use std::io::Cursor; diff --git a/azalea-protocol/src/read.rs b/azalea-protocol/src/read.rs index 8d8bc731..a459d3bb 100644 --- a/azalea-protocol/src/read.rs +++ b/azalea-protocol/src/read.rs @@ -1,3 +1,5 @@ +//! Read packets from a stream. + use crate::packets::ProtocolPacket; use azalea_buf::BufReadError; use azalea_buf::McBufVarReadable; @@ -157,6 +159,8 @@ pub enum DecompressionError { AboveCompressionThreshold { size: u32, maximum: u32 }, } +/// Get the decompressed bytes from a packet. It must have been decrypted +/// first. fn compression_decoder( stream: &mut Cursor<&[u8]>, compression_threshold: u32, @@ -192,6 +196,12 @@ fn compression_decoder( Ok(decoded_buf) } +/// Read a single packet from a stream. +/// +/// The buffer is required because servers may send multiple packets in the +/// same frame, so we need to store the packet data that's left to read. +/// +/// The current protocol state must be passed as a generic. pub async fn read_packet<'a, P: ProtocolPacket + Debug, R>( stream: &'a mut R, buffer: &mut BytesMut, diff --git a/azalea-protocol/src/resolver.rs b/azalea-protocol/src/resolver.rs index b3455a59..26baf2f9 100755 --- a/azalea-protocol/src/resolver.rs +++ b/azalea-protocol/src/resolver.rs @@ -1,3 +1,5 @@ +//! Resolve IPs from hostnames. + use crate::ServerAddress; use async_recursion::async_recursion; use std::net::{IpAddr, SocketAddr}; diff --git a/azalea-protocol/src/write.rs b/azalea-protocol/src/write.rs index 8e56e5fc..10f09165 100755 --- a/azalea-protocol/src/write.rs +++ b/azalea-protocol/src/write.rs @@ -1,3 +1,5 @@ +//! Write packets to a stream. + use crate::{packets::ProtocolPacket, read::MAXIMUM_UNCOMPRESSED_LENGTH}; use async_compression::tokio::bufread::ZlibEncoder; use azalea_buf::McBufVarWritable; @@ -6,10 +8,11 @@ use std::fmt::Debug; use thiserror::Error; use tokio::io::{AsyncReadExt, AsyncWrite, AsyncWriteExt}; -fn frame_prepender(data: &mut Vec<u8>) -> Result<Vec<u8>, std::io::Error> { +/// Prepend the length of the packet to it. +fn frame_prepender(mut data: Vec<u8>) -> Result<Vec<u8>, std::io::Error> { let mut buf = Vec::new(); (data.len() as u32).var_write_into(&mut buf)?; - buf.append(data); + buf.append(&mut data); Ok(buf) } @@ -82,7 +85,7 @@ where if let Some(threshold) = compression_threshold { buf = compression_encoder(&buf, threshold).await.unwrap(); } - buf = frame_prepender(&mut buf).unwrap(); + buf = frame_prepender(buf).unwrap(); // if we were given a cipher, encrypt the packet if let Some(cipher) = cipher { azalea_crypto::encrypt_packet(cipher, &mut buf); |
