aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDeterminant <[email protected]>2018-07-12 20:25:41 -0400
committerDeterminant <[email protected]>2018-07-12 20:25:41 -0400
commitb98265d732bad274f66de66cb93d891e9c41a112 (patch)
tree38eb8c584a4a3ab65d833c25eac08fa1b3a6f179
parent53f4c5137da249e5e955809ffe32afa3cf5c3522 (diff)
fix bugs
-rw-r--r--include/salticidae/conn.h6
-rw-r--r--include/salticidae/network.h2
-rw-r--r--include/salticidae/ref.h60
-rw-r--r--include/salticidae/util.h40
-rw-r--r--src/conn.cpp8
5 files changed, 65 insertions, 51 deletions
diff --git a/include/salticidae/conn.h b/include/salticidae/conn.h
index 589bf16..c8ccf31 100644
--- a/include/salticidae/conn.h
+++ b/include/salticidae/conn.h
@@ -212,9 +212,9 @@ class ConnPool {
protected:
/** close the connection and free all on-going or planned events. */
virtual void close() {
- ev_read.del();
- ev_write.del();
- ev_connect.del();
+ ev_read.clear();
+ ev_write.clear();
+ ev_connect.clear();
::close(fd);
fd = -1;
}
diff --git a/include/salticidae/network.h b/include/salticidae/network.h
index 1b20ce1..cede475 100644
--- a/include/salticidae/network.h
+++ b/include/salticidae/network.h
@@ -315,7 +315,7 @@ void PeerNetwork<MsgType>::Conn::on_teardown() {
auto it = pn->id2peer.find(peer_id);
if (it == pn->id2peer.end()) return;
Peer *p = it->second;
- if (this != p->conn) return;
+ if (this != p->conn.get()) return;
p->ev_ping_timer.del();
p->connected = false;
p->conn = nullptr;
diff --git a/include/salticidae/ref.h b/include/salticidae/ref.h
index a1c5dc7..c0b77a3 100644
--- a/include/salticidae/ref.h
+++ b/include/salticidae/ref.h
@@ -89,7 +89,17 @@ class _BoxObj {
return *this;
}
- operator T*() const { return obj; }
+ T &operator *() const { return *obj; }
+ T *get() const { return obj; }
+ operator bool() const { return obj != nullptr; }
+ bool operator==(const _BoxObj &other) const { return obj == other.obj; }
+ bool operator!=(const _BoxObj &other) const { return obj != other.obj; }
+ bool operator==(std::nullptr_t) const { return obj == nullptr; }
+ bool operator!=(std::nullptr_t) const { return obj != nullptr; }
+ bool operator<(const _BoxObj &other) const { return obj < other.obj; }
+ bool operator>(const _BoxObj &other) const { return obj > other.obj; }
+ bool operator<=(const _BoxObj &other) const { return obj <= other.obj; }
+ bool operator>=(const _BoxObj &other) const { return obj >= other.obj; }
};
template<typename T, typename D = default_delete<T>>
@@ -102,7 +112,7 @@ class BoxObj: public _BoxObj<T, D> {
BoxObj() = default;
BoxObj(T *obj): base_t(obj) {}
template<typename T_, typename =
- typename std::enable_if<!std::is_array<T_>::value>::value>
+ typename std::enable_if<!std::is_array<T_>::value>::type>
BoxObj(BoxObj<T_> &&other): base_t(other) {}
T *operator->() const { return base_t::obj; }
@@ -175,6 +185,7 @@ struct _ARCCtl {
~_ARCCtl() {}
};
+template<typename T, typename R, typename D> class _RcObjBase;
template<typename T, typename R, typename D> class RcObjBase;
template<typename T, typename R>
@@ -182,9 +193,8 @@ class _WeakObjBase {
T *obj;
R *ctl;
- template<typename D> using ROB = RcObjBase<T, R, D>;
- friend class ROB;
- friend std::hash<_WeakObjBase<T, R>>;
+ template<typename T_, typename R_, typename D>
+ friend class _RcObjBase;
public:
_WeakObjBase(): ctl(nullptr) {}
@@ -223,7 +233,7 @@ class _WeakObjBase {
}
template<typename D>
- _WeakObjBase(const RcObjBase<T, R, D> &other);
+ _WeakObjBase(const _RcObjBase<T, R, D> &other);
~_WeakObjBase() { if (ctl) ctl->release_weak(); }
};
@@ -231,22 +241,30 @@ class _WeakObjBase {
template<typename T, typename R>
class WeakObjBase: public _WeakObjBase<T, R> {
+ using base_t = _WeakObjBase<T, R>;
+ friend std::hash<WeakObjBase>;
public:
WeakObjBase() = default;
WeakObjBase(const WeakObjBase &other) = default;
WeakObjBase(WeakObjBase &&other) = default;
template<typename D>
- WeakObjBase(const RcObjBase<T, R, D> &other);
+ WeakObjBase(const RcObjBase<T, R, D> &other): base_t(other) {}
+ WeakObjBase &operator=(const WeakObjBase &) = default;
+ WeakObjBase &operator=(WeakObjBase &&) = default;
};
template<typename T, typename R>
class WeakObjBase<T[], R>: public _WeakObjBase<T, R> {
+ using base_t = _WeakObjBase<T, R>;
+ friend std::hash<WeakObjBase>;
public:
WeakObjBase() = default;
WeakObjBase(const WeakObjBase &other) = default;
WeakObjBase(WeakObjBase &&other) = default;
template<typename D>
- WeakObjBase(const RcObjBase<T[], R, D> &other);
+ WeakObjBase(const RcObjBase<T[], R, D> &other): base_t(other) {}
+ WeakObjBase &operator=(const WeakObjBase &) = default;
+ WeakObjBase &operator=(WeakObjBase &&) = default;
};
@@ -257,13 +275,11 @@ class _RcObjBase {
ptr_type obj;
R *ctl;
- friend WeakObjBase<T, R>;
- friend std::hash<_RcObjBase<T, R, D>>;
+ friend _WeakObjBase<T, R>;
template<typename T_, typename R_, typename D_>
friend class _RcObjBase;
public:
- operator T*() const { return obj; }
_RcObjBase(): obj(nullptr), ctl(nullptr) {}
_RcObjBase(ptr_type obj): obj(obj), ctl(new R()) {}
_RcObjBase(BoxObj<T> &&box_ref): obj(box_ref.obj), ctl(new R()) {
@@ -315,7 +331,7 @@ class _RcObjBase {
other.ctl = nullptr;
}
- _RcObjBase(const WeakObjBase<T, R> &other) {
+ _RcObjBase(const _WeakObjBase<T, R> &other) {
if (other.ctl && other.ctl->ref_cnt)
{
obj = other.obj;
@@ -335,11 +351,23 @@ class _RcObjBase {
}
size_t get_cnt() const { return ctl ? ctl->get_cnt() : 0; }
+ T &operator *() const { return *obj; }
+ T *get() const { return obj; }
+ operator bool() const { return obj != nullptr; }
+ bool operator==(const _RcObjBase &other) const { return obj == other.obj; }
+ bool operator!=(const _RcObjBase &other) const { return obj != other.obj; }
+ bool operator==(std::nullptr_t) const { return obj == nullptr; }
+ bool operator!=(std::nullptr_t) const { return obj != nullptr; }
+ bool operator<(const _RcObjBase &other) const { return obj < other.obj; }
+ bool operator>(const _RcObjBase &other) const { return obj > other.obj; }
+ bool operator<=(const _RcObjBase &other) const { return obj <= other.obj; }
+ bool operator>=(const _RcObjBase &other) const { return obj >= other.obj; }
};
template<typename T, typename R, typename D = default_delete<T>>
class RcObjBase: public _RcObjBase<T, R, D> {
using base_t = _RcObjBase<T, R, D>;
+ friend std::hash<RcObjBase>;
template<typename T__, typename D__, typename T_, typename R_, typename D_>
friend RcObjBase<T__, R_, D__> static_pointer_cast(const RcObjBase<T_, R_, D_> &other);
template<typename T__, typename D__, typename T_, typename R_, typename D_>
@@ -352,16 +380,15 @@ class RcObjBase: public _RcObjBase<T, R, D> {
RcObjBase(BoxObj<T> &&box_ref): base_t(std::move(box_ref)) {}
template<typename T_, typename D_,
- typename = typename std::enable_if<!std::is_array<T_>::value>::value>
+ typename = typename std::enable_if<!std::is_array<T_>::value>::type>
RcObjBase(const RcObjBase<T_, R, D_> &other): base_t(other) {}
template<typename T_, typename D_,
- typename = typename std::enable_if<!std::is_array<T_>::value>::value>
+ typename = typename std::enable_if<!std::is_array<T_>::value>::type>
RcObjBase(RcObjBase<T_, R, D_> &&other): base_t(std::move(other)) {}
RcObjBase(const WeakObjBase<T, R> &other): base_t(other) {}
-
RcObjBase(const RcObjBase &other) = default;
RcObjBase(RcObjBase &&other) = default;
RcObjBase &operator=(const RcObjBase &) = default;
@@ -371,6 +398,7 @@ class RcObjBase: public _RcObjBase<T, R, D> {
template<typename T, typename R, typename D>
class RcObjBase<T[], R, D>: public _RcObjBase<T, R, D> {
using base_t = _RcObjBase<T, R, D>;
+ friend std::hash<RcObjBase>;
template<typename T__, typename D__, typename T_, typename R_, typename D_>
friend RcObjBase<T__, R_, D__> static_pointer_cast(const RcObjBase<T_, R_, D_> &other);
template<typename T__, typename D__, typename T_, typename R_, typename D_>
@@ -419,7 +447,7 @@ RcObjBase<T, R_, D> static_pointer_cast(RcObjBase<T_, R_, D_> &&other) {
template<typename T, typename R>
template<typename D>
-inline _WeakObjBase<T, R>::_WeakObjBase(const RcObjBase<T, R, D> &other):
+inline _WeakObjBase<T, R>::_WeakObjBase(const _RcObjBase<T, R, D> &other):
obj(other.obj), ctl(other.ctl) {
if (ctl) ctl->add_weak();
}
diff --git a/include/salticidae/util.h b/include/salticidae/util.h
index 0324215..de63146 100644
--- a/include/salticidae/util.h
+++ b/include/salticidae/util.h
@@ -271,47 +271,33 @@ class Event {
eb(eb), fd(fd), events(events),
ev(event_new(eb, fd, events, Event::_then, this)),
callback(callback) {}
- Event(const Event &other):
- eb(other.eb), fd(other.fd), events(other.events),
- ev(event_new(eb, fd, events, Event::_then, this)),
- callback(other.callback) {}
Event(Event &&other):
eb(other.eb), fd(other.fd), events(other.events),
- ev(event_new(eb, fd, events, Event::_then, this)),
- callback(std::move(other.callback)) {}
-
- void swap(Event &other) {
- std::swap(eb, other.eb);
- std::swap(fd, other.fd);
- std::swap(events, other.events);
- std::swap(ev, other.ev);
- std::swap(callback, other.callback);
+ callback(std::move(other.callback)) {
+ other.clear();
+ ev = event_new(eb, fd, events, Event::_then, this);
}
Event &operator=(Event &&other) {
- if (this != &other)
- {
- Event tmp(std::move(other));
- tmp.swap(*this);
- }
+ clear();
+ other.clear();
+ eb = other.eb;
+ fd = other.fd;
+ events = other.events;
+ ev = event_new(eb, fd, events, Event::_then, this);
+ callback = std::move(other.callback);
return *this;
}
- Event &operator=(const Event &other) {
- if (this != &other)
- {
- Event tmp(other);
- tmp.swap(*this);
- }
- return *this;
- }
+ ~Event() { clear(); }
- ~Event() {
+ void clear() {
if (ev != nullptr)
{
event_del(ev);
event_free(ev);
+ ev = nullptr;
}
}
diff --git a/src/conn.cpp b/src/conn.cpp
index 2bcd0ae..0b42853 100644
--- a/src/conn.cpp
+++ b/src/conn.cpp
@@ -136,7 +136,7 @@ void ConnPool::accept_client(evutil_socket_t fd, short) {
NetAddr addr((struct sockaddr_in *)&client_addr);
conn_t conn = create_conn();
- Conn *conn_ptr = conn;
+ Conn *conn_ptr = conn.get();
conn->fd = client_fd;
conn->cpool = this;
conn->mode = Conn::PASSIVE;
@@ -165,7 +165,7 @@ void ConnPool::Conn::conn_server(evutil_socket_t fd, short events) {
std::bind(&Conn::send_data, this, _1, _2));
ev_read.add();
ev_write.add();
- ev_connect.del();
+ ev_connect.clear();
ready_send = false;
SALTICIDAE_LOG_INFO("connected to peer %s", std::string(*this).c_str());
on_setup();
@@ -212,7 +212,7 @@ void ConnPool::Conn::terminate() {
{
/* temporarily pin the conn before it dies */
auto conn = it->second;
- assert(conn == this);
+ assert(conn.get() == this);
pool.erase(it);
close();
/* inform the upper layer the connection will be destroyed */
@@ -252,7 +252,7 @@ ConnPool::conn_t ConnPool::create_conn(const NetAddr &addr) {
if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1)
throw ConnPoolError(std::string("unable to set nonblocking socket"));
conn_t conn = create_conn();
- Conn * conn_ptr = conn;
+ Conn * conn_ptr = conn.get();
conn->fd = fd;
conn->cpool = this;
conn->mode = Conn::ACTIVE;