|
|
@@ -1,33 +1,194 @@
|
|
1
|
|
-// Trait that is implemented by any RPC server to process incoming calls.
|
|
2
|
|
-trait RPCServer {
|
|
|
1
|
+use std::fmt;
|
|
|
2
|
+
|
|
|
3
|
+use futures::sink::{Sink, SinkExt};
|
|
|
4
|
+use futures::stream::{Stream, StreamExt};
|
|
|
5
|
+use log::{error, info};
|
|
|
6
|
+use rmps::Serializer;
|
|
|
7
|
+use serde::de::DeserializeOwned;
|
|
|
8
|
+use serde::Serialize;
|
|
|
9
|
+use tokio::sync::mpsc;
|
|
|
10
|
+
|
|
|
11
|
+use super::{Packet, SERVER_EVENT_ID};
|
|
|
12
|
+
|
|
|
13
|
+/// Trait that is implemented by any RPC server to process incoming calls.
|
|
|
14
|
+pub trait RPCServer {
|
|
3
|
15
|
type PacketType;
|
|
4
|
|
- type NetworkError;
|
|
5
|
16
|
|
|
6
|
|
- fn incoming_call<Response>(&mut self, call: Self::PacketType, response: Response)
|
|
7
|
|
- where
|
|
8
|
|
- Response: RPCResponse<PacketType = Self::PacketType, NetworkError = Self::NetworkError>;
|
|
|
17
|
+ fn incoming_call(&mut self, call: Self::PacketType, response: RPCResponse<Self::PacketType>);
|
|
9
|
18
|
}
|
|
10
|
19
|
|
|
11
|
20
|
// TODO: Document that types implementing this trait should also implement Drop and should return
|
|
12
|
21
|
// an error if the type is dropped without send() being called.
|
|
13
|
|
-trait RPCResponse {
|
|
14
|
|
- type PacketType;
|
|
15
|
|
- type NetworkError;
|
|
|
22
|
+pub struct RPCResponse<PacketType> {
|
|
|
23
|
+ send_packet: mpsc::UnboundedSender<Packet<PacketType>>,
|
|
|
24
|
+ id: u32,
|
|
|
25
|
+ error_value: Option<PacketType>,
|
|
|
26
|
+}
|
|
16
|
27
|
|
|
17
|
|
- fn send(self, packet: &Self::PacketType) -> Result<(), Self::NetworkError>;
|
|
|
28
|
+impl<PacketType> RPCResponse<PacketType>
|
|
|
29
|
+where
|
|
|
30
|
+ PacketType: Serialize,
|
|
|
31
|
+{
|
|
|
32
|
+ pub fn send(self, packet: PacketType) {
|
|
|
33
|
+ // We ignore errors here, as they mean that the connection was just closed.
|
|
|
34
|
+ self.send_packet
|
|
|
35
|
+ .send(Packet {
|
|
|
36
|
+ payload: packet,
|
|
|
37
|
+ id: self.id,
|
|
|
38
|
+ })
|
|
|
39
|
+ .ok();
|
|
|
40
|
+ }
|
|
18
|
41
|
}
|
|
19
|
42
|
|
|
20
|
|
-pub struct LowLevelIntegrationTestServer {
|
|
21
|
|
- // TODO
|
|
|
43
|
+impl<PacketType> Drop for RPCResponse<PacketType> {
|
|
|
44
|
+ fn drop(&mut self) {
|
|
|
45
|
+ // Send an error response?
|
|
|
46
|
+ self.send_packet
|
|
|
47
|
+ .send(Packet {
|
|
|
48
|
+ payload: self.error_value.take().unwrap(),
|
|
|
49
|
+ id: self.id,
|
|
|
50
|
+ })
|
|
|
51
|
+ .ok();
|
|
|
52
|
+ }
|
|
22
|
53
|
}
|
|
23
|
54
|
|
|
24
|
|
-impl LowLevelIntegrationTestServer {
|
|
25
|
|
- pub async fn start(_bind_address: &str) -> LowLevelIntegrationTestServer {
|
|
26
|
|
- // TODO
|
|
27
|
|
- LowLevelIntegrationTestServer {}
|
|
|
55
|
+/// Wraps an `RPCServer` and adds the network protocol implementation around it.
|
|
|
56
|
+pub struct RPCServerWrapper<Server: RPCServer, Connection> {
|
|
|
57
|
+ server: Server,
|
|
|
58
|
+ connection: Connection,
|
|
|
59
|
+ server_events: ServerEventStream<Server::PacketType>,
|
|
|
60
|
+ send_packet: mpsc::UnboundedSender<Packet<Server::PacketType>>,
|
|
|
61
|
+ sent_packets: mpsc::UnboundedReceiver<Packet<Server::PacketType>>,
|
|
|
62
|
+ error_value: Server::PacketType,
|
|
|
63
|
+}
|
|
|
64
|
+
|
|
|
65
|
+impl<Server: RPCServer, Connection, ConnectionError> RPCServerWrapper<Server, Connection>
|
|
|
66
|
+where
|
|
|
67
|
+ Server::PacketType: DeserializeOwned + Serialize + Clone + Send + 'static,
|
|
|
68
|
+ Connection: Stream<Item = Vec<u8>> + Sink<Vec<u8>, Error = ConnectionError> + Unpin,
|
|
|
69
|
+ ConnectionError: fmt::Debug,
|
|
|
70
|
+{
|
|
|
71
|
+ pub fn new(
|
|
|
72
|
+ server: Server,
|
|
|
73
|
+ connection: Connection,
|
|
|
74
|
+ server_events: ServerEventStream<Server::PacketType>,
|
|
|
75
|
+ error_value: Server::PacketType,
|
|
|
76
|
+ ) -> Self {
|
|
|
77
|
+ let (send_packet, sent_packets) = mpsc::unbounded_channel();
|
|
|
78
|
+ Self {
|
|
|
79
|
+ server,
|
|
|
80
|
+ connection,
|
|
|
81
|
+ server_events,
|
|
|
82
|
+ send_packet,
|
|
|
83
|
+ sent_packets,
|
|
|
84
|
+ error_value,
|
|
|
85
|
+ }
|
|
28
|
86
|
}
|
|
29
|
87
|
|
|
30
|
|
- pub async fn stop(self) {
|
|
31
|
|
- // TODO
|
|
|
88
|
+ pub async fn run(&mut self) {
|
|
|
89
|
+ loop {
|
|
|
90
|
+ tokio::select! {
|
|
|
91
|
+ packet = &mut self.connection.next() => {
|
|
|
92
|
+ match packet {
|
|
|
93
|
+ Some(data) => {
|
|
|
94
|
+ if !self.packet_received(data) {
|
|
|
95
|
+ // Disconnect on errors.
|
|
|
96
|
+ break;
|
|
|
97
|
+ }
|
|
|
98
|
+ }
|
|
|
99
|
+ None => {
|
|
|
100
|
+ // Stream closed.
|
|
|
101
|
+ println!("Disconnected.");
|
|
|
102
|
+ break;
|
|
|
103
|
+ }
|
|
|
104
|
+ }
|
|
|
105
|
+ }
|
|
|
106
|
+ cmd = &mut self.sent_packets.next() => {
|
|
|
107
|
+ match cmd {
|
|
|
108
|
+ Some(packet) => {
|
|
|
109
|
+ if !self.send_packet(packet).await {
|
|
|
110
|
+ // Disconnect on errors.
|
|
|
111
|
+ break;
|
|
|
112
|
+ }
|
|
|
113
|
+ }
|
|
|
114
|
+ None => {
|
|
|
115
|
+ // This should never happen.
|
|
|
116
|
+ break;
|
|
|
117
|
+ }
|
|
|
118
|
+ }
|
|
|
119
|
+ }
|
|
|
120
|
+ cmd = &mut self.server_events.receive.next() => {
|
|
|
121
|
+ match cmd {
|
|
|
122
|
+ Some(packet) => {
|
|
|
123
|
+ if !self.send_packet(Packet{payload: packet, id: SERVER_EVENT_ID}).await {
|
|
|
124
|
+ // Disconnect on errors.
|
|
|
125
|
+ break;
|
|
|
126
|
+ }
|
|
|
127
|
+ }
|
|
|
128
|
+ None => {
|
|
|
129
|
+ // This should never happen.
|
|
|
130
|
+ break;
|
|
|
131
|
+ }
|
|
|
132
|
+ }
|
|
|
133
|
+ }
|
|
|
134
|
+ }
|
|
|
135
|
+ }
|
|
32
|
136
|
}
|
|
|
137
|
+
|
|
|
138
|
+ fn packet_received(&mut self, packet: Vec<u8>) -> bool {
|
|
|
139
|
+ match rmps::decode::from_slice(&packet) as Result<Packet<Server::PacketType>, _> {
|
|
|
140
|
+ Ok(deserialized) => {
|
|
|
141
|
+ if deserialized.id == SERVER_EVENT_ID {
|
|
|
142
|
+ // Clients must never send server events.
|
|
|
143
|
+ info!("Client sent SERVER_EVENT_ID!");
|
|
|
144
|
+ return false;
|
|
|
145
|
+ } else {
|
|
|
146
|
+ let response = RPCResponse {
|
|
|
147
|
+ send_packet: self.send_packet.clone(),
|
|
|
148
|
+ id: deserialized.id,
|
|
|
149
|
+ error_value: Some(self.error_value.clone()),
|
|
|
150
|
+ };
|
|
|
151
|
+ self.server.incoming_call(deserialized.payload, response);
|
|
|
152
|
+ }
|
|
|
153
|
+ }
|
|
|
154
|
+ Err(e) => {
|
|
|
155
|
+ error!("Invalid packet received: {:?}", e);
|
|
|
156
|
+ return false;
|
|
|
157
|
+ }
|
|
|
158
|
+ };
|
|
|
159
|
+ return true;
|
|
|
160
|
+ }
|
|
|
161
|
+
|
|
|
162
|
+ async fn send_packet(&mut self, packet: Packet<Server::PacketType>) -> bool {
|
|
|
163
|
+ let mut serialized = Vec::new();
|
|
|
164
|
+ packet
|
|
|
165
|
+ .serialize(
|
|
|
166
|
+ &mut Serializer::new(&mut serialized)
|
|
|
167
|
+ .with_struct_map()
|
|
|
168
|
+ .with_string_variants(),
|
|
|
169
|
+ )
|
|
|
170
|
+ .unwrap();
|
|
|
171
|
+ match self.connection.send(serialized).await {
|
|
|
172
|
+ Ok(()) => true,
|
|
|
173
|
+ Err(e) => {
|
|
|
174
|
+ info!("Could not send packet: {:?}", e);
|
|
|
175
|
+ false
|
|
|
176
|
+ }
|
|
|
177
|
+ }
|
|
|
178
|
+ }
|
|
|
179
|
+}
|
|
|
180
|
+
|
|
|
181
|
+pub fn server_events<PacketType>() -> (ServerEventSink<PacketType>, ServerEventStream<PacketType>) {
|
|
|
182
|
+ let (send, receive) = mpsc::unbounded_channel();
|
|
|
183
|
+ (ServerEventSink { send }, ServerEventStream { receive })
|
|
|
184
|
+}
|
|
|
185
|
+
|
|
|
186
|
+/// Stream of asynchronous packets sent to the clients.
|
|
|
187
|
+#[derive(Clone)]
|
|
|
188
|
+pub struct ServerEventSink<PacketType> {
|
|
|
189
|
+ send: mpsc::UnboundedSender<PacketType>,
|
|
|
190
|
+}
|
|
|
191
|
+
|
|
|
192
|
+pub struct ServerEventStream<PacketType> {
|
|
|
193
|
+ receive: mpsc::UnboundedReceiver<PacketType>,
|
|
33
|
194
|
}
|