mqttpacket.h
4.22 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
This file is part of FlashMQ (https://www.flashmq.org)
Copyright (C) 2021 Wiebe Cazemier
FlashMQ is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, version 3.
FlashMQ is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public
License along with FlashMQ. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef MQTTPACKET_H
#define MQTTPACKET_H
#include "unistd.h"
#include <memory>
#include <vector>
#include <exception>
#include "forward_declarations.h"
#include "client.h"
#include "exceptions.h"
#include "types.h"
#include "subscriptionstore.h"
#include "cirbuf.h"
#include "logger.h"
#include "mainapp.h"
#include "variablebyteint.h"
#include "mqtt5properties.h"
class MqttPacket
{
#ifdef TESTING
friend class MainTests;
#endif
std::vector<char> bites;
Publish publishData;
size_t fixed_header_length = 0; // if 0, this packet does not contain the bytes of the fixed header.
VariableByteInt remainingLength;
std::shared_ptr<Client> sender;
char first_byte = 0;
size_t pos = 0;
size_t packet_id_pos = 0;
uint16_t packet_id = 0;
ProtocolVersion protocolVersion = ProtocolVersion::None;
size_t payloadStart = 0;
size_t payloadLen = 0;
bool hasTopicAlias = false;
Logger *logger = Logger::getInstance();
char *readBytes(size_t length);
char readByte();
void writeByte(char b);
void writeUint16(uint16_t x);
void writeBytes(const char *b, size_t len);
void writeProperties(const std::shared_ptr<Mqtt5PropertyBuilder> &properties);
void writeVariableByteInt(const VariableByteInt &v);
uint16_t readTwoBytesToUInt16();
uint32_t readFourBytesToUint32();
size_t remainingAfterPos();
size_t decodeVariableByteIntAtPos();
void readUserProperty();
void calculateRemainingLength();
MqttPacket(const MqttPacket &other) = delete;
public:
PacketType packetType = PacketType::Reserved;
MqttPacket(CirBuf &buf, size_t packet_len, size_t fixed_header_length, std::shared_ptr<Client> &sender); // Constructor for parsing incoming packets.
MqttPacket(MqttPacket &&other) = default;
size_t getRequiredSizeForPublish(const ProtocolVersion protocolVersion, const Publish &publishData) const;
// Constructor for outgoing packets. These may not allocate room for the fixed header, because we don't (always) know the length in advance.
MqttPacket(const ConnAck &connAck);
MqttPacket(const SubAck &subAck);
MqttPacket(const UnsubAck &unsubAck);
MqttPacket(const ProtocolVersion protocolVersion, const Publish &_publish);
MqttPacket(const PubResponse &pubAck);
static void bufferToMqttPackets(CirBuf &buf, std::vector<MqttPacket> &packetQueueIn, std::shared_ptr<Client> &sender);
void handle();
void handleConnect();
void handleDisconnect();
void handleSubscribe();
void handleUnsubscribe();
void handlePing();
void handlePublish();
void handlePubAck();
void handlePubRec();
void handlePubRel();
void handlePubComp();
size_t getSizeIncludingNonPresentHeader() const;
const std::vector<char> &getBites() const { return bites; }
char getQos() const { return publishData.qos; }
void setQos(const char new_qos);
ProtocolVersion getProtocolVersion() const { return protocolVersion;}
const std::string &getTopic() const;
const std::vector<std::string> &getSubtopics() const;
std::shared_ptr<Client> getSender() const;
void setSender(const std::shared_ptr<Client> &value);
bool containsFixedHeader() const;
void setPacketId(uint16_t packet_id);
uint16_t getPacketId() const;
void setDuplicate();
void readIntoBuf(CirBuf &buf) const;
std::string getPayloadCopy() const;
bool getRetain() const;
void setRetain();
const Publish &getPublishData();
bool containsClientSpecificProperties() const;
const std::vector<std::pair<std::string, std::string>> *getUserProperties() const;
};
#endif // MQTTPACKET_H