SNode.C
Loading...
Searching...
No Matches
SocketContext.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_MQTTFAST_SOCKETCONTEXT_H
43#define IOT_MQTTFAST_SOCKETCONTEXT_H
44
45#include "core/socket/stream/SocketContext.h" // IWYU pragma: export
46#include "iot/mqtt-fast/ControlPacketFactory.h"
47#include "iot/mqtt-fast/Topic.h" // IWYU pragma: export
48#include "iot/mqtt-fast/packets/Connack.h" // IWYU pragma: export
49#include "iot/mqtt-fast/packets/Connect.h" // IWYU pragma: export
50#include "iot/mqtt-fast/packets/Disconnect.h" // IWYU pragma: export
51#include "iot/mqtt-fast/packets/Pingreq.h" // IWYU pragma: export
52#include "iot/mqtt-fast/packets/Pingresp.h" // IWYU pragma: export
53#include "iot/mqtt-fast/packets/Puback.h" // IWYU pragma: export
54#include "iot/mqtt-fast/packets/Pubcomp.h" // IWYU pragma: export
55#include "iot/mqtt-fast/packets/Publish.h" // IWYU pragma: export
56#include "iot/mqtt-fast/packets/Pubrec.h" // IWYU pragma: export
57#include "iot/mqtt-fast/packets/Pubrel.h" // IWYU pragma: export
58#include "iot/mqtt-fast/packets/Suback.h" // IWYU pragma: export
59#include "iot/mqtt-fast/packets/Subscribe.h" // IWYU pragma: export
60#include "iot/mqtt-fast/packets/Unsuback.h" // IWYU pragma: export
61#include "iot/mqtt-fast/packets/Unsubscribe.h" // IWYU pragma: export
62
63namespace core::socket::stream {
64 class SocketConnection;
65}
66
67namespace iot::mqtt_fast {
68 class ControlPacket;
69}
70
71#ifndef DOXYGEN_SHOULD_SKIP_THIS
72
73#include <cstddef>
74#include <cstdint>
75#include <list>
76#include <string>
77#include <vector>
78
79#endif // DOXYGEN_SHOULD_SKIP_THIS
80
81namespace iot::mqtt_fast {
82
83 class SocketContext : public core::socket::stream::SocketContext {
84 public:
85 explicit SocketContext(core::socket::stream::SocketConnection* socketConnection);
86
87 void sendConnect(const std::string& clientId);
88 void sendConnack(uint8_t returnCode, uint8_t flags);
89 void sendPublish(const std::string& topic, const std::string& message, bool dup = false, uint8_t qoS = 0, bool retain = false);
90 void sendPuback(uint16_t packetIdentifier);
91 void sendPubrec(uint16_t packetIdentifier);
92 void sendPubrel(uint16_t packetIdentifier);
93 void sendPubcomp(uint16_t packetIdentifier);
94 void sendSubscribe(std::list<Topic>& topics);
95 void sendSuback(uint16_t packetIdentifier, const std::list<uint8_t>& returnCodes);
96 void sendUnsubscribe(const std::list<std::string>& topics);
97 void sendUnsuback(uint16_t packetIdentifier);
98 void sendPingreq();
99 void sendPingresp();
100 void sendDisconnect();
101
102 private:
103 virtual void onConnect(const iot::mqtt_fast::packets::Connect& connect) = 0;
104 virtual void onConnack(const iot::mqtt_fast::packets::Connack& connack) = 0;
105 virtual void onPublish(const iot::mqtt_fast::packets::Publish& publish) = 0;
106 virtual void onPuback(const iot::mqtt_fast::packets::Puback& puback) = 0;
107 virtual void onPubrec(const iot::mqtt_fast::packets::Pubrec& pubrec) = 0;
108 virtual void onPubrel(const iot::mqtt_fast::packets::Pubrel& pubrel) = 0;
109 virtual void onPubcomp(const iot::mqtt_fast::packets::Pubcomp& pubcomp) = 0;
110 virtual void onSubscribe(const iot::mqtt_fast::packets::Subscribe& subscribe) = 0;
111 virtual void onSuback(const iot::mqtt_fast::packets::Suback& suback) = 0;
112 virtual void onUnsubscribe(const iot::mqtt_fast::packets::Unsubscribe& unsubscribe) = 0;
113 virtual void onUnsuback(const iot::mqtt_fast::packets::Unsuback& unsuback) = 0;
114 virtual void onPingreq(const iot::mqtt_fast::packets::Pingreq& pingreq) = 0;
115 virtual void onPingresp(const iot::mqtt_fast::packets::Pingresp& pingresp) = 0;
116 virtual void onDisconnect(const iot::mqtt_fast::packets::Disconnect& disconnect) = 0;
117
118 std::size_t onReceivedFromPeer() final;
119
120 void send(iot::mqtt_fast::ControlPacket&& controlPacket) const;
121 void send(iot::mqtt_fast::ControlPacket& controlPacket) const;
122 void send(std::vector<char>&& data) const;
123 static void printData(const std::vector<char>& data);
124
127
128 if (_packetIdentifier == 0) {
130 }
131
132 return _packetIdentifier;
133 }
134
136
137 uint16_t _packetIdentifier = 0;
138 };
139
140} // namespace iot::mqtt_fast
141
142#endif // IOT_MQTTFAST_SOCKETCONTEXT_H
SocketContext(core::socket::stream::SocketConnection *socketConnection)
void sendToPeer(const char *chunk, std::size_t chunkLen) const final
ControlPacketFactory(core::socket::SocketContext *socketContext)
iot::mqtt_fast::types::Binary & getPacket()
std::vector< char > getPacket()
void sendPubrel(uint16_t packetIdentifier)
virtual void onPubrel(const iot::mqtt_fast::packets::Pubrel &pubrel)=0
SocketContext(core::socket::stream::SocketConnection *socketConnection)
void sendPubrec(uint16_t packetIdentifier)
virtual void onSuback(const iot::mqtt_fast::packets::Suback &suback)=0
virtual void onPingresp(const iot::mqtt_fast::packets::Pingresp &pingresp)=0
virtual void onPubcomp(const iot::mqtt_fast::packets::Pubcomp &pubcomp)=0
virtual void onDisconnect(const iot::mqtt_fast::packets::Disconnect &disconnect)=0
virtual void onConnect(const iot::mqtt_fast::packets::Connect &connect)=0
virtual void onPuback(const iot::mqtt_fast::packets::Puback &puback)=0
iot::mqtt_fast::ControlPacketFactory controlPacketFactory
void sendPubcomp(uint16_t packetIdentifier)
void sendConnect(const std::string &clientId)
void sendSuback(uint16_t packetIdentifier, const std::list< uint8_t > &returnCodes)
void send(iot::mqtt_fast::ControlPacket &controlPacket) const
void sendUnsuback(uint16_t packetIdentifier)
void sendUnsubscribe(const std::list< std::string > &topics)
void sendSubscribe(std::list< Topic > &topics)
void sendPublish(const std::string &topic, const std::string &message, bool dup=false, uint8_t qoS=0, bool retain=false)
virtual void onUnsuback(const iot::mqtt_fast::packets::Unsuback &unsuback)=0
virtual void onSubscribe(const iot::mqtt_fast::packets::Subscribe &subscribe)=0
void sendPuback(uint16_t packetIdentifier)
virtual void onConnack(const iot::mqtt_fast::packets::Connack &connack)=0
void send(std::vector< char > &&data) const
static void printData(const std::vector< char > &data)
virtual void onUnsubscribe(const iot::mqtt_fast::packets::Unsubscribe &unsubscribe)=0
virtual void onPubrec(const iot::mqtt_fast::packets::Pubrec &pubrec)=0
virtual void onPublish(const iot::mqtt_fast::packets::Publish &publish)=0
std::size_t onReceivedFromPeer() final
void send(iot::mqtt_fast::ControlPacket &&controlPacket) const
void sendConnack(uint8_t returnCode, uint8_t flags)
virtual void onPingreq(const iot::mqtt_fast::packets::Pingreq &pingreq)=0
Connack(uint8_t reason, uint8_t flags)
Definition Connack.cpp:50
Connack(mqtt_fast::ControlPacketFactory &controlPacketFactory)
Definition Connack.cpp:61
Connect(const std::string &clientId, const std::string &protocol="MQTT", uint8_t version=4, uint8_t flags=0, uint16_t keepAlive=0x003C)
Definition Connect.cpp:50
Connect(iot::mqtt_fast::ControlPacketFactory &controlPacketFactory)
Definition Connect.cpp:67
Disconnect(iot::mqtt_fast::ControlPacketFactory &controlPacketFactory)
Pingreq(iot::mqtt_fast::ControlPacketFactory &controlPacketFactory)
Definition Pingreq.cpp:56
Pingresp(iot::mqtt_fast::ControlPacketFactory &controlPacketFactory)
Definition Pingresp.cpp:56
Puback(uint16_t packetIdentifier)
Definition Puback.cpp:50
Puback(iot::mqtt_fast::ControlPacketFactory &controlPacketFactory)
Definition Puback.cpp:59
Pubcomp(uint16_t packetIdentifier)
Definition Pubcomp.cpp:50
Pubcomp(iot::mqtt_fast::ControlPacketFactory &controlPacketFactory)
Definition Pubcomp.cpp:59
Publish(iot::mqtt_fast::ControlPacketFactory &controlPacketFactory)
Definition Publish.cpp:72
Publish(uint16_t packetIdentifier, const std::string &topic, const std::string &message, bool dup=false, uint8_t qoS=0, bool retain=false)
Definition Publish.cpp:52
Pubrec(uint16_t packetIdentifier)
Definition Pubrec.cpp:50
Pubrec(iot::mqtt_fast::ControlPacketFactory &controlPacketFactory)
Definition Pubrec.cpp:59
Pubrel(iot::mqtt_fast::ControlPacketFactory &controlPacketFactory)
Definition Pubrel.cpp:59
Pubrel(uint16_t packetIdentifier)
Definition Pubrel.cpp:50
Suback(uint16_t packetIdentifier, const std::list< uint8_t > &returnCodes)
Definition Suback.cpp:50
Suback(iot::mqtt_fast::ControlPacketFactory &controlPacketFactory)
Definition Suback.cpp:61
Subscribe(uint16_t packetIdentifier, const std::list< iot::mqtt_fast::Topic > &topics)
Definition Subscribe.cpp:52
Subscribe(iot::mqtt_fast::ControlPacketFactory &controlPacketFactory)
Definition Subscribe.cpp:66
Unsuback(uint16_t packetIdentifier)
Definition Unsuback.cpp:50
Unsuback(iot::mqtt_fast::ControlPacketFactory &controlPacketFactory)
Definition Unsuback.cpp:59
Unsubscribe(iot::mqtt_fast::ControlPacketFactory &controlPacketFactory)
Unsubscribe(uint16_t packetIdentifier, const std::list< std::string > &topics)
std::vector< char > & getValue()
Definition Binary.cpp:76
#define MQTT_CONNACK
Definition Connack.h:57
#define MQTT_CONNECT
Definition Connect.h:58
#define MQTT_DISCONNECT
Definition Disconnect.h:55
#define MQTT_PINGREQ
Definition Pingreq.h:55
#define MQTT_PINGRESP
Definition Pingresp.h:55
#define MQTT_PUBACK
Definition Puback.h:57
#define MQTT_PUBCOMP
Definition Pubcomp.h:57
#define MQTT_PUBLISH
Definition Publish.h:58
#define MQTT_PUBREC
Definition Pubrec.h:57
#define MQTT_PUBREL
Definition Pubrel.h:57
#define MQTT_SUBACK
Definition Suback.h:58
#define MQTT_SUBSCRIBE
Definition Subscribe.h:59
#define MQTT_UNSUBACK
Definition Unsuback.h:57
#define MQTT_UNSUBSCRIBE
Definition Unsubscribe.h:59