mirror of
https://github.com/mutouyun/cpp-ipc.git
synced 2025-12-06 16:56:45 +08:00
The global sender could not be obtained due to different prefixes.
This commit is contained in:
parent
21648c5f47
commit
6f311805a0
@ -8,8 +8,8 @@
|
|||||||
|
|
||||||
int _tmain (int argc, TCHAR *argv[]) {
|
int _tmain (int argc, TCHAR *argv[]) {
|
||||||
_tprintf(_T("My Sample Client: Entry\n"));
|
_tprintf(_T("My Sample Client: Entry\n"));
|
||||||
ipc::channel ipc_r{"service ipc r", ipc::receiver};
|
ipc::channel ipc_r{ipc::prefix{"Global\\"}, "service ipc r", ipc::receiver};
|
||||||
ipc::channel ipc_w{"service ipc w", ipc::sender};
|
ipc::channel ipc_w{ipc::prefix{"Global\\"}, "service ipc w", ipc::sender};
|
||||||
while (1) {
|
while (1) {
|
||||||
auto msg = ipc_r.recv();
|
auto msg = ipc_r.recv();
|
||||||
if (msg.empty()) {
|
if (msg.empty()) {
|
||||||
|
|||||||
@ -160,8 +160,8 @@ VOID WINAPI ServiceCtrlHandler (DWORD CtrlCode) {
|
|||||||
|
|
||||||
DWORD WINAPI ServiceWorkerThread (LPVOID lpParam) {
|
DWORD WINAPI ServiceWorkerThread (LPVOID lpParam) {
|
||||||
OutputDebugString(_T("My Sample Service: ServiceWorkerThread: Entry"));
|
OutputDebugString(_T("My Sample Service: ServiceWorkerThread: Entry"));
|
||||||
ipc::channel ipc_r{"service ipc r", ipc::sender};
|
ipc::channel ipc_r{ipc::prefix{"Global\\"}, "service ipc r", ipc::sender};
|
||||||
ipc::channel ipc_w{"service ipc w", ipc::receiver};
|
ipc::channel ipc_w{ipc::prefix{"Global\\"}, "service ipc w", ipc::receiver};
|
||||||
|
|
||||||
// Periodically check if the service has been requested to stop
|
// Periodically check if the service has been requested to stop
|
||||||
while (WaitForSingleObject(g_ServiceStopEvent, 0) != WAIT_OBJECT_0) {
|
while (WaitForSingleObject(g_ServiceStopEvent, 0) != WAIT_OBJECT_0) {
|
||||||
|
|||||||
@ -9,6 +9,7 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
#include <mutex>
|
||||||
|
|
||||||
#include "libipc/ipc.h"
|
#include "libipc/ipc.h"
|
||||||
#include "libipc/def.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 };
|
return { ptr, size, ipc::mem::free };
|
||||||
}
|
}
|
||||||
|
|
||||||
acc_t *cc_acc() {
|
acc_t *cc_acc(ipc::string const &pref) {
|
||||||
static ipc::shm::handle acc_h("__CA_CONN__", sizeof(acc_t));
|
static ipc::unordered_map<ipc::string, ipc::shm::handle> handles;
|
||||||
return static_cast<acc_t *>(acc_h.get());
|
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 {
|
struct cache_t {
|
||||||
@ -101,11 +114,15 @@ struct conn_info_head {
|
|||||||
conn_info_head(char const * prefix, char const * name)
|
conn_info_head(char const * prefix, char const * name)
|
||||||
: prefix_ {ipc::make_string(prefix)}
|
: prefix_ {ipc::make_string(prefix)}
|
||||||
, name_ {ipc::make_string(name)}
|
, 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()}
|
, cc_waiter_{ipc::make_prefix(prefix_, {"CC_CONN__", name_}).c_str()}
|
||||||
, wt_waiter_{ipc::make_prefix(prefix_, {"WT_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()}
|
, 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_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() {
|
void quit_waiting() {
|
||||||
|
|||||||
@ -44,15 +44,15 @@ public:
|
|||||||
|
|
||||||
bool open(char const *name) noexcept {
|
bool open(char const *name) noexcept {
|
||||||
close();
|
close();
|
||||||
if (!sem_.open((std::string{"_cond_sem_"} + name).c_str())) {
|
if (!sem_.open((std::string{name} + "_COND_SEM_").c_str())) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
auto finally_sem = ipc::guard([this] { sem_.close(); }); // close when failed
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
auto finally_lock = ipc::guard([this] { lock_.close(); }); // close when failed
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
finally_lock.dismiss();
|
finally_lock.dismiss();
|
||||||
|
|||||||
@ -36,10 +36,10 @@ public:
|
|||||||
|
|
||||||
bool open(char const *name) noexcept {
|
bool open(char const *name) noexcept {
|
||||||
quit_.store(false, std::memory_order_relaxed);
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
if (!lock_.open((std::string{"_waiter_lock_"} + name).c_str())) {
|
if (!lock_.open((std::string{name} + "_WAITER_LOCK_").c_str())) {
|
||||||
cond_.close();
|
cond_.close();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user