1
+ //! Contains the PacketHeader struct. This struct is used to prepend a header to a packet.
2
+ //!
3
+ //! The header is used to ensure that the data is sent and received correctly.
4
+
1
5
use std:: {
2
6
fmt:: Debug ,
3
7
hash:: { DefaultHasher , Hash , Hasher } ,
@@ -6,18 +10,18 @@ use std::{
6
10
7
11
use crate :: { hash_type_id, Result , Sendable } ;
8
12
13
+ // RSOCK was the development name for this project.
14
+ // TODO: Maybe change this to lazi or something similar.
9
15
const HEADER : [ u8 ; 5 ] = * b"RSOCK" ;
10
16
11
17
#[ derive( Clone , Copy , PartialEq , Eq , Hash ) ]
12
18
#[ repr( C ) ] // This is important for the safety of the from_bytes_unchecked function.
13
19
/// The header of a packet. When a packet is sent over a socket, it is prepended with this header.
14
- /// # Why the type parameter?
15
- /// The type parameter is used to have some sort of type safety.
16
- /// Without the type parameter, it would be possible to create a PacketHeader with a `type_id` that does not match the actual type of the payload.
17
- /// This would lead to undefined behavior.
18
- ///
19
- /// The type parameter is used to ensure that the `type_id` matches the actual type of the payload.
20
- /// You can make an untyped PacketHeader by using `PacketHeader<UnknownType>`, but this is only intended for receiving packets.
20
+ /// This contains the type_id of the payload, the size of the payload, and a checksum of the payload.
21
+ /// The checksum is used to verify that the payload was received correctly.
22
+ /// The type_id is used to determine the type of the payload.
23
+ /// The payload_size is used to determine the size of the payload.
24
+ // TODO: Remove the type parameter. It was never used, and ended up causing some issues.
21
25
pub struct PacketHeader < T >
22
26
where
23
27
T : ' static + Sendable ,
@@ -43,6 +47,7 @@ impl<T: Sendable> Debug for PacketHeader<T> {
43
47
. finish_non_exhaustive ( )
44
48
}
45
49
}
50
+
46
51
/// A ZST that represents an unknown type.
47
52
/// This is used when the type of the payload is unknown.
48
53
#[ derive( Clone , Copy , Debug ) ]
91
96
}
92
97
}
93
98
/// Calculates the checksum of the payload. Sets the checksum field to the calculated checksum.
94
- pub fn calculate_checksum ( & mut self , payload : & [ u8 ] ) {
99
+ pub ( crate ) fn calculate_checksum ( & mut self , payload : & [ u8 ] ) {
95
100
let mut hasher = DefaultHasher :: new ( ) ;
96
101
hasher. write ( payload) ;
97
102
self . checksum = hasher. finish ( ) as u32 ;
@@ -119,7 +124,8 @@ where
119
124
}
120
125
}
121
126
122
- pub fn id ( & self ) -> u32 {
127
+ /// Gets the type_id of the payload.
128
+ pub ( crate ) fn id ( & self ) -> u32 {
123
129
self . type_id
124
130
}
125
131
}
0 commit comments