50 lines
1.3 KiB
Rust
50 lines
1.3 KiB
Rust
// file: crates/common/game-realtime-transport-lib/src/message.rs
|
|
// version: 1
|
|
|
|
/// Owned opaque binary payload carried by a realtime transport.
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
pub struct TransportMessage {
|
|
bytes: Vec<u8>,
|
|
}
|
|
|
|
impl TransportMessage {
|
|
/// Creates a transport message from owned bytes.
|
|
#[must_use]
|
|
pub fn new(bytes: Vec<u8>) -> Self {
|
|
return Self { bytes };
|
|
}
|
|
|
|
/// Borrows the opaque payload bytes.
|
|
#[must_use]
|
|
pub fn as_bytes(&self) -> &[u8] {
|
|
return self.bytes.as_slice();
|
|
}
|
|
|
|
/// Consumes the message and returns its owned payload bytes.
|
|
#[must_use]
|
|
pub fn into_bytes(self) -> Vec<u8> {
|
|
return self.bytes;
|
|
}
|
|
|
|
/// Returns whether the payload is empty.
|
|
#[must_use]
|
|
pub fn is_empty(&self) -> bool {
|
|
return self.bytes.is_empty();
|
|
}
|
|
|
|
/// Returns the payload length in bytes.
|
|
#[must_use]
|
|
pub fn len(&self) -> usize {
|
|
return self.bytes.len();
|
|
}
|
|
}
|
|
|
|
/// Outcome of one successful transport receive operation.
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
pub enum TransportReceive {
|
|
/// One opaque binary message was received.
|
|
Message(crate::TransportMessage),
|
|
/// The remote peer completed a clean transport-level close.
|
|
Closed,
|
|
}
|