1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use crate::messages::{
errors::ParseError,
ui::{
agent_update::AgentUpdate, camera_position::CameraPosition,
chat_from_simulator::ChatFromSimulator, chat_from_viewer::ChatFromUI,
coarse_location_update::CoarseLocationUpdate, disable_simulator::DisableSimulator,
errors::SessionError, land_update::LandUpdate, login_event::Login,
login_response::LoginResponse, logout::Logout, mesh_update::MeshUpdate,
play_animation::PlayAnimation, skybox_update::SkyboxUpdate, water_update::WaterUpdate,
},
};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(tag = "type", content = "data")] // for json simplicity
/// The type of the message used to notify the UI .
/// Some of these also implement PacketType, as they are part of the spec, but not all of them are.
pub enum UIResponse {
/// Custom login event for sending from the UI to the core
Login(Login),
/// Message for sending chat events from the UI to the core to the server.
ChatFromViewer(ChatFromUI),
/// Message for sending logout events from the UI to the core to the server.
Logout(Logout),
/// Message for informing the server of updates to the agent, like head position, motion, and
/// FOV.
AgentUpdate(AgentUpdate),
}
impl UIResponse {
/// converts a UiEvent object to bytes using serde.
/// Bytes can be decoded directly as JSON objects
pub fn to_bytes(&self) -> Vec<u8> {
serde_json::to_vec(self).expect("Failed to serialize UIResponse")
}
/// Converts bytes to a UIEvent.
pub fn from_bytes(bytes: &[u8]) -> Result<Self, ParseError> {
serde_json::from_slice(bytes).map_err(ParseError::SerdeError)
}
}
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(tag = "type", content = "data")] // for json simplicity
/// Message types for sending between the core and the UI. These are truncated types that do not
/// contain all of the values that their packet types contain.
pub enum UIMessage {
/// Message for sending chat events from the server, to the core, to the UI
ChatFromSimulator(ChatFromSimulator),
/// Message for sending location update events from the server to the core to the UI.
CoarseLocationUpdate(CoarseLocationUpdate),
/// Custom LoginResponse event for notifying the UI of login
LoginResponse(LoginResponse),
/// Custom MeshUpdate event for notifying the UI of a mesh to render
MeshUpdate(MeshUpdate),
/// Custom SessionError event for notifying the UI of core errors
Error(SessionError),
/// Message for informing the core and UI of a server disconnect
DisableSimulator(DisableSimulator),
/// Message for informing the UI of a land packet
LandUpdate(LandUpdate),
/// Message for informing the UI of updated water information
WaterUpdate(WaterUpdate),
/// Message for informing the UI of an update to the skybox
SkyboxUpdate(SkyboxUpdate),
/// Message for setting the position of the avatar's camera
CameraPosition(CameraPosition),
/// message for informing the UI of an animation that is currently playing
PlayAnimation(PlayAnimation),
}
impl UIMessage {
/// converts a UiEvent object to bytes using serde.
/// Bytes can be decoded directly as JSON objects
pub fn to_bytes(&self) -> Vec<u8> {
serde_json::to_vec(self).expect("Failed to serialize UIMessage")
}
/// Converts bytes to a UIEvent.
pub fn from_bytes(bytes: &[u8]) -> Result<Self, ParseError> {
serde_json::from_slice(bytes).map_err(ParseError::SerdeError)
}
}