From 77842ab5e9274b3697885127048e1345ff0c5482 Mon Sep 17 00:00:00 2001 From: Hevake Date: Sun, 9 Oct 2022 23:50:17 +0800 Subject: [PATCH 1/3] feat(StateMachine): allow pass event extra data as pointer while invoke run() --- modules/util/state_machine.cpp | 30 ++--- modules/util/state_machine.h | 34 +++--- modules/util/state_machine_test.cpp | 166 ++++++++++++++++++++-------- 3 files changed, 153 insertions(+), 77 deletions(-) diff --git a/modules/util/state_machine.cpp b/modules/util/state_machine.cpp index a6fbff6..4208faa 100644 --- a/modules/util/state_machine.cpp +++ b/modules/util/state_machine.cpp @@ -27,7 +27,7 @@ class StateMachine::Impl { bool start(); void stop(); - bool run(EventID event_id); + bool run(Event event); StateID currentState() const; bool isTerminated() const; @@ -105,9 +105,9 @@ void StateMachine::stop() impl_->stop(); } -bool StateMachine::run(EventID event_id) +bool StateMachine::run(Event event) { - return impl_->run(event_id); + return impl_->run(event); } StateMachine::StateID StateMachine::currentState() const @@ -205,7 +205,7 @@ bool StateMachine::Impl::start() ++cb_level_; if (init_state->enter_action) - init_state->enter_action(); + init_state->enter_action(Event()); --cb_level_; curr_state_ = init_state; @@ -229,13 +229,13 @@ void StateMachine::Impl::stop() ++cb_level_; if (curr_state_->exit_action) - curr_state_->exit_action(); + curr_state_->exit_action(Event()); --cb_level_; curr_state_ = nullptr; } -bool StateMachine::Impl::run(EventID event_id) +bool StateMachine::Impl::run(Event event) { if (curr_state_ == nullptr) { LogWarn("need start first"); @@ -249,7 +249,7 @@ bool StateMachine::Impl::run(EventID event_id) //! 如果有子状态机,则给子状态机处理 if (curr_state_->sub_sm != nullptr) { - bool ret = curr_state_->sub_sm->run(event_id); + bool ret = curr_state_->sub_sm->run(event); if (!curr_state_->sub_sm->isTerminated()) return ret; curr_state_->sub_sm->stop(); @@ -257,10 +257,10 @@ bool StateMachine::Impl::run(EventID event_id) //! 找出可行的路径 auto iter = std::find_if(curr_state_->routes.begin(), curr_state_->routes.end(), - [event_id] (const Route &item) -> bool { - if (item.event_id != 0 && item.event_id != event_id) + [event] (const Route &item) -> bool { + if (item.event_id != 0 && item.event_id != event.id) return false; - if (item.guard != nullptr && !item.guard()) + if (item.guard != nullptr && !item.guard(event)) return false; return true; } @@ -286,23 +286,23 @@ bool StateMachine::Impl::run(EventID event_id) ++cb_level_; if (curr_state_->exit_action) - curr_state_->exit_action(); + curr_state_->exit_action(event); if (route.action) - route.action(); + route.action(event); if (next_state->enter_action) - next_state->enter_action(); + next_state->enter_action(event); auto last_state = curr_state_; curr_state_ = next_state; if (state_changed_cb_) - state_changed_cb_(last_state->id, curr_state_->id, event_id); + state_changed_cb_(last_state->id, curr_state_->id, event); //! 如果有子状态机,则给子状态机处理 if (curr_state_->sub_sm != nullptr) { curr_state_->sub_sm->start(); - curr_state_->sub_sm->run(event_id); + curr_state_->sub_sm->run(event); } --cb_level_; diff --git a/modules/util/state_machine.h b/modules/util/state_machine.h index ca89177..3a2e619 100644 --- a/modules/util/state_machine.h +++ b/modules/util/state_machine.h @@ -9,15 +9,28 @@ namespace util { //! HFSM,多层级有限状态机 class StateMachine { public: - using StateID = unsigned int; //! StateID 为 0 与 1 的两个状态为特定状态 - //! StateID = 0 的状态为终止状态,用户可以不用定义 - //! StateID = 1 的状态为默认的初始状态。也可以通过 setInitState() 重新指定 - using EventID = unsigned int; //! EventID = 0 表示任意事件,仅在 addRoute() 时使用 + using StateID = int; //! StateID 为 0 与 1 的两个状态为特定状态 + //! StateID = 0 的状态为终止状态,用户可以不用定义 + //! StateID = 1 的状态为默认的初始状态。也可以通过 setInitState() 重新指定 + using EventID = int; //! EventID = 0 表示任意事件,仅在 addRoute() 时使用 - using ActionFunc = std::function; - using GuardFunc = std::function; + struct Event { + EventID id = 0; + void *extra = nullptr; - using StateChangedCallback = std::function; + Event() { } + + template + Event(ET e) : id(static_cast(e)) { } + + template + Event(ET e, DT *p) : id(static_cast(e)), extra(p) { } + }; + + using ActionFunc = std::function; + using GuardFunc = std::function; + + using StateChangedCallback = std::function; public: StateMachine(); @@ -98,12 +111,7 @@ class StateMachine { * * \return bool 状态是否变换 */ - bool run(EventID event_id); - - template - bool run(E event_id) { - return run(static_cast(event_id)); - } + bool run(Event event); /** * \brief 获取当前状态ID diff --git a/modules/util/state_machine_test.cpp b/modules/util/state_machine_test.cpp index 568d7f0..9f3d588 100644 --- a/modules/util/state_machine_test.cpp +++ b/modules/util/state_machine_test.cpp @@ -3,7 +3,11 @@ #include "state_machine.h" using namespace std; -using namespace tbox::util; + +namespace tbox { +namespace util { + +using SM = StateMachine; enum State { STATE_A = 1, @@ -22,7 +26,7 @@ enum Event { //! 测试创建状态 TEST(StateMachine, NewState) { - StateMachine sm; + SM sm; EXPECT_TRUE (sm.newState(STATE_A, nullptr, nullptr)); EXPECT_TRUE (sm.newState(STATE_B, nullptr, nullptr)); EXPECT_FALSE(sm.newState(STATE_A, nullptr, nullptr)); @@ -31,7 +35,7 @@ TEST(StateMachine, NewState) //! 测试创建事件 TEST(StateMachine, AddRoute) { - StateMachine sm; + SM sm; sm.newState(STATE_A, nullptr, nullptr); sm.newState(STATE_B, nullptr, nullptr); @@ -44,7 +48,7 @@ TEST(StateMachine, AddRoute) //! 测试创建两个状态,一个事件由A到B转换,没有守卫与执行函数 TEST(StateMachine, StateWithoutGuardAndAction) { - StateMachine sm; + SM sm; sm.newState(STATE_A, nullptr, nullptr); sm.newState(STATE_B, nullptr, nullptr); sm.addRoute(STATE_A, EVENT_1, STATE_B, nullptr, nullptr); @@ -60,14 +64,14 @@ TEST(StateMachine, StateWithoutGuardAndAction) //! 测试创建两个状态,一个事件由A到B转换,有守卫 TEST(StateMachine, RouteWithGuard) { - StateMachine sm; + SM sm; sm.newState(STATE_A, nullptr, nullptr); sm.newState(STATE_B, nullptr, nullptr); sm.newState(STATE_C, nullptr, nullptr); bool condition = false; - sm.addRoute(STATE_A, EVENT_1, STATE_B, [&]{ return !condition; }, nullptr); - sm.addRoute(STATE_A, EVENT_1, STATE_C, [&]{ return condition; }, nullptr); + sm.addRoute(STATE_A, EVENT_1, STATE_B, [&](SM::Event){ return !condition; }, nullptr); + sm.addRoute(STATE_A, EVENT_1, STATE_C, [&](SM::Event){ return condition; }, nullptr); sm.addRoute(STATE_B, EVENT_2, STATE_A, nullptr, nullptr); sm.addRoute(STATE_C, EVENT_2, STATE_A, nullptr, nullptr); sm.setInitState(STATE_A); @@ -93,15 +97,15 @@ TEST(StateMachine, RouteWithGuard) //! 测试创建两个状态,一个事件由A到B转换,有进入与退出动作 TEST(StateMachine, StateWithEnterAndExitAction) { - StateMachine sm; + SM sm; int a_enter_counter = 0; int a_exit_counter = 0; int b_enter_counter = 0; int b_exit_counter = 0; - sm.newState(STATE_A, [&] { ++a_enter_counter; }, [&] { ++a_exit_counter; }); - sm.newState(STATE_B, [&] { ++b_enter_counter; }, [&] { ++b_exit_counter; }); + sm.newState(STATE_A, [&](SM::Event){ ++a_enter_counter; }, [&](SM::Event){ ++a_exit_counter; }); + sm.newState(STATE_B, [&](SM::Event){ ++b_enter_counter; }, [&](SM::Event){ ++b_exit_counter; }); sm.addRoute(STATE_A, EVENT_1, STATE_B, nullptr, nullptr); sm.addRoute(STATE_B, EVENT_2, STATE_A, nullptr, nullptr); @@ -123,7 +127,7 @@ TEST(StateMachine, StateWithEnterAndExitAction) //! 测试创建两个状态,一个事件由A到B转换,事件有转换动作 TEST(StateMachine, EventWithAction) { - StateMachine sm; + SM sm; sm.newState(STATE_A, nullptr, nullptr); sm.newState(STATE_B, nullptr, nullptr); @@ -131,8 +135,8 @@ TEST(StateMachine, EventWithAction) int count_1 = 0; int count_2 = 0; - sm.addRoute(STATE_A, EVENT_1, STATE_B, nullptr, [&]{ ++count_1; }); - sm.addRoute(STATE_B, EVENT_2, STATE_A, nullptr, [&]{ ++count_2; }); + sm.addRoute(STATE_A, EVENT_1, STATE_B, nullptr, [&](SM::Event){ ++count_1; }); + sm.addRoute(STATE_B, EVENT_2, STATE_A, nullptr, [&](SM::Event){ ++count_2; }); sm.setInitState(STATE_A); sm.start(); @@ -145,7 +149,7 @@ TEST(StateMachine, EventWithAction) TEST(StateMachine, AnyEvent) { - StateMachine sm; + SM sm; sm.newState(STATE_A, nullptr, nullptr); sm.newState(STATE_B, nullptr, nullptr); @@ -165,15 +169,15 @@ TEST(StateMachine, AnyEvent) TEST(StateMachine, Restart) { - StateMachine sm; + SM sm; int a_enter_counter = 0; int a_exit_counter = 0; int b_enter_counter = 0; int b_exit_counter = 0; - sm.newState(STATE_A, [&] { ++a_enter_counter; }, [&] { ++a_exit_counter; }); - sm.newState(STATE_B, [&] { ++b_enter_counter; }, [&] { ++b_exit_counter; }); + sm.newState(STATE_A, [&](SM::Event){ ++a_enter_counter; }, [&](SM::Event){ ++a_exit_counter; }); + sm.newState(STATE_B, [&](SM::Event){ ++b_enter_counter; }, [&](SM::Event){ ++b_exit_counter; }); sm.addRoute(STATE_A, EVENT_1, STATE_B, nullptr, nullptr); sm.setInitState(STATE_A); @@ -191,14 +195,14 @@ TEST(StateMachine, Restart) //! 主要测试用 enum class 定义的状态与事件是否能编译过 TEST(StateMachine, EnumClass) { - StateMachine sm; + SM sm; enum class State { k1 = 1, k2 }; enum class Event { k1 = 1, k2 }; int enter_counter = 0; int exit_counter = 0; - sm.newState(State::k1, [&] { ++enter_counter; }, [&] { ++exit_counter; }); + sm.newState(State::k1, [&](SM::Event){ ++enter_counter; }, [&](SM::Event){ ++exit_counter; }); sm.newState(State::k2, nullptr, nullptr); sm.addRoute(State::k1, Event::k1, State::k2, nullptr, nullptr); @@ -213,12 +217,12 @@ TEST(StateMachine, EnumClass) EXPECT_EQ(exit_counter, 2); } -TEST(StateMachine, SubStateMachine) +TEST(StateMachine, SubSM) { enum class State { kTerm, kInit, k1, k2 }; enum class Event { kTerm, k1, k2 }; - StateMachine sm, sub_sm; + SM sm, sub_sm; sm.newState(State::kInit, nullptr, nullptr); sm.newState(State::k1, nullptr, nullptr); @@ -328,7 +332,7 @@ TEST(StateMachine, StateChangedCallback) enum class State { kTerm, kInit, k1 }; enum class Event { kNone, k1 }; - StateMachine sm; + SM sm; sm.newState(State::kInit, nullptr, nullptr); sm.newState(State::k1, nullptr, nullptr); @@ -340,10 +344,10 @@ TEST(StateMachine, StateChangedCallback) Event event; sm.setStateChangedCallback( - [&] (StateMachine::StateID f, StateMachine::StateID t, StateMachine::EventID e) { + [&] (SM::StateID f, SM::StateID t, SM::Event e) { from = static_cast(f); to = static_cast(t); - event = static_cast(e); + event = static_cast(e.id); } ); sm.start(); @@ -364,27 +368,27 @@ TEST(StateMachine, InitStateHasSubMachine) enum class State { kTerm, kInit }; enum class Event { kNone, k1 }; - StateMachine sm; - StateMachine sub_sm; + SM sm; + SM sub_sm; int step = 0; - sm.newState(State::kInit, [&]{ EXPECT_EQ(step, 0); ++step; }, [&]{ EXPECT_EQ(step, 6); ++step; }); - sm.newState(State::kTerm, [&]{ EXPECT_EQ(step, 8); ++step; }, [&]{ EXPECT_EQ(step, 9); ++step; }); - sm.addRoute(State::kInit, Event::k1, State::kTerm, nullptr, [&]{ EXPECT_EQ(step, 7); ++step; }); + sm.newState(State::kInit, [&](SM::Event){ EXPECT_EQ(step, 0); ++step; }, [&](SM::Event){ EXPECT_EQ(step, 6); ++step; }); + sm.newState(State::kTerm, [&](SM::Event){ EXPECT_EQ(step, 8); ++step; }, [&](SM::Event){ EXPECT_EQ(step, 9); ++step; }); + sm.addRoute(State::kInit, Event::k1, State::kTerm, nullptr, [&](SM::Event){ EXPECT_EQ(step, 7); ++step; }); - sub_sm.newState(State::kInit, [&]{ EXPECT_EQ(step, 1); ++step; }, [&]{ EXPECT_EQ(step, 2); ++step; }); - sub_sm.newState(State::kTerm, [&]{ EXPECT_EQ(step, 4); ++step; }, [&]{ EXPECT_EQ(step, 5); ++step; }); - sub_sm.addRoute(State::kInit, Event::k1, State::kTerm, nullptr, [&]{ EXPECT_EQ(step, 3); ++step; }); + sub_sm.newState(State::kInit, [&](SM::Event){ EXPECT_EQ(step, 1); ++step; }, [&](SM::Event){ EXPECT_EQ(step, 2); ++step; }); + sub_sm.newState(State::kTerm, [&](SM::Event){ EXPECT_EQ(step, 4); ++step; }, [&](SM::Event){ EXPECT_EQ(step, 5); ++step; }); + sub_sm.addRoute(State::kInit, Event::k1, State::kTerm, nullptr, [&](SM::Event){ EXPECT_EQ(step, 3); ++step; }); sm.setSubStateMachine(State::kInit, &sub_sm); #if 0 sm.setStateChangedCallback( - [&] (StateMachine::StateID f, StateMachine::StateID t, StateMachine::EventID e) { + [&] (SM::StateID f, SM::StateID t, SM::EventID e) { cout << f << "-->" << t << "," << e << endl; } ); sub_sm.setStateChangedCallback( - [&] (StateMachine::StateID f, StateMachine::StateID t, StateMachine::EventID e) { + [&] (SM::StateID f, SM::StateID t, SM::EventID e) { cout << " " << f << "-->" << t << "," << e << endl; } ); @@ -395,35 +399,35 @@ TEST(StateMachine, InitStateHasSubMachine) } //! 测试各种Action之间的执行顺序 -TEST(StateMachine, SubStateMachineActionOrder) +TEST(StateMachine, SubSMActionOrder) { enum class State { kTerm, kInit, k1}; enum class Event { kNone, k1 }; - StateMachine sm; - StateMachine sub_sm; + SM sm; + SM sub_sm; int step = 0; - sm.newState(State::kInit, [&]{ EXPECT_EQ(step, 0); ++step; }, [&]{ EXPECT_EQ(step, 1); ++step; }); - sm.newState(State::k1, [&]{ EXPECT_EQ(step, 3); ++step; }, [&]{ EXPECT_EQ(step, 9); ++step; }); - sm.newState(State::kTerm, [&]{ EXPECT_EQ(step, 11); ++step; }, [&]{ EXPECT_EQ(step, 12); ++step; }); + sm.newState(State::kInit, [&](SM::Event){ EXPECT_EQ(step, 0); ++step; }, [&](SM::Event){ EXPECT_EQ(step, 1); ++step; }); + sm.newState(State::k1, [&](SM::Event){ EXPECT_EQ(step, 3); ++step; }, [&](SM::Event){ EXPECT_EQ(step, 9); ++step; }); + sm.newState(State::kTerm, [&](SM::Event){ EXPECT_EQ(step, 11); ++step; }, [&](SM::Event){ EXPECT_EQ(step, 12); ++step; }); - sm.addRoute(State::kInit, Event::k1, State::k1, nullptr, [&]{ EXPECT_EQ(step, 2); ++step; }); - sm.addRoute(State::k1, Event::k1, State::kTerm, nullptr, [&]{ EXPECT_EQ(step, 10); ++step; }); + sm.addRoute(State::kInit, Event::k1, State::k1, nullptr, [&](SM::Event){ EXPECT_EQ(step, 2); ++step; }); + sm.addRoute(State::k1, Event::k1, State::kTerm, nullptr, [&](SM::Event){ EXPECT_EQ(step, 10); ++step; }); - sub_sm.newState(State::kInit, [&]{ EXPECT_EQ(step, 4); ++step; }, [&]{ EXPECT_EQ(step, 5); ++step; }); - sub_sm.newState(State::kTerm, [&]{ EXPECT_EQ(step, 7); ++step; }, [&]{ EXPECT_EQ(step, 8); ++step; }); - sub_sm.addRoute(State::kInit, Event::k1, State::kTerm, nullptr, [&]{ EXPECT_EQ(step, 6); ++step; }); + sub_sm.newState(State::kInit, [&](SM::Event){ EXPECT_EQ(step, 4); ++step; }, [&](SM::Event){ EXPECT_EQ(step, 5); ++step; }); + sub_sm.newState(State::kTerm, [&](SM::Event){ EXPECT_EQ(step, 7); ++step; }, [&](SM::Event){ EXPECT_EQ(step, 8); ++step; }); + sub_sm.addRoute(State::kInit, Event::k1, State::kTerm, nullptr, [&](SM::Event){ EXPECT_EQ(step, 6); ++step; }); sm.setSubStateMachine(State::k1, &sub_sm); #if 0 sm.setStateChangedCallback( - [&] (StateMachine::StateID f, StateMachine::StateID t, StateMachine::EventID e) { + [&] (SM::StateID f, SM::StateID t, SM::EventID e) { cout << f << "-->" << t << "," << e << endl; } ); sub_sm.setStateChangedCallback( - [&] (StateMachine::StateID f, StateMachine::StateID t, StateMachine::EventID e) { + [&] (SM::StateID f, SM::StateID t, SM::EventID e) { cout << " " << f << "-->" << t << "," << e << endl; } ); @@ -434,12 +438,73 @@ TEST(StateMachine, SubStateMachineActionOrder) sm.stop(); } +TEST(StateMachine, EventExtra) +{ + enum class State { kTerm, kInit }; + + SM sm; + + int extra_data = 100; + int count = 0; + sm.newState(State::kInit, + [&](SM::Event e){ + ++count; + EXPECT_EQ(e.id, 0); + EXPECT_EQ(e.extra, nullptr); + }, + [&](SM::Event e){ + ++count; + EXPECT_EQ(e.id, 10); + EXPECT_EQ(e.extra, &extra_data); + } + ); + sm.newState(State::kTerm, + [&](SM::Event e){ + ++count; + EXPECT_EQ(e.id, 10); + EXPECT_EQ(e.extra, &extra_data); + }, + [&](SM::Event e){ + ++count; + EXPECT_EQ(e.id, 0); + EXPECT_EQ(e.extra, nullptr); + } + ); + + sm.addRoute(State::kInit, 10, State::kTerm, + [&](SM::Event e){ + ++count; + EXPECT_EQ(e.id, 10); + EXPECT_EQ(e.extra, &extra_data); + return true; + }, + [&](SM::Event e){ + ++count; + EXPECT_EQ(e.id, 10); + EXPECT_EQ(e.extra, &extra_data); + } + ); + + sm.setStateChangedCallback( + [&] (SM::StateID f, SM::StateID t, SM::Event e) { + ++count; + EXPECT_EQ(e.id, 10); + EXPECT_EQ(e.extra, &extra_data); + } + ); + + sm.start(); + sm.run(SM::Event(10, &extra_data)); + sm.stop(); + EXPECT_EQ(count, 7); +} + TEST(StateMachine, SetInitState) { enum class State { kTerm, k1, kInit }; enum class Event { kNone, k1 }; - StateMachine sm; + SM sm; sm.setInitState(State::kInit); sm.newState(State::kInit, nullptr, nullptr); @@ -454,10 +519,13 @@ TEST(StateMachine, SetInitState_Fail) enum class State { kTerm, k1, kInit }; enum class Event { kNone, k1 }; - StateMachine sm; + SM sm; sm.setInitState(State::kInit); sm.newState(State::k1, nullptr, nullptr); EXPECT_FALSE(sm.start()); } + +} +} -- Gitee From 9cd46ad03a39c5f16536aaed635019b53c09da75 Mon Sep 17 00:00:00 2001 From: hevake_lcj Date: Tue, 11 Oct 2022 11:35:58 +0800 Subject: [PATCH 2/3] =?UTF-8?q?fix:=E4=BF=AE=E5=A4=8Dmqtt=E4=B8=AD?= =?UTF-8?q?=E7=9A=84=E6=8B=BC=E5=86=99=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/mqtt/client.cpp | 2 +- modules/mqtt/client.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/mqtt/client.cpp b/modules/mqtt/client.cpp index 1b62d1d..a1844d2 100644 --- a/modules/mqtt/client.cpp +++ b/modules/mqtt/client.cpp @@ -293,7 +293,7 @@ int Client::subscribe(const std::string &topic, int *p_mid, int qos) return ret == MOSQ_ERR_SUCCESS; } -int Client::ubsubscribe(const std::string &topic, int *p_mid) +int Client::unsubscribe(const std::string &topic, int *p_mid) { if (d_->state != State::kConnected) { LogWarn("broke is disconnected"); diff --git a/modules/mqtt/client.h b/modules/mqtt/client.h index 2406ff0..8435fb7 100644 --- a/modules/mqtt/client.h +++ b/modules/mqtt/client.h @@ -86,7 +86,7 @@ class Client { //! 订阅与取消订阅 int subscribe(const std::string &topic, int *mid = nullptr, int qos = 0); - int ubsubscribe(const std::string &topic, int *mid = nullptr); + int unsubscribe(const std::string &topic, int *mid = nullptr); //! 发布消息 int publish(const std::string &topic, -- Gitee From eba3c5202fc187acd736a7344b1061c0801c3f85 Mon Sep 17 00:00:00 2001 From: Hevake Date: Tue, 11 Oct 2022 23:50:54 +0800 Subject: [PATCH 3/3] =?UTF-8?q?fix:=20=E8=A7=A3=E5=86=B3UnixDomainSocket?= =?UTF-8?q?=E5=9C=A8bind=E6=97=B6=E5=9B=A0=E5=B7=B2=E5=AD=98=E5=9C=A8socke?= =?UTF-8?q?t=E6=96=87=E4=BB=B6=E5=BC=95=E8=B5=B7=E7=9A=84bind=E5=A4=B1?= =?UTF-8?q?=E8=B4=A5=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/network/tcp_acceptor.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/modules/network/tcp_acceptor.cpp b/modules/network/tcp_acceptor.cpp index 727e0c8..776e18e 100644 --- a/modules/network/tcp_acceptor.cpp +++ b/modules/network/tcp_acceptor.cpp @@ -75,7 +75,11 @@ int TcpAcceptor::bindAddress(SocketFd sock_fd, const SockAddr &bind_addr) struct sockaddr_in sock_addr; socklen_t len = bind_addr.toSockAddr(sock_addr); return sock_fd.bind((const struct sockaddr*)&sock_addr, len); + } else if (bind_addr.type() == SockAddr::Type::kLocal) { + //! 为防止存在的同名文件导致bind失败,在bind之前要先尝试删除原有的文件 + ::unlink(bind_addr_.toString().c_str()); + struct sockaddr_un sock_addr; socklen_t len = bind_addr.toSockAddr(sock_addr); return sock_fd.bind((const struct sockaddr*)&sock_addr, len); -- Gitee