mqtt5 0.31.4

Complete MQTT v5.0 platform with high-performance async client and full-featured broker supporting TCP, TLS, WebSocket, authentication, bridging, and resource monitoring
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
use crate::error::{MqttError, Result};
use crate::packet::publish::PublishPacket;
use crate::session::flow_control::{FlowControlManager, TopicAliasManager};
use crate::session::limits::LimitsManager;
use crate::session::queue::{MessageQueue, QueuedMessage};
#[cfg(not(target_arch = "wasm32"))]
use crate::session::quic_flow::{FlowRegistry, FlowState};
use crate::session::retained::{RetainedMessage, RetainedMessageStore};
use crate::session::subscription::{Subscription, SubscriptionManager};
use crate::time::{Duration, Instant};
#[cfg(not(target_arch = "wasm32"))]
use crate::transport::flow::{FlowFlags, FlowId};
use crate::types::WillMessage;
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::RwLock;

/// Session configuration
#[derive(Debug, Clone)]
pub struct SessionConfig {
    /// Session expiry interval in seconds (0 = session ends on disconnect)
    pub session_expiry_interval: u32,
    /// Maximum number of queued messages
    pub max_queued_messages: usize,
    /// Maximum size of queued messages in bytes
    pub max_queued_size: usize,
    /// Whether to persist session state
    pub persistent: bool,
}

impl Default for SessionConfig {
    fn default() -> Self {
        Self {
            session_expiry_interval: 0,
            max_queued_messages: 1000,
            max_queued_size: crate::constants::buffer::DEFAULT_CAPACITY
                * crate::constants::buffer::DEFAULT_CAPACITY, // 1MB
            persistent: false,
        }
    }
}

/// MQTT session state
#[derive(Debug)]
pub struct SessionState {
    /// Client identifier
    client_id: String,
    /// Session configuration
    config: SessionConfig,
    /// Subscriptions
    subscriptions: Arc<RwLock<SubscriptionManager>>,
    /// `QoS` 1 and 2 message queue
    message_queue: Arc<RwLock<MessageQueue>>,
    /// Unacknowledged PUBLISH packets (`packet_id` -> packet)
    unacked_publishes: Arc<RwLock<HashMap<u16, PublishPacket>>>,
    /// Unacknowledged PUBREL packets (`packet_id` -> timestamp)
    unacked_pubrels: Arc<RwLock<HashMap<u16, Instant>>>,
    #[cfg(not(target_arch = "wasm32"))]
    publish_flows: Arc<RwLock<HashMap<u16, FlowId>>>,
    /// Session creation time
    created_at: Instant,
    /// Last activity time
    last_activity: Arc<RwLock<Instant>>,
    /// Whether this is a clean session
    clean_start: bool,
    /// Flow control manager
    flow_control: Arc<RwLock<FlowControlManager>>,
    /// Topic alias manager for outgoing messages
    topic_alias_out: Arc<RwLock<TopicAliasManager>>,
    /// Topic alias manager for incoming messages
    topic_alias_in: Arc<RwLock<TopicAliasManager>>,
    /// Retained message store
    retained_messages: Arc<RetainedMessageStore>,
    /// Will message (to be published on abnormal disconnection)
    will_message: Arc<RwLock<Option<WillMessage>>>,
    /// Will delay timer handle
    will_delay_handle: Arc<RwLock<Option<tokio::task::JoinHandle<()>>>>,
    /// Limits manager for packet size and message expiry
    limits: Arc<RwLock<LimitsManager>>,
    #[cfg(not(target_arch = "wasm32"))]
    flow_registry: Arc<RwLock<FlowRegistry>>,
}

impl SessionState {
    /// Creates a new session state
    #[must_use]
    pub fn new(client_id: String, config: SessionConfig, clean_start: bool) -> Self {
        let now = Instant::now();
        Self {
            client_id,
            subscriptions: Arc::new(RwLock::new(SubscriptionManager::new())),
            message_queue: Arc::new(RwLock::new(MessageQueue::new(
                config.max_queued_messages,
                config.max_queued_size,
            ))),
            config,
            unacked_publishes: Arc::new(RwLock::new(HashMap::new())),
            unacked_pubrels: Arc::new(RwLock::new(HashMap::new())),
            #[cfg(not(target_arch = "wasm32"))]
            publish_flows: Arc::new(RwLock::new(HashMap::new())),
            created_at: now,
            last_activity: Arc::new(RwLock::new(now)),
            clean_start,
            flow_control: Arc::new(RwLock::new(FlowControlManager::new(65535))), // Default to max
            topic_alias_out: Arc::new(RwLock::new(TopicAliasManager::new(0))), // Default to disabled
            topic_alias_in: Arc::new(RwLock::new(TopicAliasManager::new(0))), // Default to disabled
            retained_messages: Arc::new(RetainedMessageStore::new()),
            will_message: Arc::new(RwLock::new(None)),
            will_delay_handle: Arc::new(RwLock::new(None)),
            limits: Arc::new(RwLock::new(LimitsManager::with_defaults())),
            #[cfg(not(target_arch = "wasm32"))]
            flow_registry: Arc::new(RwLock::new(FlowRegistry::new(256))),
        }
    }

    #[must_use]
    /// Gets the client ID
    pub fn client_id(&self) -> &str {
        &self.client_id
    }

