SNode.C
Loading...
Searching...
No Matches
Mqtt.h
Go to the documentation of this file.
1/*
2 * SNode.C - A Slim Toolkit for Network Communication
3 * Copyright (C) Volker Christian <me@vchrist.at>
4 * 2020, 2021, 2022, 2023, 2024, 2025
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published
8 * by the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20/*
21 * MIT License
22 *
23 * Permission is hereby granted, free of charge, to any person obtaining a copy
24 * of this software and associated documentation files (the "Software"), to deal
25 * in the Software without restriction, including without limitation the rights
26 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
27 * copies of the Software, and to permit persons to whom the Software is
28 * furnished to do so, subject to the following conditions:
29 *
30 * The above copyright notice and this permission notice shall be included in
31 * all copies or substantial portions of the Software.
32 *
33 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
39 * THE SOFTWARE.
40 */
41
42#ifndef IOT_MQTT_SERVER_SOCKETCONTEXT_H
43#define IOT_MQTT_SERVER_SOCKETCONTEXT_H
44
45#include "iot/mqtt/Mqtt.h" // IWYU pragma: export
46
47namespace iot::mqtt {
48 namespace packets {
49 class Connect;
50 class Disconnect;
51 class Pingreq;
52 class Subscribe;
53 class Unsubscribe;
54 } // namespace packets
55
56 namespace server {
57 namespace broker {
58 class Broker;
59 } // namespace broker
60
61 namespace packets {
62 class Connect;
63 class Disconnect;
64 class Pingreq;
65 class Publish;
66 class Puback;
67 class Pubcomp;
68 class Pubrec;
69 class Pubrel;
70 class Subscribe;
71 class Unsubscribe;
72 } // namespace packets
73 } // namespace server
74} // namespace iot::mqtt
75
76#ifndef DOXYGEN_SHOULD_SKIP_THIS
77
78#include <cstdint>
79#include <list>
80#include <memory>
81#include <string>
82
83#endif // DOXYGEN_SHOULD_SKIP_THIS
84
85namespace iot::mqtt::server {
86
87 class Mqtt : public iot::mqtt::Mqtt {
88 private:
89 using Super = iot::mqtt::Mqtt;
90
91 public:
92 explicit Mqtt(const std::string& connectionName, const std::shared_ptr<broker::Broker>& broker);
93
94 ~Mqtt() override;
95
96 protected:
97 bool onSignal(int sig) override;
98
99 private:
100 ControlPacketDeserializer* createControlPacketDeserializer(iot::mqtt::FixedHeader& fixedHeader) const final;
101 void deliverPacket(iot::mqtt::ControlPacketDeserializer* controlPacketDeserializer) override;
102
103 using Super::initSession;
104 bool initSession(const utils::Timeval& keepAlive);
105 void releaseSession();
106
107 virtual void onConnect(const iot::mqtt::packets::Connect& connect);
108 virtual void onSubscribe(const iot::mqtt::packets::Subscribe& subscribe);
109 virtual void onUnsubscribe(const iot::mqtt::packets::Unsubscribe& unsubscribe);
110 virtual void onPingreq(const iot::mqtt::packets::Pingreq& pingreq);
111 virtual void onDisconnect(const iot::mqtt::packets::Disconnect& disconnect);
112
113 void _onConnect(const iot::mqtt::server::packets::Connect& connect);
114 void _onPublish(const iot::mqtt::server::packets::Publish& publish);
115 void _onSubscribe(const iot::mqtt::server::packets::Subscribe& subscribe);
116 void _onUnsubscribe(const iot::mqtt::server::packets::Unsubscribe& unsubscribe);
117 void _onPingreq(const iot::mqtt::server::packets::Pingreq& pingreq);
118 void _onDisconnect(const iot::mqtt::server::packets::Disconnect& disconnect);
119
120 void deliverPublish(const iot::mqtt::packets::Publish& publish) final;
121
122 public:
123 void sendConnack(uint8_t returnCode, uint8_t flags) const;
124 void sendSuback(uint16_t packetIdentifier, const std::list<uint8_t>& returnCodes) const;
125 void sendUnsuback(uint16_t packetIdentifier) const;
126 void sendPingresp() const;
127
128 std::list<std::string> getSubscriptions() const;
129
130 std::string getProtocol() const;
131 uint8_t getLevel() const;
132 uint8_t getConnectFlags() const;
133 uint16_t getKeepAlive() const;
134 std::string getClientId() const;
135 std::string getWillTopic() const;
136 std::string getWillMessage() const;
137 std::string getUsername() const;
138 std::string getPassword() const;
139 bool getUsernameFlag() const;
140 bool getPasswordFlag() const;
141 bool getWillRetain() const;
142 uint8_t getWillQoS() const;
143 bool getWillFlag() const;
144 bool getCleanSession() const;
145 bool getReflect() const;
146
147 protected:
148 std::string protocol;
149 uint8_t level = 0;
150 uint8_t connectFlags = 0;
151 uint16_t keepAlive = 0;
152
153 std::string willTopic;
154 std::string willMessage;
155 std::string username;
156 std::string password;
157
158 bool usernameFlag = false;
159 bool passwordFlag = false;
160 bool willRetain = false;
161 uint8_t willQoS = 0;
162 bool willFlag = false;
163 bool cleanSession = false;
164 bool reflect = true;
165
166 std::shared_ptr<iot::mqtt::server::broker::Broker> broker;
167
168 friend class iot::mqtt::server::packets::Connect;
169 friend class iot::mqtt::server::packets::Subscribe;
170 friend class iot::mqtt::server::packets::Unsubscribe;
171 friend class iot::mqtt::server::packets::Pingreq;
172 friend class iot::mqtt::server::packets::Disconnect;
173 friend class iot::mqtt::server::packets::Publish;
174 friend class iot::mqtt::server::packets::Pubcomp;
175 friend class iot::mqtt::server::packets::Pubrec;
176 friend class iot::mqtt::server::packets::Puback;
177 friend class iot::mqtt::server::packets::Pubrel;
178 };
179
180} // namespace iot::mqtt::server
181
182#endif // IOT_MQTT_SERVER_SOCKETCONTEXT_H
void printVP(const iot::mqtt::ControlPacket &packet) const
Definition Mqtt.cpp:400
void _onPuback(const iot::mqtt::packets::Puback &puback)
Definition Mqtt.cpp:323
iot::mqtt::types::UInt16 packetIdentifier
Definition Puback.h:68
virtual void deliverPacket(iot::mqtt::server::Mqtt *mqtt)=0
bool getReflect() const
Definition Mqtt.cpp:444
virtual void onConnect(const iot::mqtt::packets::Connect &connect)
Definition Mqtt.cpp:198
ControlPacketDeserializer * createControlPacketDeserializer(iot::mqtt::FixedHeader &fixedHeader) const final
Definition Mqtt.cpp:91
uint8_t getLevel() const
Definition Mqtt.cpp:388
bool getPasswordFlag() const
Definition Mqtt.cpp:424
void sendSuback(uint16_t packetIdentifier, const std::list< uint8_t > &returnCodes) const
Definition Mqtt.cpp:368
bool getCleanSession() const
Definition Mqtt.cpp:440
std::string password
Definition Mqtt.h:156
bool onSignal(int sig) override
Definition Mqtt.cpp:86
uint8_t getConnectFlags() const
Definition Mqtt.cpp:392
virtual void onSubscribe(const iot::mqtt::packets::Subscribe &subscribe)
Definition Mqtt.cpp:201
uint16_t getKeepAlive() const
Definition Mqtt.cpp:396
void _onPingreq(const iot::mqtt::server::packets::Pingreq &pingreq)
Definition Mqtt.cpp:344
virtual void onPingreq(const iot::mqtt::packets::Pingreq &pingreq)
Definition Mqtt.cpp:207
bool getWillRetain() const
Definition Mqtt.cpp:428
std::string getProtocol() const
Definition Mqtt.cpp:384
void _onUnsubscribe(const iot::mqtt::server::packets::Unsubscribe &unsubscribe)
Definition Mqtt.cpp:322
bool getUsernameFlag() const
Definition Mqtt.cpp:420
virtual void onDisconnect(const iot::mqtt::packets::Disconnect &disconnect)
Definition Mqtt.cpp:210
void _onPublish(const iot::mqtt::server::packets::Publish &publish)
Definition Mqtt.cpp:291
void deliverPacket(iot::mqtt::ControlPacketDeserializer *controlPacketDeserializer) override
Definition Mqtt.cpp:143
void _onSubscribe(const iot::mqtt::server::packets::Subscribe &subscribe)
Definition Mqtt.cpp:297
bool initSession(const utils::Timeval &keepAlive)
Definition Mqtt.cpp:147
std::list< std::string > getSubscriptions() const
Definition Mqtt.cpp:380
iot::mqtt::Mqtt Super
Definition Mqtt.h:89
std::string getWillMessage() const
Definition Mqtt.cpp:408
void _onConnect(const iot::mqtt::server::packets::Connect &connect)
Definition Mqtt.cpp:213
void sendPingresp() const
Definition Mqtt.cpp:376
std::string willMessage
Definition Mqtt.h:154
void sendConnack(uint8_t returnCode, uint8_t flags) const
Definition Mqtt.cpp:364
std::string username
Definition Mqtt.h:155
std::string protocol
Definition Mqtt.h:148
std::string getClientId() const
Definition Mqtt.cpp:400
uint8_t getWillQoS() const
Definition Mqtt.cpp:432
virtual void onUnsubscribe(const iot::mqtt::packets::Unsubscribe &unsubscribe)
Definition Mqtt.cpp:204
std::string getWillTopic() const
Definition Mqtt.cpp:404
bool getWillFlag() const
Definition Mqtt.cpp:436
std::string willTopic
Definition Mqtt.h:153
void sendUnsuback(uint16_t packetIdentifier) const
Definition Mqtt.cpp:372
void _onDisconnect(const iot::mqtt::server::packets::Disconnect &disconnect)
Definition Mqtt.cpp:350
Mqtt(const std::string &connectionName, const std::shared_ptr< broker::Broker > &broker)
Definition Mqtt.cpp:73
std::string getUsername() const
Definition Mqtt.cpp:412
std::string getPassword() const
Definition Mqtt.cpp:416
void deliverPublish(const iot::mqtt::packets::Publish &publish) final
Definition Mqtt.cpp:358
std::size_t deserializeVP(iot::mqtt::MqttContext *mqttContext) override
Definition Puback.cpp:57
void deliverPacket(iot::mqtt::server::Mqtt *mqtt) override
Definition Puback.cpp:67
Puback(uint32_t remainingLength, uint8_t flags)
Definition Puback.cpp:52
virtual std::size_t deserialize(iot::mqtt::MqttContext *mqttContext)
Definition TypeBase.hpp:59
#define MQTT_PUBACK_FLAGS
Definition Config.h:59