scroll.server/crates/titan/src/message.rs
WiseDev d25a6de423 move all crates into crates/, glob members
pure move, no code touched. readme and protocol.md paths fixed up.
2026-08-23 08:15:45 +03:00

68 lines
2 KiB
Rust

use std::any::Any;
use std::fmt::Debug;
use crate::error::Result;
use crate::io::{ByteStreamReader, ByteStreamWriter};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Direction {
ClientToServer,
ServerToClient,
Bidirectional,
}
impl Direction {
pub const fn accepts_from_client(self) -> bool {
matches!(self, Direction::ClientToServer | Direction::Bidirectional)
}
pub const fn accepts_from_server(self) -> bool {
matches!(self, Direction::ServerToClient | Direction::Bidirectional)
}
}
pub trait Payload: Sized {
fn encode(&self, writer: &mut ByteStreamWriter) -> Result<()>;
fn decode(reader: &mut ByteStreamReader<'_>) -> Result<Self>;
fn to_bytes(&self) -> Result<Vec<u8>> {
let mut writer = ByteStreamWriter::with_capacity(64);
self.encode(&mut writer)?;
Ok(writer.into_inner())
}
fn from_bytes(bytes: &[u8]) -> Result<Self> {
let mut reader = ByteStreamReader::new(bytes);
Self::decode(&mut reader)
}
}
pub trait MessageMeta: Payload {
const MESSAGE_TYPE: u16;
const MESSAGE_VERSION: u16;
const DIRECTION: Direction;
const NAME: &'static str;
}
pub trait Message: Debug + Send + Sync + 'static {
fn message_type(&self) -> u16;
fn message_version(&self) -> u16;
fn direction(&self) -> Direction;
fn name(&self) -> &'static str;
fn encode_payload(&self, writer: &mut ByteStreamWriter) -> Result<()>;
fn as_any(&self) -> &dyn Any;
}
impl<T> Message for T
where
T: MessageMeta + Debug + Send + Sync + 'static,
{
fn message_type(&self) -> u16 {
T::MESSAGE_TYPE
}
fn message_version(&self) -> u16 {
T::MESSAGE_VERSION
}
fn direction(&self) -> Direction {
T::DIRECTION
}
fn name(&self) -> &'static str {
T::NAME
}
fn encode_payload(&self, writer: &mut ByteStreamWriter) -> Result<()> {
Payload::encode(self, writer)
}
fn as_any(&self) -> &dyn Any {
self
}
}