    #[must_use]
    /// Checks if this is a clean session
    pub fn is_clean(&self) -> bool {
        self.clean_start
    }

    /// Updates last activity time
    pub async fn touch(&self) {
        *self.last_activity.write().await = Instant::now();
    }

    /// Checks if session has expired
    pub async fn is_expired(&self) -> bool {
        if self.config.session_expiry_interval == 0 {
            return false; // Session doesn't expire
        }

        let last_activity = *self.last_activity.read().await;
        let expiry_duration = Duration::from_secs(u64::from(self.config.session_expiry_interval));
        last_activity.elapsed() > expiry_duration
    }

    /// Adds a subscription
    ///
    /// # Errors
    ///
    /// Returns an error if the operation fails
    pub async fn add_subscription(
        &self,
        topic_filter: String,
        subscription: Subscription,
    ) -> Result<()> {
        self.touch().await;
        self.subscriptions
            .write()
            .await
            .add(topic_filter, subscription)
    }

    /// Removes a subscription
    ///
    /// Returns `Ok(true)` if the subscription existed and was removed,
    /// `Ok(false)` if the subscription did not exist.
    ///
    /// # Errors
    ///
    /// Returns an error if the operation fails
    pub async fn remove_subscription(&self, topic_filter: &str) -> Result<bool> {
        self.touch().await;
        self.subscriptions.write().await.remove(topic_filter)
    }

    /// Gets subscriptions matching a topic
    pub async fn matching_subscriptions(&self, topic: &str) -> Vec<(String, Subscription)> {
        self.subscriptions
            .read()
            .await
            .matching_subscriptions(topic)
    }

    /// Gets all subscriptions
    pub async fn all_subscriptions(&self) -> HashMap<String, Subscription> {
        self.subscriptions.read().await.all()
    }

    /// Queues a message for delivery
    ///
    /// Returns information about the queue operation including whether the message
    /// was queued and how many messages were dropped to make room.
    ///
    /// # Errors
    ///
    /// Returns an error if the operation fails
    pub async fn queue_message(
        &self,
        message: QueuedMessage,
    ) -> Result<crate::session::queue::QueueResult> {
        self.touch().await;
        let limits = self.limits.read().await;
        let expiring_message = message.to_expiring(&limits);
        drop(limits);
        self.message_queue.write().await.enqueue(expiring_message)
    }

    /// Dequeues messages up to a limit
    pub async fn dequeue_messages(&self, limit: usize) -> Vec<QueuedMessage> {
        self.touch().await;
        self.message_queue
            .write()
            .await
            .dequeue_batch(limit)
            .into_iter()
            .map(|expiring| QueuedMessage {
                topic: expiring.topic,
                payload: expiring.payload,
                qos: expiring.qos,
                retain: expiring.retain,
                packet_id: expiring.packet_id,
            })
            .collect()
    }

    /// Gets the number of queued messages
    pub async fn queued_message_count(&self) -> usize {
        self.message_queue.read().await.len()
    }

    /// Stores an unacknowledged PUBLISH packet
    ///
    /// # Errors
    ///
    /// Returns an error if the operation fails
    pub async fn store_unacked_publish(&self, packet: PublishPacket) -> Result<()> {
        if let Some(packet_id) = packet.packet_id {
            self.touch().await;
            self.unacked_publishes
                .write()
                .await
                .insert(packet_id, packet);
            Ok(())
        } else {
            Err(MqttError::ProtocolError(
                "PUBLISH packet missing packet ID".to_string(),
            ))
        }
    }

    /// Removes an acknowledged PUBLISH packet
    pub async fn remove_unacked_publish(&self, packet_id: u16) -> Option<PublishPacket> {
        self.touch().await;
        self.unacked_publishes.write().await.remove(&packet_id)
    }

    /// Gets all unacknowledged PUBLISH packets
    pub async fn get_unacked_publishes(&self) -> Vec<PublishPacket> {
        self.unacked_publishes
            .read()
            .await
            .values()
            .cloned()
            .collect()
    }

    /// Stores an unacknowledged PUBREL packet
    pub async fn store_unacked_pubrel(&self, packet_id: u16) {
        self.touch().await;
        self.unacked_pubrels
            .write()
            .await
            .insert(packet_id, Instant::now());
    }

    /// Removes an acknowledged PUBREL packet
    pub async fn remove_unacked_pubrel(&self, packet_id: u16) -> bool {
        self.touch().await;
        self.unacked_pubrels
            .write()
            .await
            .remove(&packet_id)
            .is_some()
    }

    /// Gets all unacknowledged PUBREL packet IDs
    pub async fn get_unacked_pubrels(&self) -> Vec<u16> {
        self.unacked_pubrels.read().await.keys().copied().collect()
    }

    /// Clears all session state
    pub async fn clear(&self) {
        self.subscriptions.write().await.clear();
        self.message_queue.write().await.clear();
        self.unacked_publishes.write().await.clear();
        self.unacked_pubrels.write().await.clear();
        #[cfg(not(target_arch = "wasm32"))]
        self.publish_flows.write().await.clear();
    }

