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#include <vector>
55
56#endif // DOXYGEN_SHOULD_SKIP_THIS
57
58namespace iot::mqtt_fast {
59
60 SocketContext::SocketContext(core::socket::stream::SocketConnection* socketConnection)
61 : core::socket::stream::SocketContext(socketConnection)
63 }
64
65 void SocketContext::sendConnect(const std::string& clientId) {
66 LOG(DEBUG) << "MQTT (fast): Send CONNECT";
67 LOG(DEBUG) << "MQTT (fast): ============";
68
69 send(iot::mqtt_fast::packets::Connect(clientId));
70 }
71
72 void SocketContext::sendConnack(uint8_t returnCode, uint8_t flags) {
73 LOG(DEBUG) << "MQTT (fast): Send CONNACK";
74 LOG(DEBUG) << "MQTT (fast): ============";
75
77 }
78
79 void SocketContext::sendPublish(const std::string& topic, const std::string& message, bool dup, uint8_t qoS, bool retain) {
80 LOG(DEBUG) << "MQTT (fast): Send PUBLISH";
81 LOG(DEBUG) << "MQTT (fast): ============";
82
83 send(iot::mqtt_fast::packets::Publish(qoS == 0 ? 0 : getPacketIdentifier(), topic, message, dup, qoS, retain));
84 }
85
86 void SocketContext::sendPuback(uint16_t packetIdentifier) {
87 LOG(DEBUG) << "MQTT (fast): Send PUBACK";
88 LOG(DEBUG) << "MQTT (fast): ===========";
89
90 send(iot::mqtt_fast::packets::Puback(packetIdentifier));
91 }
92
93 void SocketContext::sendPubrec(uint16_t packetIdentifier) {
94 LOG(DEBUG) << "MQTT (fast): Send PUBREC";
95 LOG(DEBUG) << "MQTT (fast): ===========";
96
97 send(iot::mqtt_fast::packets::Pubrec(packetIdentifier));
98 }
99
100 void SocketContext::sendPubrel(uint16_t packetIdentifier) {
101 LOG(DEBUG) << "MQTT (fast): Send PUBREL";
102 LOG(DEBUG) << "MQTT (fast): ===========";
103
104 send(iot::mqtt_fast::packets::Pubrel(packetIdentifier));
105 }
106
107 void SocketContext::sendPubcomp(uint16_t packetIdentifier) {
108 LOG(DEBUG) << "MQTT (fast): Send PUBCOMP";
109 LOG(DEBUG) << "MQTT (fast): ============";
110
111 send(iot::mqtt_fast::packets::Pubcomp(packetIdentifier));
112 }
113
114 void SocketContext::sendSubscribe(std::list<iot::mqtt_fast::Topic>& topics) {
115 LOG(DEBUG) << "MQTT (fast): Send SUBSCRIBE";
116 LOG(DEBUG) << "MQTT (fast): ==============";
117
119 }
120
121 void SocketContext::sendSuback(uint16_t packetIdentifier, const std::list<uint8_t>& returnCodes) {
122 LOG(DEBUG) << "MQTT (fast): Send SUBACK";
123 LOG(DEBUG) << "MQTT (fast): ===========";
124
125 send(iot::mqtt_fast::packets::Suback(packetIdentifier, returnCodes));
126 }
127
128 void SocketContext::sendUnsubscribe(const std::list<std::string>& topics) {
129 LOG(DEBUG) << "MQTT (fast): Send UNSUBSCRIBE";
130 LOG(DEBUG) << "MQTT (fast): ================";
131
133 }
134
135 void SocketContext::sendUnsuback(uint16_t packetIdentifier) {
136 LOG(DEBUG) << "MQTT (fast): Send UNSUBACK";
137 LOG(DEBUG) << "MQTT (fast): =============";
138
139 send(iot::mqtt_fast::packets::Unsuback(packetIdentifier));
140 }
141
143 LOG(DEBUG) << "MQTT (fast): Send Pingreq";
144 LOG(DEBUG) << "MQTT (fast): ============";
145
147 }
148
150 LOG(DEBUG) << "MQTT (fast): Send Pingresp";
151 LOG(DEBUG) << "MQTT (fast): =============";
152
154 }
155
157 LOG(DEBUG) << "MQTT (fast): Send Disconnect";
158 LOG(DEBUG) << "MQTT (fast): ===============";
159
161 }
162
164 const std::size_t consumed = controlPacketFactory.construct();
165
167 LOG(ERROR) << "MQTT (fast): SocketContext: Error during ControlPacket construction";
168 close();
170 LOG(DEBUG) << "MQTT (fast): ======================================================";
171 LOG(DEBUG) << "MQTT (fast): PacketType: " << static_cast<uint16_t>(controlPacketFactory.getPacketType());
172 LOG(DEBUG) << "MQTT (fast): PacketFlags: " << static_cast<uint16_t>(controlPacketFactory.getPacketFlags());
173 LOG(DEBUG) << "MQTT (fast): RemainingLength: " << static_cast<uint16_t>(controlPacketFactory.getRemainingLength());
174
176
178 case MQTT_CONNECT:
180 break;
181 case MQTT_CONNACK:
183 break;
184 case MQTT_PUBLISH:
186 break;
187 case MQTT_PUBACK:
189 break;
190 case MQTT_PUBREC:
192 break;
193 case MQTT_PUBREL:
195 break;
196 case MQTT_PUBCOMP:
198 break;
199 case MQTT_SUBSCRIBE:
201 break;
202 case MQTT_SUBACK:
204 break;
205 case MQTT_UNSUBSCRIBE:
207 break;
208 case MQTT_UNSUBACK:
210 break;
211 case MQTT_PINGREQ:
213 break;
214 case MQTT_PINGRESP:
216 break;
217 case MQTT_DISCONNECT:
219 break;
220 default:
221 close();
222 break;
223 }
224
226 }
227
228 return consumed;
229 }
230
231 void SocketContext::send(ControlPacket&& controlPacket) const {
232 send(controlPacket.getPacket());
233 }
234
235 void SocketContext::send(ControlPacket& controlPacket) const {
236 send(controlPacket.getPacket());
237 }
238
239 void SocketContext::send(std::vector<char>&& data) const {
240 printData(data);
241 sendToPeer(data.data(), data.size());
242 }
243
244 void SocketContext::printData(const std::vector<char>& data) {
245 std::stringstream ss;
246
247 ss << "Data: ";
248 unsigned long i = 0;
249 for (char const ch : data) {
250 if (i != 0 && i % 8 == 0 && i + 1 != data.size()) {
251 ss << std::endl;
252 ss << " ";
253 }
254 ++i;
255 ss << "0x" << std::hex << std::setfill('0') << std::setw(2) << static_cast<uint16_t>(static_cast<uint8_t>(ch))
256 << " "; // << " | ";
257 }
258
259 LOG(DEBUG) << ss.str();
260 }
261
262} // 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