| @@ -72,6 +72,12 @@ version = "1.0.0" | |||||
| source = "registry+https://github.com/rust-lang/crates.io-index" | source = "registry+https://github.com/rust-lang/crates.io-index" | ||||
| checksum = "f8aac770f1885fd7e387acedd76065302551364496e46b3dd00860b2f8359b9d" | checksum = "f8aac770f1885fd7e387acedd76065302551364496e46b3dd00860b2f8359b9d" | ||||
| [[package]] | |||||
| name = "base64" | |||||
| version = "0.12.1" | |||||
| source = "registry+https://github.com/rust-lang/crates.io-index" | |||||
| checksum = "53d1ccbaf7d9ec9537465a97bf19edc1a4e158ecb49fc16178202238c569cc42" | |||||
| [[package]] | [[package]] | ||||
| name = "bitflags" | name = "bitflags" | ||||
| version = "1.2.1" | version = "1.2.1" | ||||
| @@ -430,6 +436,7 @@ dependencies = [ | |||||
| "futures", | "futures", | ||||
| "futures-util", | "futures-util", | ||||
| "lazy_static", | "lazy_static", | ||||
| "orion", | |||||
| "serde", | "serde", | ||||
| "serde_json", | "serde_json", | ||||
| "structopt", | "structopt", | ||||
| @@ -483,6 +490,18 @@ version = "1.3.1" | |||||
| source = "registry+https://github.com/rust-lang/crates.io-index" | source = "registry+https://github.com/rust-lang/crates.io-index" | ||||
| checksum = "b1c601810575c99596d4afc46f78a678c80105117c379eb3650cf99b8a21ce5b" | checksum = "b1c601810575c99596d4afc46f78a678c80105117c379eb3650cf99b8a21ce5b" | ||||
| [[package]] | |||||
| name = "orion" | |||||
| version = "0.15.1" | |||||
| source = "registry+https://github.com/rust-lang/crates.io-index" | |||||
| checksum = "6e2dd0d645e94ec75aacc27460cb68438263342f4e4e1aeaf7af67847687e7a8" | |||||
| dependencies = [ | |||||
| "base64", | |||||
| "getrandom", | |||||
| "subtle", | |||||
| "zeroize", | |||||
| ] | |||||
| [[package]] | [[package]] | ||||
| name = "pin-project" | name = "pin-project" | ||||
| version = "0.4.15" | version = "0.4.15" | ||||
| @@ -697,6 +716,12 @@ dependencies = [ | |||||
| "syn", | "syn", | ||||
| ] | ] | ||||
| [[package]] | |||||
| name = "subtle" | |||||
| version = "2.2.2" | |||||
| source = "registry+https://github.com/rust-lang/crates.io-index" | |||||
| checksum = "7c65d530b10ccaeac294f349038a597e435b18fb456aadd0840a623f83b9e941" | |||||
| [[package]] | [[package]] | ||||
| name = "syn" | name = "syn" | ||||
| version = "1.0.19" | version = "1.0.19" | ||||
| @@ -835,3 +860,9 @@ dependencies = [ | |||||
| "winapi 0.2.8", | "winapi 0.2.8", | ||||
| "winapi-build", | "winapi-build", | ||||
| ] | ] | ||||
| [[package]] | |||||
| name = "zeroize" | |||||
| version = "1.1.0" | |||||
| source = "registry+https://github.com/rust-lang/crates.io-index" | |||||
| checksum = "3cbac2ed2ba24cc90f5e06485ac8c7c1e5449fe8911aef4d8877218af021a5b8" | |||||
| @@ -15,3 +15,4 @@ structopt = "0.3.14" | |||||
| chrono = "0.4.11" | chrono = "0.4.11" | ||||
| futures = "0.3.5" | futures = "0.3.5" | ||||
| toml = "0.5.6" | toml = "0.5.6" | ||||
| orion = "0.15.1" | |||||
| @@ -1,7 +1,6 @@ | |||||
| // modules | // modules | ||||
| mod client; | mod client; | ||||
| mod config; | mod config; | ||||
| #[allow(dead_code)] | |||||
| mod packet; | mod packet; | ||||
| mod server; | mod server; | ||||
| @@ -3,7 +3,7 @@ use crate::Result; | |||||
| use chrono::prelude::*; | use chrono::prelude::*; | ||||
| use serde::{Deserialize, Serialize}; | use serde::{Deserialize, Serialize}; | ||||
| #[derive(Deserialize, Serialize, Debug)] | |||||
| #[derive(Deserialize, Serialize, Debug, Clone)] | |||||
| pub struct Join { | pub struct Join { | ||||
| user: String, | user: String, | ||||
| timestamp: i64, | timestamp: i64, | ||||
| @@ -5,7 +5,7 @@ use chrono::prelude::*; | |||||
| use serde::{Deserialize, Serialize}; | use serde::{Deserialize, Serialize}; | ||||
| /// a Message | /// a Message | ||||
| #[derive(Deserialize, Serialize, Debug)] | |||||
| #[derive(Deserialize, Serialize, Debug, Clone)] | |||||
| pub struct Message { | pub struct Message { | ||||
| user: String, | user: String, | ||||
| contents: String, | contents: String, | ||||
| @@ -8,8 +8,7 @@ use async_std::{ | |||||
| task, | task, | ||||
| }; | }; | ||||
| use futures::io::{ReadHalf, WriteHalf}; | use futures::io::{ReadHalf, WriteHalf}; | ||||
| use futures_util::io::AsyncReadExt; | |||||
| use futures_util::stream::StreamExt; | |||||
| use futures_util::{io::AsyncReadExt, stream::StreamExt}; | |||||
| use lazy_static::lazy_static; | use lazy_static::lazy_static; | ||||
| use std::{collections::HashMap, sync::Mutex}; | use std::{collections::HashMap, sync::Mutex}; | ||||
| use uuid::Uuid; | use uuid::Uuid; | ||||
| @@ -62,10 +61,12 @@ async fn handle_stream(mut stream: ReadHalf<TcpStream>, stream_id: Uuid) -> Resu | |||||
| PacketType::Message => { | PacketType::Message => { | ||||
| let msg = Message::from_packet(packet)?; | let msg = Message::from_packet(packet)?; | ||||
| println!("{:?}", msg); | println!("{:?}", msg); | ||||
| task::spawn(relay_packet(msg)); | |||||
| } | } | ||||
| PacketType::Join => { | PacketType::Join => { | ||||
| let join = Join::from_packet(packet)?; | let join = Join::from_packet(packet)?; | ||||
| println!("{:?}", join); | println!("{:?}", join); | ||||
| task::spawn(relay_packet(join)); | |||||
| } | } | ||||
| } | } | ||||
| @@ -83,12 +84,10 @@ async fn relay_packet<T: Clone + Sendable>(packet: T) -> Result<()> { | |||||
| let stream = futures::stream::iter(locked_write_streams.iter_mut()); | let stream = futures::stream::iter(locked_write_streams.iter_mut()); | ||||
| let packet = &packet; | let packet = &packet; | ||||
| stream | |||||
| .for_each_concurrent(None, |(_, mut stream)| async move { | |||||
| let packet = packet.clone().to_packet().expect("failed to convert to packet"); | |||||
| // in case any of the writes fail just ignore them | |||||
| let _ = packet.write(&mut stream); | |||||
| }) | |||||
| .await; | |||||
| stream.for_each_concurrent(None, |(_, mut stream)| async move { | |||||
| let packet = packet.clone().to_packet().expect("failed to convert to packet"); | |||||
| // in case any of the writes fail just ignore them | |||||
| let _ = packet.write(&mut stream); | |||||
| }); | |||||
| Ok(()) | Ok(()) | ||||
| } | } | ||||