diff --git a/build/src.pro b/build/src.pro index e1a3a4b..0fb29af 100644 --- a/build/src.pro +++ b/build/src.pro @@ -27,7 +27,8 @@ HEADERS += \ SOURCES += \ ../src/shm.cpp \ - ../src/ipc.cpp + ../src/ipc.cpp \ + ../src/route.cpp unix { diff --git a/include/ipc.h b/include/ipc.h index b0eb043..38690ab 100644 --- a/include/ipc.h +++ b/include/ipc.h @@ -36,6 +36,16 @@ IPC_EXPORT buff_t recv(handle_t const * hs, std::size_t size); template buff_t recv(handle_t const (& hs)[N]) { return recv(hs, N); } +/* + * class route + * + * You could use one producer/server/sender for sending messages to a route, + * then all the consumers/clients/receivers which are receiving with this route, + * would receive your sent messages. + * + * A route could only be used in 1 to N + * (one producer/server/sender to multi consumers/clients/receivers) +*/ class IPC_EXPORT route { public: route(); @@ -68,4 +78,28 @@ private: route_* p_; }; +///* +// * class channel +//*/ +//class IPC_EXPORT channel { +//public: +// channel(); +// channel(char const * name); +// channel(channel&& rhs); + +// ~channel(); + +// void swap(channel& rhs); +// channel& operator=(channel rhs); + +// bool valid() const; +// char const * name () const; + +// channel clone() const; + +//private: +// class channel_; +// channel_* p_; +//}; + } // namespace ipc diff --git a/src/ipc.cpp b/src/ipc.cpp index 33f09c1..6901928 100644 --- a/src/ipc.cpp +++ b/src/ipc.cpp @@ -183,82 +183,4 @@ buff_t recv(handle_t const * hs, std::size_t size) { } } -class route::route_ : public pimpl { -public: - handle_t h_ = nullptr; - std::string n_; -}; - -route::route() - : p_(p_->make()) { -} - -route::route(char const * name) - : route() { - this->connect(name); -} - -route::route(route&& rhs) - : route() { - swap(rhs); -} - -route::~route() { - disconnect(); - p_->clear(); -} - -void route::swap(route& rhs) { - std::swap(p_, rhs.p_); -} - -route& route::operator=(route rhs) { - swap(rhs); - return *this; -} - -bool route::valid() const { - return (impl(p_)->h_ != nullptr); -} - -char const * route::name() const { - return impl(p_)->n_.c_str(); -} - -route route::clone() const { - return { name() }; -} - -bool route::connect(char const * name) { - if (name == nullptr || name[0] == '\0') return false; - this->disconnect(); - impl(p_)->h_ = ipc::connect((impl(p_)->n_ = name).c_str()); - return valid(); -} - -void route::disconnect() { - ipc::disconnect(impl(p_)->h_); - impl(p_)->h_ = nullptr; -} - -std::size_t route::recv_count() const { - return ipc::recv_count(impl(p_)->h_); -} - -bool route::send(void const *data, std::size_t size) { - return ipc::send(impl(p_)->h_, data, size); -} - -bool route::send(buff_t const & buff) { - return route::send(buff.data(), buff.size()); -} - -bool route::send(std::string const & str) { - return route::send(str.c_str(), str.size() + 1); -} - -buff_t route::recv() { - return ipc::recv(impl(p_)->h_); -} - } // namespace ipc diff --git a/src/route.cpp b/src/route.cpp new file mode 100644 index 0000000..4748b27 --- /dev/null +++ b/src/route.cpp @@ -0,0 +1,84 @@ +#include "ipc.h" +#include "def.h" + +namespace ipc { + +class route::route_ : public pimpl { +public: + handle_t h_ = nullptr; + std::string n_; +}; + +route::route() + : p_(p_->make()) { +} + +route::route(char const * name) + : route() { + this->connect(name); +} + +route::route(route&& rhs) + : route() { + swap(rhs); +} + +route::~route() { + disconnect(); + p_->clear(); +} + +void route::swap(route& rhs) { + std::swap(p_, rhs.p_); +} + +route& route::operator=(route rhs) { + swap(rhs); + return *this; +} + +bool route::valid() const { + return (impl(p_)->h_ != nullptr); +} + +char const * route::name() const { + return impl(p_)->n_.c_str(); +} + +route route::clone() const { + return { name() }; +} + +bool route::connect(char const * name) { + if (name == nullptr || name[0] == '\0') return false; + this->disconnect(); + impl(p_)->h_ = ipc::connect((impl(p_)->n_ = name).c_str()); + return valid(); +} + +void route::disconnect() { + ipc::disconnect(impl(p_)->h_); + impl(p_)->h_ = nullptr; +} + +std::size_t route::recv_count() const { + return ipc::recv_count(impl(p_)->h_); +} + +bool route::send(void const *data, std::size_t size) { + return ipc::send(impl(p_)->h_, data, size); +} + +bool route::send(buff_t const & buff) { + return route::send(buff.data(), buff.size()); +} + +bool route::send(std::string const & str) { + return route::send(str.c_str(), str.size() + 1); +} + +buff_t route::recv() { + return ipc::recv(impl(p_)->h_); +} + +} // namespace ipc