feat: ✨ new protocol and server implementation
This commit is contained in:
parent
d6e5389743
commit
51c29f7921
11 changed files with 445 additions and 198 deletions
|
@ -4,5 +4,7 @@ version = "0.1.0"
|
|||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
serde = { workspace = true }
|
||||
# serde = { workspace = true }
|
||||
# serde_json = { workspace = true }
|
||||
chrono = { workspace = true }
|
||||
bincode = { workspace = true }
|
||||
|
|
|
@ -5,9 +5,9 @@ use std::{
|
|||
io::Read,
|
||||
};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
pub mod protocol;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum Error {
|
||||
ConnectionSetup(String),
|
||||
Connection(String),
|
||||
|
@ -17,6 +17,9 @@ pub enum Error {
|
|||
Record(String),
|
||||
Play(String),
|
||||
NotSupposeTo(String),
|
||||
Serialization(String),
|
||||
Deserialization(String),
|
||||
UnexpectedSignalType(String),
|
||||
}
|
||||
|
||||
impl std::error::Error for Error {
|
||||
|
@ -36,16 +39,20 @@ impl Display for Error {
|
|||
Error::Record(inner) => write!(f, "Record | {}", inner),
|
||||
Error::Play(inner) => write!(f, "Play | {}", inner),
|
||||
Error::NotSupposeTo(inner) => write!(f, "Not Suppose To | {}", inner),
|
||||
Error::Serialization(inner) => write!(f, "Serialization | {}", inner),
|
||||
Error::Deserialization(inner) => write!(f, "Deserialization | {}", inner),
|
||||
Error::UnexpectedSignalType(inner) => write!(f, "Unexpected Signal Type | {}", inner),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[derive(Debug, Clone)]
|
||||
struct TOML {
|
||||
header: String,
|
||||
fields: HashMap<String, String>,
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
fn naive_toml_parser(file_location: &str) -> TOML {
|
||||
let mut toml_file = File::open(file_location).unwrap();
|
||||
let mut toml_ingredients = String::default();
|
||||
|
|
88
protocol/src/protocol.rs
Normal file
88
protocol/src/protocol.rs
Normal file
|
@ -0,0 +1,88 @@
|
|||
use bincode::{Decode, Encode};
|
||||
|
||||
use crate::Error;
|
||||
|
||||
const SIGNAL_DATA_LENGTH: usize = 4;
|
||||
const NETWORK_DATA_LENGTH: usize = 6;
|
||||
|
||||
type SignalBufferReturn = [u8; SIGNAL_DATA_LENGTH];
|
||||
type NetworkBufferReturn = [u8; NETWORK_DATA_LENGTH];
|
||||
|
||||
static BINCODE_CONFIG: bincode::config::Configuration<bincode::config::BigEndian> =
|
||||
bincode::config::standard().with_big_endian();
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Encode, Decode)]
|
||||
pub struct Speaker {
|
||||
id: u8,
|
||||
}
|
||||
|
||||
impl Speaker {
|
||||
pub fn new(id: u8) -> Self {
|
||||
Self { id }
|
||||
}
|
||||
|
||||
pub fn get_id(&self) -> u8 {
|
||||
self.id
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Encode, Decode)]
|
||||
pub enum SignalType {
|
||||
AudioDatum,
|
||||
SpeakerLeft,
|
||||
}
|
||||
|
||||
#[derive(Debug, Encode, Decode)]
|
||||
pub struct Signal {
|
||||
signal_type: SignalType,
|
||||
speaker: Speaker,
|
||||
data: [u8; SIGNAL_DATA_LENGTH],
|
||||
}
|
||||
|
||||
impl Signal {
|
||||
pub fn unpack(data: NetworkBufferReturn) -> Result<Self, Error> {
|
||||
Ok(bincode::decode_from_slice::<Self, _>(&data, BINCODE_CONFIG)
|
||||
.map_err(|inner| Error::Deserialization(inner.to_string()))?
|
||||
.0)
|
||||
}
|
||||
|
||||
fn pack(self) -> NetworkBufferReturn {
|
||||
let encoded = serialize(self);
|
||||
|
||||
assert_eq!(encoded.len(), NETWORK_DATA_LENGTH);
|
||||
|
||||
let mut buffer = NetworkBufferReturn::default();
|
||||
buffer.copy_from_slice(&encoded);
|
||||
|
||||
buffer
|
||||
}
|
||||
|
||||
pub fn pack_audio_datum(speaker: Speaker, audio_datum: f32) -> NetworkBufferReturn {
|
||||
let signal = Self {
|
||||
signal_type: SignalType::AudioDatum,
|
||||
speaker,
|
||||
data: audio_datum.to_be_bytes(),
|
||||
};
|
||||
|
||||
Self::pack(signal)
|
||||
}
|
||||
|
||||
pub fn pack_speaker_left(speaker: Speaker) -> NetworkBufferReturn {
|
||||
let signal = Self {
|
||||
signal_type: SignalType::SpeakerLeft,
|
||||
speaker,
|
||||
data: SignalBufferReturn::default(),
|
||||
};
|
||||
|
||||
signal.pack()
|
||||
}
|
||||
}
|
||||
|
||||
fn serialize<E>(value: E) -> Vec<u8>
|
||||
where
|
||||
E: bincode::enc::Encode,
|
||||
{
|
||||
bincode::encode_to_vec(value, BINCODE_CONFIG)
|
||||
.map_err(|inner| Error::Serialization(inner.to_string()))
|
||||
.unwrap()
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue