Files
tuliprox/shared/src/model/web_socket.rs
T

115 lines
3.0 KiB
Rust
Raw Normal View History

2025-07-21 22:17:57 +02:00
use std::io;
use bytes::Bytes;
2025-08-03 10:24:23 +02:00
use crate::model::{ConfigType, PlaylistUpdateState, StatusCheck};
2025-07-21 22:17:57 +02:00
use serde::{Deserialize, Serialize};
pub const PROTOCOL_VERSION: u8 = 1;
2025-07-29 18:19:13 +02:00
#[derive(Default, PartialOrd, PartialEq, Debug, Clone)]
2025-07-29 18:14:43 +02:00
pub enum UserRole {
#[default]
Unauthorized,
Admin,
User,
2025-07-29 10:44:55 +02:00
}
2025-07-29 18:14:43 +02:00
impl UserRole {
pub fn is_admin(&self) -> bool {
self.eq(&UserRole::Admin)
2025-07-29 10:44:55 +02:00
}
2025-07-29 18:14:43 +02:00
pub fn is_user(&self) -> bool {
self.eq(&UserRole::User)
}
}
#[derive(Default)]
pub struct ProtocolHandlerMemory {
pub token: Option<String>,
pub role: UserRole,
2025-07-29 10:44:55 +02:00
}
2025-07-21 22:17:57 +02:00
pub enum ProtocolHandler {
Version(u8),
2025-07-29 10:44:55 +02:00
Default(ProtocolHandlerMemory),
2025-07-21 22:17:57 +02:00
}
pub enum WsCloseCode {
// Normal,
// Away,
Protocol,
// Unsupported,
// Abnormal,
// Invalid,
// Policy,
// Size,
// Extension,
// Error,
// Restart,
// Again,
// Tls,
}
impl WsCloseCode {
pub fn code(&self) -> u16 {
match self {
// WsCloseCode::Normal => 1000,
// WsCloseCode::Away => 1001,
WsCloseCode::Protocol => 1002,
// WsCloseCode::Unsupported => 1003,
// WsCloseCode::Abnormal => 1006,
// WsCloseCode::Invalid => 1007,
// WsCloseCode::Policy => 1008,
// WsCloseCode::Size => 1009,
// WsCloseCode::Extension => 1010,
// WsCloseCode::Error => 1011,
// WsCloseCode::Restart => 1012,
// WsCloseCode::Again => 1013,
// WsCloseCode::Tls => 1015,
}
}
}
#[derive(Serialize, Deserialize, Debug)]
pub enum ProtocolMessage {
2025-07-23 12:49:43 +02:00
Unauthorized,
Error(String),
2025-07-21 22:17:57 +02:00
Version(u8),
2025-07-29 10:44:55 +02:00
Auth(String),
Authorized,
2025-08-06 11:28:01 +02:00
ServerError(String),
2025-07-21 22:17:57 +02:00
StatusRequest(String),
StatusResponse(StatusCheck),
ActiveUserResponse(usize, usize), // user_count, connection count
2025-08-01 21:03:50 +02:00
ActiveProviderResponse(String, usize),
2025-08-03 10:24:23 +02:00
ConfigChangeResponse(ConfigType),
PlaylistUpdateResponse(PlaylistUpdateState),
PlaylistUpdateProgressResponse(String, String),
2025-07-21 22:17:57 +02:00
}
impl ProtocolMessage {
pub fn to_bytes(&self) -> io::Result<Bytes> {
match self {
ProtocolMessage::Version(version) => {
Ok(Bytes::from(vec![*version]))
}
_ => {
2025-07-23 12:49:43 +02:00
//let encoded = bincode_serialize(self)?;
let json = serde_json::to_string(self)
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
Ok(Bytes::from(json.into_bytes()))
2025-07-21 22:17:57 +02:00
}
}
}
pub fn from_bytes(bytes: Bytes) -> io::Result<Self> {
if bytes.len() == 1 {
Ok(ProtocolMessage::Version(bytes[0]))
} else {
2025-07-23 12:49:43 +02:00
//bincode_deserialize::<ProtocolMessage>(bytes.as_ref())
let s = std::str::from_utf8(&bytes)
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
serde_json::from_str(s)
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))
2025-07-21 22:17:57 +02:00
}
}
}