From f09378b481350c19052c4b7160088b3f128c5fac Mon Sep 17 00:00:00 2001 From: mutouyun Date: Sun, 16 Dec 2018 21:30:55 +0800 Subject: [PATCH] fix some bugs; if __has_include(), using tls_pointer_linux.cpp; add some channel::send overloads --- include/ipc.h | 8 ++++++-- src/ipc.cpp | 17 ++++++++++++----- src/platform/shm_win.cpp | 2 +- src/platform/tls_pointer_win.cpp | 18 ++++++++++++------ test/test_ipc.cpp | 19 +++++++------------ 5 files changed, 38 insertions(+), 26 deletions(-) diff --git a/include/ipc.h b/include/ipc.h index 64ca2c7..f910744 100644 --- a/include/ipc.h +++ b/include/ipc.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include "export.h" #include "def.h" @@ -13,7 +14,7 @@ using shm::handle_t; IPC_EXPORT handle_t connect (char const * name); IPC_EXPORT void disconnect(handle_t h); -IPC_EXPORT bool send(handle_t h, void* data, std::size_t size); +IPC_EXPORT bool send(handle_t h, void const * data, std::size_t size); IPC_EXPORT std::vector recv(handle_t h); class IPC_EXPORT channel { @@ -35,7 +36,10 @@ public: bool connect(char const * name); void disconnect(void); - bool send(void* data, std::size_t size); + bool send(void const * data, std::size_t size); + bool send(std::vector const & buff); + bool send(std::string const & str); + std::vector recv(); private: diff --git a/src/ipc.cpp b/src/ipc.cpp index 148b75e..13be34f 100644 --- a/src/ipc.cpp +++ b/src/ipc.cpp @@ -4,7 +4,6 @@ #include #include #include -#include #include #include #include @@ -107,7 +106,7 @@ void disconnect(handle_t h) { shm::release(h, sizeof(queue_t)); } -bool send(handle_t h, void* data, std::size_t size) { +bool send(handle_t h, void const * data, std::size_t size) { if (data == nullptr) { return false; } @@ -127,7 +126,7 @@ bool send(handle_t h, void* data, std::size_t size) { static_cast(size) - offset - static_cast(data_length), msg_id, { 0 } }; - std::memcpy(msg.data_, static_cast(data) + offset, data_length); + std::memcpy(msg.data_, static_cast(data) + offset, data_length); queue->push(msg); } // if remain > 0, this is the last message fragment @@ -137,7 +136,7 @@ bool send(handle_t h, void* data, std::size_t size) { remain - static_cast(data_length), msg_id, { 0 } }; - std::memcpy(msg.data_, static_cast(data) + offset, + std::memcpy(msg.data_, static_cast(data) + offset, static_cast(remain)); queue->push(msg); } @@ -240,10 +239,18 @@ void channel::disconnect(void) { ipc::disconnect(impl(p_)->h_); } -bool channel::send(void* data, std::size_t size) { +bool channel::send(void const *data, std::size_t size) { return ipc::send(impl(p_)->h_, data, size); } +bool channel::send(std::vector const & buff) { + return channel::send(buff.data(), buff.size()); +} + +bool channel::send(std::string const & str) { + return channel::send(str.c_str(), str.size() + 1); +} + std::vector channel::recv() { return ipc::recv(impl(p_)->h_); } diff --git a/src/platform/shm_win.cpp b/src/platform/shm_win.cpp index 1f558af..ce706db 100644 --- a/src/platform/shm_win.cpp +++ b/src/platform/shm_win.cpp @@ -1,6 +1,6 @@ #include "shm.h" -#include +#include #include #include diff --git a/src/platform/tls_pointer_win.cpp b/src/platform/tls_pointer_win.cpp index e6f70c5..f2c0212 100644 --- a/src/platform/tls_pointer_win.cpp +++ b/src/platform/tls_pointer_win.cpp @@ -1,6 +1,10 @@ +#if __has_include() +#include "tls_pointer_linux.cpp" +#else /*!__has_include()*/ + #include "tls_pointer.h" -#include // ::Tls... +#include // ::Tls... #include // std::unordered_map namespace ipc { @@ -117,7 +121,7 @@ void NTAPI OnTlsCallback(PVOID, DWORD dwReason, PVOID) { #if defined(_MSC_VER) -#if defined(IPC_OS_WIN64_) +#if defined(WIN64) || defined(_WIN64) || defined(__WIN64__) #pragma comment(linker, "/INCLUDE:_tls_used") #pragma comment(linker, "/INCLUDE:_tls_xl_b__") @@ -129,7 +133,7 @@ extern "C" { # pragma const_seg() } -#else /*!IPC_OS_WIN64_*/ +#else /*!WIN64*/ #pragma comment(linker, "/INCLUDE:__tls_used") #pragma comment(linker, "/INCLUDE:__tls_xl_b__") @@ -140,7 +144,7 @@ extern "C" { # pragma data_seg() } -#endif/*!IPC_OS_WIN64_*/ +#endif/*!WIN64*/ #elif defined(__GNUC__) @@ -153,7 +157,7 @@ extern "C" { IPC_CRTALLOC__(".CRT$XLB") PIMAGE_TLS_CALLBACK _tls_xl_b__ = OnTlsCallback; } -#else /*!__MINGW*/ +#else /*!MINGW*/ extern "C" { ULONG _tls_index__ = 0; @@ -174,8 +178,10 @@ extern "C" NX_CRTALLOC_(".tls") const IMAGE_TLS_DIRECTORY _tls_used = { (ULONG)0, (ULONG)0 } -#endif/*!__MINGW*/ +#endif/*!MINGW*/ #endif/*_MSC_VER, __GNUC__*/ } // namespace ipc + +#endif/*!__has_include()*/ diff --git a/test/test_ipc.cpp b/test/test_ipc.cpp index acb192c..a890f3c 100644 --- a/test/test_ipc.cpp +++ b/test/test_ipc.cpp @@ -7,6 +7,8 @@ #include #include #include +#include +#include #if defined(__GNUC__) # include // abi::__cxa_demangle @@ -163,25 +165,18 @@ void Unit::test_channel() { std::cout << id << "-recv: " << std::string { reinterpret_cast(dd.data()), dd.size() } << "[" << dd.size() << "]" << std::endl; - if ((unmatched = (ack.size() != dd.size()) || - (ack != reinterpret_cast(dd.data())))) { - bool need_cp = true; + if ((unmatched = (std::memcmp(dd.data(), ack.c_str(), (std::min)(dd.size(), ack.size())) != 0))) { const char cp[] = "copy:"; - for (std::size_t i = 0; i < dd.size() && i < sizeof(cp); ++i) { - if (dd[i] != cp[i]) { - need_cp = false; - break; - } - } - if (need_cp) { - cc.send(dd.data(), dd.size()); + if (std::memcmp(dd.data(), cp, sizeof(cp) - 1) == 0) { + std::cout << "cc.send(dd)" << std::endl; + cc.send(dd); } } else std::cout << id << " matched!" << std::endl; } while (unmatched.load(std::memory_order_relaxed)); }}; while (unmatched.load(std::memory_order_relaxed)) { - if (!cc.send(const_cast(ack.c_str()), ack.size())) { + if (!cc.send(ack)) { std::cout << "send failed!" << std::endl; unmatched = false; break;