This repository has been archived by the owner on Jan 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 48
/
mqttGuard.hpp
70 lines (57 loc) · 2.05 KB
/
mqttGuard.hpp
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
#ifndef BASECAMP_MQTT_GUARD_HPP
#define BASECAMP_MQTT_GUARD_HPP
#include "log.hpp"
#include <functional>
#include <vector>
/**
Helper to guard outgoing mqtt packets.
On packet sending, register the packets within this class, unregister them within the onPublish and
pull the allSent() function to see if everything has been sent completely.
*/
class MqttGuard
{
public:
/// Typesafety forward of AsyncMqttClients packet_id type
using IdType = uint16_t;
/**
Construct a new guard.
@param LogCallback Optional log callback.
*/
explicit MqttGuard(basecampLog::LogCallback logCallback = {});
~MqttGuard() = default;
/**
Register a new packet that is going to sent by mqtt.
@param packetId Packet-ID returned by AsyncMqttClient::publish()
*/
void registerPacket(IdType packetId);
/**
Unregister a packet after it has been sent successfully.
@param packetId Packet-ID returned by AsyncMqttClient::onPublish()
*/
void unregisterPacket(IdType packetId);
/// Returns the amount of packets waiting to be sent.
size_t remainingPacketCount() const;
/// Returns true if all packets have been sent.
bool allSent() const;
/**
Reset to empty state (force).
Maybe necessary if getting disconnected.
*/
void reset();
private:
/**
Check a packetId for validity.
@param packetId Packet-ID to be checked.
@return True if the packetId can be safely added.
*/
bool isValidPacketId(IdType packetId) const;
/// Try to log message if logCallback_ has been set in ctor.
void tryLog(basecampLog::Severity severity, const std::string& message);
/// Try to log message with packetId if logCallback_ has been set in ctor.
void tryLog(basecampLog::Severity severity, const std::string& message, IdType packetId);
/// Optional callback for log-messages
basecampLog::LogCallback logCallback_;
/// List of all remaining packets
std::vector<IdType> packets_;
};
#endif // #define BASECAMP_MQTT_GUARD_HPP