mirror of
https://github.com/mutouyun/cpp-ipc.git
synced 2025-12-06 16:56:45 +08:00
del: old demos
This commit is contained in:
parent
9c331ba03d
commit
b98329c062
@ -1,8 +0,0 @@
|
||||
project(chat)
|
||||
|
||||
file(GLOB SRC_FILES ./*.cpp)
|
||||
file(GLOB HEAD_FILES ./*.h)
|
||||
|
||||
add_executable(${PROJECT_NAME} ${SRC_FILES} ${HEAD_FILES})
|
||||
|
||||
target_link_libraries(${PROJECT_NAME} ipc)
|
||||
@ -1,62 +0,0 @@
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <regex>
|
||||
#include <atomic>
|
||||
|
||||
#include "libipc/ipc.h"
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr char const name__[] = "ipc-chat";
|
||||
constexpr char const quit__[] = "q";
|
||||
constexpr char const id__ [] = "c";
|
||||
|
||||
inline std::size_t calc_unique_id() {
|
||||
static ipc::shm::handle g_shm { "__CHAT_ACC_STORAGE__", sizeof(std::atomic<std::size_t>) };
|
||||
return static_cast<std::atomic<std::size_t>*>(g_shm.get())->fetch_add(1, std::memory_order_relaxed);
|
||||
}
|
||||
|
||||
ipc::channel sender__ { name__, ipc::sender };
|
||||
ipc::channel receiver__ { name__, ipc::receiver };
|
||||
|
||||
} // namespace
|
||||
|
||||
int main() {
|
||||
std::string buf, id = id__ + std::to_string(calc_unique_id());
|
||||
std::regex reg { "(c\\d+)> (.*)" };
|
||||
|
||||
std::thread r {[&id, ®] {
|
||||
std::cout << id << " is ready." << std::endl;
|
||||
while (1) {
|
||||
ipc::buff_t buf = receiver__.recv();
|
||||
if (buf.empty()) break; // quit
|
||||
std::string dat { buf.get<char const *>(), buf.size() - 1 };
|
||||
std::smatch mid;
|
||||
if (std::regex_match(dat, mid, reg)) {
|
||||
if (mid.str(1) == id) {
|
||||
if (mid.str(2) == quit__) {
|
||||
break;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
}
|
||||
std::cout << dat << std::endl;
|
||||
}
|
||||
std::cout << id << " receiver is quit..." << std::endl;
|
||||
}};
|
||||
|
||||
for (/*int i = 1*/;; /*++i*/) {
|
||||
std::cin >> buf;
|
||||
if (buf.empty() || (buf == quit__)) break;
|
||||
// std::cout << "[" << i << "]" << std::endl;
|
||||
sender__.send(id + "> " + buf);
|
||||
buf.clear();
|
||||
}
|
||||
|
||||
receiver__.disconnect();
|
||||
r.join();
|
||||
std::cout << id << " sender is quit..." << std::endl;
|
||||
return 0;
|
||||
}
|
||||
@ -1,11 +0,0 @@
|
||||
project(msg_que)
|
||||
|
||||
include_directories(
|
||||
${LIBIPC_PROJECT_DIR}/3rdparty)
|
||||
|
||||
file(GLOB SRC_FILES ./*.cpp)
|
||||
file(GLOB HEAD_FILES ./*.h)
|
||||
|
||||
add_executable(${PROJECT_NAME} ${SRC_FILES} ${HEAD_FILES})
|
||||
|
||||
target_link_libraries(${PROJECT_NAME} ipc)
|
||||
@ -1,137 +0,0 @@
|
||||
|
||||
#include <signal.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <atomic>
|
||||
#include <thread>
|
||||
#include <chrono>
|
||||
#include <cstddef>
|
||||
|
||||
#include "libipc/ipc.h"
|
||||
#include "capo/random.hpp"
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr char const name__ [] = "ipc-msg-que";
|
||||
constexpr char const mode_s__[] = "s";
|
||||
constexpr char const mode_r__[] = "r";
|
||||
|
||||
constexpr std::size_t const min_sz = 128;
|
||||
constexpr std::size_t const max_sz = 1024 * 16;
|
||||
|
||||
std::atomic<bool> is_quit__ {false};
|
||||
std::atomic<std::size_t> size_counter__ {0};
|
||||
|
||||
using msg_que_t = ipc::chan<ipc::relat::single, ipc::relat::multi, ipc::trans::broadcast>;
|
||||
|
||||
msg_que_t que__{ name__ };
|
||||
ipc::byte_t buff__[max_sz];
|
||||
capo::random<> rand__{
|
||||
static_cast<int>(min_sz),
|
||||
static_cast<int>(max_sz)
|
||||
};
|
||||
|
||||
inline std::string str_of_size(std::size_t sz) noexcept {
|
||||
if (sz > 1024 * 1024) {
|
||||
return std::to_string(sz / (1024 * 1024)) + " MB";
|
||||
}
|
||||
if (sz > 1024) {
|
||||
return std::to_string(sz / 1024) + " KB";
|
||||
}
|
||||
return std::to_string(sz) + " bytes";
|
||||
}
|
||||
|
||||
inline std::string speed_of(std::size_t sz) noexcept {
|
||||
return str_of_size(sz) + "/s";
|
||||
}
|
||||
|
||||
void do_counting() {
|
||||
for (int i = 1; !is_quit__.load(std::memory_order_acquire); ++i) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100)); // 100 ms
|
||||
if (i % 10) continue;
|
||||
i = 0;
|
||||
std::cout
|
||||
<< speed_of(size_counter__.exchange(0, std::memory_order_relaxed))
|
||||
<< std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void do_send() {
|
||||
std::cout
|
||||
<< __func__ << ": start ["
|
||||
<< str_of_size(min_sz) << " - " << str_of_size(max_sz)
|
||||
<< "]...\n";
|
||||
if (!que__.reconnect(ipc::sender)) {
|
||||
std::cerr << __func__ << ": connect failed.\n";
|
||||
}
|
||||
else {
|
||||
std::thread counting{ do_counting };
|
||||
while (!is_quit__.load(std::memory_order_acquire)) {
|
||||
std::size_t sz = static_cast<std::size_t>(rand__());
|
||||
if (!que__.send(ipc::buff_t(buff__, sz))) {
|
||||
std::cerr << __func__ << ": send failed.\n";
|
||||
std::cout << __func__ << ": waiting for receiver...\n";
|
||||
if (!que__.wait_for_recv(1)) {
|
||||
std::cerr << __func__ << ": wait receiver failed.\n";
|
||||
is_quit__.store(true, std::memory_order_release);
|
||||
break;
|
||||
}
|
||||
}
|
||||
size_counter__.fetch_add(sz, std::memory_order_relaxed);
|
||||
std::this_thread::yield();
|
||||
}
|
||||
counting.join();
|
||||
}
|
||||
std::cout << __func__ << ": quit...\n";
|
||||
}
|
||||
|
||||
void do_recv() {
|
||||
std::cout
|
||||
<< __func__ << ": start ["
|
||||
<< str_of_size(min_sz) << " - " << str_of_size(max_sz)
|
||||
<< "]...\n";
|
||||
if (!que__.reconnect(ipc::receiver)) {
|
||||
std::cerr << __func__ << ": connect failed.\n";
|
||||
}
|
||||
else {
|
||||
std::thread counting{ do_counting };
|
||||
while (!is_quit__.load(std::memory_order_acquire)) {
|
||||
auto msg = que__.recv();
|
||||
if (msg.empty()) break;
|
||||
size_counter__.fetch_add(msg.size(), std::memory_order_relaxed);
|
||||
}
|
||||
counting.join();
|
||||
}
|
||||
std::cout << __func__ << ": quit...\n";
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main(int argc, char ** argv) {
|
||||
if (argc < 2) return 0;
|
||||
|
||||
auto exit = [](int) {
|
||||
is_quit__.store(true, std::memory_order_release);
|
||||
que__.disconnect();
|
||||
};
|
||||
::signal(SIGINT , exit);
|
||||
::signal(SIGABRT , exit);
|
||||
::signal(SIGSEGV , exit);
|
||||
::signal(SIGTERM , exit);
|
||||
#if defined(WIN64) || defined(_WIN64) || defined(__WIN64__) || \
|
||||
defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) || \
|
||||
defined(WINCE) || defined(_WIN32_WCE)
|
||||
::signal(SIGBREAK, exit);
|
||||
#else
|
||||
::signal(SIGHUP , exit);
|
||||
#endif
|
||||
|
||||
std::string mode {argv[1]};
|
||||
if (mode == mode_s__) {
|
||||
do_send();
|
||||
} else if (mode == mode_r__) {
|
||||
do_recv();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@ -1,11 +0,0 @@
|
||||
project(send_recv)
|
||||
|
||||
include_directories(
|
||||
${LIBIPC_PROJECT_DIR}/3rdparty)
|
||||
|
||||
file(GLOB SRC_FILES ./*.cpp)
|
||||
file(GLOB HEAD_FILES ./*.h)
|
||||
|
||||
add_executable(${PROJECT_NAME} ${SRC_FILES} ${HEAD_FILES})
|
||||
|
||||
target_link_libraries(${PROJECT_NAME} ipc)
|
||||
@ -1,72 +0,0 @@
|
||||
|
||||
#include <signal.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <chrono>
|
||||
#include <atomic>
|
||||
|
||||
#include "libipc/ipc.h"
|
||||
|
||||
namespace {
|
||||
|
||||
std::atomic<bool> is_quit__ {false};
|
||||
ipc::channel *ipc__ = nullptr;
|
||||
|
||||
void do_send(int size, int interval) {
|
||||
ipc::channel ipc {"ipc", ipc::sender};
|
||||
ipc__ = &ipc;
|
||||
std::string buffer(size, 'A');
|
||||
while (!is_quit__.load(std::memory_order_acquire)) {
|
||||
std::cout << "send size: " << buffer.size() + 1 << "\n";
|
||||
ipc.send(buffer, 0/*tm*/);
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(interval));
|
||||
}
|
||||
}
|
||||
|
||||
void do_recv(int interval) {
|
||||
ipc::channel ipc {"ipc", ipc::receiver};
|
||||
ipc__ = &ipc;
|
||||
while (!is_quit__.load(std::memory_order_acquire)) {
|
||||
ipc::buff_t recv;
|
||||
for (int k = 1; recv.empty(); ++k) {
|
||||
std::cout << "recv waiting... " << k << "\n";
|
||||
recv = ipc.recv(interval);
|
||||
if (is_quit__.load(std::memory_order_acquire)) return;
|
||||
}
|
||||
std::cout << "recv size: " << recv.size() << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main(int argc, char ** argv) {
|
||||
if (argc < 3) return -1;
|
||||
|
||||
auto exit = [](int) {
|
||||
is_quit__.store(true, std::memory_order_release);
|
||||
if (ipc__ != nullptr) ipc__->disconnect();
|
||||
};
|
||||
::signal(SIGINT , exit);
|
||||
::signal(SIGABRT , exit);
|
||||
::signal(SIGSEGV , exit);
|
||||
::signal(SIGTERM , exit);
|
||||
#if defined(WIN64) || defined(_WIN64) || defined(__WIN64__) || \
|
||||
defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) || \
|
||||
defined(WINCE) || defined(_WIN32_WCE)
|
||||
::signal(SIGBREAK, exit);
|
||||
#else
|
||||
::signal(SIGHUP , exit);
|
||||
#endif
|
||||
|
||||
std::string mode {argv[1]};
|
||||
if (mode == "send") {
|
||||
if (argc < 4) return -1;
|
||||
do_send(std::stoi(argv[2]) /*size*/,
|
||||
std::stoi(argv[3]) /*interval*/);
|
||||
} else if (mode == "recv") {
|
||||
do_recv(std::stoi(argv[2]) /*interval*/);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user