Fix payload content

This commit is contained in:
hsaturn
2021-03-22 02:33:54 +01:00
parent 0cb2e99b4b
commit 5cee67095e
2 changed files with 8 additions and 5 deletions

View File

@@ -400,7 +400,7 @@ MqttError MqttClient::publish(const Topic& topic, const char* payload, size_t pa
MqttMessage msg; MqttMessage msg;
msg.create(MqttMessage::Publish); msg.create(MqttMessage::Publish);
msg.add(topic); msg.add(topic);
msg.add(payload, pay_length); msg.add(payload, pay_length, false);
if (parent) if (parent)
return parent->publish(this, topic, msg); return parent->publish(this, topic, msg);
else if (client) else if (client)
@@ -491,10 +491,13 @@ void MqttMessage::incoming(char in_byte)
} }
} }
void MqttMessage::add(const char* p, size_t len) void MqttMessage::add(const char* p, size_t len, bool addLength)
{
if (addLength)
{ {
incoming(len>>8); incoming(len>>8);
incoming(len & 0xFF); incoming(len & 0xFF);
}
while(len--) incoming(*p++); while(len--) incoming(*p++);
} }

View File

@@ -57,7 +57,7 @@ class MqttMessage
MqttMessage(Type t) { create(t); } MqttMessage(Type t) { create(t); }
void incoming(char byte); void incoming(char byte);
void add(char byte) { incoming(byte); } void add(char byte) { incoming(byte); }
void add(const char* p, size_t len); void add(const char* p, size_t len, bool addLength=true );
void add(const std::string& s) { add(s.c_str(), s.length()); } void add(const std::string& s) { add(s.c_str(), s.length()); }
void add(const Topic& t) { add(t.str()); } void add(const Topic& t) { add(t.str()); }
const char* end() const { return &buffer[0]+buffer.size(); } const char* end() const { return &buffer[0]+buffer.size(); }