瀏覽代碼

join / leave pogchamp

master
Isabelle L. 5 年之前
父節點
當前提交
36898f4890
共有 4 個檔案被更改,包括 92 行新增1 行删除
  1. +2
    -1
      README.md
  2. +43
    -0
      src/join.rs
  3. +43
    -0
      src/leave.rs
  4. +4
    -0
      src/lib.rs

+ 2
- 1
README.md 查看文件

@@ -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

| byte | packet kind |
|--------|-------------------------------------------------------|
| ------ | ----------------------------------------------------- |
| `0x00` | message - a simple text packet |
| `0xfe` | join - announces a new connection |
| `0xff` | agreement - used to help generate an aggreed upon key |

### encrypt flag


+ 43
- 0
src/join.rs 查看文件

@@ -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
}
}

+ 43
- 0
src/leave.rs 查看文件

@@ -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
}
}

+ 4
- 0
src/lib.rs 查看文件

@@ -20,6 +20,10 @@ mod message;
pub use message::Message;
mod agreement;
pub use agreement::Agreement;
mod join;
pub use join::Join;
mod leave;
pub use leave::Leave;
/// encryption types and functions
pub mod encrypt;



Loading…
取消
儲存