separate def of route into a single cpp (test)

This commit is contained in:
mutouyun 2018-12-19 12:18:53 +08:00
parent 69449c0d4f
commit 9cbfb8624f
4 changed files with 120 additions and 79 deletions

View File

@ -27,7 +27,8 @@ HEADERS += \
SOURCES += \ SOURCES += \
../src/shm.cpp \ ../src/shm.cpp \
../src/ipc.cpp ../src/ipc.cpp \
../src/route.cpp
unix { unix {

View File

@ -36,6 +36,16 @@ IPC_EXPORT buff_t recv(handle_t const * hs, std::size_t size);
template <std::size_t N> template <std::size_t N>
buff_t recv(handle_t const (& hs)[N]) { return recv(hs, N); } 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 { class IPC_EXPORT route {
public: public:
route(); route();
@ -68,4 +78,28 @@ private:
route_* p_; 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 } // namespace ipc

View File

@ -183,82 +183,4 @@ buff_t recv(handle_t const * hs, std::size_t size) {
} }
} }
class route::route_ : public pimpl<route_> {
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 } // namespace ipc

84
src/route.cpp Normal file
View File

@ -0,0 +1,84 @@
#include "ipc.h"
#include "def.h"
namespace ipc {
class route::route_ : public pimpl<route_> {
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