aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
Diffstat (limited to 'include')
-rw-r--r--include/salticidae/conn.h5
-rw-r--r--include/salticidae/event.h89
-rw-r--r--include/salticidae/network.h9
-rw-r--r--include/salticidae/ref.h4
-rw-r--r--include/salticidae/stream.h4
-rw-r--r--include/salticidae/util.h62
6 files changed, 103 insertions, 70 deletions
diff --git a/include/salticidae/conn.h b/include/salticidae/conn.h
index c8ccf31..f91e30e 100644
--- a/include/salticidae/conn.h
+++ b/include/salticidae/conn.h
@@ -39,6 +39,7 @@
#include "salticidae/type.h"
#include "salticidae/ref.h"
+#include "salticidae/event.h"
#include "salticidae/util.h"
#include "salticidae/netaddr.h"
#include "salticidae/msg.h"
@@ -233,12 +234,12 @@ class ConnPool {
conn_t add_conn(conn_t conn);
protected:
- struct event_base *eb;
+ EventContext eb;
virtual conn_t create_conn() = 0;
public:
friend Conn;
- ConnPool(struct event_base *eb): eb(eb) {}
+ ConnPool(const EventContext &eb): eb(eb) {}
~ConnPool() {
for (auto it: pool)
diff --git a/include/salticidae/event.h b/include/salticidae/event.h
new file mode 100644
index 0000000..91ebd91
--- /dev/null
+++ b/include/salticidae/event.h
@@ -0,0 +1,89 @@
+#ifndef _SALTICIDAE_EVENT_H
+#define _SALTICIDAE_EVENT_H
+
+namespace salticidae {
+
+struct _event_context_deleter {
+ constexpr _event_context_deleter() = default;
+ void operator()(struct event_base *ptr) {
+ event_base_free(ptr);
+ }
+};
+
+using _event_context_ot = RcObj<struct event_base, _event_context_deleter>;
+
+class EventContext: public _event_context_ot {
+ public:
+ EventContext(): _event_context_ot(event_base_new()) {}
+ EventContext(const EventContext &) = default;
+ EventContext(EventContext &&) = default;
+ EventContext &operator=(const EventContext &) = default;
+ EventContext &operator=(EventContext &&) = default;
+ void dispatch() { event_base_dispatch(get()); }
+};
+
+class Event {
+ public:
+ using callback_t = std::function<void(evutil_socket_t fd, short events)>;
+
+ private:
+ EventContext eb;
+ evutil_socket_t fd;
+ short events;
+ struct event *ev;
+ callback_t callback;
+ static inline void _then(evutil_socket_t fd, short events, void *arg) {
+ (static_cast<Event *>(arg))->callback(fd, events);
+ }
+
+ public:
+ Event(): ev(nullptr) {}
+ Event(const EventContext &eb,
+ evutil_socket_t fd,
+ short events,
+ callback_t callback):
+ eb(eb), fd(fd), events(events),
+ ev(event_new(eb.get(), fd, events, Event::_then, this)),
+ callback(callback) {}
+
+ Event(Event &&other):
+ eb(std::move(other.eb)), fd(other.fd), events(other.events),
+ callback(std::move(other.callback)) {
+ other.clear();
+ ev = event_new(eb.get(), fd, events, Event::_then, this);
+ }
+
+ Event &operator=(Event &&other) {
+ clear();
+ other.clear();
+ eb = std::move(other.eb);
+ fd = other.fd;
+ events = other.events;
+ ev = event_new(eb.get(), fd, events, Event::_then, this);
+ callback = std::move(other.callback);
+ return *this;
+ }
+
+ ~Event() { clear(); }
+
+ void clear() {
+ if (ev != nullptr)
+ {
+ event_del(ev);
+ event_free(ev);
+ ev = nullptr;
+ }
+ }
+
+ void add() { event_add(ev, nullptr); }
+ void del() { event_del(ev); }
+ void add_with_timeout(double timeout) {
+ event_add_with_timeout(ev, timeout);
+ }
+
+ operator bool() const { return ev != nullptr; }
+};
+
+}
+
+#endif
diff --git a/include/salticidae/network.h b/include/salticidae/network.h
index cede475..04679dd 100644
--- a/include/salticidae/network.h
+++ b/include/salticidae/network.h
@@ -25,6 +25,7 @@
#ifndef _SALTICIDAE_NETWORK_H
#define _SALTICIDAE_NETWORK_H
+#include "salticidae/event.h"
#include "salticidae/netaddr.h"
#include "salticidae/msg.h"
#include "salticidae/conn.h"
@@ -87,7 +88,7 @@ class MsgNetwork: public ConnPool {
ConnPool::conn_t create_conn() override { return (new Conn(this))->self(); }
public:
- MsgNetwork(struct event_base *eb): ConnPool(eb) {}
+ MsgNetwork(const EventContext &eb): ConnPool(eb) {}
void reg_handler(typename MsgType::opcode_t opcode, msg_callback_t handler);
void send_msg(const MsgType &msg, conn_t conn);
void init(NetAddr listen_addr);
@@ -123,7 +124,7 @@ class ClientNetwork: public MsgNetwork<MsgType> {
ConnPool::conn_t create_conn() override { return (new Conn(this))->self(); }
public:
- ClientNetwork(struct event_base *eb): MsgNet(eb) {}
+ ClientNetwork(const EventContext &eb): MsgNet(eb) {}
void send_msg(const MsgType &msg, const NetAddr &addr);
};
@@ -179,7 +180,7 @@ class PeerNetwork: public MsgNetwork<MsgType> {
bool connected;
Peer() = delete;
- Peer(NetAddr addr, conn_t conn, PeerNetwork *pn, struct event_base *eb):
+ Peer(NetAddr addr, conn_t conn, PeerNetwork *pn, const EventContext &eb):
addr(addr), conn(conn), pn(pn),
ev_ping_timer(
Event(eb, -1, 0, std::bind(&Peer::ping_timer, this, _1, _2))),
@@ -215,7 +216,7 @@ class PeerNetwork: public MsgNetwork<MsgType> {
ConnPool::conn_t create_conn() override { return (new Conn(this))->self(); }
public:
- PeerNetwork(struct event_base *eb,
+ PeerNetwork(const EventContext &eb,
double ping_period = 30,
double conn_timeout = 180,
IdentityMode id_mode = IP_PORT_BASED):
diff --git a/include/salticidae/ref.h b/include/salticidae/ref.h
index 972a3d1..92d6b0d 100644
--- a/include/salticidae/ref.h
+++ b/include/salticidae/ref.h
@@ -461,10 +461,10 @@ inline _WeakObjBase<T, R>::_WeakObjBase(const _RcObjBase<T, R, D> &other):
if (ctl) ctl->add_weak();
}
-template<typename T> using RcObj = RcObjBase<T, _RCCtl>;
+template<typename T, typename D = default_delete<T>> using RcObj = RcObjBase<T, _RCCtl, D>;
template<typename T> using WeakObj = WeakObjBase<T, _RCCtl>;
-template<typename T> using ArcObj = RcObjBase<T, _ARCCtl>;
+template<typename T, typename D = default_delete<T>> using ArcObj = RcObjBase<T, _ARCCtl, D>;
template<typename T> using AweakObj = WeakObjBase<T, _ARCCtl>;
}
diff --git a/include/salticidae/stream.h b/include/salticidae/stream.h
index efca394..03b9d13 100644
--- a/include/salticidae/stream.h
+++ b/include/salticidae/stream.h
@@ -374,6 +374,10 @@ class _Bits {
auto pos = idx & (bit_per_datum - 1);
data[i] ^= ((_impl_type)1) << pos;
}
+
+ void clear() {
+ memset(data.get(), 0, ndata * sizeof(_impl_type));
+ }
uint8_t operator[](uint32_t idx) const { return get(idx); }
diff --git a/include/salticidae/util.h b/include/salticidae/util.h
index 47a260a..d0fd984 100644
--- a/include/salticidae/util.h
+++ b/include/salticidae/util.h
@@ -297,68 +297,6 @@ class Config {
void print_help(FILE *output = stderr);
};
-class Event {
- public:
- using callback_t = std::function<void(evutil_socket_t fd, short events)>;
-
- private:
- struct event_base *eb;
- evutil_socket_t fd;
- short events;
- struct event *ev;
- callback_t callback;
- static inline void _then(evutil_socket_t fd, short events, void *arg) {
- (static_cast<Event *>(arg))->callback(fd, events);
- }
-
- public:
- Event(): ev(nullptr) {}
- Event(struct event_base *eb,
- evutil_socket_t fd,
- short events,
- callback_t callback):
- eb(eb), fd(fd), events(events),
- ev(event_new(eb, fd, events, Event::_then, this)),
- callback(callback) {}
-
- Event(Event &&other):
- eb(other.eb), fd(other.fd), events(other.events),
- callback(std::move(other.callback)) {
- other.clear();
- ev = event_new(eb, fd, events, Event::_then, this);
- }
-
- Event &operator=(Event &&other) {
- 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() { clear(); }
-
- void clear() {
- if (ev != nullptr)
- {
- event_del(ev);
- event_free(ev);
- ev = nullptr;
- }
- }
-
- void add() { event_add(ev, nullptr); }
- void del() { event_del(ev); }
- void add_with_timeout(double timeout) {
- event_add_with_timeout(ev, timeout);
- }
-
- operator bool() const { return ev != nullptr; }
-};
-
}
#endif