    /// Gets session statistics
    pub async fn stats(&self) -> SessionStats {
        SessionStats {
            subscription_count: self.subscriptions.read().await.count(),
            queued_message_count: self.message_queue.read().await.len(),
            unacked_publish_count: self.unacked_publishes.read().await.len(),
            unacked_pubrel_count: self.unacked_pubrels.read().await.len(),
            uptime: self.created_at.elapsed(),
            last_activity: self.last_activity.read().await.elapsed(),
        }
    }

    /// Sets the receive maximum for flow control
    pub async fn set_receive_maximum(&self, receive_maximum: u16) {
        let mut flow_control = self.flow_control.write().await;
        flow_control.set_receive_maximum(receive_maximum).await;
    }

    /// Sets the topic alias maximum for outgoing messages
    pub async fn set_topic_alias_maximum_out(&self, max: u16) {
        let mut topic_alias = self.topic_alias_out.write().await;
        *topic_alias = TopicAliasManager::new(max);
    }

    /// Sets the topic alias maximum for incoming messages
    pub async fn set_topic_alias_maximum_in(&self, max: u16) {
        let mut topic_alias = self.topic_alias_in.write().await;
        *topic_alias = TopicAliasManager::new(max);
    }

    /// Checks if we can send a `QoS` 1/2 message according to flow control
    pub async fn can_send_qos_message(&self) -> bool {
        self.flow_control.read().await.can_send()
    }

    /// Registers a `QoS` 1/2 message as in-flight for flow control
    ///
    /// # Errors
    ///
    /// Returns an error if the operation fails
    pub async fn register_in_flight(&self, packet_id: u16) -> Result<()> {
        self.flow_control
            .write()
            .await
            .register_send(packet_id)
            .await
    }

    /// Acknowledges a `QoS` 1/2 message for flow control
    ///
    /// # Errors
    ///
    /// Returns an error if the operation fails
    pub async fn acknowledge_in_flight(&self, packet_id: u16) -> Result<()> {
        self.flow_control.write().await.acknowledge(packet_id).await
    }

    #[must_use]
    /// Gets the flow control manager
    pub fn flow_control(&self) -> &Arc<RwLock<FlowControlManager>> {
        &self.flow_control
    }

    #[must_use]
    /// Gets the outgoing topic alias manager
    pub fn topic_alias_out(&self) -> &Arc<RwLock<TopicAliasManager>> {
        &self.topic_alias_out
    }

    #[must_use]
    /// Gets the limits manager
    pub fn limits(&self) -> &Arc<RwLock<LimitsManager>> {
        &self.limits
    }

    /// Sets the server's maximum packet size from CONNACK
    pub async fn set_server_maximum_packet_size(&self, size: u32) {
        let mut limits = self.limits.write().await;
        limits.set_server_maximum_packet_size(size);
    }

    /// Sets the client's maximum packet size from `ConnectOptions`
    pub async fn set_client_maximum_packet_size(&self, size: u32) {
        let mut limits = self.limits.write().await;
        limits.set_client_maximum_packet_size(size);
    }

    /// Checks if a packet size is within limits
    ///
    /// # Errors
    ///
    /// Returns an error if the operation fails
    pub async fn check_packet_size(&self, size: usize) -> Result<()> {
        self.limits.read().await.check_packet_size(size)
    }

    /// Gets the effective maximum packet size
    pub async fn effective_maximum_packet_size(&self) -> u32 {
        self.limits.read().await.effective_maximum_packet_size()
    }

    /// Gets or creates a topic alias for outgoing messages
    pub async fn get_or_create_topic_alias(&self, topic: &str) -> Option<u16> {
        self.topic_alias_out
            .write()
            .await
            .get_or_create_alias(topic)
    }

    /// Registers a topic alias from incoming messages
    ///
    /// # Errors
    ///
    /// Returns an error if the operation fails
    pub async fn register_incoming_topic_alias(&self, alias: u16, topic: &str) -> Result<()> {
        self.topic_alias_in
            .write()
            .await
            .register_alias(alias, topic)
    }

    /// Gets the topic for an incoming alias
    pub async fn get_topic_for_alias(&self, alias: u16) -> Option<String> {
        self.topic_alias_in
            .read()
            .await
            .get_topic(alias)
            .map(String::from)
    }

    /// Removes expired messages from the queue
    pub async fn remove_expired_messages(&self, timeout: crate::time::Duration) {
        self.message_queue.write().await.remove_expired(timeout);
    }

    /// Stores or clears a retained message
    pub async fn store_retained_message(&self, packet: &PublishPacket) {
        let topic = packet.topic_name.clone();

        if packet.payload.is_empty() {
            // Empty payload clears the retained message
            self.retained_messages.store(topic, None).await;
        } else {
            // Store the retained message
            let message = RetainedMessage::from(packet);
            self.retained_messages.store(topic, Some(message)).await;
        }
    }

    /// Gets retained messages matching a topic filter
    pub async fn get_retained_messages(&self, topic_filter: &str) -> Vec<RetainedMessage> {
        self.retained_messages.get_matching(topic_filter).await
    }

    #[must_use]
    /// Gets the retained message store
    pub fn retained_messages(&self) -> &Arc<RetainedMessageStore> {
        &self.retained_messages
    }

    /// Sets the Will message for this session
    pub async fn set_will_message(&self, will: Option<WillMessage>) {
        let mut will_message = self.will_message.write().await;
        *will_message = will;
    }

