diff options
| author | mat <github@matdoes.dev> | 2021-12-06 00:28:40 -0600 |
|---|---|---|
| committer | mat <github@matdoes.dev> | 2021-12-06 00:28:40 -0600 |
| commit | 5029a09963b5753c1f9b7f777f28e1c0951343e7 (patch) | |
| tree | 2e0e37029bf031adc3e28713828e7d4be7336ccb /minecraft-protocol/src/connection.rs | |
| download | azalea-drasl-5029a09963b5753c1f9b7f777f28e1c0951343e7.tar.xz | |
Initial commit
Diffstat (limited to 'minecraft-protocol/src/connection.rs')
| -rw-r--r-- | minecraft-protocol/src/connection.rs | 38 |
1 files changed, 38 insertions, 0 deletions
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<TcpStream>, + /// The read buffer + pub buffer: BytesMut, +} + +impl Connection { + pub async fn new(address: &ServerIpAddress) -> Result<Connection, 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(Connection { + flow: PacketFlow::ClientToServer, + stream: BufWriter::new(stream), + // 4mb read buffer + buffer: BytesMut::with_capacity(4 * 1024 * 1024), + }) + } +} |
