#include <unistd.h>
-#include "dasync.h"
+#include "dasynq.h"
#include "dinit-log.h"
#include "control-cmds.h"
// Control connection for dinit
-using namespace dasync;
-using EventLoop_t = EventLoop<NullMutex>;
+using namespace dasynq;
+using eventloop_t = event_loop<null_mutex>;
-class ControlConn;
-class ControlConnWatcher;
+class control_conn_t;
+class control_conn_watcher;
// forward-declaration of callback:
-static dasync::Rearm control_conn_cb(EventLoop_t *loop, ControlConnWatcher *watcher, int revents);
+static rearm control_conn_cb(eventloop_t *loop, control_conn_watcher *watcher, int revents);
// Pointer to the control connection that is listening for rollback completion
-extern ControlConn * rollback_handler_conn;
+extern control_conn_t * rollback_handler_conn;
extern int active_control_conns;
// (1 byte) packet length (including all fields)
// N bytes: packet data (N = (length - 2))
-class ServiceSet;
-class ServiceRecord;
+class service_set;
+class service_record;
-class ControlConnWatcher : public PosixBidiFdWatcher<NullMutex>
+class control_conn_watcher : public eventloop_t::bidi_fd_watcher_impl<control_conn_watcher>
{
- inline Rearm receiveEvent(EventLoop_t * loop, int fd, int flags) noexcept;
+ inline rearm receiveEvent(eventloop_t &loop, int fd, int flags) noexcept;
- Rearm readReady(EventLoop_t * loop, int fd) noexcept override
+ public:
+ rearm read_ready(eventloop_t &loop, int fd) noexcept
{
- return receiveEvent(loop, fd, in_events);
+ return receiveEvent(loop, fd, IN_EVENTS);
}
- Rearm writeReady(EventLoop_t * loop, int fd) noexcept override
+ rearm write_ready(eventloop_t &loop, int fd) noexcept
{
- return receiveEvent(loop, fd, out_events);
+ return receiveEvent(loop, fd, OUT_EVENTS);
}
- public:
- int fd; // TODO this is already stored, find a better way to access it.
- EventLoop_t * eventLoop;
+ eventloop_t * eventLoop;
- void setWatchFlags(int flags)
+ void set_watches(int flags)
{
- PosixBidiFdWatcher<NullMutex>::setWatchFlags(eventLoop, flags);
+ eventloop_t::bidi_fd_watcher::set_watches(*eventLoop, flags);
}
- void registerWith(EventLoop_t *loop, int fd, int flags)
+ void registerWith(eventloop_t &loop, int fd, int flags)
{
- this->fd = fd;
- this->eventLoop = loop;
- PosixBidiFdWatcher<NullMutex>::registerWith(loop, fd, flags);
+ this->eventLoop = &loop;
+ bidi_fd_watcher<eventloop_t>::add_watch(loop, fd, flags);
}
};
-inline Rearm ControlConnWatcher::receiveEvent(EventLoop_t * loop, int fd, int flags) noexcept
+inline rearm control_conn_watcher::receiveEvent(eventloop_t &loop, int fd, int flags) noexcept
{
- return control_conn_cb(loop, this, flags);
+ return control_conn_cb(&loop, this, flags);
}
-class ControlConn : private ServiceListener
+class control_conn_t : private service_listener
{
- friend dasync::Rearm control_conn_cb(EventLoop_t *loop, ControlConnWatcher *watcher, int revents);
+ friend rearm control_conn_cb(eventloop_t *loop, control_conn_watcher *watcher, int revents);
- ControlConnWatcher iob;
- EventLoop_t *loop;
- ServiceSet *service_set;
+ control_conn_watcher iob;
+ eventloop_t *loop;
+ service_set *services;
bool bad_conn_close = false; // close when finished output?
bool oom_close = false; // send final 'out of memory' indicator
int chklen;
// Receive buffer
- CPBuffer<1024> rbuf;
+ cpbuffer<1024> rbuf;
template <typename T> using list = std::list<T>;
template <typename T> using vector = std::vector<T>;
// A mapping between service records and their associated numerical identifier used
// in communction
using handle_t = uint32_t;
- std::unordered_multimap<ServiceRecord *, handle_t> serviceKeyMap;
- std::unordered_map<handle_t, ServiceRecord *> keyServiceMap;
+ std::unordered_multimap<service_record *, handle_t> serviceKeyMap;
+ std::unordered_map<handle_t, service_record *> keyServiceMap;
// Buffer for outgoing packets. Each outgoing back is represented as a vector<char>.
list<vector<char>> outbuf;
// Process an UNPINSERVICE packet. May throw std::bad_alloc.
bool processUnpinService();
+
+ bool listServices();
// Notify that data is ready to be read from the socket. Returns true if the connection should
// be closed.
bool sendData() noexcept;
// Allocate a new handle for a service; may throw std::bad_alloc
- handle_t allocateServiceHandle(ServiceRecord *record);
+ handle_t allocateServiceHandle(service_record *record);
- ServiceRecord *findServiceForKey(uint32_t key)
+ service_record *findServiceForKey(uint32_t key)
{
try {
return keyServiceMap.at(key);
{
bad_conn_close = true;
oom_close = true;
- iob.setWatchFlags(out_events);
+ iob.set_watches(OUT_EVENTS);
}
// Process service event broadcast.
// Note that this can potentially be called during packet processing (upon issuing
// service start/stop orders etc).
- void serviceEvent(ServiceRecord * service, ServiceEvent event) noexcept final override
+ void serviceEvent(service_record * service, service_event event) noexcept final override
{
// For each service handle corresponding to the event, send an information packet.
auto range = serviceKeyMap.equal_range(service);
}
public:
- ControlConn(EventLoop_t * loop, ServiceSet * service_set, int fd) : loop(loop), service_set(service_set), chklen(0)
+ control_conn_t(eventloop_t * loop, service_set * services_p, int fd)
+ : loop(loop), services(services_p), chklen(0)
{
- iob.registerWith(loop, fd, in_events);
+ iob.registerWith(*loop, fd, IN_EVENTS);
active_control_conns++;
}
bool rollbackComplete() noexcept;
- virtual ~ControlConn() noexcept;
+ virtual ~control_conn_t() noexcept;
};
-static dasync::Rearm control_conn_cb(EventLoop_t * loop, ControlConnWatcher * watcher, int revents)
+static rearm control_conn_cb(eventloop_t * loop, control_conn_watcher * watcher, int revents)
{
- using Rearm = dasync::Rearm;
-
- char * cc_addr = (reinterpret_cast<char *>(watcher)) - offsetof(ControlConn, iob);
- ControlConn *conn = reinterpret_cast<ControlConn *>(cc_addr);
- if (revents & in_events) {
+ char * cc_addr = (reinterpret_cast<char *>(watcher)) - offsetof(control_conn_t, iob);
+ control_conn_t *conn = reinterpret_cast<control_conn_t *>(cc_addr);
+ if (revents & IN_EVENTS) {
if (conn->dataReady()) {
delete conn;
- return Rearm::REMOVED;
+ return rearm::REMOVED;
}
}
- if (revents & out_events) {
+ if (revents & OUT_EVENTS) {
if (conn->sendData()) {
delete conn;
- return Rearm::REMOVED;
+ return rearm::REMOVED;
}
}
- return Rearm::NOOP;
+ return rearm::NOOP;
}
#endif