@@ -19,8 +19,9 @@ but I'm lazy and it seems to work so gonna roll with it lol | |||||
packet kind has defined values for packets but leaves many open for user defined packets to be added to the protocol | packet kind has defined values for packets but leaves many open for user defined packets to be added to the protocol | ||||
| byte | packet kind | | | byte | packet kind | | ||||
|--------|-------------------------------------------------------| | |||||
| ------ | ----------------------------------------------------- | | |||||
| `0x00` | message - a simple text packet | | | `0x00` | message - a simple text packet | | ||||
| `0xfe` | join - announces a new connection | | |||||
| `0xff` | agreement - used to help generate an aggreed upon key | | | `0xff` | agreement - used to help generate an aggreed upon key | | ||||
### encrypt flag | ### encrypt flag | ||||
@@ -0,0 +1,43 @@ | |||||
use crate::{Packet, Result}; | |||||
use chrono::prelude::*; | |||||
use serde::{Deserialize, Serialize}; | |||||
use uuid::Uuid; | |||||
/// packet for when a user connects to the server | |||||
#[derive(Debug, Clone, Serialize, Deserialize)] | |||||
pub struct Join { | |||||
pub timestamp: i64, | |||||
pub message_id: u128, | |||||
pub username: String, | |||||
} | |||||
impl Join { | |||||
pub fn new(username: String) -> Join { | |||||
let timestamp = Utc::now().timestamp(); | |||||
let message_id = Uuid::new_v4().as_u128(); | |||||
Join { | |||||
timestamp, | |||||
message_id, | |||||
username, | |||||
} | |||||
} | |||||
} | |||||
impl crate::Sendable for Join { | |||||
fn to_packet(&self, encrypt_flag: crate::EncryptFlag) -> Result<Packet> { | |||||
let contents: Vec<u8> = serde_json::to_string(&self)?.into_bytes(); | |||||
let kind = 0xfe; | |||||
Ok(Packet::new(kind, contents, encrypt_flag)) | |||||
} | |||||
fn from_packet(packet: Packet) -> Result<Self> { | |||||
let contents = &String::from_utf8(packet.contents)?; | |||||
let join: Join = serde_json::from_str(contents)?; | |||||
Ok(join) | |||||
} | |||||
fn packet_kind(&self) -> u8 { | |||||
0xfe | |||||
} | |||||
} |
@@ -0,0 +1,43 @@ | |||||
use crate::{Packet, Result}; | |||||
use chrono::prelude::*; | |||||
use serde::{Deserialize, Serialize}; | |||||
use uuid::Uuid; | |||||
/// packet for when a user connects to the server | |||||
#[derive(Debug, Clone, Serialize, Deserialize)] | |||||
pub struct Leave { | |||||
pub timestamp: i64, | |||||
pub message_id: u128, | |||||
pub username: String, | |||||
} | |||||
impl Leave { | |||||
pub fn new(username: String) -> Leave { | |||||
let timestamp = Utc::now().timestamp(); | |||||
let message_id = Uuid::new_v4().as_u128(); | |||||
Leave { | |||||
timestamp, | |||||
message_id, | |||||
username, | |||||
} | |||||
} | |||||
} | |||||
impl crate::Sendable for Leave { | |||||
fn to_packet(&self, encrypt_flag: crate::EncryptFlag) -> Result<Packet> { | |||||
let contents: Vec<u8> = serde_json::to_string(&self)?.into_bytes(); | |||||
let kind = 0xfd; | |||||
Ok(Packet::new(kind, contents, encrypt_flag)) | |||||
} | |||||
fn from_packet(packet: Packet) -> Result<Self> { | |||||
let contents = &String::from_utf8(packet.contents)?; | |||||
let leave: Leave = serde_json::from_str(contents)?; | |||||
Ok(leave) | |||||
} | |||||
fn packet_kind(&self) -> u8 { | |||||
0xfd | |||||
} | |||||
} |
@@ -20,6 +20,10 @@ mod message; | |||||
pub use message::Message; | pub use message::Message; | ||||
mod agreement; | mod agreement; | ||||
pub use agreement::Agreement; | pub use agreement::Agreement; | ||||
mod join; | |||||
pub use join::Join; | |||||
mod leave; | |||||
pub use leave::Leave; | |||||
/// encryption types and functions | /// encryption types and functions | ||||
pub mod encrypt; | pub mod encrypt; | ||||