37 const std::string& willTopic,
38 const std::string& willMessage,
41 const std::string& username,
42 const std::string& password,
45 this->protocol =
"MQTT";
48 this->keepAlive = keepAlive;
49 this->clientId = clientId;
50 this->willTopic = willTopic;
51 this->willMessage = willMessage;
54 this->willFlag = !willMessage.empty();
55 this->username = username;
56 this->usernameFlag = !username.empty();
57 this->password = password;
58 this->passwordFlag = !password.empty();
60 this->connectFlags =
static_cast<uint8_t>((!
usernameFlag ? 0 : 0x80) | (!
passwordFlag ? 0 : 0x40) | (!willRetain ? 0 : 0x20) |
61 ((willQoS << 3) & 0x18) | (!
willFlag ? 0 : 0x04) | (!cleanSession ? 0 : 0x02));
65 std::vector<
char> packet(protocol.serialize());
67 std::vector<
char> tmpVector = level.serialize();
68 packet.insert(packet.end(), tmpVector.begin(), tmpVector.end());
70 tmpVector = connectFlags.serialize();
71 packet.insert(packet.end(), tmpVector.begin(), tmpVector.end());
73 tmpVector = keepAlive.serialize();
74 packet.insert(packet.end(), tmpVector.begin(), tmpVector.end());
76 tmpVector = clientId.serialize();
77 packet.insert(packet.end(), tmpVector.begin(), tmpVector.end());
80 tmpVector = willTopic.serialize();
81 packet.insert(packet.end(), tmpVector.begin(), tmpVector.end());
83 tmpVector = willMessage.serialize();
84 packet.insert(packet.end(), tmpVector.begin(), tmpVector.end());
87 tmpVector = username.serialize();
88 packet.insert(packet.end(), tmpVector.begin(), tmpVector.end());
91 tmpVector = password.serialize();
92 packet.insert(packet.end(), tmpVector.begin(), tmpVector.end());
Connect(const std::string &clientId, uint16_t keepAlive, bool cleanSession, const std::string &willTopic, const std::string &willMessage, uint8_t willQoS, bool willRetain, const std::string &username, const std::string &password, bool loopPrevention)