Quellcode durchsuchen

More network types.

Mathias Gottschlag vor 5 Jahren
Ursprung
Commit
2e1880fde8
4 geänderte Dateien mit 49 neuen und 17 gelöschten Zeilen
  1. 11
    12
      src/network/client.rs
  2. 3
    3
      src/network/mod.rs
  3. 13
    2
      src/network/packet_connection.rs
  4. 22
    0
      src/network/server.rs

+ 11
- 12
src/network/client.rs Datei anzeigen

@@ -1,21 +1,20 @@
1 1
 use futures::Future;
2
-use serde::de::DeserializeOwned;
3
-use serde::Serialize;
2
+use tokio::stream::Stream;
4 3
 
5
-use std::time::Duration;
4
+use super::packet_connection::IncomingPacket;
6 5
 
7
-trait Connection {
8
-    type Request: Serialize;
9
-    type Response: DeserializeOwned; // TODO: Better lifetime for zero-copy deserialization of large amounts of data?
10
-    type Call: RemoteCall<Output = Self::Response>;
6
+use std::time::Duration;
11 7
 
12
-    fn call(call: Self::Request) -> Self::Call;
8
+pub trait RPCInterface {
9
+    type PacketType;
10
+    type NetworkError;
11
+    type Call: RPC<Output = Result<IncomingPacket<Self::PacketType>, Self::NetworkError>>;
13 12
 
14
-    // TODO: Server-client events.
13
+    fn call(call: &Self::PacketType) -> Self::Call;
15 14
 }
16 15
 
17
-trait RemoteCall: Future {
18
-    // TODO
19
-
16
+pub trait RPC: Future {
20 17
     fn with_timeout(self, timeout: Duration) -> Self;
21 18
 }
19
+
20
+pub trait ServerEventStream<PacketType>: Stream<Item = PacketType> {}

+ 3
- 3
src/network/mod.rs Datei anzeigen

@@ -1,3 +1,3 @@
1
-mod client;
2
-mod packet_connection;
3
-mod server;
1
+pub mod client;
2
+pub mod packet_connection;
3
+pub mod server;

+ 13
- 2
src/network/packet_connection.rs Datei anzeigen

@@ -30,16 +30,27 @@
30 30
 use serde::de::DeserializeOwned;
31 31
 use serde::{Deserialize, Serialize};
32 32
 
33
-trait PacketConnection {
33
+pub const SERVER_EVENT_ID: u32 = u32::max_value();
34
+
35
+pub trait PacketConnection {
34 36
     type Payload: Serialize + DeserializeOwned;
35 37
 
36 38
     // TODO
37 39
 }
38 40
 
41
+/// Type which holds a byte vector received via the network and which allows access to the
42
+/// deserialized packet.
43
+///
44
+/// The type can be used for efficient zero-copy deserialization of large packets.
45
+pub struct IncomingPacket<Payload> {
46
+    // TODO
47
+    _packet: Payload,
48
+}
49
+
39 50
 #[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
40 51
 pub struct Packet<Payload> {
41 52
     pub id: u32,
42
-    pub response: bool,
53
+    pub response: bool, // TODO: Not really required.
43 54
     pub payload: Payload,
44 55
 }
45 56
 

+ 22
- 0
src/network/server.rs Datei anzeigen

@@ -1 +1,23 @@
1
+use super::packet_connection::IncomingPacket;
1 2
 
3
+// Trait that is implemented by any RPC server to process incoming calls.
4
+trait RPCServer {
5
+    type PacketType;
6
+    type NetworkError;
7
+
8
+    fn incoming_call<Response>(
9
+        &mut self,
10
+        call: IncomingPacket<Self::PacketType>,
11
+        response: Response,
12
+    ) where
13
+        Response: RPCResponse<PacketType = Self::PacketType, NetworkError = Self::NetworkError>;
14
+}
15
+
16
+// TODO: Document that types implementing this trait should also implement Drop and should return
17
+// an error if the type is dropped without send() being called.
18
+trait RPCResponse {
19
+    type PacketType;
20
+    type NetworkError;
21
+
22
+    fn send(self, packet: &Self::PacketType) -> Result<(), Self::NetworkError>;
23
+}

Laden…
Abbrechen
Speichern