2023-04-24 17:18:55 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct Message {
|
|
|
|
pub init: Option<Init>,
|
|
|
|
pub segment: Option<Segment>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
2023-05-05 02:43:43 +00:00
|
|
|
pub struct Init {}
|
2023-04-24 17:18:55 +00:00
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct Segment {
|
2023-05-05 02:43:43 +00:00
|
|
|
pub track_id: u32,
|
2023-04-24 17:18:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Message {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Message {
|
|
|
|
init: None,
|
|
|
|
segment: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn serialize(&self) -> anyhow::Result<Vec<u8>> {
|
|
|
|
let str = serde_json::to_string(self)?;
|
|
|
|
let bytes = str.as_bytes();
|
|
|
|
let size = bytes.len() + 8;
|
|
|
|
|
|
|
|
let mut out = Vec::with_capacity(size);
|
2023-04-24 18:45:46 +00:00
|
|
|
out.extend_from_slice(&(size as u32).to_be_bytes());
|
2023-04-24 17:18:55 +00:00
|
|
|
out.extend_from_slice(b"warp");
|
|
|
|
out.extend_from_slice(bytes);
|
|
|
|
|
|
|
|
Ok(out)
|
|
|
|
}
|
2023-05-02 18:05:21 +00:00
|
|
|
}
|