    /// Gets the Will message for this session
    pub async fn will_message(&self) -> Option<WillMessage> {
        let will_message = self.will_message.read().await;
        will_message.clone()
    }

    /// Triggers Will message publication (called on abnormal disconnection)
    pub async fn trigger_will_message(&self) -> Option<WillMessage> {
        let mut will_message = self.will_message.write().await;
        let will = will_message.take(); // Remove the will message so it's only sent once

        if let Some(ref will) = will {
            // If there's a will delay interval, start the delay timer
            if let Some(delay_seconds) = will.properties.will_delay_interval {
                if delay_seconds > 0 {
                    let delay_handle_clone = Arc::clone(&self.will_delay_handle);

                    // Spawn a task to handle the delay
                    let handle = tokio::spawn(async move {
                        tokio::time::sleep(Duration::from_secs(u64::from(delay_seconds))).await;
                        // The actual will message publication will be handled by the caller
                        // This just implements the delay
                    });

                    let mut delay_handle = delay_handle_clone.write().await;
                    *delay_handle = Some(handle);

                    // Return None to indicate the will should be delayed
                    return None;
                }
            }
        }

        will
    }

    /// Cancels the Will message (called on normal disconnection)
    pub async fn cancel_will_message(&self) {
        // Clear the will message
        let mut will_message = self.will_message.write().await;
        *will_message = None;

        // Cancel any pending will delay timer
        let mut delay_handle = self.will_delay_handle.write().await;
        if let Some(handle) = delay_handle.take() {
            handle.abort();
        }
    }

    /// Checks if the will delay has completed
    pub async fn is_will_delay_complete(&self) -> bool {
        let delay_handle = self.will_delay_handle.read().await;
        if let Some(ref handle) = *delay_handle {
            handle.is_finished()
        } else {
            true // No delay, so it's "complete"
        }
    }

    /// Complete a publish (`QoS` 1 - after PUBACK received)
    pub async fn complete_publish(&self, packet_id: u16) {
        self.touch().await;
        self.unacked_publishes.write().await.remove(&packet_id);
    }

    /// Store PUBREC for `QoS` 2 flow
    pub async fn store_pubrec(&self, packet_id: u16) {
        self.touch().await;
        // For incoming QoS 2 messages, we need to track that we've sent PUBREC
        // and are waiting for PUBREL. Store the packet ID with the current timestamp.
        self.unacked_pubrels
            .write()
            .await
            .insert(packet_id, Instant::now());
    }

    /// Check if we have a stored PUBREC for the given packet ID
    pub async fn has_pubrec(&self, packet_id: u16) -> bool {
        // Check if there's an unacked PUBREL with this packet ID
        // For received QoS 2 messages, this indicates we sent PUBREC and are waiting for PUBREL
        self.unacked_pubrels.read().await.contains_key(&packet_id)
    }

    /// Remove PUBREC state for the given packet ID (called when handling PUBREL)
    pub async fn remove_pubrec(&self, packet_id: u16) {
        self.touch().await;
        // Remove the stored PUBREL state as we're completing the QoS 2 flow
        self.unacked_pubrels.write().await.remove(&packet_id);
    }

    /// Store PUBREL for `QoS` 2 flow
    pub async fn store_pubrel(&self, packet_id: u16) {
        self.touch().await;
        self.unacked_pubrels
            .write()
            .await
            .insert(packet_id, Instant::now());
    }

    /// Complete PUBREC (after sending PUBREL)
    pub async fn complete_pubrec(&self, packet_id: u16) {
        self.touch().await;
        // Remove from unacked publishes as we've moved to PUBREL phase
        self.unacked_publishes.write().await.remove(&packet_id);
    }

    /// Complete PUBREL (after receiving PUBCOMP)
    pub async fn complete_pubrel(&self, packet_id: u16) {
        self.touch().await;
        self.unacked_pubrels.write().await.remove(&packet_id);
    }

    #[cfg(not(target_arch = "wasm32"))]
    #[must_use]
    pub fn flow_registry(&self) -> &Arc<RwLock<FlowRegistry>> {
        &self.flow_registry
    }

    #[cfg(not(target_arch = "wasm32"))]
    pub async fn register_flow(&self, state: FlowState) -> bool {
        self.touch().await;
        self.flow_registry.write().await.register_flow(state)
    }

    #[cfg(not(target_arch = "wasm32"))]
    pub async fn create_client_flow(
        &self,
        flags: FlowFlags,
        expire_interval: Option<std::time::Duration>,
    ) -> Option<FlowId> {
        self.touch().await;
        self.flow_registry
            .write()
            .await
            .new_client_flow(flags, expire_interval)
    }

    #[cfg(not(target_arch = "wasm32"))]
    pub async fn get_flow(&self, id: FlowId) -> Option<FlowState> {
        self.flow_registry.read().await.get(id).cloned()
    }

    #[cfg(not(target_arch = "wasm32"))]
    pub async fn remove_flow(&self, id: FlowId) -> Option<FlowState> {
        self.touch().await;
        self.flow_registry.write().await.remove(id)
    }

    #[cfg(not(target_arch = "wasm32"))]
    pub async fn touch_flow(&self, id: FlowId) {
        self.flow_registry.write().await.touch(id);
    }

