Skip to main content

gled_protocol/
message.rs

1//! The messages of a connection between a remote user interface and the
2//! engine, over a WebSocket. The interface opens with `Hello`; the server
3//! answers `Welcome` or `Rejected`, then sends the state whenever it changes.
4
5use std::sync::Arc;
6
7use gled_model::input::event::InputEvent;
8use gled_model::timing::BeatState;
9use serde::{Deserialize, Serialize};
10
11use crate::assets::{AnyAsset, AssetChanges};
12use crate::bytes::SharedBytes;
13use crate::midi::MidiMonitorEvent;
14use crate::project_update::ProjectUpdate;
15use crate::state::{ClientInput, EngineEvent, EngineState, RenderStats, SoundSnapshot};
16
17/// Changes whenever a message changes incompatibly. Both ends must agree.
18///
19/// 2: the patch replaced the SVG and the groups (`ProjectUpdate::Changed`,
20/// `UiAction::SetProjectPatch`, `SetSceneEffectLayout`).
21pub const PROTOCOL_VERSION: u32 = 2;
22
23/// The optional streams an interface can ask for. All are off until it
24/// subscribes, so a hidden page costs nothing.
25#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
26#[serde(default)]
27pub struct Subscriptions {
28    pub preview: bool,
29    pub sound: bool,
30    /// New input events (keys, gamepad, Art-Net), for assigning inputs.
31    pub input_events: bool,
32    /// The MIDI monitor and the messages MIDI learn captured.
33    pub midi: bool,
34}
35
36/// From a user interface to the server.
37#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
38pub enum ClientMessage {
39    Hello {
40        protocol_version: u32,
41        /// The interface's gled version, for the logs.
42        app_version: String,
43    },
44    Input(ClientInput),
45    /// Which of the optional streams this interface wants.
46    Subscribe(Subscriptions),
47    /// Asks for a `Pong` with the same number, to measure the round trip.
48    Ping(u64),
49}
50
51/// From the server to a user interface.
52#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
53pub enum ServerMessage {
54    Welcome {
55        client_id: u64,
56        /// The server's gled version.
57        app_version: String,
58    },
59    /// The connection is closed after this.
60    Rejected {
61        reason: String,
62    },
63    State {
64        state: Arc<EngineState>,
65        /// The number of the newest action of this interface applied.
66        applied_sequence: u64,
67    },
68    /// What changed since the last `State` or `Update`: the state without
69    /// its project, which comes as a patch.
70    Update {
71        /// The state with `project: None`.
72        state: Arc<EngineState>,
73        project: ProjectUpdate,
74        applied_sequence: u64,
75    },
76    /// The timing, when nothing else changed since the last `State`.
77    Timing {
78        beat: BeatState,
79        render: RenderStats,
80    },
81    /// Every asset, sent once after `Welcome`.
82    Assets(Vec<AnyAsset>),
83    /// Assets that changed since `Assets` or the last `AssetsChanged`.
84    AssetsChanged(AssetChanges),
85    /// The LED output as rendered, for the preview: three bytes per lamp,
86    /// 512 bytes per universe in the order of `OutputConfig::universes`.
87    /// Only sent to interfaces that subscribed to it.
88    Preview(SharedBytes),
89    /// Sound levels, at most 30 times a second, to interfaces that subscribed.
90    Sound(Arc<SoundSnapshot>),
91    /// Input events that happened since the last of these, to interfaces
92    /// that subscribed.
93    InputEvents(Vec<InputEvent>),
94    /// MIDI traffic since the last of these, to interfaces that subscribed.
95    MidiMonitor(Vec<MidiMonitorEvent>),
96    /// Messages MIDI learn captured since the last of these.
97    MidiLearn(Vec<[u8; 3]>),
98    Event(EngineEvent),
99    Pong(u64),
100}