Skip to content

Commit 5964ead

Browse files
authored
Merge pull request #613 from gytxxsy/feat/check_if_txt_changed_when_browsing
feat(mdns): check if the txt items is changed when browsing
2 parents e583848 + e2f0477 commit 5964ead

File tree

1 file changed

+33
-4
lines changed

1 file changed

+33
-4
lines changed

components/mdns/mdns.c

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7521,6 +7521,21 @@ static mdns_browse_t *_mdns_browse_find_from(mdns_browse_t *b, mdns_name_t *name
75217521
return b;
75227522
}
75237523

7524+
static bool is_txt_item_in_list(mdns_txt_item_t txt, uint8_t txt_value_len, mdns_txt_item_t *txt_list, uint8_t *txt_value_len_list, size_t txt_count)
7525+
{
7526+
for (size_t i = 0; i < txt_count; i++) {
7527+
if (strcmp(txt.key, txt_list[i].key) == 0) {
7528+
if (txt_value_len == txt_value_len_list[i] && memcmp(txt.value, txt_list[i].value, txt_value_len) == 0) {
7529+
return true;
7530+
} else {
7531+
// The key value is unique, so there is no need to continue searching.
7532+
return false;
7533+
}
7534+
}
7535+
}
7536+
return false;
7537+
}
7538+
75247539
/**
75257540
* @brief Called from parser to add TXT data to search result
75267541
*/
@@ -7541,9 +7556,20 @@ static void _mdns_browse_result_add_txt(mdns_browse_t *browse, const char *insta
75417556
!_str_null_or_empty(r->instance_name) && !strcasecmp(instance, r->instance_name) &&
75427557
!_str_null_or_empty(r->service_type) && !strcasecmp(service, r->service_type) &&
75437558
!_str_null_or_empty(r->proto) && !strcasecmp(proto, r->proto)) {
7559+
bool should_update = false;
75447560
if (r->txt) {
7561+
// Check if txt changed
7562+
if (txt_count != r->txt_count) {
7563+
should_update = true;
7564+
} else {
7565+
for (size_t txt_index = 0; txt_index < txt_count; txt_index++) {
7566+
if (!is_txt_item_in_list(txt[txt_index], txt_value_len[txt_index], r->txt, r->txt_value_len, r->txt_count)) {
7567+
should_update = true;
7568+
break;
7569+
}
7570+
}
7571+
}
75457572
// If the result has a previous txt entry, we delete it and re-add.
7546-
// TODO: we need to check if txt changes.
75477573
for (size_t i = 0; i < r->txt_count; i++) {
75487574
free((char *)(r->txt[i].key));
75497575
free((char *)(r->txt[i].value));
@@ -7562,9 +7588,12 @@ static void _mdns_browse_result_add_txt(mdns_browse_t *browse, const char *insta
75627588
_mdns_result_update_ttl(r, ttl);
75637589
}
75647590
if (previous_ttl != r->ttl) {
7565-
if (_mdns_add_browse_result(out_sync_browse, r) != ESP_OK) {
7566-
return;
7567-
}
7591+
should_update = true;
7592+
}
7593+
}
7594+
if (should_update) {
7595+
if (_mdns_add_browse_result(out_sync_browse, r) != ESP_OK) {
7596+
return;
75687597
}
75697598
}
75707599
return;

0 commit comments

Comments
 (0)