From b358478b4585879b8126a990312a543f58562dbc Mon Sep 17 00:00:00 2001 From: Isabelle L Date: Fri, 15 May 2020 22:07:03 -0500 Subject: [PATCH] new protocol works! :D --- Cargo.lock | 40 +++++++++++++++++++++++++++++++++++++++- Cargo.toml | 4 +++- src/client.rs | 18 +++++++++--------- src/lib.rs | 3 +-- src/server.rs | 33 ++++++++++++++++----------------- 5 files changed, 68 insertions(+), 30 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a4ced72..00d9878 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -9,6 +9,12 @@ dependencies = [ "winapi 0.3.8", ] +[[package]] +name = "anyhow" +version = "1.0.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85bb70cc08ec97ca5450e6eba421deeea5f172c0fc61f78b5357b2a8e8be195f" + [[package]] name = "async-attributes" version = "1.1.1" @@ -122,6 +128,15 @@ dependencies = [ "vec_map", ] +[[package]] +name = "crc32fast" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba125de2af0df55319f41944744ad91c71113bf74a4646efff39afe1f6842db1" +dependencies = [ + "cfg-if", +] + [[package]] name = "crossbeam-channel" version = "0.4.2" @@ -318,14 +333,16 @@ dependencies = [ [[package]] name = "ilmp" version = "0.1.0" -source = "git+https://github.com/izzabelle/ilmp#cb0257e9665b6fc9f0004c928ec043662ea275e0" dependencies = [ + "anyhow", "chrono", + "crc32fast", "futures", "futures-util", "ring", "serde", "serde_json", + "thiserror", "uuid", ] @@ -460,6 +477,7 @@ dependencies = [ name = "msg" version = "0.1.0" dependencies = [ + "anyhow", "async-std", "chrono", "futures", @@ -786,6 +804,26 @@ dependencies = [ "unicode-width", ] +[[package]] +name = "thiserror" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5976891d6950b4f68477850b5b9e5aa64d955961466f9e174363f573e54e8ca7" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ab81dbd1cd69cd2ce22ecfbdd3bdb73334ba25350649408cc6c085f46d89573d" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "time" version = "0.1.43" diff --git a/Cargo.toml b/Cargo.toml index e29c824..22c6fad 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,5 +15,7 @@ structopt = "0.3.14" chrono = "0.4.11" futures = "0.3.5" toml = "0.5.6" -ilmp = { git = "https://github.com/izzabelle/ilmp" } +# ilmp = { git = "https://github.com/izzabelle/ilmp" } +ilmp = { path = "../ilmp"} ring = "0.16.13" +anyhow = "1.0.31" diff --git a/src/client.rs b/src/client.rs index e2ce55b..eaeb292 100644 --- a/src/client.rs +++ b/src/client.rs @@ -2,19 +2,19 @@ use crate::config::ClientConfig as Config; use crate::Result; use async_std::net::TcpStream; -use futures_util::io::AsyncReadExt; /// wraps the client pub async fn client(port: u16) -> Result<()> { - let config = Config::load()?; + let _config = Config::load()?; - let stream = TcpStream::connect(format!("127.0.0.1:{}", &port)).await?; - println!( - "connection established to: {}:{}", - stream.peer_addr()?.ip(), - port - ); - let (_read, mut write) = stream.split(); + let mut stream = TcpStream::connect(format!("127.0.0.1:{}", &port)).await?; + println!("connection established to: {}:{}", stream.peer_addr()?.ip(), port); + + /*let (_read, mut write) = stream.split();*/ + + let message = + ilmp::Message::new("Isabelle".to_string(), "new message protocol working".to_string()); + ilmp::write(&mut stream, message).await?; loop {} } diff --git a/src/lib.rs b/src/lib.rs index 9ae3249..34c9995 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,5 +8,4 @@ pub use client::client; pub use server::server; // lazy idiot error/result type -pub type Error = std::io::Error; -pub type Result = std::result::Result; +pub type Result = anyhow::Result; diff --git a/src/server.rs b/src/server.rs index 0b5d179..7c43762 100644 --- a/src/server.rs +++ b/src/server.rs @@ -6,6 +6,7 @@ use async_std::{ }; use futures::io::{ReadHalf, WriteHalf}; use futures_util::{io::AsyncReadExt, stream::StreamExt}; +use ilmp::Sendable; use lazy_static::lazy_static; use std::{collections::HashMap, sync::Mutex}; use uuid::Uuid; @@ -18,11 +19,7 @@ lazy_static! { /// wraps the server pub async fn server(port: u16) -> Result<()> { let listener = TcpListener::bind(format!("127.0.0.1:{}", &port)).await?; - println!( - "online as server at: {}:{}", - listener.local_addr()?.ip(), - port - ); + println!("online as server at: {}:{}", listener.local_addr()?.ip(), port); let mut incoming = listener.incoming(); while let Some(stream) = incoming.next().await { @@ -34,26 +31,28 @@ pub async fn server(port: u16) -> Result<()> { let (read, write) = stream.split(); let stream_id = Uuid::new_v4(); - WRITE_STREAMS - .lock() - .expect("could not aqcuire lock") - .insert(stream_id.clone(), write); + WRITE_STREAMS.lock().expect("could not aqcuire lock").insert(stream_id.clone(), write); task::spawn(handle_stream(read, stream_id)); } Ok(()) } -async fn handle_stream(mut stream: ReadHalf, stream_id: Uuid) -> Result<()> { - loop {} - println!("disconnecting"); +async fn handle_stream(mut stream: ReadHalf, _stream_id: Uuid) -> Result<()> { + loop { + let packet = ilmp::read(&mut stream).await?; + if let Some(packet) = packet { + let res = match packet.kind { + ilmp::PacketKind::Message => ilmp::Message::from_packet(packet), + }; + println!("{:?}", res); + } + } + /* println!("disconnecting"); - WRITE_STREAMS - .lock() - .expect("failed to aqcuire lock") - .remove(&stream_id); + WRITE_STREAMS.lock().expect("failed to aqcuire lock").remove(&stream_id); - Ok(()) + Ok(())*/ } /*async fn relay_packet(packet: T) -> Result<()> {