Skip to content

Commit

Permalink
The global sender could not be obtained due to different prefixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
mutouyun committed Sep 24, 2023
1 parent 21648c5 commit 6f31180
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 13 deletions.
4 changes: 2 additions & 2 deletions demo/win_service/client/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
4 changes: 2 additions & 2 deletions demo/win_service/service/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
25 changes: 21 additions & 4 deletions src/libipc/ipc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <vector>
#include <array>
#include <cassert>
#include <mutex>

#include "libipc/ipc.h"
#include "libipc/def.h"
Expand Down Expand Up @@ -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_t *>(acc_h.get());
acc_t *cc_acc(ipc::string const &pref) {
static ipc::unordered_map<ipc::string, ipc::shm::handle> handles;
static std::mutex lock;
std::lock_guard<std::mutex> 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<acc_t *>(it->second.get());
}

struct cache_t {
Expand Down Expand Up @@ -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() {
Expand Down
6 changes: 3 additions & 3 deletions src/libipc/platform/win/condition.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions src/libipc/waiter.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 6f31180

Please sign in to comment.