From e2587ee259cf915c951cba46cb19574cecd94d3e Mon Sep 17 00:00:00 2001 From: Isabelle L Date: Mon, 11 May 2020 21:15:25 -0500 Subject: [PATCH] holy fuck that's a lot of code that she changed. god damn she should have made smaller commits --- Cargo.toml | 8 ++++---- client_config.toml | 2 +- justfile | 4 ++++ src/client.rs | 17 ++++++++++++----- src/config.rs | 2 +- src/packet.rs | 13 ++++++++----- src/packet/join.rs | 28 +++++++++++----------------- src/packet/message.rs | 22 ++++++++-------------- src/server.rs | 27 ++++++++++++++++++--------- 9 files changed, 67 insertions(+), 56 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a6d380b..77e6568 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,13 +5,13 @@ authors = ["Isabelle L. "] edition = "2018" [dependencies] -structopt = "0.3.14" async-std = { version = "1.5.0",features = ["attributes"] } -chrono = "0.4.11" serde = { version = "1.0.107", features = ["derive"] } -serde_json = "1.0.52" uuid = { version = "0.8.1", features = ["v4"] } +futures-util = "0.3.5" +serde_json = "1.0.52" lazy_static = "1.4.0" +structopt = "0.3.14" +chrono = "0.4.11" futures = "0.3.5" -futures-util = "0.3.5" toml = "0.5.6" diff --git a/client_config.toml b/client_config.toml index d052b8c..8df1e12 100644 --- a/client_config.toml +++ b/client_config.toml @@ -1 +1 @@ -username="Isabelle" \ No newline at end of file +user="Isabelle" \ No newline at end of file diff --git a/justfile b/justfile index 167c91d..daa92d3 100644 --- a/justfile +++ b/justfile @@ -1,3 +1,7 @@ +alias rs := run-server +alias rc := run-client +alias b := build + build: cargo build run-server: diff --git a/src/client.rs b/src/client.rs index 702aa33..81a0a05 100644 --- a/src/client.rs +++ b/src/client.rs @@ -1,18 +1,25 @@ // namespacing -use crate::packet::{Message, Packet}; +use crate::config::ClientConfig as Config; +use crate::packet::{Join, Message, Packet, Sendable}; use crate::Result; use async_std::net::TcpStream; -use std::convert::TryInto; +use futures_util::io::AsyncReadExt; /// wraps the client pub async fn client(port: u16) -> Result<()> { - let mut stream = TcpStream::connect(format!("127.0.0.1:{}", &port)).await?; + 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 join: Packet = Join::new(config.user).to_packet()?; + join.write(&mut write).await?; // testing stuffs let message: Packet = - Message::new("Isabelle".to_owned(), "Hello Server".to_owned()).try_into()?; - message.write(&mut stream).await?; + Message::new("Isabelle".to_owned(), "Hello Server".to_owned()).to_packet()?; + message.write(&mut write).await?; loop {} } diff --git a/src/config.rs b/src/config.rs index c58fa16..c1a9627 100644 --- a/src/config.rs +++ b/src/config.rs @@ -3,7 +3,7 @@ use serde::Deserialize; #[derive(Deserialize)] pub struct ClientConfig { - user: String, + pub user: String, } impl ClientConfig { diff --git a/src/packet.rs b/src/packet.rs index bcfb9bd..6b554d9 100644 --- a/src/packet.rs +++ b/src/packet.rs @@ -2,7 +2,7 @@ use crate::Result; use async_std::net::TcpStream; use async_std::prelude::*; -use futures_util::io::ReadHalf; +use futures_util::io::{ReadHalf, WriteHalf}; use std::convert::TryInto; mod join; @@ -29,12 +29,13 @@ impl std::convert::Into for Packet { } } -pub trait Sendable { - fn to_packet(self) -> Packet; - fn from_packet(packet: Packet) -> Self; +pub trait Sendable: Sized { + fn to_packet(self) -> Result; + fn from_packet(packet: Packet) -> Result; } /// contains data to be turned into a network packet or into a more specific packet +#[derive(Debug, Clone)] pub struct Packet { pub packet_type: PacketType, packet_contents: Vec, @@ -65,7 +66,7 @@ impl Packet { } /// write a packet to the tcpstream - pub async fn write(self, stream: &mut TcpStream) -> Result<()> { + pub async fn write(self, stream: &mut WriteHalf) -> Result<()> { let network_packet: NetworkPacket = self.into(); stream.write(&network_packet.0).await?; Ok(()) @@ -73,6 +74,7 @@ impl Packet { } /// represent the specific packet type +#[derive(Debug, Clone)] #[repr(u8)] pub enum PacketType { Message = 0, @@ -84,6 +86,7 @@ impl PacketType { pub fn from_u8(packet_type: u8) -> Option { match packet_type { 0 => Some(Self::Message), + 1 => Some(Self::Join), _ => None, } } diff --git a/src/packet/join.rs b/src/packet/join.rs index 545fa69..0f82a9f 100644 --- a/src/packet/join.rs +++ b/src/packet/join.rs @@ -1,5 +1,5 @@ use crate::packet::{Packet, PacketType}; -use crate::{Error, Result}; +use crate::Result; use chrono::prelude::*; use serde::{Deserialize, Serialize}; @@ -10,29 +10,23 @@ pub struct Join { } impl Join { - fn new(user: String) -> Self { + pub fn new(user: String) -> Self { let timestamp = Utc::now().timestamp(); Self { user, timestamp } } } -impl std::convert::TryFrom for Join { - type Error = Error; - - fn try_from(packet: Packet) -> Result { - let packet_contents = - &String::from_utf8(packet.packet_contents).expect("could not decode as utf8"); - let message: Join = serde_json::from_str(packet_contents)?; - Ok(message) - } -} - -impl std::convert::TryInto for Join { - type Error = Error; - - fn try_into(self) -> Result { +impl crate::packet::Sendable for Join { + fn to_packet(self) -> Result { let packet_contents: Vec = serde_json::to_string(&self)?.into_bytes(); let packet_type = PacketType::Join; Ok(Packet { packet_type, packet_contents }) } + + fn from_packet(packet: Packet) -> Result { + let packet_contents = + &String::from_utf8(packet.packet_contents).expect("could not decode as utf8"); + let join: Join = serde_json::from_str(packet_contents)?; + Ok(join) + } } diff --git a/src/packet/message.rs b/src/packet/message.rs index 8d23b80..b30f6eb 100644 --- a/src/packet/message.rs +++ b/src/packet/message.rs @@ -1,6 +1,6 @@ // namespacing use crate::packet::{Packet, PacketType}; -use crate::{Error, Result}; +use crate::Result; use chrono::prelude::*; use serde::{Deserialize, Serialize}; @@ -20,23 +20,17 @@ impl Message { } } -impl std::convert::TryFrom for Message { - type Error = Error; +impl crate::packet::Sendable for Message { + fn to_packet(self) -> Result { + let packet_contents: Vec = serde_json::to_string(&self)?.into_bytes(); + let packet_type = PacketType::Message; + Ok(Packet { packet_type, packet_contents }) + } - fn try_from(packet: Packet) -> Result { + fn from_packet(packet: Packet) -> Result { let packet_contents = &String::from_utf8(packet.packet_contents).expect("could not decode as utf8"); let message: Message = serde_json::from_str(packet_contents)?; Ok(message) } } - -impl std::convert::TryInto for Message { - type Error = Error; - - fn try_into(self) -> Result { - let packet_contents: Vec = serde_json::to_string(&self)?.into_bytes(); - let packet_type = PacketType::Message; - Ok(Packet { packet_type, packet_contents }) - } -} diff --git a/src/server.rs b/src/server.rs index 7427ac3..400beea 100644 --- a/src/server.rs +++ b/src/server.rs @@ -1,17 +1,17 @@ // namespacing use crate::{ - packet::{Join, Message, Packet, PacketType}, + packet::{Join, Message, Packet, PacketType, Sendable}, Result, }; use async_std::{ net::{TcpListener, TcpStream}, - prelude::*, task, }; use futures::io::{ReadHalf, WriteHalf}; use futures_util::io::AsyncReadExt; +use futures_util::stream::StreamExt; use lazy_static::lazy_static; -use std::{collections::HashMap, convert::TryFrom, sync::Mutex}; +use std::{collections::HashMap, sync::Mutex}; use uuid::Uuid; lazy_static! { @@ -60,11 +60,11 @@ async fn handle_stream(mut stream: ReadHalf, stream_id: Uuid) -> Resu match packet.packet_type { PacketType::Message => { - let msg = Message::try_from(packet)?; + let msg = Message::from_packet(packet)?; println!("{:?}", msg); } PacketType::Join => { - let join = Join::try_from(packet)?; + let join = Join::from_packet(packet)?; println!("{:?}", join); } } @@ -78,8 +78,17 @@ async fn handle_stream(mut stream: ReadHalf, stream_id: Uuid) -> Resu Ok(()) } -/* -async fn relay_packet() -> Result<()> { - let locked_stream = WRITE_STREAMS.lock(). +async fn relay_packet(packet: T) -> Result<()> { + let mut locked_write_streams = WRITE_STREAMS.lock().expect("failed to aqcuire lock"); + let stream = futures::stream::iter(locked_write_streams.iter_mut()); -}*/ + 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; + Ok(()) +}