浏览代码

network: Remove unnecessary "response" field.

Whether a packet is a request or a response is completely defined by
whether the packet is sent by the server or the client.
父节点
当前提交
35c1ad464b
共有 3 个文件被更改,包括 21 次插入30 次删除
  1. 10
    13
      src/network/client.rs
  2. 3
    4
      src/network/mod.rs
  3. 8
    13
      src/protocol.rs

+ 10
- 13
src/network/client.rs 查看文件

129
                         let id = free_ids.alloc();
129
                         let id = free_ids.alloc();
130
                         let packet = Packet {
130
                         let packet = Packet {
131
                             id: id,
131
                             id: id,
132
-                            response: false,
133
                             payload: call.data,
132
                             payload: call.data,
134
                         };
133
                         };
135
                         let mut serialized = Vec::new();
134
                         let mut serialized = Vec::new();
314
 
313
 
315
     type TestPayload = u32;
314
     type TestPayload = u32;
316
 
315
 
317
-    fn serialize_packet<T>(id: u32, response: bool, payload: T) -> Vec<u8>
316
+    fn serialize_packet<T>(id: u32, payload: T) -> Vec<u8>
318
     where
317
     where
319
         T: Serialize,
318
         T: Serialize,
320
     {
319
     {
321
         let packet = Packet {
320
         let packet = Packet {
322
             id,
321
             id,
323
-            response,
324
             payload,
322
             payload,
325
         };
323
         };
326
         let mut serialized = Vec::new();
324
         let mut serialized = Vec::new();
352
                 }),
350
                 }),
353
                 tokio::spawn(async move {
351
                 tokio::spawn(async move {
354
                     // Invalid ID, should be ignored.
352
                     // Invalid ID, should be ignored.
355
-                    pipe2.send(serialize_packet(42, true, 42)).await.unwrap();
353
+                    pipe2.send(serialize_packet(42, 42)).await.unwrap();
356
 
354
 
357
-                    assert_eq!(pipe2.next().await, Some(Ok(serialize_packet(0, false, 42))));
358
-                    pipe2.send(serialize_packet(0, true, 1337)).await.unwrap();
355
+                    assert_eq!(pipe2.next().await, Some(Ok(serialize_packet(0, 42))));
356
+                    pipe2.send(serialize_packet(0, 1337)).await.unwrap();
359
 
357
 
360
                     // Network errors shall be ignored if the stream is not closed.
358
                     // Network errors shall be ignored if the stream is not closed.
361
                     pipe2.inject_error();
359
                     pipe2.inject_error();
362
 
360
 
363
                     // The ID has to be reused, and a second concurrent call has to have a
361
                     // The ID has to be reused, and a second concurrent call has to have a
364
                     // different ID.
362
                     // different ID.
365
-                    assert_eq!(pipe2.next().await, Some(Ok(serialize_packet(0, false, 43))));
366
-                    assert_eq!(pipe2.next().await, Some(Ok(serialize_packet(1, false, 44))));
367
-                    pipe2.send(serialize_packet(1, true, 1339)).await.unwrap();
368
-                    pipe2.send(serialize_packet(0, true, 1338)).await.unwrap();
363
+                    assert_eq!(pipe2.next().await, Some(Ok(serialize_packet(0, 43))));
364
+                    assert_eq!(pipe2.next().await, Some(Ok(serialize_packet(1, 44))));
365
+                    pipe2.send(serialize_packet(1, 1339)).await.unwrap();
366
+                    pipe2.send(serialize_packet(0, 1338)).await.unwrap();
369
                 }),
367
                 }),
370
             );
368
             );
371
 
369
 
412
                     assert_eq!(events.next().await, None);
410
                     assert_eq!(events.next().await, None);
413
                 }),
411
                 }),
414
                 tokio::spawn(async move {
412
                 tokio::spawn(async move {
415
-                    pipe2.send(serialize_packet(SERVER_EVENT_ID, false, 42)).await.unwrap();
413
+                    pipe2.send(serialize_packet(SERVER_EVENT_ID, 42)).await.unwrap();
416
                     drop(pipe2);
414
                     drop(pipe2);
417
                 }),
415
                 }),
418
             );
416
             );
424
 
422
 
425
     // TODO:
423
     // TODO:
426
     // - Test for unparseable response.
424
     // - Test for unparseable response.
427
-    // - Test for server events.
428
-    // - Test wether the client correctly closes the connection and whether futures return an error
425
+    // - Test whether the client correctly closes the connection and whether futures return an error
429
     // in this case.
426
     // in this case.
430
 }
427
 }
431
 
428
 

+ 3
- 4
src/network/mod.rs 查看文件

8
 //! Remote procedure call:
8
 //! Remote procedure call:
9
 //! - The client allocates an ID from the range between 0..2^31-1 that is not currently in use
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.
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.
11
+//! - The client prepares a packet with this ID and the appropriate payload and sends the packet to
12
+//!   the server.
13
 //! - The server receives the packet, processes the request and sends a packet back with the
13
 //! - The server receives the packet, processes the request and sends a packet back with the
14
-//!   identical ID and "response = true".
14
+//!   identical ID.
15
 //! - In case of an error, the payload has the type "Error" which in turn contains the error
15
 //! - In case of an error, the payload has the type "Error" which in turn contains the error
16
 //!   type.
16
 //!   type.
17
 //! - Unpon reception of the response, the client is free to reuse the ID for further RPCs.
17
 //! - Unpon reception of the response, the client is free to reuse the ID for further RPCs.
39
 #[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
39
 #[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
40
 pub struct Packet<Payload> {
40
 pub struct Packet<Payload> {
41
     pub id: u32,
41
     pub id: u32,
42
-    pub response: bool, // TODO: Not really required.
43
     pub payload: Payload,
42
     pub payload: Payload,
44
 }
43
 }
45
 
44
 

+ 8
- 13
src/protocol.rs 查看文件

152
             (
152
             (
153
                 Packet {
153
                 Packet {
154
                     id: 42,
154
                     id: 42,
155
-                    response: false,
156
                     payload: PacketPayload::ServerInfo,
155
                     payload: PacketPayload::ServerInfo,
157
                 },
156
                 },
158
                 vec![
157
                 vec![
159
-                    131, 162, 105, 100, 42, 168, 114, 101, 115, 112, 111, 110, 115, 101, 194, 167,
160
-                    112, 97, 121, 108, 111, 97, 100, 129, 170, 83, 101, 114, 118, 101, 114, 73,
161
-                    110, 102, 111, 192,
158
+                    130, 162, 105, 100, 42, 167, 112, 97, 121, 108, 111, 97, 100, 129, 170, 83,
159
+                    101, 114, 118, 101, 114, 73, 110, 102, 111, 192,
162
                 ],
160
                 ],
163
             ),
161
             ),
164
             (
162
             (
165
                 Packet {
163
                 Packet {
166
                     id: 42,
164
                     id: 42,
167
-                    response: true,
168
                     payload: PacketPayload::ServerInfoResponse(ServerInfoResponse {
165
                     payload: PacketPayload::ServerInfoResponse(ServerInfoResponse {
169
                         version: "0.1".to_owned(),
166
                         version: "0.1".to_owned(),
170
                     }),
167
                     }),
171
                 },
168
                 },
172
                 vec![
169
                 vec![
173
-                    131, 162, 105, 100, 42, 168, 114, 101, 115, 112, 111, 110, 115, 101, 195, 167,
174
-                    112, 97, 121, 108, 111, 97, 100, 129, 178, 83, 101, 114, 118, 101, 114, 73,
175
-                    110, 102, 111, 82, 101, 115, 112, 111, 110, 115, 101, 129, 167, 118, 101, 114,
176
-                    115, 105, 111, 110, 163, 48, 46, 49,
170
+                    130, 162, 105, 100, 42, 167, 112, 97, 121, 108, 111, 97, 100, 129, 178, 83,
171
+                    101, 114, 118, 101, 114, 73, 110, 102, 111, 82, 101, 115, 112, 111, 110, 115,
172
+                    101, 129, 167, 118, 101, 114, 115, 105, 111, 110, 163, 48, 46, 49,
177
                 ],
173
                 ],
178
             ),
174
             ),
179
             (
175
             (
180
                 Packet {
176
                 Packet {
181
                     id: 42,
177
                     id: 42,
182
-                    response: true,
183
                     payload: PacketPayload::Error(crate::protocol::Error::InvalidParameter),
178
                     payload: PacketPayload::Error(crate::protocol::Error::InvalidParameter),
184
                 },
179
                 },
185
                 vec![
180
                 vec![
186
-                    131, 162, 105, 100, 42, 168, 114, 101, 115, 112, 111, 110, 115, 101, 195, 167,
187
-                    112, 97, 121, 108, 111, 97, 100, 129, 165, 69, 114, 114, 111, 114, 129, 176,
188
-                    73, 110, 118, 97, 108, 105, 100, 80, 97, 114, 97, 109, 101, 116, 101, 114, 192,
181
+                    130, 162, 105, 100, 42, 167, 112, 97, 121, 108, 111, 97, 100, 129, 165, 69,
182
+                    114, 114, 111, 114, 129, 176, 73, 110, 118, 97, 108, 105, 100, 80, 97, 114, 97,
183
+                    109, 101, 116, 101, 114, 192,
189
                 ],
184
                 ],
190
             ),
185
             ),
191
         ];
186
         ];

正在加载...
取消
保存