From 5029a09963b5753c1f9b7f777f28e1c0951343e7 Mon Sep 17 00:00:00 2001 From: mat Date: Mon, 6 Dec 2021 00:28:40 -0600 Subject: Initial commit --- minecraft-protocol/src/connection.rs | 38 ++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 minecraft-protocol/src/connection.rs (limited to 'minecraft-protocol/src/connection.rs') diff --git a/minecraft-protocol/src/connection.rs b/minecraft-protocol/src/connection.rs new file mode 100644 index 00000000..4fa1cde7 --- /dev/null +++ b/minecraft-protocol/src/connection.rs @@ -0,0 +1,38 @@ +use crate::ServerIpAddress; +use bytes::BytesMut; +use tokio::{io::BufWriter, net::TcpStream}; + +pub enum PacketFlow { + ClientToServer, + ServerToClient, +} + +pub struct Connection { + pub flow: PacketFlow, + pub stream: BufWriter, + /// The read buffer + pub buffer: BytesMut, +} + +impl Connection { + pub async fn new(address: &ServerIpAddress) -> Result { + 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(Connection { + flow: PacketFlow::ClientToServer, + stream: BufWriter::new(stream), + // 4mb read buffer + buffer: BytesMut::with_capacity(4 * 1024 * 1024), + }) + } +} -- cgit v1.2.3