    #[cfg(not(target_arch = "wasm32"))]
    pub async fn clear_flows(&self) {
        self.flow_registry.write().await.clear();
    }

    #[cfg(not(target_arch = "wasm32"))]
    pub async fn get_all_flow_ids(&self) -> Vec<FlowId> {
        self.flow_registry
            .read()
            .await
            .iter()
            .map(|(id, _)| *id)
            .collect()
    }

    #[cfg(not(target_arch = "wasm32"))]
    pub async fn get_recoverable_flows(&self) -> Vec<(FlowId, FlowFlags)> {
        self.flow_registry
            .read()
            .await
            .iter()
            .filter(|(_, state)| !state.is_expired())
            .map(|(id, state)| (*id, state.flags))
            .collect()
    }

    #[cfg(not(target_arch = "wasm32"))]
    pub async fn expire_flows(&self) -> Vec<FlowId> {
        self.flow_registry.write().await.expire_flows()
    }

    #[cfg(not(target_arch = "wasm32"))]
    pub async fn flow_count(&self) -> usize {
        self.flow_registry.read().await.len()
    }

    #[cfg(not(target_arch = "wasm32"))]
    pub async fn store_publish_flow(&self, packet_id: u16, flow_id: FlowId) {
        self.touch().await;
        self.publish_flows.write().await.insert(packet_id, flow_id);
    }

    #[cfg(not(target_arch = "wasm32"))]
    pub async fn get_publish_flow(&self, packet_id: u16) -> Option<FlowId> {
        self.publish_flows.read().await.get(&packet_id).copied()
    }

    #[cfg(not(target_arch = "wasm32"))]
    pub async fn remove_publish_flow(&self, packet_id: u16) -> Option<FlowId> {
        self.touch().await;
        self.publish_flows.write().await.remove(&packet_id)
    }

    #[cfg(not(target_arch = "wasm32"))]
    pub async fn clear_publish_flows(&self) {
        self.publish_flows.write().await.clear();
    }
}

/// Session statistics
#[derive(Debug, Clone)]
pub struct SessionStats {
    /// Number of active subscriptions
    pub subscription_count: usize,
    /// Number of queued messages
    pub queued_message_count: usize,
    /// Number of unacknowledged PUBLISH packets
    pub unacked_publish_count: usize,
    /// Number of unacknowledged PUBREL packets
    pub unacked_pubrel_count: usize,
    /// Session uptime
    pub uptime: Duration,
    /// Time since last activity
    pub last_activity: Duration,
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::packet::subscribe::SubscriptionOptions;
    use crate::types::{WillMessage, WillProperties};
    use crate::{Properties, QoS};

    #[tokio::test]
    async fn test_session_creation() {
        let config = SessionConfig::default();
        let session = SessionState::new("test-client".to_string(), config, true);

        assert_eq!(session.client_id(), "test-client");
        assert!(session.is_clean());
        assert!(!session.is_expired().await);
    }

    #[tokio::test]
    async fn test_session_expiry() {
        let config = SessionConfig {
            session_expiry_interval: 1, // 1 second
            ..Default::default()
        };
        let session = SessionState::new("test-client".to_string(), config, false);

        // Initially not expired
        assert!(!session.is_expired().await);

        // Update last activity to past
        *session.last_activity.write().await =
            Instant::now().checked_sub(Duration::from_secs(2)).unwrap();

        // Now should be expired
        assert!(session.is_expired().await);
    }

    #[tokio::test]
    async fn test_subscription_management() {
        let session = SessionState::new("test-client".to_string(), SessionConfig::default(), true);

        let sub = Subscription {
            topic_filter: "test/topic".to_string(),
            options: SubscriptionOptions::default(),
        };

        // Add subscription
        session
            .add_subscription("test/topic".to_string(), sub.clone())
            .await
            .unwrap();

        // Check matching
        let matches = session.matching_subscriptions("test/topic").await;
        assert_eq!(matches.len(), 1);
        assert_eq!(matches[0].0, "test/topic");

        // Remove subscription
        session.remove_subscription("test/topic").await.unwrap();
        let matches = session.matching_subscriptions("test/topic").await;
        assert_eq!(matches.len(), 0);
    }

    #[tokio::test]
    async fn test_message_queueing() {
        let session = SessionState::new("test-client".to_string(), SessionConfig::default(), true);

        // Queue messages
        let msg1 = QueuedMessage {
            topic: "test/1".to_string(),
            payload: vec![1, 2, 3],
            qos: QoS::AtLeastOnce,
            retain: false,
            packet_id: Some(1),
        };

        let msg2 = QueuedMessage {
            topic: "test/2".to_string(),
            payload: vec![4, 5, 6],
            qos: QoS::AtMostOnce,
            retain: false,
            packet_id: None,
        };

        session.queue_message(msg1).await.unwrap();
        session.queue_message(msg2).await.unwrap();

        assert_eq!(session.queued_message_count().await, 2);

        // Dequeue messages
        let messages = session.dequeue_messages(1).await;
        assert_eq!(messages.len(), 1);
        assert_eq!(session.queued_message_count().await, 1);
    }

