|
|
@@ -1,112 +0,0 @@
|
|
1
|
|
-//! Module which implements a low-level packet protocol over websockets.
|
|
2
|
|
-//!
|
|
3
|
|
-//! The low-level protocol needs to implement two basic pieces of functionality: It needs to be
|
|
4
|
|
-//! able to implement remote procedure calls from the client to the server, and it needs to
|
|
5
|
|
-//! provide a method for the server to send asynchronous events. A protocol for both cases is
|
|
6
|
|
-//! outlined below, where each packet is sent as one WebSocket message.
|
|
7
|
|
-//!
|
|
8
|
|
-//! Remote procedure call:
|
|
9
|
|
-//! - The client allocates an ID from the range between 0..2^31-1 that is not currently in use
|
|
10
|
|
-//! for another RPC in progress.
|
|
11
|
|
-//! - The client prepares a packet with this ID, "response = false", and the appropriate
|
|
12
|
|
-//! payload and sends the packet to the server.
|
|
13
|
|
-//! - The server receives the packet, processes the request and sends a packet back with the
|
|
14
|
|
-//! identical ID and "response = true".
|
|
15
|
|
-//! - In case of an error, the payload has the type "Error" which in turn contains the error
|
|
16
|
|
-//! type.
|
|
17
|
|
-//! - Unpon reception of the response, the client is free to reuse the ID for further RPCs.
|
|
18
|
|
-//! Note that this protocol does not allow the client to cancel an RPC that is currently in
|
|
19
|
|
-//! progress - implementing such functionality is difficult, error-prone, and most likely not
|
|
20
|
|
-//! worth the effort.
|
|
21
|
|
-//!
|
|
22
|
|
-//! Server-side event:
|
|
23
|
|
-//! - The server creates a packet with the ID 2^31 and the event content and sends it to the
|
|
24
|
|
-//! client.
|
|
25
|
|
-//! Note that the client does not have any method to acknowledge event reception. As events are
|
|
26
|
|
-//! asynchronous compared to RPCs on the same connection, the event type needs to be designed in
|
|
27
|
|
-//! a way that no such temporal information is necessary to process the events.
|
|
28
|
|
-//! TODO: Example to demonstrate the problem.
|
|
29
|
|
-
|
|
30
|
|
-use serde::{Deserialize, Serialize};
|
|
31
|
|
-
|
|
32
|
|
-pub const SERVER_EVENT_ID: u32 = u32::max_value();
|
|
33
|
|
-
|
|
34
|
|
-#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
|
|
35
|
|
-pub struct Packet<Payload> {
|
|
36
|
|
- pub id: u32,
|
|
37
|
|
- pub response: bool, // TODO: Not really required.
|
|
38
|
|
- pub payload: Payload,
|
|
39
|
|
-}
|
|
40
|
|
-
|
|
41
|
|
-#[cfg(test)]
|
|
42
|
|
-mod tests {
|
|
43
|
|
- use rmps::Serializer;
|
|
44
|
|
-
|
|
45
|
|
- use super::*;
|
|
46
|
|
- use crate::protocol::{PacketPayload, ServerInfoResponse};
|
|
47
|
|
-
|
|
48
|
|
- /// Test whether serialization of packets matches the expected values.
|
|
49
|
|
- /// TODO: TGhese tests should be in protocol.rs.
|
|
50
|
|
- #[test]
|
|
51
|
|
- fn test_serde_compatibility() {
|
|
52
|
|
- let tests = [
|
|
53
|
|
- (
|
|
54
|
|
- Packet {
|
|
55
|
|
- id: 42,
|
|
56
|
|
- response: false,
|
|
57
|
|
- payload: PacketPayload::ServerInfo,
|
|
58
|
|
- },
|
|
59
|
|
- vec![
|
|
60
|
|
- 131, 162, 105, 100, 42, 168, 114, 101, 115, 112, 111, 110, 115, 101, 194, 167,
|
|
61
|
|
- 112, 97, 121, 108, 111, 97, 100, 129, 170, 83, 101, 114, 118, 101, 114, 73,
|
|
62
|
|
- 110, 102, 111, 192,
|
|
63
|
|
- ],
|
|
64
|
|
- ),
|
|
65
|
|
- (
|
|
66
|
|
- Packet {
|
|
67
|
|
- id: 42,
|
|
68
|
|
- response: true,
|
|
69
|
|
- payload: PacketPayload::ServerInfoResponse(ServerInfoResponse {
|
|
70
|
|
- version: "0.1".to_owned(),
|
|
71
|
|
- }),
|
|
72
|
|
- },
|
|
73
|
|
- vec![
|
|
74
|
|
- 131, 162, 105, 100, 42, 168, 114, 101, 115, 112, 111, 110, 115, 101, 195, 167,
|
|
75
|
|
- 112, 97, 121, 108, 111, 97, 100, 129, 178, 83, 101, 114, 118, 101, 114, 73,
|
|
76
|
|
- 110, 102, 111, 82, 101, 115, 112, 111, 110, 115, 101, 129, 167, 118, 101, 114,
|
|
77
|
|
- 115, 105, 111, 110, 163, 48, 46, 49,
|
|
78
|
|
- ],
|
|
79
|
|
- ),
|
|
80
|
|
- (
|
|
81
|
|
- Packet {
|
|
82
|
|
- id: 42,
|
|
83
|
|
- response: true,
|
|
84
|
|
- payload: PacketPayload::Error(crate::protocol::Error::InvalidParameter),
|
|
85
|
|
- },
|
|
86
|
|
- vec![
|
|
87
|
|
- 131, 162, 105, 100, 42, 168, 114, 101, 115, 112, 111, 110, 115, 101, 195, 167,
|
|
88
|
|
- 112, 97, 121, 108, 111, 97, 100, 129, 165, 69, 114, 114, 111, 114, 129, 176,
|
|
89
|
|
- 73, 110, 118, 97, 108, 105, 100, 80, 97, 114, 97, 109, 101, 116, 101, 114, 192,
|
|
90
|
|
- ],
|
|
91
|
|
- ),
|
|
92
|
|
- ];
|
|
93
|
|
-
|
|
94
|
|
- for test in tests.iter() {
|
|
95
|
|
- let mut serialized = Vec::new();
|
|
96
|
|
- test.0
|
|
97
|
|
- .serialize(
|
|
98
|
|
- &mut Serializer::new(&mut serialized)
|
|
99
|
|
- .with_struct_map()
|
|
100
|
|
- .with_string_variants(),
|
|
101
|
|
- )
|
|
102
|
|
- .unwrap();
|
|
103
|
|
- let deserialized: Packet<PacketPayload> =
|
|
104
|
|
- rmps::decode::from_slice(&serialized).unwrap();
|
|
105
|
|
-
|
|
106
|
|
- // Test that serializing produces the expected binary results.
|
|
107
|
|
- assert_eq!(serialized, test.1);
|
|
108
|
|
- // Test that deserializing yields the original again.
|
|
109
|
|
- assert_eq!(deserialized, test.0);
|
|
110
|
|
- }
|
|
111
|
|
- }
|
|
112
|
|
-}
|