Skip to content

Commit 51f7852

Browse files
committed
Merge branch 'develop'
2 parents 8eace39 + 01b5177 commit 51f7852

19 files changed

+71
-7
lines changed

examples/mqtt/conn/conn.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ int main(int argc, char **argv)
4242

4343
mqtt::Client::Config conf;
4444
conf.auto_reconnect_enable = true;
45-
conf.auto_reconnect_wait_sec = 5;
45+
//conf.auto_reconnect_wait_sec = 5;
46+
conf.auto_reconnect_wait_sec_gen_func = [](int fail_count) {
47+
return 1 << std::min(fail_count, 4);
48+
};
4649
#if 0
4750
conf.base.broker.domain = "cppmain.cpp";
4851
conf.base.broker.port = 1883;

modules/eventx/async.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
#include <tbox/util/fs.h>
2525
#include <tbox/util/execute_cmd.h>
2626

27+
#undef MODULE_ID
28+
#define MODULE_ID "tbox.async"
29+
2730
namespace tbox {
2831
namespace eventx {
2932

modules/eventx/loop_thread.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
#include "loop_thread.h"
2121
#include "loop_wdog.h"
2222

23+
#undef MODULE_ID
24+
#define MODULE_ID "tbox.loop_thread"
25+
2326
namespace tbox {
2427
namespace eventx {
2528

modules/eventx/loop_wdog.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
#include <tbox/base/defines.h>
3333
#include <tbox/base/wrapped_recorder.h>
3434

35+
#undef MODULE_ID
36+
#define MODULE_ID "tbox.loop_wdog"
37+
3538
namespace tbox {
3639
namespace eventx {
3740

modules/eventx/thread_pool.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@
3838
#include <tbox/base/wrapped_recorder.h>
3939
#include <tbox/event/loop.h>
4040

41+
#undef MODULE_ID
42+
#define MODULE_ID "tbox.thread_pool"
43+
4144
namespace tbox {
4245
namespace eventx {
4346

modules/eventx/timer_fd.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
#include <tbox/event/loop.h>
3131
#include <tbox/event/fd_event.h>
3232

33+
#undef MODULE_ID
34+
#define MODULE_ID "tbox.timer_fd"
35+
3336
namespace tbox {
3437
namespace eventx {
3538

modules/eventx/timer_pool.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
#include <tbox/event/loop.h>
2626
#include <tbox/event/timer_event.h>
2727

28+
#undef MODULE_ID
29+
#define MODULE_ID "tbox.timer_pool"
30+
2831
namespace tbox {
2932
namespace eventx {
3033

modules/eventx/work_thread.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
#include <tbox/base/wrapped_recorder.h>
3838
#include <tbox/event/loop.h>
3939

40+
#undef MODULE_ID
41+
#define MODULE_ID "tbox.work_thread"
42+
4043
namespace tbox {
4144
namespace eventx {
4245

modules/mqtt/client.cpp

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ struct Client::Data {
8282
/// 在回调函数执行之前,检查一下这个 std::weak_ptr 是否过期就可以得知 this 指针是否有效。
8383
/// 进而避免继续访问到无效 this 指向的内容。
8484

85+
int connect_fail_count = 0; //!< 单次连接失败
86+
8587
static int _instance_count;
8688
};
8789

@@ -289,6 +291,7 @@ bool Client::start()
289291

290292
CHECK_DELETE_RESET_OBJ(d_->sp_thread);
291293
auto is_alive = d_->alive_tag.get(); //! 原理见Q1
294+
d_->connect_fail_count = 0;
292295

293296
//! 由于 mosquitto_connect() 是阻塞函数,为了避免阻塞其它事件,特交给子线程去做
294297
d_->sp_thread = new thread(
@@ -609,13 +612,17 @@ void Client::onTcpConnectDone(int ret)
609612
CHECK_DELETE_RESET_OBJ(d_->sp_thread);
610613

611614
if (ret == MOSQ_ERR_SUCCESS) {
615+
d_->connect_fail_count = 0;
612616
LogDbg("connect success");
613617
enableSocketRead();
614618
enableSocketWriteIfNeed();
615619
updateStateTo(State::kTcpConnected);
616620

617621
} else {
618-
LogNotice("connect fail, rc:%d, %s", ret, mosquitto_strerror(ret));
622+
++d_->connect_fail_count;
623+
LogNotice("connect fail, rc:%d, %s, fail_count:%d",
624+
ret, mosquitto_strerror(ret), d_->connect_fail_count);
625+
619626
tryReconnect();
620627

621628
++d_->cb_level;
@@ -679,17 +686,24 @@ void Client::tryReconnect()
679686
{
680687
//! 如果开启了自动重连
681688
if (d_->config.auto_reconnect_enable) {
682-
if (d_->config.auto_reconnect_wait_sec > 0) {
683-
LogDbg("reconnect after %d sec", d_->config.auto_reconnect_wait_sec);
684-
d_->reconnect_wait_remain_sec = d_->config.auto_reconnect_wait_sec;
689+
auto wait_remain_sec = d_->config.auto_reconnect_wait_sec;
690+
691+
//! 如果设置了 auto_reconnect_wait_sec_gen_func 函数
692+
//! 就则使用 auto_reconnect_wait_sec_gen_func 函数生成 wait_remain_sec
693+
if (d_->config.auto_reconnect_wait_sec_gen_func)
694+
wait_remain_sec = d_->config.auto_reconnect_wait_sec_gen_func(d_->connect_fail_count);
695+
696+
if (wait_remain_sec > 0) {
697+
LogDbg("reconnect after %d sec", wait_remain_sec);
685698
updateStateTo(State::kReconnWaiting);
686699

687700
} else {
688701
LogDbg("reconnect now");
689-
d_->reconnect_wait_remain_sec = 0;
690702
updateStateTo(State::kConnecting);
691703
}
692704

705+
d_->reconnect_wait_remain_sec = wait_remain_sec;
706+
693707
} else { //! 如果不需要自动重连
694708
LogDbg("no need reconnect, end");
695709
updateStateTo(State::kEnd);

modules/mqtt/client.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ class Client {
8787

8888
bool auto_reconnect_enable = true; //! 是否自动重连
8989
int auto_reconnect_wait_sec = 0; //! 自动重连等待时长,秒
90+
//! 根据失败次数动态生成自动重连等待时长,秒
91+
std::function<int(int)> auto_reconnect_wait_sec_gen_func;
9092

9193
bool isValid() const;
9294
};

0 commit comments

Comments
 (0)