Skip to content

Commit 102ce67

Browse files
committed
Instead of simply discard the message containing a splitted multivalue property, do not advance the property index and mark discarded properties has appended but not sended.
Compute the exact number of properties that would fit the CBOR buffer and retry send next loop. When no error occurs restore property limit to NO_LIMIT and advance property index.
1 parent be1ccbd commit 102ce67

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

src/cbor/CBOREncoder.cpp

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ CborError CBOREncoder::encode(PropertyContainer & property_container, uint8_t *
4747
CborError error = CborNoError;
4848
int num_encoded_properties = 0;
4949
int num_checked_properties = 0;
50+
static int encoded_properties_message_limit = -1;
5051

5152
if(current_property_index >= property_container.size())
5253
current_property_index = 0;
@@ -58,7 +59,7 @@ CborError CBOREncoder::encode(PropertyContainer & property_container, uint8_t *
5859
property_container.end(),
5960
[lightPayload, &arrayEncoder, &error, &num_encoded_properties, &num_checked_properties](Property * p)
6061
{
61-
if(error == CborNoError)
62+
if((error == CborNoError) && ((num_encoded_properties < encoded_properties_message_limit) || (encoded_properties_message_limit == -1)))
6263
{
6364
if (p->shouldBeUpdated() && p->isReadableByCloud())
6465
{
@@ -72,14 +73,36 @@ CborError CBOREncoder::encode(PropertyContainer & property_container, uint8_t *
7273
}
7374
});
7475

75-
current_property_index += num_checked_properties;
76-
77-
if ((CborNoError != error) &&
78-
(CborErrorOutOfMemory != error))
76+
if ((CborNoError != error) && (CborErrorOutOfMemory != error))
77+
{
78+
/* Trim the number of properties to be included in the next message to avoid multivalue property split */
79+
encoded_properties_message_limit = num_encoded_properties;
7980
return error;
81+
}
82+
83+
/* Restore property message limit to NO_LIMIT */
84+
encoded_properties_message_limit = -1;
8085

8186
CHECK_CBOR(cbor_encoder_close_container(&encoder, &arrayEncoder));
8287

88+
/* The append process has been successful, so we don't need to terty to send this properties set. Cleanup _has_been_appended_but_not_sended flag */
89+
iter = property_container.begin();
90+
std::advance(iter, current_property_index);
91+
int num_appended_properties = 0;
92+
std::for_each(iter,
93+
property_container.end(),
94+
[&num_appended_properties, &num_checked_properties](Property * p)
95+
{
96+
if (num_appended_properties < num_checked_properties)
97+
{
98+
p->appendCompleted();
99+
num_appended_properties++;
100+
}
101+
});
102+
103+
/* Advance property index for the nex message */
104+
current_property_index += num_checked_properties;
105+
83106
if (num_encoded_properties > 0)
84107
bytes_encoded = cbor_encoder_get_buffer_size(&encoder, data);
85108
else

src/property/Property.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ CborError Property::append(CborEncoder *encoder, bool lightPayload) {
161161
_has_been_updated_once = true;
162162
_has_been_modified_in_callback = false;
163163
_update_requested = false;
164+
_has_been_appended_but_not_sended = true;
164165
_last_updated_millis = millis();
165166
return CborNoError;
166167
}

0 commit comments

Comments
 (0)