Explorar el Código

join / leave pogchamp

master
Isabelle L. hace 5 años
padre
commit
36898f4890
Se han modificado 4 ficheros con 92 adiciones y 1 borrados
  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 Ver fichero

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

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

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

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



Cargando…
Cancelar
Guardar