    #[tokio::test]
    async fn test_unacked_publish_tracking() {
        let session = SessionState::new("test-client".to_string(), SessionConfig::default(), true);

        let packet = PublishPacket {
            topic_name: "test/topic".to_string(),
            packet_id: Some(123),
            payload: vec![1, 2, 3].into(),
            qos: QoS::AtLeastOnce,
            retain: false,
            dup: false,
            properties: Properties::default(),
            protocol_version: 5,
            stream_id: None,
        };

        session.store_unacked_publish(packet.clone()).await.unwrap();

        let unacked = session.get_unacked_publishes().await;
        assert_eq!(unacked.len(), 1);
        assert_eq!(unacked[0].packet_id, Some(123));

        // Remove acknowledged publish
        let removed = session.remove_unacked_publish(123).await;
        assert!(removed.is_some());
        assert_eq!(session.get_unacked_publishes().await.len(), 0);
    }

    #[tokio::test]
    async fn test_unacked_pubrel_tracking() {
        let session = SessionState::new("test-client".to_string(), SessionConfig::default(), true);

        // Store unacked pubrels
        session.store_unacked_pubrel(100).await;
        session.store_unacked_pubrel(101).await;

        let pubrels = session.get_unacked_pubrels().await;
        assert_eq!(pubrels.len(), 2);
        assert!(pubrels.contains(&100));
        assert!(pubrels.contains(&101));

        // Remove acknowledged pubrel
        assert!(session.remove_unacked_pubrel(100).await);
        assert_eq!(session.get_unacked_pubrels().await.len(), 1);
    }

    #[tokio::test]
    async fn test_session_clear() {
        let session = SessionState::new("test-client".to_string(), SessionConfig::default(), true);

        // Add some data
        let sub = Subscription {
            topic_filter: "test/#".to_string(),
            options: SubscriptionOptions::default(),
        };
        session
            .add_subscription("test/#".to_string(), sub)
            .await
            .unwrap();

        let msg = QueuedMessage {
            topic: "test".to_string(),
            payload: vec![1],
            qos: QoS::AtMostOnce,
            retain: false,
            packet_id: None,
        };
        session.queue_message(msg).await.unwrap();

        session.store_unacked_pubrel(1).await;

        // Clear session
        session.clear().await;

        // Verify everything is cleared
        assert_eq!(session.all_subscriptions().await.len(), 0);
        assert_eq!(session.queued_message_count().await, 0);
        assert_eq!(session.get_unacked_pubrels().await.len(), 0);
    }

    #[tokio::test]
    async fn test_session_stats() {
        let session = SessionState::new("test-client".to_string(), SessionConfig::default(), true);

        // Add some data
        let sub = Subscription {
            topic_filter: "test/#".to_string(),
            options: SubscriptionOptions::default(),
        };
        session
            .add_subscription("test/#".to_string(), sub)
            .await
            .unwrap();

        let stats = session.stats().await;
        assert_eq!(stats.subscription_count, 1);
        assert_eq!(stats.queued_message_count, 0);
        // Uptime might be 0 on very fast systems, so just check it exists
        let _ = stats.uptime.as_nanos();
    }

    #[tokio::test]
    async fn test_flow_control_integration() {
        let session = SessionState::new("test-client".to_string(), SessionConfig::default(), true);

        // Set receive maximum
        session.set_receive_maximum(2).await;

        // Should be able to send initially
        assert!(session.can_send_qos_message().await);

        // Register in-flight messages
        session.register_in_flight(1).await.unwrap();
        session.register_in_flight(2).await.unwrap();

        // Should not be able to send more
        assert!(!session.can_send_qos_message().await);

        // Try to register another
        assert!(session.register_in_flight(3).await.is_err());

        // Acknowledge one
        session.acknowledge_in_flight(1).await.unwrap();

        // Should be able to send again
        assert!(session.can_send_qos_message().await);
    }

    #[tokio::test]
    async fn test_topic_alias_integration() {
        let session = SessionState::new("test-client".to_string(), SessionConfig::default(), true);

        // Set topic alias maximum
        session.set_topic_alias_maximum_out(10).await;
        session.set_topic_alias_maximum_in(10).await;

        // Get or create alias for outgoing
        let alias1 = session.get_or_create_topic_alias("topic/1").await;
        assert_eq!(alias1, Some(1));

        // Same topic should get same alias
        let alias1_again = session.get_or_create_topic_alias("topic/1").await;
        assert_eq!(alias1_again, Some(1));

        // Register incoming alias
        session
            .register_incoming_topic_alias(5, "incoming/topic")
            .await
            .unwrap();

        // Get topic for alias
        let topic = session.get_topic_for_alias(5).await;
        assert_eq!(topic, Some("incoming/topic".to_string()));
    }

    #[tokio::test]
    async fn test_session_expiry_zero_interval() {
        let config = SessionConfig {
            session_expiry_interval: 0, // Session doesn't expire
            ..Default::default()
        };
        let session = SessionState::new("test-client".to_string(), config, false);

        // Update last activity to past
        *session.last_activity.write().await = Instant::now()
            .checked_sub(Duration::from_secs(100))
            .unwrap();

        // Should not be expired
        assert!(!session.is_expired().await);
    }

