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
/
mqttGuardInterface.cpp
52 lines (46 loc) · 1.85 KB
/
mqttGuardInterface.cpp
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
#include "mqttGuardInterface.hpp"
MqttGuardInterface::MqttGuardInterface(AsyncMqttClient& mqttClient)
: mqttClient_(mqttClient)
, mqttGuard_(std::make_shared<MqttGuard>())
{
// Default callbacks in case the user does not set one so internally everything is kept fine.
mqttOnDisconnect([](AsyncMqttClientDisconnectReason /*reason*/){});
mqttOnPublish([](uint16_t /*packetId*/) {});
}
uint16_t MqttGuardInterface::mqttPublish(const char* topic, uint8_t qos, bool retain,
const char* payload, size_t length, bool dup, uint16_t message_id)
{
mqttGuard_->registerPacket(mqttClient_.publish(topic, qos, retain, payload, length, dup, message_id));
}
AsyncMqttClient& MqttGuardInterface::mqttOnPublish(AsyncMqttClientInternals::OnPublishUserCallback callback)
{
// Intercept the onPublish and make sure that mqttGuard is held alive until the callback is fired.
auto mqttGuard = mqttGuard_;
return mqttClient_.onPublish([mqttGuard, callback](uint16_t packetId){
mqttGuard->unregisterPacket(packetId);
if (callback) {
callback(packetId);
}
});
}
AsyncMqttClient& MqttGuardInterface::mqttOnDisconnect(AsyncMqttClientInternals::OnDisconnectUserCallback callback)
{
// Intercept the onDisconnect and make sure that mqttGuard is held alive until the callback is fired.
auto mqttGuard = mqttGuard_;
return mqttClient_.onDisconnect([mqttGuard, callback]
(AsyncMqttClientDisconnectReason reason){
// Make sure that disconnet respects that there could be no more packets be sent.
mqttGuard->reset();
if (callback) {
callback(reason);
}
});
}
bool MqttGuardInterface::mqttAllSent() const
{
return mqttGuard_->allSent();
}
size_t MqttGuardInterface::mqttRemainingPackets() const
{
return mqttGuard_->remainingPacketCount();
}