aboutsummaryrefslogtreecommitdiff
path: root/azalea-protocol/src/connect.rs
diff options
context:
space:
mode:
authormat <github@matdoes.dev>2021-12-15 23:10:55 -0600
committermat <github@matdoes.dev>2021-12-15 23:10:55 -0600
commit9642558f8f8d983a7087f15d68be8cf07a85f0c2 (patch)
tree5f0a967f005cd5db510a13ab290c8ad6669b25aa /azalea-protocol/src/connect.rs
parent72aefe871ca4983431b1a0b707b472e73ffea836 (diff)
downloadazalea-drasl-9642558f8f8d983a7087f15d68be8cf07a85f0c2.tar.xz
azalea
Diffstat (limited to 'azalea-protocol/src/connect.rs')
-rw-r--r--azalea-protocol/src/connect.rs116
1 files changed, 116 insertions, 0 deletions
diff --git a/azalea-protocol/src/connect.rs b/azalea-protocol/src/connect.rs
new file mode 100644
index 00000000..f6dd9fe7
--- /dev/null
+++ b/azalea-protocol/src/connect.rs
@@ -0,0 +1,116 @@
+//! parse sending and receiving packets with a server.
+
+use crate::packets::game::GamePacket;
+use crate::packets::handshake::HandshakePacket;
+use crate::packets::login::LoginPacket;
+use crate::packets::status::StatusPacket;
+use crate::read::read_packet;
+use crate::write::write_packet;
+use crate::ServerIpAddress;
+use tokio::net::TcpStream;
+
+pub enum PacketFlow {
+ ClientToServer,
+ ServerToClient,
+}
+
+pub struct HandshakeConnection {
+ pub flow: PacketFlow,
+ /// The buffered writer
+ pub stream: TcpStream,
+}
+
+pub struct GameConnection {
+ pub flow: PacketFlow,
+ /// The buffered writer
+ pub stream: TcpStream,
+}
+
+pub struct StatusConnection {
+ pub flow: PacketFlow,
+ /// The buffered writer
+ pub stream: TcpStream,
+}
+
+pub struct LoginConnection {
+ pub flow: PacketFlow,
+ /// The buffered writer
+ pub stream: TcpStream,
+}
+
+impl HandshakeConnection {
+ pub async fn new(address: &ServerIpAddress) -> Result<HandshakeConnection, String> {
+ let ip = address.ip;
+ let port = address.port;
+
+ let stream = TcpStream::connect(format!("{}:{}", ip, port))
+ .await
+ .map_err(|_| "Failed to connect to server")?;
+
+ // enable tcp_nodelay
+ stream
+ .set_nodelay(true)
+ .expect("Error enabling tcp_nodelay");
+
+ Ok(HandshakeConnection {
+ flow: PacketFlow::ServerToClient,
+ stream,
+ })
+ }
+
+ pub fn login(self) -> LoginConnection {
+ LoginConnection {
+ flow: self.flow,
+ stream: self.stream,
+ }
+ }
+
+ pub fn status(self) -> StatusConnection {
+ StatusConnection {
+ flow: self.flow,
+ stream: self.stream,
+ }
+ }
+
+ pub async fn read(&mut self) -> Result<HandshakePacket, String> {
+ read_packet::<HandshakePacket>(&self.flow, &mut self.stream).await
+ }
+
+ /// Write a packet to the server
+ pub async fn write(&mut self, packet: HandshakePacket) {
+ write_packet(packet, &mut self.stream).await;
+ }
+}
+
+impl GameConnection {
+ pub async fn read(&mut self) -> Result<GamePacket, String> {
+ read_packet::<GamePacket>(&self.flow, &mut self.stream).await
+ }
+
+ /// Write a packet to the server
+ pub async fn write(&mut self, packet: GamePacket) {
+ write_packet(packet, &mut self.stream).await;
+ }
+}
+
+impl StatusConnection {
+ pub async fn read(&mut self) -> Result<StatusPacket, String> {
+ read_packet::<StatusPacket>(&self.flow, &mut self.stream).await
+ }
+
+ /// Write a packet to the server
+ pub async fn write(&mut self, packet: StatusPacket) {
+ write_packet(packet, &mut self.stream).await;
+ }
+}
+
+impl LoginConnection {
+ pub async fn read(&mut self) -> Result<LoginPacket, String> {
+ read_packet::<LoginPacket>(&self.flow, &mut self.stream).await
+ }
+
+ /// Write a packet to the server
+ pub async fn write(&mut self, packet: LoginPacket) {
+ write_packet(packet, &mut self.stream).await;
+ }
+}