diff options
| author | mat <27899617+mat-1@users.noreply.github.com> | 2022-08-06 07:22:19 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-08-06 02:22:19 -0500 |
| commit | 5a9fca0ca9cdb46f4b866781f219756c89e2293a (patch) | |
| tree | b006e28b91a181734fb9702bb6ec510f5b2af3df /azalea-protocol/src/connect.rs | |
| parent | 1d48c3fe34edd4e2295f54bd3d79f81f58c38a8e (diff) | |
| download | azalea-drasl-5a9fca0ca9cdb46f4b866781f219756c89e2293a.tar.xz | |
Better errors (#14)
* make reading use thiserror
* finish implementing all the error things
* clippy warnings related to ok_or
* fix some errors in other places
* thiserror in more places
* don't use closures in a couple places
* errors in writing packet
* rip backtraces
* change some BufReadError::Custom to UnexpectedEnumVariant
* Errors say what packet is bad
* error on leftover data and fix
it wasn't reading the properties for gameprofile
Diffstat (limited to 'azalea-protocol/src/connect.rs')
| -rwxr-xr-x | azalea-protocol/src/connect.rs | 25 |
1 files changed, 14 insertions, 11 deletions
diff --git a/azalea-protocol/src/connect.rs b/azalea-protocol/src/connect.rs index cd51f86c..e29ba1df 100755 --- a/azalea-protocol/src/connect.rs +++ b/azalea-protocol/src/connect.rs @@ -5,12 +5,13 @@ use crate::packets::handshake::{ClientboundHandshakePacket, ServerboundHandshake use crate::packets::login::{ClientboundLoginPacket, ServerboundLoginPacket}; use crate::packets::status::{ClientboundStatusPacket, ServerboundStatusPacket}; use crate::packets::ProtocolPacket; -use crate::read::read_packet; +use crate::read::{read_packet, ReadPacketError}; use crate::write::write_packet; use crate::ServerIpAddress; use azalea_crypto::{Aes128CfbDec, Aes128CfbEnc}; use std::fmt::Debug; use std::marker::PhantomData; +use thiserror::Error; use tokio::net::TcpStream; pub struct Connection<R: ProtocolPacket, W: ProtocolPacket> { @@ -28,7 +29,7 @@ where R: ProtocolPacket + Debug, W: ProtocolPacket + Debug, { - pub async fn read(&mut self) -> Result<R, String> { + pub async fn read(&mut self) -> Result<R, ReadPacketError> { read_packet::<R, _>( &mut self.stream, self.compression_threshold, @@ -38,30 +39,32 @@ where } /// Write a packet to the server - pub async fn write(&mut self, packet: W) { + pub async fn write(&mut self, packet: W) -> std::io::Result<()> { write_packet( packet, &mut self.stream, self.compression_threshold, &mut self.enc_cipher, ) - .await; + .await } } +#[derive(Error, Debug)] +pub enum ConnectionError { + #[error("{0}")] + Io(#[from] std::io::Error), +} + impl Connection<ClientboundHandshakePacket, ServerboundHandshakePacket> { - pub async fn new(address: &ServerIpAddress) -> Result<Self, String> { + pub async fn new(address: &ServerIpAddress) -> Result<Self, ConnectionError> { let ip = address.ip; let port = address.port; - let stream = TcpStream::connect(format!("{}:{}", ip, port)) - .await - .map_err(|_| "Failed to connect to server")?; + let stream = TcpStream::connect(format!("{}:{}", ip, port)).await?; // enable tcp_nodelay - stream - .set_nodelay(true) - .expect("Error enabling tcp_nodelay"); + stream.set_nodelay(true)?; Ok(Connection { stream, |
