adjust interface for connect

This commit is contained in:
zhangyi 2019-04-01 18:07:15 +08:00
parent 40711e0ced
commit ca6815c601
2 changed files with 19 additions and 9 deletions

View File

@ -15,7 +15,7 @@ using buff_t = buffer;
template <typename Flag> template <typename Flag>
struct IPC_EXPORT chan_impl { struct IPC_EXPORT chan_impl {
static handle_t connect (char const * name); static handle_t connect (char const * name, bool start);
static void disconnect(handle_t h); static void disconnect(handle_t h);
static std::size_t recv_count(handle_t h); static std::size_t recv_count(handle_t h);
@ -39,8 +39,8 @@ private:
public: public:
chan_wrapper() = default; chan_wrapper() = default;
explicit chan_wrapper(char const * name) { explicit chan_wrapper(char const * name, bool start = false) {
this->connect(name); this->connect(name, start);
} }
chan_wrapper(chan_wrapper&& rhs) { chan_wrapper(chan_wrapper&& rhs) {
@ -77,10 +77,10 @@ public:
return chan_wrapper { name() }; return chan_wrapper { name() };
} }
bool connect(char const * name) { bool connect(char const * name, bool start = true) {
if (name == nullptr || name[0] == '\0') return false; if (name == nullptr || name[0] == '\0') return false;
this->disconnect(); this->disconnect();
h_ = detail_t::connect((n_ = name).c_str()); h_ = detail_t::connect((n_ = name).c_str(), start);
return valid(); return valid();
} }

View File

@ -157,8 +157,18 @@ static auto& recv_cache() {
/* API implementations */ /* API implementations */
static ipc::handle_t connect(char const * name) { static ipc::handle_t connect(char const * name, bool start) {
return mem::alloc<conn_info_t>(name); auto h = mem::alloc<conn_info_t>(name);
auto que = queue_of(h);
if (que == nullptr) {
return nullptr;
}
if (start) {
if (que->connect()) { // wouldn't connect twice
info_of(h)->cc_waiter_.broadcast();
}
}
return h;
} }
static void disconnect(ipc::handle_t h) { static void disconnect(ipc::handle_t h) {
@ -336,8 +346,8 @@ using policy_t = policy::choose<circ::elem_array, Flag>;
namespace ipc { namespace ipc {
template <typename Flag> template <typename Flag>
ipc::handle_t chan_impl<Flag>::connect(char const * name) { ipc::handle_t chan_impl<Flag>::connect(char const * name, bool start) {
return detail_impl<policy_t<Flag>>::connect(name); return detail_impl<policy_t<Flag>>::connect(name, start);
} }
template <typename Flag> template <typename Flag>