@@ -45,7 +45,7 @@ struct Dht::Announce {
45
45
bool permanent;
46
46
Sp<Value> value;
47
47
time_point created;
48
- DoneCallback callback ;
48
+ std::vector< DoneCallback> callbacks ;
49
49
};
50
50
51
51
struct Dht ::SearchNode {
@@ -442,8 +442,9 @@ struct Dht::Search {
442
442
g.second .done_cb = {};
443
443
}
444
444
for (auto & a : announce) {
445
- a.callback (false , {});
446
- a.callback = {};
445
+ for (auto & cb : a.callbacks )
446
+ cb (false , {});
447
+ a.callbacks .clear ();
447
448
}
448
449
}
449
450
@@ -627,7 +628,9 @@ struct Dht::Search {
627
628
return a.value ->id == value->id ;
628
629
});
629
630
if (a_sr == announce.end ()) {
630
- announce.emplace_back (Announce {permanent, value, created, callback});
631
+ auto & a = announce.emplace_back (Announce {permanent, value, created, {}} );
632
+ if (callback)
633
+ a.callbacks .emplace_back (std::move (callback));
631
634
for (auto & n : nodes) {
632
635
n->probe_query .reset ();
633
636
n->acked [value->id ].req .reset ();
@@ -636,23 +639,25 @@ struct Dht::Search {
636
639
a_sr->permanent = permanent;
637
640
a_sr->created = created;
638
641
if (a_sr->value != value) {
642
+ // Value is updated, previous ops are failed
639
643
a_sr->value = value;
640
644
for (auto & n : nodes) {
641
645
n->acked [value->id ].req .reset ();
642
646
n->probe_query .reset ();
643
647
}
644
- }
645
- if (isAnnounced (value->id )) {
646
- if (a_sr->callback )
647
- a_sr->callback (true , {});
648
- a_sr->callback = {};
648
+ for (auto & cb: a_sr->callbacks )
649
+ cb (false , {});
650
+ a_sr->callbacks .clear ();
651
+ if (callback)
652
+ a_sr->callbacks .emplace_back (std::move (callback));
653
+ } else if (isAnnounced (value->id )) {
654
+ // Same value, already announced
649
655
if (callback)
650
656
callback (true , {});
651
- return ;
652
657
} else {
653
- if (a_sr-> callback )
654
- a_sr-> callback ( false , {});
655
- a_sr->callback = callback ;
658
+ // Same value, not announced yet
659
+ if (callback)
660
+ a_sr->callbacks . emplace_back ( std::move ( callback)) ;
656
661
}
657
662
}
658
663
}
@@ -722,8 +727,8 @@ struct Dht::Search {
722
727
std::vector<DoneCallback> a_cbs;
723
728
a_cbs.reserve (announce.size ());
724
729
for (auto ait = announce.begin () ; ait != announce.end (); ) {
725
- if (ait->callback )
726
- a_cbs. emplace_back ( std::move ( ait->callback ) );
730
+ a_cbs. insert (a_cbs. end (), std::make_move_iterator (ait->callbacks . begin ()), std::make_move_iterator (ait-> callbacks . end ()));
731
+ ait->callbacks . clear ( );
727
732
if (not ait->permanent )
728
733
ait = announce.erase (ait);
729
734
else
@@ -747,9 +752,11 @@ struct Dht::Search {
747
752
if (vid != Value::INVALID_ID and (!a.value || a.value ->id != vid))
748
753
return true ;
749
754
if (isAnnounced (a.value ->id )) {
750
- if (a.callback ) {
751
- a.callback (true , getNodes ());
752
- a.callback = nullptr ;
755
+ if (!a.callbacks .empty ()) {
756
+ const auto & nodes = getNodes ();
757
+ for (auto & cb : a.callbacks )
758
+ cb (true , nodes);
759
+ a.callbacks .clear ();
753
760
}
754
761
if (not a.permanent )
755
762
return false ;
0 commit comments