Skip to content

Commit cf72d02

Browse files
committed
The global sender could not be obtained due to different prefixes.
1 parent fab3f6f commit cf72d02

File tree

5 files changed

+30
-13
lines changed

5 files changed

+30
-13
lines changed

demo/win_service/client/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
int _tmain (int argc, TCHAR *argv[]) {
1010
_tprintf(_T("My Sample Client: Entry\n"));
11-
ipc::channel ipc_r{"service ipc r", ipc::receiver};
12-
ipc::channel ipc_w{"service ipc w", ipc::sender};
11+
ipc::channel ipc_r{ipc::prefix{"Global\\"}, "service ipc r", ipc::receiver};
12+
ipc::channel ipc_w{ipc::prefix{"Global\\"}, "service ipc w", ipc::sender};
1313
while (1) {
1414
auto msg = ipc_r.recv();
1515
if (msg.empty()) {

demo/win_service/service/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,8 @@ VOID WINAPI ServiceCtrlHandler (DWORD CtrlCode) {
160160

161161
DWORD WINAPI ServiceWorkerThread (LPVOID lpParam) {
162162
OutputDebugString(_T("My Sample Service: ServiceWorkerThread: Entry"));
163-
ipc::channel ipc_r{"service ipc r", ipc::sender};
164-
ipc::channel ipc_w{"service ipc w", ipc::receiver};
163+
ipc::channel ipc_r{ipc::prefix{"Global\\"}, "service ipc r", ipc::sender};
164+
ipc::channel ipc_w{ipc::prefix{"Global\\"}, "service ipc w", ipc::receiver};
165165

166166
// Periodically check if the service has been requested to stop
167167
while (WaitForSingleObject(g_ServiceStopEvent, 0) != WAIT_OBJECT_0) {

src/libipc/ipc.cpp

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <vector>
1010
#include <array>
1111
#include <cassert>
12+
#include <mutex>
1213

1314
#include "libipc/ipc.h"
1415
#include "libipc/def.h"
@@ -69,9 +70,21 @@ ipc::buff_t make_cache(T& data, std::size_t size) {
6970
return { ptr, size, ipc::mem::free };
7071
}
7172

72-
acc_t *cc_acc() {
73-
static ipc::shm::handle acc_h("__CA_CONN__", sizeof(acc_t));
74-
return static_cast<acc_t *>(acc_h.get());
73+
acc_t *cc_acc(ipc::string const &pref) {
74+
static ipc::unordered_map<ipc::string, ipc::shm::handle> handles;
75+
static std::mutex lock;
76+
std::lock_guard<std::mutex> guard {lock};
77+
auto it = handles.find(pref);
78+
if (it == handles.end()) {
79+
ipc::string shm_name {ipc::make_prefix(pref, {"CA_CONN__"})};
80+
ipc::shm::handle h;
81+
if (!h.acquire(shm_name.c_str(), sizeof(acc_t))) {
82+
ipc::error("[cc_acc] acquire failed: %s\n", shm_name.c_str());
83+
return nullptr;
84+
}
85+
it = handles.emplace(pref, std::move(h)).first;
86+
}
87+
return static_cast<acc_t *>(it->second.get());
7588
}
7689

7790
struct cache_t {
@@ -101,11 +114,15 @@ struct conn_info_head {
101114
conn_info_head(char const * prefix, char const * name)
102115
: prefix_ {ipc::make_string(prefix)}
103116
, name_ {ipc::make_string(name)}
104-
, cc_id_ {(cc_acc() == nullptr) ? 0 : cc_acc()->fetch_add(1, std::memory_order_relaxed)}
117+
, cc_id_ {}
105118
, cc_waiter_{ipc::make_prefix(prefix_, {"CC_CONN__", name_}).c_str()}
106119
, wt_waiter_{ipc::make_prefix(prefix_, {"WT_CONN__", name_}).c_str()}
107120
, rd_waiter_{ipc::make_prefix(prefix_, {"RD_CONN__", name_}).c_str()}
108121
, acc_h_ {ipc::make_prefix(prefix_, {"AC_CONN__", name_}).c_str(), sizeof(acc_t)} {
122+
acc_t *pacc = cc_acc(prefix_);
123+
if (pacc != nullptr) {
124+
cc_id_ = pacc->fetch_add(1, std::memory_order_relaxed);
125+
}
109126
}
110127

111128
void quit_waiting() {

src/libipc/platform/win/condition.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,15 @@ class condition {
4444

4545
bool open(char const *name) noexcept {
4646
close();
47-
if (!sem_.open((std::string{"_cond_sem_"} + name).c_str())) {
47+
if (!sem_.open((std::string{name} + "_COND_SEM_").c_str())) {
4848
return false;
4949
}
5050
auto finally_sem = ipc::guard([this] { sem_.close(); }); // close when failed
51-
if (!lock_.open((std::string{"_cond_lock_"} + name).c_str())) {
51+
if (!lock_.open((std::string{name} + "_COND_LOCK_").c_str())) {
5252
return false;
5353
}
5454
auto finally_lock = ipc::guard([this] { lock_.close(); }); // close when failed
55-
if (!shm_.acquire((std::string{"_cond_shm_"} + name).c_str(), sizeof(std::int32_t))) {
55+
if (!shm_.acquire((std::string{name} + "_COND_SHM_").c_str(), sizeof(std::int32_t))) {
5656
return false;
5757
}
5858
finally_lock.dismiss();

src/libipc/waiter.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ class waiter {
3636

3737
bool open(char const *name) noexcept {
3838
quit_.store(false, std::memory_order_relaxed);
39-
if (!cond_.open((std::string{"_waiter_cond_"} + name).c_str())) {
39+
if (!cond_.open((std::string{name} + "_WAITER_COND_").c_str())) {
4040
return false;
4141
}
42-
if (!lock_.open((std::string{"_waiter_lock_"} + name).c_str())) {
42+
if (!lock_.open((std::string{name} + "_WAITER_LOCK_").c_str())) {
4343
cond_.close();
4444
return false;
4545
}

0 commit comments

Comments
 (0)