    #[tokio::test]
    async fn test_wildcard_subscriptions() {
        let session = SessionState::new("test-client".to_string(), SessionConfig::default(), true);

        let sub1 = Subscription {
            topic_filter: "test/+/topic".to_string(),
            options: SubscriptionOptions::default(),
        };

        let sub2 = Subscription {
            topic_filter: "test/#".to_string(),
            options: SubscriptionOptions::default(),
        };

        session
            .add_subscription("test/+/topic".to_string(), sub1)
            .await
            .unwrap();
        session
            .add_subscription("test/#".to_string(), sub2)
            .await
            .unwrap();

        // Check matching
        let matches = session.matching_subscriptions("test/foo/topic").await;
        assert_eq!(matches.len(), 2);

        let all_subs = session.all_subscriptions().await;
        assert_eq!(all_subs.len(), 2);
    }

    #[tokio::test]
    async fn test_message_queue_limits() {
        let config = SessionConfig {
            max_queued_messages: 2,
            max_queued_size: 100,
            ..Default::default()
        };

        let session = SessionState::new("test-client".to_string(), config, true);

        // Queue messages up to limit
        let msg1 = QueuedMessage {
            topic: "test/1".to_string(),
            payload: vec![0; 40],
            qos: QoS::AtLeastOnce,
            retain: false,
            packet_id: Some(1),
        };

        let msg2 = QueuedMessage {
            topic: "test/2".to_string(),
            payload: vec![0; 40],
            qos: QoS::AtLeastOnce,
            retain: false,
            packet_id: Some(2),
        };

        let msg3 = QueuedMessage {
            topic: "test/3".to_string(),
            payload: vec![0; 40],
            qos: QoS::AtLeastOnce,
            retain: false,
            packet_id: Some(3),
        };

        session.queue_message(msg1).await.unwrap();
        session.queue_message(msg2).await.unwrap();

        // Third message should succeed but drop the oldest message
        session.queue_message(msg3).await.unwrap();

        // Should still have 2 messages
        assert_eq!(session.queued_message_count().await, 2);

        // Dequeue all and verify oldest was dropped
        let messages = session.dequeue_messages(3).await;
        assert_eq!(messages.len(), 2);
        assert_eq!(messages[0].topic, "test/2");
        assert_eq!(messages[1].topic, "test/3");
    }

    #[tokio::test]
    async fn test_unacked_publish_no_packet_id() {
        let session = SessionState::new("test-client".to_string(), SessionConfig::default(), true);

        let packet = PublishPacket {
            topic_name: "test/topic".to_string(),
            packet_id: None,
            payload: vec![1, 2, 3].into(),
            qos: QoS::AtMostOnce,
            retain: false,
            dup: false,
            properties: Properties::default(),
            protocol_version: 5,
            stream_id: None,
        };

        assert!(session.store_unacked_publish(packet).await.is_err());
    }

    #[tokio::test]
    async fn test_qos2_flow() {
        let session = SessionState::new("test-client".to_string(), SessionConfig::default(), true);

        let packet = PublishPacket {
            topic_name: "test/topic".to_string(),
            packet_id: Some(123),
            payload: vec![1, 2, 3].into(),
            qos: QoS::ExactlyOnce,
            retain: false,
            dup: false,
            properties: Properties::default(),
            protocol_version: 5,
            stream_id: None,
        };

        session.store_unacked_publish(packet).await.unwrap();

        session.store_pubrec(123).await;
        assert_eq!(session.get_unacked_publishes().await.len(), 1);

        // Complete PUBREC (removes publish, adds to pubrel)
        session.complete_pubrec(123).await;
        session.store_pubrel(123).await;
        assert_eq!(session.get_unacked_publishes().await.len(), 0);
        assert_eq!(session.get_unacked_pubrels().await.len(), 1);

        // Complete PUBREL
        session.complete_pubrel(123).await;
        assert_eq!(session.get_unacked_pubrels().await.len(), 0);
    }

    #[tokio::test]
    async fn test_packet_size_limits() {
        let session = SessionState::new("test-client".to_string(), SessionConfig::default(), true);

        // Set server maximum packet size
        session.set_server_maximum_packet_size(1000).await;

        // Check packet size within limit
        assert!(session.check_packet_size(500).await.is_ok());

        // Check packet size exceeds limit
        assert!(session.check_packet_size(1001).await.is_err());

        // Get effective maximum
        assert_eq!(session.effective_maximum_packet_size().await, 1000);
    }

    #[tokio::test]
    async fn test_retained_messages() {
        let session = SessionState::new("test-client".to_string(), SessionConfig::default(), true);

        let packet1 = PublishPacket {
            topic_name: "test/retained".to_string(),
            packet_id: None,
            payload: vec![1, 2, 3].into(),
            qos: QoS::AtMostOnce,
            retain: true,
            dup: false,
            properties: Properties::default(),
            protocol_version: 5,
            stream_id: None,
        };

        session.store_retained_message(&packet1).await;

        // Get retained messages
        let retained = session.get_retained_messages("test/retained").await;
        assert_eq!(retained.len(), 1);
        assert_eq!(retained[0].payload, vec![1, 2, 3]);

        let packet2 = PublishPacket {
            topic_name: "test/retained".to_string(),
            packet_id: None,
            payload: vec![].into(),
            qos: QoS::AtMostOnce,
            retain: true,
            dup: false,
            properties: Properties::default(),
            protocol_version: 5,
            stream_id: None,
        };

        session.store_retained_message(&packet2).await;

        // Should be cleared
        let retained = session.get_retained_messages("test/retained").await;
        assert_eq!(retained.len(), 0);
    }

