| @@ -2,11 +2,17 @@ use async_std::net::TcpListener; | |||||
| use async_std::net::TcpStream; | use async_std::net::TcpStream; | ||||
| use async_std::prelude::*; | use async_std::prelude::*; | ||||
| mod packet; | |||||
| pub type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>; | pub type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>; | ||||
| pub async fn server(port: u16) -> Result<()> { | pub async fn server(port: u16) -> Result<()> { | ||||
| let listener = TcpListener::bind(format!("127.0.0.1:{}", &port)).await?; | 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(); | let mut incoming = listener.incoming(); | ||||
| while let Some(stream) = incoming.next().await { | while let Some(stream) = incoming.next().await { | ||||
| @@ -21,9 +27,14 @@ pub async fn server(port: u16) -> Result<()> { | |||||
| pub async fn client(port: u16) -> Result<()> { | pub async fn client(port: u16) -> Result<()> { | ||||
| let mut stream = TcpStream::connect(format!("127.0.0.1:{}", &port)).await?; | let mut stream = TcpStream::connect(format!("127.0.0.1:{}", &port)).await?; | ||||
| println!("connection established to: {}:{}", stream.peer_addr()?.ip(), port); | |||||
| let mut buf = vec![0u8; 1024]; | |||||
| println!( | |||||
| "connection established to: {}:{}", | |||||
| stream.peer_addr()?.ip(), | |||||
| port | |||||
| ); | |||||
| /*let mut buf = vec![0u8; 1024]; | |||||
| stream.read(&mut buf).await?; | stream.read(&mut buf).await?; | ||||
| println!("{}", String::from_utf8_lossy(&mut buf)); | |||||
| println!("{}", String::from_utf8_lossy(&mut buf));*/ | |||||
| Ok(()) | Ok(()) | ||||
| } | } | ||||
| @@ -1,10 +1,34 @@ | |||||
| // namespacing | |||||
| use chrono::prelude::*; | use chrono::prelude::*; | ||||
| use serde::Serialize; | |||||
| struct NetworkPacket(String); | |||||
| /// structured [packet type byte][four bytes of packet length][contents of packet] | |||||
| struct NetworkPacket(Vec<u8>); | |||||
| impl std::convert::TryInto<NetworkPacket> for Packet { | |||||
| type Error = Box<dyn std::error::Error>; | |||||
| fn try_into(self) -> crate::Result<NetworkPacket> { | |||||
| let mut contents: Vec<u8> = Vec::new(); | |||||
| // packet type byte | |||||
| contents.push(self.packet_type as u8); | |||||
| // create room for the packet length | |||||
| (1..5).for_each(|_| contents.push(0x00)); | |||||
| // write the rest of the contents | |||||
| self.packet_contents | |||||
| .iter() | |||||
| .for_each(|byte| contents.push(*byte)); | |||||
| // write the packet len bytes | |||||
| let packet_length = ((self.packet_contents.len() + 5) as u32).to_le_bytes(); | |||||
| (1..5).for_each(|i| contents[i] = packet_length[i - 1]); | |||||
| Ok(NetworkPacket(contents)) | |||||
| } | |||||
| } | |||||
| struct Packet { | struct Packet { | ||||
| packet_type: PacketType, | packet_type: PacketType, | ||||
| packet_length: u64, | |||||
| packet_contents: Vec<u8>, | packet_contents: Vec<u8>, | ||||
| } | } | ||||
| @@ -13,6 +37,7 @@ enum PacketType { | |||||
| NewMessage = 0, | NewMessage = 0, | ||||
| } | } | ||||
| #[derive(Serialize)] | |||||
| struct NewMessage { | struct NewMessage { | ||||
| user: String, | user: String, | ||||
| contents: String, | contents: String, | ||||
| @@ -22,6 +47,23 @@ struct NewMessage { | |||||
| impl NewMessage { | impl NewMessage { | ||||
| pub fn new(user: String, contents: String) -> Self { | pub fn new(user: String, contents: String) -> Self { | ||||
| let timestamp = Utc::now().timestamp(); | let timestamp = Utc::now().timestamp(); | ||||
| Self { user, contents, timestamp } | |||||
| Self { | |||||
| user, | |||||
| contents, | |||||
| timestamp, | |||||
| } | |||||
| } | |||||
| } | |||||
| impl std::convert::TryInto<Packet> for NewMessage { | |||||
| type Error = Box<dyn std::error::Error>; | |||||
| fn try_into(self) -> crate::Result<Packet> { | |||||
| let packet_contents: Vec<u8> = serde_json::to_string(&self)?.into_bytes(); | |||||
| let packet_type = PacketType::NewMessage; | |||||
| Ok(Packet { | |||||
| packet_type, | |||||
| packet_contents, | |||||
| }) | |||||
| } | } | ||||
| } | } | ||||