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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
use std::convert::TryInto;
use crate::{
deserialize::Deserialize,
primitive::{VariantList, VariantMap},
serialize::Serialize,
session::Session,
};
use num_derive::{FromPrimitive, ToPrimitive};
mod heartbeat;
mod initdata;
mod initrequest;
pub mod objects;
mod rpccall;
mod syncmessage;
mod translation;
pub use translation::*;
pub use heartbeat::*;
pub use initdata::*;
pub use initrequest::*;
pub use rpccall::*;
pub use syncmessage::*;
/// SyncProxy sends sync and rpc messages
pub trait SyncProxy: Session {
fn sync(
&self,
class_name: &str,
object_name: Option<&str>,
function: &str,
params: VariantList,
);
/// Send a RpcCall
fn rpc(&self, function: &str, params: VariantList);
}
/// A base Syncable Object
pub trait Syncable {
/// Send a SyncMessage.
///
/// Must implement a call to session.sync() that sets the class and object names
///
/// Example:
/// ```ignore
/// impl Syncable for AliasManager {
/// fn sync(&self, session: impl SyncProxy, function: &str, params: VariantList) {
/// session.sync("AliasManager", None, function, params)
/// }
/// }
/// ```
fn sync(&self, session: impl SyncProxy, function: &str, params: VariantList);
/// Send a RpcCall
fn rpc(&self, session: impl SyncProxy, function: &str, params: VariantList) {
session.rpc(function, params);
}
}
// TODO handle client vs server with feature flag
/// A Stateful Syncable Object
#[allow(unused_variables)]
pub trait StatefulSyncable: Syncable {
/// Client -> Server: Update the whole object with received data
fn update(&mut self, session: impl SyncProxy, params: VariantMap)
where
Self: Sized + From<VariantMap>,
{
#[cfg(feature = "client")]
{
self.sync(session, "update", vec![params.into()]);
}
#[cfg(feature = "server")]
{
*self = params.try_into().unwrap();
}
}
/// Server -> Client: Update the whole object with received data
fn request_update(&mut self, session: impl SyncProxy, params: VariantMap)
where
Self: Sized + From<VariantMap>,
{
#[cfg(feature = "client")]
{
*self = params.try_into().unwrap();
}
#[cfg(feature = "server")]
{
self.sync(session, "requestUpdate", vec![params.into()]);
}
}
}
#[derive(Clone, Debug, std::cmp::PartialEq)]
pub enum Message {
/// Bidirectional
SyncMessage(SyncMessage),
/// Bidirectional
RpcCall(RpcCall),
InitRequest(InitRequest),
InitData(InitData),
/// Bidirectional
HeartBeat(HeartBeat),
/// Bidirectional
HeartBeatReply(HeartBeatReply),
}
// impl Message {
// fn act(&self) {
// match &self {
// Message::SyncMessage(value) => value.serialize(),
// Message::RpcCall(value) => value.serialize(),
// Message::InitRequest(value) => value.serialize(),
// Message::InitData(value) => value.serialize(),
// Message::HeartBeat(value) => value.serialize(),
// Message::HeartBeatReply(value) => value.serialize(),
// }
// }
// }
impl Serialize for Message {
fn serialize(&self) -> Result<Vec<std::primitive::u8>, failure::Error> {
match &self {
Message::SyncMessage(value) => value.serialize(),
Message::RpcCall(value) => value.serialize(),
Message::InitRequest(value) => value.serialize(),
Message::InitData(value) => value.serialize(),
Message::HeartBeat(value) => value.serialize(),
Message::HeartBeatReply(value) => value.serialize(),
}
}
}
impl Deserialize for Message {
fn parse(b: &[std::primitive::u8]) -> Result<(std::primitive::usize, Self), failure::Error> {
let (_, message_type) = i32::parse(&b[9..13])?;
match MessageType::from(message_type) {
MessageType::SyncMessage => {
let (size, res) = SyncMessage::parse(&b)?;
Ok((size, Message::SyncMessage(res)))
}
MessageType::RpcCall => {
let (size, res) = RpcCall::parse(&b)?;
Ok((size, Message::RpcCall(res)))
}
MessageType::InitRequest => {
let (size, res) = InitRequest::parse(&b)?;
Ok((size, Message::InitRequest(res)))
}
MessageType::InitData => {
let (size, res) = InitData::parse(&b)?;
Ok((size, Message::InitData(res)))
}
MessageType::HeartBeat => {
let (size, res) = HeartBeat::parse(&b)?;
Ok((size, Message::HeartBeat(res)))
}
MessageType::HeartBeatReply => {
let (size, res) = HeartBeatReply::parse(&b)?;
Ok((size, Message::HeartBeatReply(res)))
}
}
}
}
/// Type of an SignalProxy Message
/// The first element in the VariantList that is received
#[repr(i32)]
#[derive(Debug, Copy, Clone, PartialEq, FromPrimitive, ToPrimitive)]
pub enum MessageType {
/// Bidirectional
SyncMessage = 0x00000001,
/// Bidirectional
RpcCall = 0x00000002,
InitRequest = 0x00000003,
InitData = 0x00000004,
/// Bidirectional
HeartBeat = 0x00000005,
/// Bidirectional
HeartBeatReply = 0x00000006,
}
impl From<i32> for MessageType {
fn from(val: i32) -> Self {
match val {
0x00000001 => MessageType::SyncMessage,
0x00000002 => MessageType::RpcCall,
0x00000003 => MessageType::InitRequest,
0x00000004 => MessageType::InitData,
0x00000005 => MessageType::HeartBeat,
0x00000006 => MessageType::HeartBeatReply,
_ => unimplemented!(),
}
}
}
|