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`).
21/// 3: pictures travel through the engine (`ClientInput::SavePicture`,
22/// `RequestPicture`, `EngineEvent::Picture`).
23/// 4: controllers select rows and columns (`UiAction::SelectSceneRowRelative`,
24/// `SelectSceneColumnRelative`, `EngineState::controller_line`) and switch
25/// modes (`UiAction::SetMidiMode`, `EngineState::midi_modes`,
26/// `last_mode_switch`) and show touched faders (`UiAction::SetTouchedControl`,
27/// `EngineState::touched_controls`, `midi_inputs`); controller mappings have
28/// modes, touch notes and default devices.
29pub const PROTOCOL_VERSION: u32 = 4;
30
31/// The optional streams an interface can ask for. All are off until it
32/// subscribes, so a hidden page costs nothing.
33#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
34#[serde(default)]
35pub struct Subscriptions {
36    pub preview: bool,
37    pub sound: bool,
38    /// New input events (keys, gamepad, Art-Net), for assigning inputs.
39    pub input_events: bool,
40    /// The MIDI monitor and the messages MIDI learn captured.
41    pub midi: bool,
42}
43
44/// From a user interface to the server.
45#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
46pub enum ClientMessage {
47    Hello {
48        protocol_version: u32,
49        /// The interface's gled version, for the logs.
50        app_version: String,
51    },
52    Input(ClientInput),
53    /// Which of the optional streams this interface wants.
54    Subscribe(Subscriptions),
55    /// Asks for a `Pong` with the same number, to measure the round trip.
56    Ping(u64),
57}
58
59/// From the server to a user interface.
60#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
61pub enum ServerMessage {
62    Welcome {
63        client_id: u64,
64        /// The server's gled version.
65        app_version: String,
66    },
67    /// The connection is closed after this.
68    Rejected {
69        reason: String,
70    },
71    State {
72        state: Arc<EngineState>,
73        /// The number of the newest action of this interface applied.
74        applied_sequence: u64,
75    },
76    /// What changed since the last `State` or `Update`: the state without
77    /// its project, which comes as a patch.
78    Update {
79        /// The state with `project: None`.
80        state: Arc<EngineState>,
81        project: ProjectUpdate,
82        applied_sequence: u64,
83    },
84    /// The timing, when nothing else changed since the last `State`.
85    Timing {
86        beat: BeatState,
87        render: RenderStats,
88    },
89    /// Every asset, sent once after `Welcome`.
90    Assets(Vec<AnyAsset>),
91    /// Assets that changed since `Assets` or the last `AssetsChanged`.
92    AssetsChanged(AssetChanges),
93    /// The LED output as rendered, for the preview: three bytes per lamp,
94    /// 512 bytes per universe in the order of `OutputConfig::universes`.
95    /// Only sent to interfaces that subscribed to it.
96    Preview(SharedBytes),
97    /// Sound levels, at most 30 times a second, to interfaces that subscribed.
98    Sound(Arc<SoundSnapshot>),
99    /// Input events that happened since the last of these, to interfaces
100    /// that subscribed.
101    InputEvents(Vec<InputEvent>),
102    /// MIDI traffic since the last of these, to interfaces that subscribed.
103    MidiMonitor(Vec<MidiMonitorEvent>),
104    /// Messages MIDI learn captured since the last of these.
105    MidiLearn(Vec<[u8; 3]>),
106    Event(EngineEvent),
107    Pong(u64),
108}