mirror of
https://github.com/mutouyun/cpp-ipc.git
synced 2025-12-06 16:56:45 +08:00
fix some bugs; if __has_include(<pthread.h>), using tls_pointer_linux.cpp; add some channel::send overloads
This commit is contained in:
parent
8bc8feb68f
commit
f09378b481
@ -1,6 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
#include "export.h"
|
#include "export.h"
|
||||||
#include "def.h"
|
#include "def.h"
|
||||||
@ -13,7 +14,7 @@ using shm::handle_t;
|
|||||||
IPC_EXPORT handle_t connect (char const * name);
|
IPC_EXPORT handle_t connect (char const * name);
|
||||||
IPC_EXPORT void disconnect(handle_t h);
|
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<byte_t> recv(handle_t h);
|
IPC_EXPORT std::vector<byte_t> recv(handle_t h);
|
||||||
|
|
||||||
class IPC_EXPORT channel {
|
class IPC_EXPORT channel {
|
||||||
@ -35,7 +36,10 @@ public:
|
|||||||
bool connect(char const * name);
|
bool connect(char const * name);
|
||||||
void disconnect(void);
|
void disconnect(void);
|
||||||
|
|
||||||
bool send(void* data, std::size_t size);
|
bool send(void const * data, std::size_t size);
|
||||||
|
bool send(std::vector<byte_t> const & buff);
|
||||||
|
bool send(std::string const & str);
|
||||||
|
|
||||||
std::vector<byte_t> recv();
|
std::vector<byte_t> recv();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|||||||
17
src/ipc.cpp
17
src/ipc.cpp
@ -4,7 +4,6 @@
|
|||||||
#include <memory>
|
#include <memory>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <string>
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include <shared_mutex>
|
#include <shared_mutex>
|
||||||
@ -107,7 +106,7 @@ void disconnect(handle_t h) {
|
|||||||
shm::release(h, sizeof(queue_t));
|
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) {
|
if (data == nullptr) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -127,7 +126,7 @@ bool send(handle_t h, void* data, std::size_t size) {
|
|||||||
static_cast<int>(size) - offset - static_cast<int>(data_length),
|
static_cast<int>(size) - offset - static_cast<int>(data_length),
|
||||||
msg_id, { 0 }
|
msg_id, { 0 }
|
||||||
};
|
};
|
||||||
std::memcpy(msg.data_, static_cast<byte_t*>(data) + offset, data_length);
|
std::memcpy(msg.data_, static_cast<byte_t const *>(data) + offset, data_length);
|
||||||
queue->push(msg);
|
queue->push(msg);
|
||||||
}
|
}
|
||||||
// if remain > 0, this is the last message fragment
|
// 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<int>(data_length),
|
remain - static_cast<int>(data_length),
|
||||||
msg_id, { 0 }
|
msg_id, { 0 }
|
||||||
};
|
};
|
||||||
std::memcpy(msg.data_, static_cast<byte_t*>(data) + offset,
|
std::memcpy(msg.data_, static_cast<byte_t const *>(data) + offset,
|
||||||
static_cast<std::size_t>(remain));
|
static_cast<std::size_t>(remain));
|
||||||
queue->push(msg);
|
queue->push(msg);
|
||||||
}
|
}
|
||||||
@ -240,10 +239,18 @@ void channel::disconnect(void) {
|
|||||||
ipc::disconnect(impl(p_)->h_);
|
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);
|
return ipc::send(impl(p_)->h_, data, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool channel::send(std::vector<byte_t> 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<byte_t> channel::recv() {
|
std::vector<byte_t> channel::recv() {
|
||||||
return ipc::recv(impl(p_)->h_);
|
return ipc::recv(impl(p_)->h_);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
#include "shm.h"
|
#include "shm.h"
|
||||||
|
|
||||||
#include <windows.h>
|
#include <Windows.h>
|
||||||
|
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|||||||
@ -1,6 +1,10 @@
|
|||||||
|
#if __has_include(<pthread.h>)
|
||||||
|
#include "tls_pointer_linux.cpp"
|
||||||
|
#else /*!__has_include(<pthread.h>)*/
|
||||||
|
|
||||||
#include "tls_pointer.h"
|
#include "tls_pointer.h"
|
||||||
|
|
||||||
#include <windows.h> // ::Tls...
|
#include <Windows.h> // ::Tls...
|
||||||
#include <unordered_map> // std::unordered_map
|
#include <unordered_map> // std::unordered_map
|
||||||
|
|
||||||
namespace ipc {
|
namespace ipc {
|
||||||
@ -117,7 +121,7 @@ void NTAPI OnTlsCallback(PVOID, DWORD dwReason, PVOID) {
|
|||||||
|
|
||||||
#if defined(_MSC_VER)
|
#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_used")
|
||||||
#pragma comment(linker, "/INCLUDE:_tls_xl_b__")
|
#pragma comment(linker, "/INCLUDE:_tls_xl_b__")
|
||||||
@ -129,7 +133,7 @@ extern "C" {
|
|||||||
# pragma const_seg()
|
# pragma const_seg()
|
||||||
}
|
}
|
||||||
|
|
||||||
#else /*!IPC_OS_WIN64_*/
|
#else /*!WIN64*/
|
||||||
|
|
||||||
#pragma comment(linker, "/INCLUDE:__tls_used")
|
#pragma comment(linker, "/INCLUDE:__tls_used")
|
||||||
#pragma comment(linker, "/INCLUDE:__tls_xl_b__")
|
#pragma comment(linker, "/INCLUDE:__tls_xl_b__")
|
||||||
@ -140,7 +144,7 @@ extern "C" {
|
|||||||
# pragma data_seg()
|
# pragma data_seg()
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif/*!IPC_OS_WIN64_*/
|
#endif/*!WIN64*/
|
||||||
|
|
||||||
#elif defined(__GNUC__)
|
#elif defined(__GNUC__)
|
||||||
|
|
||||||
@ -153,7 +157,7 @@ extern "C" {
|
|||||||
IPC_CRTALLOC__(".CRT$XLB") PIMAGE_TLS_CALLBACK _tls_xl_b__ = OnTlsCallback;
|
IPC_CRTALLOC__(".CRT$XLB") PIMAGE_TLS_CALLBACK _tls_xl_b__ = OnTlsCallback;
|
||||||
}
|
}
|
||||||
|
|
||||||
#else /*!__MINGW*/
|
#else /*!MINGW*/
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
ULONG _tls_index__ = 0;
|
ULONG _tls_index__ = 0;
|
||||||
@ -174,8 +178,10 @@ extern "C" NX_CRTALLOC_(".tls") const IMAGE_TLS_DIRECTORY _tls_used = {
|
|||||||
(ULONG)0, (ULONG)0
|
(ULONG)0, (ULONG)0
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif/*!__MINGW*/
|
#endif/*!MINGW*/
|
||||||
|
|
||||||
#endif/*_MSC_VER, __GNUC__*/
|
#endif/*_MSC_VER, __GNUC__*/
|
||||||
|
|
||||||
} // namespace ipc
|
} // namespace ipc
|
||||||
|
|
||||||
|
#endif/*!__has_include(<pthread.h>)*/
|
||||||
|
|||||||
@ -7,6 +7,8 @@
|
|||||||
#include <typeinfo>
|
#include <typeinfo>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <cstring>
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
#if defined(__GNUC__)
|
#if defined(__GNUC__)
|
||||||
# include <cxxabi.h> // abi::__cxa_demangle
|
# include <cxxabi.h> // abi::__cxa_demangle
|
||||||
@ -163,25 +165,18 @@ void Unit::test_channel() {
|
|||||||
std::cout << id << "-recv: "
|
std::cout << id << "-recv: "
|
||||||
<< std::string { reinterpret_cast<char*>(dd.data()), dd.size() }
|
<< std::string { reinterpret_cast<char*>(dd.data()), dd.size() }
|
||||||
<< "[" << dd.size() << "]" << std::endl;
|
<< "[" << dd.size() << "]" << std::endl;
|
||||||
if ((unmatched = (ack.size() != dd.size()) ||
|
if ((unmatched = (std::memcmp(dd.data(), ack.c_str(), (std::min)(dd.size(), ack.size())) != 0))) {
|
||||||
(ack != reinterpret_cast<char*>(dd.data())))) {
|
|
||||||
bool need_cp = true;
|
|
||||||
const char cp[] = "copy:";
|
const char cp[] = "copy:";
|
||||||
for (std::size_t i = 0; i < dd.size() && i < sizeof(cp); ++i) {
|
if (std::memcmp(dd.data(), cp, sizeof(cp) - 1) == 0) {
|
||||||
if (dd[i] != cp[i]) {
|
std::cout << "cc.send(dd)" << std::endl;
|
||||||
need_cp = false;
|
cc.send(dd);
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (need_cp) {
|
|
||||||
cc.send(dd.data(), dd.size());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else std::cout << id << " matched!" << std::endl;
|
else std::cout << id << " matched!" << std::endl;
|
||||||
} while (unmatched.load(std::memory_order_relaxed));
|
} while (unmatched.load(std::memory_order_relaxed));
|
||||||
}};
|
}};
|
||||||
while (unmatched.load(std::memory_order_relaxed)) {
|
while (unmatched.load(std::memory_order_relaxed)) {
|
||||||
if (!cc.send(const_cast<char*>(ack.c_str()), ack.size())) {
|
if (!cc.send(ack)) {
|
||||||
std::cout << "send failed!" << std::endl;
|
std::cout << "send failed!" << std::endl;
|
||||||
unmatched = false;
|
unmatched = false;
|
||||||
break;
|
break;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user