From 6f311805a0ea9dbdd94a4fea8d05d35d59e39a74 Mon Sep 17 00:00:00 2001 From: mutouyun Date: Sun, 24 Sep 2023 16:19:12 +0800 Subject: [PATCH] The global sender could not be obtained due to different prefixes. --- demo/win_service/client/main.cpp | 4 ++-- demo/win_service/service/main.cpp | 4 ++-- src/libipc/ipc.cpp | 25 +++++++++++++++++++++---- src/libipc/platform/win/condition.h | 6 +++--- src/libipc/waiter.h | 4 ++-- 5 files changed, 30 insertions(+), 13 deletions(-) diff --git a/demo/win_service/client/main.cpp b/demo/win_service/client/main.cpp index fea348e9..5e62d2c4 100644 --- a/demo/win_service/client/main.cpp +++ b/demo/win_service/client/main.cpp @@ -8,8 +8,8 @@ int _tmain (int argc, TCHAR *argv[]) { _tprintf(_T("My Sample Client: Entry\n")); - ipc::channel ipc_r{"service ipc r", ipc::receiver}; - ipc::channel ipc_w{"service ipc w", ipc::sender}; + ipc::channel ipc_r{ipc::prefix{"Global\\"}, "service ipc r", ipc::receiver}; + ipc::channel ipc_w{ipc::prefix{"Global\\"}, "service ipc w", ipc::sender}; while (1) { auto msg = ipc_r.recv(); if (msg.empty()) { diff --git a/demo/win_service/service/main.cpp b/demo/win_service/service/main.cpp index d61b08d5..ce522280 100644 --- a/demo/win_service/service/main.cpp +++ b/demo/win_service/service/main.cpp @@ -160,8 +160,8 @@ VOID WINAPI ServiceCtrlHandler (DWORD CtrlCode) { DWORD WINAPI ServiceWorkerThread (LPVOID lpParam) { OutputDebugString(_T("My Sample Service: ServiceWorkerThread: Entry")); - ipc::channel ipc_r{"service ipc r", ipc::sender}; - ipc::channel ipc_w{"service ipc w", ipc::receiver}; + ipc::channel ipc_r{ipc::prefix{"Global\\"}, "service ipc r", ipc::sender}; + ipc::channel ipc_w{ipc::prefix{"Global\\"}, "service ipc w", ipc::receiver}; // Periodically check if the service has been requested to stop while (WaitForSingleObject(g_ServiceStopEvent, 0) != WAIT_OBJECT_0) { diff --git a/src/libipc/ipc.cpp b/src/libipc/ipc.cpp index a892c486..3f3d3178 100755 --- a/src/libipc/ipc.cpp +++ b/src/libipc/ipc.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include "libipc/ipc.h" #include "libipc/def.h" @@ -69,9 +70,21 @@ ipc::buff_t make_cache(T& data, std::size_t size) { return { ptr, size, ipc::mem::free }; } -acc_t *cc_acc() { - static ipc::shm::handle acc_h("__CA_CONN__", sizeof(acc_t)); - return static_cast(acc_h.get()); +acc_t *cc_acc(ipc::string const &pref) { + static ipc::unordered_map handles; + static std::mutex lock; + std::lock_guard guard {lock}; + auto it = handles.find(pref); + if (it == handles.end()) { + ipc::string shm_name {ipc::make_prefix(pref, {"CA_CONN__"})}; + ipc::shm::handle h; + if (!h.acquire(shm_name.c_str(), sizeof(acc_t))) { + ipc::error("[cc_acc] acquire failed: %s\n", shm_name.c_str()); + return nullptr; + } + it = handles.emplace(pref, std::move(h)).first; + } + return static_cast(it->second.get()); } struct cache_t { @@ -101,11 +114,15 @@ struct conn_info_head { conn_info_head(char const * prefix, char const * name) : prefix_ {ipc::make_string(prefix)} , name_ {ipc::make_string(name)} - , cc_id_ {(cc_acc() == nullptr) ? 0 : cc_acc()->fetch_add(1, std::memory_order_relaxed)} + , cc_id_ {} , cc_waiter_{ipc::make_prefix(prefix_, {"CC_CONN__", name_}).c_str()} , wt_waiter_{ipc::make_prefix(prefix_, {"WT_CONN__", name_}).c_str()} , rd_waiter_{ipc::make_prefix(prefix_, {"RD_CONN__", name_}).c_str()} , acc_h_ {ipc::make_prefix(prefix_, {"AC_CONN__", name_}).c_str(), sizeof(acc_t)} { + acc_t *pacc = cc_acc(prefix_); + if (pacc != nullptr) { + cc_id_ = pacc->fetch_add(1, std::memory_order_relaxed); + } } void quit_waiting() { diff --git a/src/libipc/platform/win/condition.h b/src/libipc/platform/win/condition.h index 7f853c02..538aa082 100644 --- a/src/libipc/platform/win/condition.h +++ b/src/libipc/platform/win/condition.h @@ -44,15 +44,15 @@ class condition { bool open(char const *name) noexcept { close(); - if (!sem_.open((std::string{"_cond_sem_"} + name).c_str())) { + if (!sem_.open((std::string{name} + "_COND_SEM_").c_str())) { return false; } auto finally_sem = ipc::guard([this] { sem_.close(); }); // close when failed - if (!lock_.open((std::string{"_cond_lock_"} + name).c_str())) { + if (!lock_.open((std::string{name} + "_COND_LOCK_").c_str())) { return false; } auto finally_lock = ipc::guard([this] { lock_.close(); }); // close when failed - if (!shm_.acquire((std::string{"_cond_shm_"} + name).c_str(), sizeof(std::int32_t))) { + if (!shm_.acquire((std::string{name} + "_COND_SHM_").c_str(), sizeof(std::int32_t))) { return false; } finally_lock.dismiss(); diff --git a/src/libipc/waiter.h b/src/libipc/waiter.h index bb5b3b34..628ce5ea 100644 --- a/src/libipc/waiter.h +++ b/src/libipc/waiter.h @@ -36,10 +36,10 @@ class waiter { bool open(char const *name) noexcept { quit_.store(false, std::memory_order_relaxed); - if (!cond_.open((std::string{"_waiter_cond_"} + name).c_str())) { + if (!cond_.open((std::string{name} + "_WAITER_COND_").c_str())) { return false; } - if (!lock_.open((std::string{"_waiter_lock_"} + name).c_str())) { + if (!lock_.open((std::string{name} + "_WAITER_LOCK_").c_str())) { cond_.close(); return false; }