diff --git a/Cargo.lock b/Cargo.lock index eed5cec..6b0eea8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11,6 +11,7 @@ dependencies = [ "postcard", "rand", "serde", + "tokio", "uuid", ] @@ -112,6 +113,12 @@ version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" +[[package]] +name = "bytes" +version = "1.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc652a48c352aef3ea3aed32080501cf3ef6ed5da78602a020c991775b0aff04" + [[package]] name = "cfg-if" version = "1.0.4" @@ -231,6 +238,16 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f" +[[package]] +name = "errno" +version = "0.3.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" +dependencies = [ + "libc", + "windows-sys", +] + [[package]] name = "foldhash" version = "0.1.5" @@ -429,6 +446,17 @@ version = "2.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98" +[[package]] +name = "mio" +version = "1.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "30d65c71f1ce40ab09135ce117d742b9f8a19ff91a41a8b57ed50bc2de59c427" +dependencies = [ + "libc", + "wasi", + "windows-sys", +] + [[package]] name = "once_cell" version = "1.21.4" @@ -441,6 +469,29 @@ version = "1.70.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" +[[package]] +name = "parking_lot" +version = "0.12.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93857453250e3077bd71ff98b6a65ea6621a19bb0f559a85248955ac12c45a1a" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2621685985a2ebf1c516881c026032ac7deafcda1a2c9b7850dc81e3dfcb64c1" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "smallvec", + "windows-link", +] + [[package]] name = "pin-project-lite" version = "0.2.17" @@ -526,6 +577,15 @@ version = "0.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "63b8176103e19a2643978565ca18b50549f6101881c443590420e4dc998a3c69" +[[package]] +name = "redox_syscall" +version = "0.5.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed2bf2547551a7053d6fdfafda3f938979645c44812fbfcda098faae3f1a362d" +dependencies = [ + "bitflags 2.13.1", +] + [[package]] name = "regex" version = "1.13.1" @@ -625,12 +685,38 @@ dependencies = [ "zmij", ] +[[package]] +name = "signal-hook-registry" +version = "1.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4db69cba1110affc0e9f7bcd48bbf87b3f4fc7c61fc9155afd4c469eb3d6c1b" +dependencies = [ + "errno", + "libc", +] + [[package]] name = "slab" version = "0.4.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0c790de23124f9ab44544d7ac05d60440adc586479ce501c1d6d7da3cd8c9cf5" +[[package]] +name = "smallvec" +version = "1.15.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ed6a63f02c8539c91a8685a86f4099661ba3da017932f6ebbea6de3f0fa7c90" + +[[package]] +name = "socket2" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3d1e2c7f27f8d4cb10542a02c49005dbd6e93095799d6f3be745fae9f8fedd4" +dependencies = [ + "libc", + "windows-sys", +] + [[package]] name = "spin" version = "0.9.9" @@ -688,6 +774,34 @@ dependencies = [ "syn 3.0.3", ] +[[package]] +name = "tokio" +version = "1.53.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "202caea871b69668250d242070849eb495be178ed697a3e98aebce5bc81a0bed" +dependencies = [ + "bytes", + "libc", + "mio", + "parking_lot", + "pin-project-lite", + "signal-hook-registry", + "socket2", + "tokio-macros", + "windows-sys", +] + +[[package]] +name = "tokio-macros" +version = "2.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78773a2a397f451582ce068015985c33193cf6dea8b74d2a639fe457b2f07b0e" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.3", +] + [[package]] name = "unicode-ident" version = "1.0.24" @@ -718,6 +832,12 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "wasi" +version = "0.11.1+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b" + [[package]] name = "wasip2" version = "1.0.3+wasi-0.2.9" diff --git a/Cargo.toml b/Cargo.toml index 11d7e76..ba9e96a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,7 @@ log = "0.4.33" postcard = { version = "1.1.3", features = ["alloc"] } rand = "0.10.2" serde = { version = "1.0.229", features = ["derive"] } +tokio = { version = "1.53.1", features = ["full"] } [dependencies.uuid] version = "1.24.0" diff --git a/src/network/connection.rs b/src/network/connection.rs index e69de29..a127b46 100644 --- a/src/network/connection.rs +++ b/src/network/connection.rs @@ -0,0 +1,141 @@ +use std::marker::PhantomData; +use std::net::SocketAddr; + +use log::{debug, error, warn}; +use postcard::from_bytes; +use tokio::{net::TcpStream, sync::mpsc::Sender}; + +use tokio::io::AsyncReadExt; + +use crate::network::message::{Message, MessageLike}; + +/// Enum defines __TO WHAT__ connection is connecting +pub enum ConnectionType { + Server, + Client, +} + +pub struct Connection +where + Inbound: MessageLike, +{ + conn_type: ConnectionType, + sender: Sender>, + tcp: Option, + outbound_type: PhantomData, +} + +impl Connection +where + Inbound: MessageLike, + Outbound: MessageLike, +{ + pub fn new( + conn_type: ConnectionType, + sender: Sender>, + ) -> Connection { + Connection { + conn_type, + sender, + tcp: None, + outbound_type: PhantomData, + } + } + + pub async fn connect_to_server(&mut self, url: SocketAddr) -> bool { + match self.conn_type { + ConnectionType::Server => { + self.tcp = match TcpStream::connect(url).await { + Ok(conn) => Some(conn), + Err(e) => { + error!("Error while connecting to the server: {}", e); + None + } + }; + + self.tcp.is_some() + } + ConnectionType::Client => false, // only client can connect to the server + } + } + + pub async fn send(&mut self, msg: Message) { + todo!(); + } + + pub async fn start_listening(&self) { + todo!() + } + + async fn read_header(&mut self) { + let mut buff = [0u8; 4]; + let Some(tcp) = &mut self.tcp else { + error!("Tried reading header, but tcp connection is not established!"); + return; + }; + + let result = tcp.read_exact(&mut buff).await; + + if let Err(e) = result { + error!("Error while reading header: {}", e); + return; + } + + let size_to_read: u32 = u32::from_be_bytes(buff); + + self.read_content(size_to_read).await; + } + + async fn read_content(&mut self, size_to_read: u32) { + let mut buff: Vec = vec![0; size_to_read as usize]; + + let Some(tcp) = &mut self.tcp else { + error!( + "Tried reading content, but tcp connection is not established! This should __never__ happen" + ); + panic!(); + }; + + let bytes_read = tcp.read_exact(&mut buff).await; + + match bytes_read { + Ok(bytes_size) => { + debug!("Read {} bytes during read_content", bytes_size); + } + Err(e) => { + error!( + "Error while recieving bytes from network during read_content: {}", + e + ); + return; + } + } + + let msg_result: Result = MessageLike::from_bits(&buff); + + match msg_result { + Ok(msg_content) => { + let new_msg = Message::new(msg_content); + + match new_msg { + Ok(msg) => { + if let Err(e) = self.sender.send(msg).await { + error!("Erro while sending message to main thread: {}", e); + } + } + Err(e) => { + error!( + "Error while preparing message in read_content for sender: {}", + e + ); + return; + } + } + } + Err(e) => { + error!("Error while getting the message from bits: {}", e); + return; + } + } + } +}