SNode.C
Loading...
Searching...
No Matches
SocketContext.cpp
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#include "iot/mqtt-fast/SocketContext.h"
43
44#include "iot/mqtt-fast/ControlPacket.h"
45#include "iot/mqtt-fast/types/Binary.h"
46
47#ifndef DOXYGEN_SHOULD_SKIP_THIS
48
49#include "log/Logger.h"
50
51#include <cstdint>
52#include <iomanip>
53#include <sstream>
54
55#endif // DOXYGEN_SHOULD_SKIP_THIS
56
57namespace iot::mqtt_fast {
58
59 SocketContext::SocketContext(core::socket::stream::SocketConnection* socketConnection)
60 : core::socket::stream::SocketContext(socketConnection)
62 }
63
64 void SocketContext::sendConnect(const std::string& clientId) {
65 LOG(DEBUG) << "MQTT (fast): Send CONNECT";
66 LOG(DEBUG) << "MQTT (fast): ============";
67
68 send(iot::mqtt_fast::packets::Connect(clientId));
69 }
70
71 void SocketContext::sendConnack(uint8_t returnCode, uint8_t flags) {
72 LOG(DEBUG) << "MQTT (fast): Send CONNACK";
73 LOG(DEBUG) << "MQTT (fast): ============";
74
76 }
77
78 void SocketContext::sendPublish(const std::string& topic, const std::string& message, bool dup, uint8_t qoS, bool retain) {
79 LOG(DEBUG) << "MQTT (fast): Send PUBLISH";
80 LOG(DEBUG) << "MQTT (fast): ============";
81
82 send(iot::mqtt_fast::packets::Publish(qoS == 0 ? 0 : getPacketIdentifier(), topic, message, dup, qoS, retain));
83 }
84
85 void SocketContext::sendPuback(uint16_t packetIdentifier) {
86 LOG(DEBUG) << "MQTT (fast): Send PUBACK";
87 LOG(DEBUG) << "MQTT (fast): ===========";
88
89 send(iot::mqtt_fast::packets::Puback(packetIdentifier));
90 }
91
92 void SocketContext::sendPubrec(uint16_t packetIdentifier) {
93 LOG(DEBUG) << "MQTT (fast): Send PUBREC";
94 LOG(DEBUG) << "MQTT (fast): ===========";
95
96 send(iot::mqtt_fast::packets::Pubrec(packetIdentifier));
97 }
98
99 void SocketContext::sendPubrel(uint16_t packetIdentifier) {
100 LOG(DEBUG) << "MQTT (fast): Send PUBREL";
101 LOG(DEBUG) << "MQTT (fast): ===========";
102
103 send(iot::mqtt_fast::packets::Pubrel(packetIdentifier));
104 }
105
106 void SocketContext::sendPubcomp(uint16_t packetIdentifier) {
107 LOG(DEBUG) << "MQTT (fast): Send PUBCOMP";
108 LOG(DEBUG) << "MQTT (fast): ============";
109
110 send(iot::mqtt_fast::packets::Pubcomp(packetIdentifier));
111 }
112
113 void SocketContext::sendSubscribe(std::list<iot::mqtt_fast::Topic>& topics) {
114 LOG(DEBUG) << "MQTT (fast): Send SUBSCRIBE";
115 LOG(DEBUG) << "MQTT (fast): ==============";
116
118 }
119
120 void SocketContext::sendSuback(uint16_t packetIdentifier, const std::list<uint8_t>& returnCodes) {
121 LOG(DEBUG) << "MQTT (fast): Send SUBACK";
122 LOG(DEBUG) << "MQTT (fast): ===========";
123
124 send(iot::mqtt_fast::packets::Suback(packetIdentifier, returnCodes));
125 }
126
127 void SocketContext::sendUnsubscribe(const std::list<std::string>& topics) {
128 LOG(DEBUG) << "MQTT (fast): Send UNSUBSCRIBE";
129 LOG(DEBUG) << "MQTT (fast): ================";
130
132 }
133
134 void SocketContext::sendUnsuback(uint16_t packetIdentifier) {
135 LOG(DEBUG) << "MQTT (fast): Send UNSUBACK";
136 LOG(DEBUG) << "MQTT (fast): =============";
137
138 send(iot::mqtt_fast::packets::Unsuback(packetIdentifier));
139 }
140
142 LOG(DEBUG) << "MQTT (fast): Send Pingreq";
143 LOG(DEBUG) << "MQTT (fast): ============";
144
146 }
147
149 LOG(DEBUG) << "MQTT (fast): Send Pingresp";
150 LOG(DEBUG) << "MQTT (fast): =============";
151
153 }
154
156 LOG(DEBUG) << "MQTT (fast): Send Disconnect";
157 LOG(DEBUG) << "MQTT (fast): ===============";
158
160 }
161
163 const std::size_t consumed = controlPacketFactory.construct();
164
166 LOG(ERROR) << "MQTT (fast): SocketContext: Error during ControlPacket construction";
167 close();
169 LOG(DEBUG) << "MQTT (fast): ======================================================";
170 LOG(DEBUG) << "MQTT (fast): PacketType: " << static_cast<uint16_t>(controlPacketFactory.getPacketType());
171 LOG(DEBUG) << "MQTT (fast): PacketFlags: " << static_cast<uint16_t>(controlPacketFactory.getPacketFlags());
172 LOG(DEBUG) << "MQTT (fast): RemainingLength: " << static_cast<uint16_t>(controlPacketFactory.getRemainingLength());
173
175
177 case MQTT_CONNECT:
179 break;
180 case MQTT_CONNACK:
182 break;
183 case MQTT_PUBLISH:
185 break;
186 case MQTT_PUBACK:
188 break;
189 case MQTT_PUBREC:
191 break;
192 case MQTT_PUBREL:
194 break;
195 case MQTT_PUBCOMP:
197 break;
198 case MQTT_SUBSCRIBE:
200 break;
201 case MQTT_SUBACK:
203 break;
204 case MQTT_UNSUBSCRIBE:
206 break;
207 case MQTT_UNSUBACK:
209 break;
210 case MQTT_PINGREQ:
212 break;
213 case MQTT_PINGRESP:
215 break;
216 case MQTT_DISCONNECT:
218 break;
219 default:
220 close();
221 break;
222 }
223
225 }
226
227 return consumed;
228 }
229
230 void SocketContext::send(ControlPacket&& controlPacket) const {
231 send(controlPacket.getPacket());
232 }
233
234 void SocketContext::send(ControlPacket& controlPacket) const {
235 send(controlPacket.getPacket());
236 }
237
238 void SocketContext::send(std::vector<char>&& data) const {
239 printData(data);
240 sendToPeer(data.data(), data.size());
241 }
242
243 void SocketContext::printData(const std::vector<char>& data) {
244 std::stringstream ss;
245
246 ss << "Data: ";
247 unsigned long i = 0;
248 for (char const ch : data) {
249 if (i != 0 && i % 8 == 0 && i + 1 != data.size()) {
250 ss << std::endl;
251 ss << " ";
252 }
253 ++i;
254 ss << "0x" << std::hex << std::setfill('0') << std::setw(2) << static_cast<uint16_t>(static_cast<uint8_t>(ch))
255 << " "; // << " | ";
256 }
257
258 LOG(DEBUG) << ss.str();
259 }
260
261} // namespace iot::mqtt_fast
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