@@ -57,6 +57,9 @@ pub const FALSE_VAL: &str = "FALSE";
5757/// Value: Half of u32::MAX to allow a separate ID space for out-of-band messages.
5858pub const MAX_PUBLISH_ID : u32 = u32:: MAX / 2 ;
5959
60+ /// Default TTL value for messages that do not have an explicit TTL set.
61+ pub const DEFAULT_TTL : u32 = 16 ;
62+
6063#[ derive( Error , Debug , PartialEq ) ]
6164pub enum MessageError {
6265 #[ error( "SLIM header not found" ) ]
@@ -181,6 +184,7 @@ pub struct SlimHeaderFlags {
181184 pub forward_to : Option < u64 > ,
182185 pub incoming_conn : Option < u64 > ,
183186 pub error : Option < bool > ,
187+ pub ttl : u32 ,
184188}
185189
186190impl Default for SlimHeaderFlags {
@@ -191,6 +195,7 @@ impl Default for SlimHeaderFlags {
191195 forward_to : None ,
192196 incoming_conn : None ,
193197 error : None ,
198+ ttl : DEFAULT_TTL ,
194199 }
195200 }
196201}
@@ -199,8 +204,8 @@ impl Display for SlimHeaderFlags {
199204 fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
200205 write ! (
201206 f,
202- "fanout: {}, recv_from: {:?}, forward_to: {:?}, incoming_conn: {:?}, error: {:?}" ,
203- self . fanout, self . recv_from, self . forward_to, self . incoming_conn, self . error
207+ "fanout: {}, recv_from: {:?}, forward_to: {:?}, incoming_conn: {:?}, error: {:?}, ttl: {:?} " ,
208+ self . fanout, self . recv_from, self . forward_to, self . incoming_conn, self . error, self . ttl
204209 )
205210 }
206211}
@@ -219,6 +224,7 @@ impl SlimHeaderFlags {
219224 forward_to,
220225 incoming_conn,
221226 error,
227+ ttl : DEFAULT_TTL ,
222228 }
223229 }
224230
@@ -253,6 +259,10 @@ impl SlimHeaderFlags {
253259 ..self
254260 }
255261 }
262+
263+ pub fn with_ttl ( self , ttl : u32 ) -> Self {
264+ Self { ttl, ..self }
265+ }
256266}
257267
258268/// SLIM Header
@@ -277,6 +287,7 @@ impl SlimHeader {
277287 incoming_conn : flags. incoming_conn ,
278288 error : flags. error ,
279289 header_mac : None ,
290+ ttl : flags. ttl ,
280291 }
281292 }
282293
@@ -365,6 +376,20 @@ impl SlimHeader {
365376 self . error = error;
366377 }
367378
379+ pub fn get_ttl ( & self ) -> u32 {
380+ self . ttl
381+ }
382+
383+ pub fn set_ttl ( & mut self , ttl : u32 ) {
384+ self . ttl = ttl;
385+ }
386+
387+ /// Decrements TTL by 1. Returns the new value.
388+ pub fn decrement_ttl ( & mut self ) -> u32 {
389+ self . ttl = self . ttl . saturating_sub ( 1 ) ;
390+ self . ttl
391+ }
392+
368393 // returns (incoming, recv_from, forward_to) for subscription processing
369394 pub ( crate ) fn get_connections ( & self ) -> ( u64 , Option < u64 > , Option < u64 > ) {
370395 // when calling this function, incoming connection is set
@@ -865,6 +890,19 @@ impl ProtoMessage {
865890 self . get_slim_header_mut ( ) . set_error_flag ( error) ;
866891 }
867892
893+ pub fn get_ttl ( & self ) -> u32 {
894+ self . get_slim_header ( ) . get_ttl ( )
895+ }
896+
897+ pub fn set_ttl ( & mut self , ttl : u32 ) {
898+ self . get_slim_header_mut ( ) . set_ttl ( ttl) ;
899+ }
900+
901+ /// Decrements TTL by 1. Returns the new value.
902+ pub fn decrement_ttl ( & mut self ) -> u32 {
903+ self . get_slim_header_mut ( ) . decrement_ttl ( )
904+ }
905+
868906 pub fn set_session_message_type ( & mut self , message_type : SessionMessageType ) {
869907 self . get_session_header_mut ( )
870908 . set_session_message_type ( message_type) ;
@@ -1448,36 +1486,37 @@ impl ProtoMessageBuilder {
14481486
14491487 /// Sets the fanout value
14501488 pub fn fanout ( mut self , fanout : u32 ) -> Self {
1451- let flags = self . flags . take ( ) . unwrap_or_default ( ) ;
1452- self . flags = Some ( flags. with_fanout ( fanout) ) ;
1489+ self . flags . get_or_insert_default ( ) . fanout = fanout;
14531490 self
14541491 }
14551492
14561493 /// Sets the recv_from connection
14571494 pub fn recv_from ( mut self , recv_from : u64 ) -> Self {
1458- let flags = self . flags . take ( ) . unwrap_or_default ( ) ;
1459- self . flags = Some ( flags. with_recv_from ( recv_from) ) ;
1495+ self . flags . get_or_insert_default ( ) . recv_from = Some ( recv_from) ;
14601496 self
14611497 }
14621498
14631499 /// Sets the forward_to connection
14641500 pub fn forward_to ( mut self , forward_to : u64 ) -> Self {
1465- let flags = self . flags . take ( ) . unwrap_or_default ( ) ;
1466- self . flags = Some ( flags. with_forward_to ( forward_to) ) ;
1501+ self . flags . get_or_insert_default ( ) . forward_to = Some ( forward_to) ;
14671502 self
14681503 }
14691504
14701505 /// Sets the incoming connection
14711506 pub fn incoming_conn ( mut self , incoming_conn : u64 ) -> Self {
1472- let flags = self . flags . take ( ) . unwrap_or_default ( ) ;
1473- self . flags = Some ( flags. with_incoming_conn ( incoming_conn) ) ;
1507+ self . flags . get_or_insert_default ( ) . incoming_conn = Some ( incoming_conn) ;
14741508 self
14751509 }
14761510
14771511 /// Sets the error flag
14781512 pub fn error ( mut self , error : bool ) -> Self {
1479- let flags = self . flags . take ( ) . unwrap_or_default ( ) ;
1480- self . flags = Some ( flags. with_error ( error) ) ;
1513+ self . flags . get_or_insert_default ( ) . error = Some ( error) ;
1514+ self
1515+ }
1516+
1517+ /// Sets the TTL (time-to-live) value
1518+ pub fn ttl ( mut self , ttl : u32 ) -> Self {
1519+ self . flags . get_or_insert_default ( ) . ttl = ttl;
14811520 self
14821521 }
14831522
@@ -1547,6 +1586,7 @@ impl ProtoMessageBuilder {
15471586 forward_to : header. forward_to ,
15481587 incoming_conn : header. incoming_conn ,
15491588 error : header. error ,
1589+ ttl : header. ttl ,
15501590 } ;
15511591 self . flags = Some ( flags) ;
15521592 self
@@ -2011,6 +2051,7 @@ mod tests {
20112051 incoming_conn : None ,
20122052 error : None ,
20132053 header_mac : None ,
2054+ ttl : DEFAULT_TTL ,
20142055 } ;
20152056
20162057 // the operations to retrieve source and destination should fail with panic
0 commit comments