Skip to content

Commit c8f426a

Browse files
committed
Merge branch 'develop'
2 parents 51f7852 + 8d700d0 commit c8f426a

34 files changed

+627
-161
lines changed

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,10 @@ modules 3rd-party:
7171
test: modules
7272
$(MAKE) -C modules test
7373

74-
tools examples: modules
74+
examples: modules
75+
$(MAKE) -C $@
76+
77+
tools:
7578
$(MAKE) -C $@
7679

7780
run_test : test

examples/jsonrpc/message/ping/ping.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ using namespace tbox;
4545

4646
int main(int argc, char **argv)
4747
{
48-
jsonrpc::Rpc::IdType id_type = jsonrpc::Rpc::IdType::kInt;
48+
jsonrpc::IdType id_type = jsonrpc::IdType::kInt;
4949
if (argc >= 2) {
5050
std::string type_str(argv[1]);
5151
if (type_str == "str") {
52-
id_type = jsonrpc::Rpc::IdType::kString;
52+
id_type = jsonrpc::IdType::kString;
5353
} else if (type_str != "int") {
5454
std::cout << "id_type invalid!" << std::endl
5555
<< "Usage: " << argv[0] << " int|str" << std::endl;
@@ -115,7 +115,7 @@ int main(int argc, char **argv)
115115
};
116116

117117
//! 定义收到pong的动作
118-
rpc.addService("pong", [&] (int id, const Json &js_params, int &, Json &) {
118+
rpc.addService("pong", [&] (int id, const Json &js_params, tbox::jsonrpc::Response &) {
119119
int pong_count = 0;
120120
util::json::GetField(js_params, "count", pong_count);
121121
send_ping();

examples/jsonrpc/message/pong/pong.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ using namespace tbox;
4242

4343
int main(int argc, char **argv)
4444
{
45-
jsonrpc::Rpc::IdType id_type = jsonrpc::Rpc::IdType::kInt;
45+
jsonrpc::IdType id_type = jsonrpc::IdType::kInt;
4646
if (argc >= 2) {
4747
std::string type_str(argv[1]);
4848
if (type_str == "str") {
49-
id_type = jsonrpc::Rpc::IdType::kString;
49+
id_type = jsonrpc::IdType::kString;
5050
} else if (type_str != "int") {
5151
std::cout << "id_type invalid!" << std::endl
5252
<< "Usage: " << argv[0] << " int|str" << std::endl;
@@ -111,7 +111,7 @@ int main(int argc, char **argv)
111111
tcp_server.start(); //! 启动tcp服务
112112

113113
//! 注册ping的服务处理函数
114-
rpc.addService("ping", [&] (int id, const Json &js_params, int &, Json &) {
114+
rpc.addService("ping", [&] (int id, const Json &js_params, tbox::jsonrpc::Response &) {
115115
int ping_count = 0;
116116
util::json::GetField(js_params, "count", ping_count);
117117
LogDbg("id: %d, got ping_count: %d", id, ping_count);

examples/jsonrpc/req_rsp/ping/ping.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ using namespace tbox;
4545

4646
int main(int argc, char **argv)
4747
{
48-
jsonrpc::Rpc::IdType id_type = jsonrpc::Rpc::IdType::kInt;
48+
jsonrpc::IdType id_type = jsonrpc::IdType::kInt;
4949
if (argc >= 2) {
5050
std::string type_str(argv[1]);
5151
if (type_str == "str") {
52-
id_type = jsonrpc::Rpc::IdType::kString;
52+
id_type = jsonrpc::IdType::kString;
5353
} else if (type_str != "int") {
5454
std::cout << "id_type invalid!" << std::endl
5555
<< "Usage: " << argv[0] << " int|str" << std::endl;
@@ -111,14 +111,15 @@ int main(int argc, char **argv)
111111

112112
//! 发送ping请求,并在收到回复后,进行下一个ping动作
113113
rpc.request("ping", js_params,
114-
[&] (int errcode, const Json &js_result) {
115-
int pong_count = 0;
116-
util::json::GetField(js_result, "count", pong_count);
117-
if (errcode == 0) {
114+
[&] (const tbox::jsonrpc::Response &r) {
115+
if (r.error.code == 0) {
116+
int pong_count = 0;
117+
util::json::GetField(r.js_result, "count", pong_count);
118118
LogDbg("got pong: %d", pong_count);
119119
send_ping();
120-
} else
121-
LogNotice("got erro: %d", errcode);
120+
} else {
121+
LogNotice("got erro: %d", r.error.code);
122+
}
122123
});
123124
LogDbg("send ping: %d", ping_count);
124125
};

examples/jsonrpc/req_rsp/pong/pong.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ using namespace tbox;
4242

4343
int main(int argc, char **argv)
4444
{
45-
jsonrpc::Rpc::IdType id_type = jsonrpc::Rpc::IdType::kInt;
45+
jsonrpc::IdType id_type = jsonrpc::IdType::kInt;
4646
if (argc >= 2) {
4747
std::string type_str(argv[1]);
4848
if (type_str == "str") {
49-
id_type = jsonrpc::Rpc::IdType::kString;
49+
id_type = jsonrpc::IdType::kString;
5050
} else if (type_str != "int") {
5151
std::cout << "id_type invalid!" << std::endl
5252
<< "Usage: " << argv[0] << " int|str" << std::endl;
@@ -111,11 +111,11 @@ int main(int argc, char **argv)
111111
tcp_server.start(); //! 启动tcp服务
112112

113113
//! 注册ping的服务处理函数
114-
rpc.addService("ping", [&] (int id, const Json &js_params, int &errcode, Json &js_result) {
114+
rpc.addService("ping", [&] (int id, const Json &js_params, tbox::jsonrpc::Response &r) {
115115
int ping_count = 0;
116116
util::json::GetField(js_params, "count", ping_count);
117117
LogDbg("id:%d, got ping_count: %d", id, ping_count);
118-
js_result = js_params;
118+
r.js_result = js_params;
119119
return true; //! 表示在函数返回后立即发送回复
120120
});
121121

modules/eventx/timeout_monitor.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class TimeoutMonitor {
5757
void setCallback(const Callback &cb) { cb_ = cb; }
5858

5959
void add(const T &value);
60+
void clear();
6061

6162
void cleanup();
6263

modules/eventx/timeout_monitor_impl.hpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,31 @@ void TimeoutMonitor<T>::add(const T &value)
7373
++value_number_;
7474
}
7575

76+
template <typename T>
77+
void TimeoutMonitor<T>::clear()
78+
{
79+
if (value_number_ > 0) {
80+
value_number_ = 0;
81+
sp_timer_->disable();
82+
83+
PollItem *item = curr_item_;
84+
do {
85+
item->items.clear();
86+
item = item->next;
87+
} while (item != curr_item_);
88+
}
89+
}
90+
7691
template <typename T>
7792
void TimeoutMonitor<T>::cleanup()
7893
{
7994
if (curr_item_ == nullptr)
8095
return;
8196

82-
if (value_number_ > 0)
97+
if (value_number_ > 0) {
8398
sp_timer_->disable();
84-
value_number_ = 0;
99+
value_number_ = 0;
100+
}
85101

86102
PollItem *item = curr_item_->next;
87103
curr_item_->next = nullptr;

modules/eventx/timeout_monitor_test.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,39 @@ TEST(TimeoutMonitor, Basic)
5555
EXPECT_TRUE(run);
5656
}
5757

58+
//! 测试中途clear()的操作,观察有没有被误触发
59+
TEST(TimeoutMonitor, Clear)
60+
{
61+
auto sp_loop = Loop::New();
62+
auto sp_timer = sp_loop->newTimerEvent();
63+
SetScopeExitAction(
64+
[=] {
65+
delete sp_loop;
66+
delete sp_timer;
67+
}
68+
);
69+
sp_timer->initialize(std::chrono::milliseconds(25), Event::Mode::kOneshot);
70+
71+
TimeoutMonitor<int> tm(sp_loop);
72+
tm.initialize(milliseconds(10), 3);
73+
74+
sp_timer->setCallback([&] { tm.clear(); });
75+
sp_timer->enable();
76+
77+
bool run = false;
78+
tm.setCallback([&] (int value) {
79+
run = true;
80+
});
81+
82+
tm.add(100);
83+
tm.add(101);
84+
85+
sp_loop->exitLoop(milliseconds(120));
86+
sp_loop->runLoop();
87+
88+
EXPECT_FALSE(run);
89+
}
90+
5891
}
5992
}
6093
}

modules/flow/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ set(TBOX_FLOW_SOURCES
5151
actions/switch_action.cpp
5252
actions/execute_cmd_action.cpp
5353
actions/execute_in_thread_action.cpp
54+
actions/random_select_action.cpp
5455
to_graphviz.cpp)
5556

5657
set(TBOX_FLOW_TEST_SOURCES
@@ -74,6 +75,7 @@ set(TBOX_FLOW_TEST_SOURCES
7475
actions/switch_action_test.cpp
7576
actions/execute_cmd_action_test.cpp
7677
actions/execute_in_thread_action_test.cpp
78+
actions/random_select_action_test.cpp
7779
to_graphviz_test.cpp)
7880

7981
add_library(${TBOX_LIBRARY_NAME} ${TBOX_BUILD_LIB_TYPE} ${TBOX_FLOW_SOURCES})
@@ -136,6 +138,7 @@ install(
136138
actions/switch_action.h
137139
actions/execute_cmd_action.h
138140
actions/execute_in_thread_action.h
141+
actions/random_select_action.h
139142
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/tbox/flow/actions
140143
)
141144

modules/flow/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ HEAD_FILES = \
5151
actions/switch_action.h \
5252
actions/execute_cmd_action.h \
5353
actions/execute_in_thread_action.h \
54+
actions/random_select_action.h \
5455
to_graphviz.h \
5556

5657
CPP_SRC_FILES = \
@@ -75,6 +76,7 @@ CPP_SRC_FILES = \
7576
actions/switch_action.cpp \
7677
actions/execute_cmd_action.cpp \
7778
actions/execute_in_thread_action.cpp \
79+
actions/random_select_action.cpp \
7880
to_graphviz.cpp \
7981

8082
CXXFLAGS := -DMODULE_ID='"tbox.flow"' $(CXXFLAGS)
@@ -101,6 +103,7 @@ TEST_CPP_SRC_FILES = \
101103
actions/switch_action_test.cpp \
102104
actions/execute_cmd_action_test.cpp \
103105
actions/execute_in_thread_action_test.cpp \
106+
actions/random_select_action_test.cpp \
104107
to_graphviz_test.cpp \
105108

106109
TEST_LDFLAGS := $(LDFLAGS) -ltbox_flow -ltbox_eventx -ltbox_event -ltbox_util -ltbox_base -ldl

0 commit comments

Comments
 (0)