#include #include #include #include #include #include "libipc/ipc.h" namespace { char name__[] = "ipc-chat"; char quit__[] = "q"; char id__ [] = "c"; std::size_t calc_unique_id() { static ipc::shm::handle g_shm { "__CHAT_ACC_STORAGE__", sizeof(std::atomic) }; return static_cast*>(g_shm.get())->fetch_add(1, std::memory_order_relaxed); } } // namespace int main() { std::string buf, id = id__ + std::to_string(calc_unique_id()); std::regex reg { "(c\\d+)> (.*)" }; ipc::channel cc { name__, ipc::sender }; std::thread r {[&id, ®] { ipc::channel cc { name__, ipc::receiver }; std::cout << id << " is ready." << std::endl; while (1) { auto buf = cc.recv(); if (buf.empty()) continue; std::string dat { buf.get(), buf.size() - 1 }; std::smatch mid; if (std::regex_match(dat, mid, reg)) { if (mid.str(1) == id) { if (mid.str(2) == quit__) { std::cout << "receiver quit..." << std::endl; return; } continue; } } std::cout << dat << std::endl; } }}; for (/*int i = 1*/;; /*++i*/) { std::cin >> buf; // std::cout << "[" << i << "]" << std::endl; cc.send(id + "> " + buf); if (buf == quit__) break; } r.join(); return 0; }