    #[tokio::test]
    async fn test_retained_message_wildcard_matching() {
        let session = SessionState::new("test-client".to_string(), SessionConfig::default(), true);

        let packet1 = PublishPacket {
            topic_name: "test/device1/status".to_string(),
            packet_id: None,
            payload: b"online".to_vec().into(),
            qos: QoS::AtMostOnce,
            retain: true,
            dup: false,
            properties: Properties::default(),
            protocol_version: 5,
            stream_id: None,
        };

        let packet2 = PublishPacket {
            topic_name: "test/device2/status".to_string(),
            packet_id: None,
            payload: b"offline".to_vec().into(),
            qos: QoS::AtMostOnce,
            retain: true,
            dup: false,
            properties: Properties::default(),
            protocol_version: 5,
            stream_id: None,
        };

        session.store_retained_message(&packet1).await;
        session.store_retained_message(&packet2).await;

        // Get with wildcard
        let retained = session.get_retained_messages("test/+/status").await;
        assert_eq!(retained.len(), 2);
    }

    #[tokio::test]
    async fn test_will_message() {
        let session = SessionState::new("test-client".to_string(), SessionConfig::default(), true);

        // Set will message
        let will = WillMessage {
            topic: "test/will".to_string(),
            payload: b"disconnected".to_vec(),
            qos: QoS::AtLeastOnce,
            retain: false,
            properties: WillProperties::default(),
        };

        session.set_will_message(Some(will.clone())).await;

        // Get will message
        let stored_will = session.will_message().await;
        assert!(stored_will.is_some());
        assert_eq!(stored_will.unwrap().topic, "test/will");

        // Trigger will message (abnormal disconnection)
        let triggered = session.trigger_will_message().await;
        assert!(triggered.is_some());

        // Will should be cleared after triggering
        assert!(session.will_message().await.is_none());
    }

    #[tokio::test]
    async fn test_will_message_cancellation() {
        let session = SessionState::new("test-client".to_string(), SessionConfig::default(), true);

        // Set will message
        let will = WillMessage {
            topic: "test/will".to_string(),
            payload: b"disconnected".to_vec(),
            qos: QoS::AtLeastOnce,
            retain: false,
            properties: WillProperties::default(),
        };

        session.set_will_message(Some(will)).await;

        // Cancel will message (normal disconnection)
        session.cancel_will_message().await;

        // Will should be cleared
        assert!(session.will_message().await.is_none());
    }

    #[tokio::test]
    async fn test_will_delay() {
        let session = SessionState::new("test-client".to_string(), SessionConfig::default(), true);

        // Set will message with delay
        let will_props = WillProperties {
            will_delay_interval: Some(1), // 1 second delay
            ..Default::default()
        };

        let will = WillMessage {
            topic: "test/will".to_string(),
            payload: b"disconnected".to_vec(),
            qos: QoS::AtLeastOnce,
            retain: false,
            properties: will_props,
        };

        session.set_will_message(Some(will)).await;

        // Trigger will with delay
        let triggered = session.trigger_will_message().await;
        assert!(triggered.is_none()); // Should return None due to delay

        // Check delay is not complete yet
        assert!(!session.is_will_delay_complete().await);

        // Wait for delay
        tokio::time::sleep(Duration::from_millis(1100)).await;

        // Now delay should be complete
        assert!(session.is_will_delay_complete().await);
    }

    #[tokio::test]
    async fn test_touch_updates_activity() {
        let session = SessionState::new("test-client".to_string(), SessionConfig::default(), true);

        let initial_activity = *session.last_activity.read().await;

        // Wait a bit
        tokio::time::sleep(Duration::from_millis(10)).await;

        // Touch should update activity
        session.touch().await;

        let new_activity = *session.last_activity.read().await;
        assert!(new_activity > initial_activity);
    }

    #[tokio::test]
    async fn test_activity_tracking_on_operations() {
        let session = SessionState::new("test-client".to_string(), SessionConfig::default(), true);

        let initial_activity = *session.last_activity.read().await;
        tokio::time::sleep(Duration::from_millis(10)).await;

        // Various operations should update activity
        let sub = Subscription {
            topic_filter: "test".to_string(),
            options: SubscriptionOptions::default(),
        };
        session
            .add_subscription("test".to_string(), sub)
            .await
            .unwrap();

        let activity_after_sub = *session.last_activity.read().await;
        assert!(activity_after_sub > initial_activity);
    }

    #[tokio::test]
    async fn test_complete_publish_flow() {
        let session = SessionState::new("test-client".to_string(), SessionConfig::default(), true);

        let packet = PublishPacket {
            topic_name: "test/topic".to_string(),
            packet_id: Some(100),
            payload: vec![1, 2, 3].into(),
            qos: QoS::AtLeastOnce,
            retain: false,
            dup: false,
            properties: Properties::default(),
            protocol_version: 5,
            stream_id: None,
        };

        session.store_unacked_publish(packet).await.unwrap();
        assert_eq!(session.get_unacked_publishes().await.len(), 1);

        session.complete_publish(100).await;
        assert_eq!(session.get_unacked_publishes().await.len(), 0);
    }
}