Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support will delay #839

Merged
merged 2 commits into from
Aug 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 35 additions & 52 deletions include/mqtt/broker/broker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1065,6 +1065,10 @@ class broker_t {
spep,
client_id,
force_move(will),
// will_sender
[this](auto&&... params) {
do_publish(std::forward<decltype(params)>(params)...);
},
force_move(will_expiry_interval),
force_move(session_expiry_interval)
);
Expand Down Expand Up @@ -1094,7 +1098,7 @@ class broker_t {
);
}
else {
// inherit offline session
// inherit online session
MQTT_LOG("mqtt_broker", trace)
<< MQTT_ADD_VALUE(address, this)
<< "cid:" << client_id
Expand All @@ -1103,7 +1107,7 @@ class broker_t {
idx.modify(
it,
[&](auto& e) {
e.reset_con(spep);
e.renew(spep, clean_start);
e.update_will(ioc_, force_move(will), will_expiry_interval);
// TODO: e.will_delay = force_move(will_delay);
e.renew_session_expiry(force_move(session_expiry_interval));
Expand All @@ -1129,6 +1133,10 @@ class broker_t {
spep,
client_id,
force_move(will),
// will_sender
[this](auto&&... params) {
do_publish(std::forward<decltype(params)>(params)...);
},
force_move(will_expiry_interval),
force_move(session_expiry_interval)
);
Expand All @@ -1149,7 +1157,7 @@ class broker_t {
it,
[&](auto& e) {
e.clean();
e.reset_con(spep);
e.renew(spep, clean_start);
e.update_will(ioc_, force_move(will), will_expiry_interval);
// TODO: e.will_delay = force_move(will_delay);
e.renew_session_expiry(force_move(session_expiry_interval));
Expand All @@ -1167,7 +1175,7 @@ class broker_t {
idx.modify(
it,
[&](auto& e) {
e.reset_con(spep);
e.renew(spep, clean_start);
e.update_will(ioc_, force_move(will), will_expiry_interval);
// TODO: e.will_delay = force_move(will_delay);
e.renew_session_expiry(force_move(session_expiry_interval));
Expand Down Expand Up @@ -1225,47 +1233,21 @@ class broker_t {
} ();

auto do_send_will =
[&](session_state& session) {
if (session.will()) {
if (send_will) {
// TODO: This should be triggered by the will delay
// Not sent immediately.
auto props = force_move(session.will().value().props());

if (session.get_tim_will_expiry()) {
auto d =
std::chrono::duration_cast<std::chrono::seconds>(
session.get_tim_will_expiry()->expiry() - std::chrono::steady_clock::now()
).count();
if (d < 0) d = 0;
set_property<v5::property::message_expiry_interval>(
props,
v5::property::message_expiry_interval(
static_cast<uint32_t>(d)
)
);
}

do_publish(
ep,
force_move(session.will().value().topic()),
force_move(session.will().value().message()),
session.will().value().get_qos() | session.will().value().get_retain(),
props
);
}
else {
session.reset_will();
}
[&](session_state& ss) {
if (send_will) {
ss.send_will();
}
else {
ss.clear_will();
}
};

if (session_clear) {
idx.modify(
it,
[&](session_state& e) {
do_send_will(e);
e.con()->force_disconnect();
[&](session_state& ss) {
do_send_will(ss);
ss.con()->force_disconnect();
},
[](auto&) { BOOST_ASSERT(false); }
);
Expand All @@ -1276,10 +1258,10 @@ class broker_t {
else {
idx.modify(
it,
[&](session_state& e) {
do_send_will(e);
e.con()->force_disconnect();
e.become_offline(
[&](session_state& ss) {
do_send_will(ss);
ss.con()->force_disconnect();
ss.become_offline(
[this]
(std::shared_ptr<as::steady_timer> const& sp_tim) {
sessions_.get<tag_tim>().erase(sp_tim);
Expand Down Expand Up @@ -1364,7 +1346,7 @@ class broker_t {
}

do_publish(
ep,
*it,
force_move(topic_name),
force_move(contents),
pubopts.get_qos() | pubopts.get_retain(), // remove dup flag
Expand Down Expand Up @@ -1527,8 +1509,8 @@ class broker_t {
optional<session_state_ref> ssr_opt;
idx.modify(
it,
[&](session_state& e) {
ssr_opt.emplace(e);
[&](session_state& ss) {
ssr_opt.emplace(ss);
},
[](auto&) { BOOST_ASSERT(false); }
);
Expand Down Expand Up @@ -1665,8 +1647,8 @@ class broker_t {
optional<session_state_ref> ssr_opt;
idx.modify(
it,
[&](session_state& e) {
ssr_opt.emplace(e);
[&](session_state& ss) {
ssr_opt.emplace(ss);
},
[](auto&) { BOOST_ASSERT(false); }
);
Expand Down Expand Up @@ -1715,11 +1697,12 @@ class broker_t {
* be sent to newly added subscriptions in the future.\
*/
void do_publish(
endpoint_t& ep,
session_state const& source_ss,
buffer topic,
buffer contents,
publish_options pubopts,
v5::properties props) {
v5::properties props
) {

// publish the message to subscribers.
// retain is delivered as the original only if rap_value is rap::retain.
Expand Down Expand Up @@ -1765,7 +1748,7 @@ class broker_t {
// If NL (no local) subscription option is set and
// publisher is the same as subscriber, then skip it.
if (sub.subopts.get_nl() == nl::yes &&
sub.ss.get().con().get() == &ep) return;
sub.ss.get().client_id() == source_ss.client_id()) return;
deliver(sub.ss.get(), sub);
}
else {
Expand All @@ -1782,7 +1765,7 @@ class broker_t {
);

optional<std::chrono::steady_clock::duration> message_expiry_interval;
if (ep.get_protocol_version() == protocol_version::v5) {
if (source_ss.get_protocol_version() == protocol_version::v5) {
auto v = get_property<v5::property::message_expiry_interval>(props);
if (v) {
message_expiry_interval.emplace(std::chrono::seconds(v.value().val()));
Expand Down
Loading