This commit is contained in:
2026-01-26 13:53:51 -05:00
parent e1eff49be9
commit 53ed4775ae
19 changed files with 565 additions and 350 deletions

67
src/net/data.rs Normal file
View File

@@ -0,0 +1,67 @@
use rand::TryRngCore;
#[repr(u32)]
#[derive(Debug, bitcode::Encode, bitcode::Decode)]
pub enum ClientMsgInst {
CreateAccountV0(CreateAccountV0) = 0,
RequestMsgsV0 = 1,
SendMsgV0(SendMsgV0) = 2,
}
#[repr(u32)]
#[derive(Debug, bitcode::Encode, bitcode::Decode)]
pub enum ServerMsgInst {
AccountCreatedV0(AccountCreatedV0) = 0,
LoadMsgV0(LoadMsgV0) = 1,
LoadMsgsV0(Vec<LoadMsgV0>) = 2,
ServerErrorV0(ServerErrorV0) = 3,
}
#[derive(Debug, bitcode::Encode, bitcode::Decode)]
pub struct CreateAccountV0 {
pub username: String,
pub password: String,
pub login_key: LoginKeyV0,
}
#[derive(Debug, bitcode::Encode, bitcode::Decode)]
pub struct AccountCreatedV0 {}
#[derive(Debug, bitcode::Encode, bitcode::Decode)]
pub struct LoginKeyV0(Vec<u8>);
impl LoginKeyV0 {
pub const BIT_LEN: usize = 1024;
pub const BYTE_LEN: usize = Self::BIT_LEN / 8;
pub fn new() -> Self {
let mut key = [0u8; Self::BYTE_LEN];
rand::rngs::OsRng
.try_fill_bytes(&mut key)
.expect("failed to generate random key");
Self(key.to_vec())
}
}
impl From<Vec<u8>> for LoginKeyV0 {
fn from(value: Vec<u8>) -> Self {
Self(value)
}
}
#[derive(Debug, Clone, bitcode::Encode, bitcode::Decode)]
pub struct SendMsgV0 {
pub content: String,
}
#[derive(Debug, Clone, bitcode::Encode, bitcode::Decode)]
pub struct LoadMsgV0 {
pub content: String,
pub user: String,
}
#[derive(Debug, bitcode::Encode, bitcode::Decode)]
pub enum ServerErrorV0 {
NotLoggedIn,
UnknownUsername,
InvalidPassword,
UsernameTaken,
}