From 0af49f6a2e6fe7ddcb70edbc3cf951624c157fb1 Mon Sep 17 00:00:00 2001 From: Isabelle L Date: Wed, 13 May 2020 23:21:18 -0500 Subject: [PATCH] initial commit --- .gitignore | 2 ++ Cargo.toml | 14 ++++++++ LICENSE | 7 ++++ README.md | 2 ++ src/lib.rs | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/message.rs | 40 ++++++++++++++++++++++ 6 files changed, 156 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.toml create mode 100644 LICENSE create mode 100644 README.md create mode 100644 src/lib.rs create mode 100644 src/message.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..96ef6c0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +Cargo.lock diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..8d05511 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "ilmp" +version = "0.1.0" +authors = ["Isabelle L. "] +edition = "2018" + +[dependencies] +orion = "0.15.1" +serde = { version = "1.0.110", features = ["derive"]} +serde_json = "1.0.53" +futures = "0.3.5" +futures-util = "0.3.5" +uuid = { version = "0.8.1", features = ["v4"] } +chrono = "0.4.11" diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..8c86aec --- /dev/null +++ b/LICENSE @@ -0,0 +1,7 @@ +Copyright 2020 Isabelle L. + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CO \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..61018b8 --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# Isabelle's Lazy Message Protocol +idfk lol \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..2c96a11 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,91 @@ +#![allow(dead_code)] + +use futures_util::io::{AsyncReadExt, AsyncWriteExt}; +use std::convert::TryInto; +use std::marker::Unpin; + +mod message; +pub use message::Message; + +/// lazy error +pub type Error = Box; +/// lazy result +pub type Result = std::result::Result; + +struct NetworkPacket(Vec); + +pub trait Sendable: Sized { + fn to_packet(self) -> Result; + fn from_packet(packet: Packet) -> Result; +} + +pub struct Packet { + kind: PacketKind, + contents: Vec, +} + +impl Packet { + pub fn new(kind: PacketKind, contents: Vec) -> Packet { + Packet { kind, contents } + } + + fn to_network_packet(self) -> NetworkPacket { + let mut contents: Vec = Vec::new(); + + // write packet kind byte + contents.push(self.kind as u8); + // write the packet length + let contents_length = self.contents.len() as u32; + contents.extend_from_slice(&contents_length.to_le_bytes()); + // write contents + contents.extend_from_slice(&self.contents); + + NetworkPacket(contents) + } +} + +pub async fn read(stream: &mut S) -> Result> +where + S: AsyncReadExt + Unpin, +{ + let mut info_buf = [0u8; 5]; + let check = stream.read(&mut info_buf).await?; + if check == 0 { + return Ok(None); + } + + let packet_kind = PacketKind::from_u8(info_buf[0]).unwrap(); + let length = u32::from_le_bytes(info_buf[1..5].try_into().unwrap()) as usize; + + let mut contents: Vec = vec![0; length]; + stream.read(&mut contents).await?; + + let packet = Packet::new(packet_kind, contents); + + Ok(Some(packet)) +} + +pub async fn write(stream: &mut S, packet: P) -> Result<()> +where + S: AsyncWriteExt + Unpin, + P: Sendable, +{ + let network_packet = packet.to_packet()?.to_network_packet(); + stream.write(&network_packet.0).await?; + Ok(()) +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[repr(u8)] +pub enum PacketKind { + Message = 0, +} + +impl PacketKind { + pub fn from_u8(kind: u8) -> Option { + match kind { + 0 => Some(PacketKind::Message), + _ => None, + } + } +} diff --git a/src/message.rs b/src/message.rs new file mode 100644 index 0000000..c8bb46c --- /dev/null +++ b/src/message.rs @@ -0,0 +1,40 @@ +use crate::{Packet, PacketKind, Result}; +use chrono::prelude::*; +use serde::{Deserialize, Serialize}; +use uuid::Uuid; + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct Message { + pub timestamp: i64, + pub message_id: u128, + pub username: String, + pub contents: String, +} + +impl Message { + pub fn new(username: String, contents: String) -> Message { + let timestamp = Utc::now().timestamp(); + let message_id = Uuid::new_v4().as_u128(); + + Message { + username, + message_id, + timestamp, + contents, + } + } +} + +impl crate::Sendable for Message { + fn to_packet(self) -> Result { + let contents: Vec = serde_json::to_string(&self)?.into_bytes(); + let kind = PacketKind::Message; + + Ok(Packet { kind, contents }) + } + fn from_packet(packet: Packet) -> Result { + let contents = &String::from_utf8(packet.contents)?; + let message: Message = serde_json::from_str(contents)?; + Ok(message) + } +}