Commit d61c593d4fd7996c8481a2c826f53e40818d7b13
1 parent
7edc3b83
clean up
Showing
2 changed files
with
4 additions
and
24 deletions
mqttpacket.cpp
| 1 | 1 | #include "mqttpacket.h" |
| 2 | 2 | #include <cstring> |
| 3 | 3 | |
| 4 | -MqttPacket::MqttPacket(char *buf, size_t len, size_t fixed_header_length, Client *sender) : // TODO: length of remaining length | |
| 4 | +MqttPacket::MqttPacket(char *buf, size_t len, size_t fixed_header_length, Client *sender) : | |
| 5 | 5 | bites(len), |
| 6 | 6 | fixed_header_length(fixed_header_length), |
| 7 | 7 | sender(sender) |
| ... | ... | @@ -11,6 +11,8 @@ MqttPacket::MqttPacket(char *buf, size_t len, size_t fixed_header_length, Client |
| 11 | 11 | pos += fixed_header_length; |
| 12 | 12 | |
| 13 | 13 | std::memcpy(&bites[0], buf, len); |
| 14 | + | |
| 15 | + variable_header_length = readTwoBytesToUInt16(); | |
| 14 | 16 | } |
| 15 | 17 | |
| 16 | 18 | void MqttPacket::handle() |
| ... | ... | @@ -24,8 +26,6 @@ void MqttPacket::handleConnect() |
| 24 | 26 | if (sender->hasConnectPacketSeen()) |
| 25 | 27 | throw ProtocolError("Client already sent a CONNECT."); |
| 26 | 28 | |
| 27 | - // TODO: Do all packets have a variable header? | |
| 28 | - variable_header_length = readTwoBytesToUInt16(); | |
| 29 | 29 | |
| 30 | 30 | if (variable_header_length == 4 || variable_header_length == 6) |
| 31 | 31 | { |
| ... | ... | @@ -86,7 +86,7 @@ void MqttPacket::handleConnect() |
| 86 | 86 | |
| 87 | 87 | // TODO: validate UTF8 encoded username/password. |
| 88 | 88 | |
| 89 | - sender->setClientProperties(clientid, username, true, keep_alive); | |
| 89 | + sender->setClientProperties(client_id, username, true, keep_alive); | |
| 90 | 90 | } |
| 91 | 91 | else |
| 92 | 92 | { |
| ... | ... | @@ -125,22 +125,7 @@ uint16_t MqttPacket::readTwoBytesToUInt16() |
| 125 | 125 | |
| 126 | 126 | |
| 127 | 127 | |
| 128 | -std::string MqttPacket::getClientId() | |
| 129 | -{ | |
| 130 | - if (packetType != PacketType::CONNECT) | |
| 131 | - throw ProtocolError("Can't get clientid from non-connect packet."); | |
| 132 | - | |
| 133 | - uint16_t clientid_length = (bites[fixed_header_length + 10] << 8) | (bites[fixed_header_length + 11]); | |
| 134 | - size_t client_id_start = fixed_header_length + 12; | |
| 135 | 128 | |
| 136 | - if (clientid_length + 12 < bites.size()) | |
| 137 | - { | |
| 138 | - std::string result(&bites[client_id_start], clientid_length); | |
| 139 | - return result; | |
| 140 | - } | |
| 141 | - | |
| 142 | - throw ProtocolError("Can't get clientid"); | |
| 143 | -} | |
| 144 | 129 | |
| 145 | 130 | |
| 146 | 131 | ... | ... |
mqttpacket.h
| ... | ... | @@ -15,21 +15,16 @@ class Client; |
| 15 | 15 | |
| 16 | 16 | class MqttPacket |
| 17 | 17 | { |
| 18 | - bool valid = false; | |
| 19 | - | |
| 20 | 18 | std::vector<char> bites; |
| 21 | 19 | const size_t fixed_header_length; |
| 22 | 20 | uint16_t variable_header_length; |
| 23 | 21 | Client *sender; |
| 24 | - std::string clientid; | |
| 25 | 22 | size_t pos = 0; |
| 26 | 23 | ProtocolVersion protocolVersion = ProtocolVersion::None; |
| 27 | 24 | public: |
| 28 | 25 | PacketType packetType = PacketType::Reserved; |
| 29 | 26 | MqttPacket(char *buf, size_t len, size_t fixed_header_length, Client *sender); |
| 30 | 27 | |
| 31 | - bool isValid() { return valid; } | |
| 32 | - std::string getClientId(); | |
| 33 | 28 | void handle(); |
| 34 | 29 | void handleConnect(); |
| 35 | 30 | char *readBytes(size_t length); | ... | ... |