1 #ifndef DINIT_CONTROL_H
2 #define DINIT_CONTROL_H
10 #include "control-cmds.h"
12 // Control connection for dinit
14 // TODO: Use the input buffer as a circular buffer, instead of chomping data from
15 // the front using a data move.
17 // forward-declaration of callback:
18 static void control_conn_cb(struct ev_loop * loop, ev_io * w, int revents);
22 // Pointer to the control connection that is listening for rollback completion
23 extern ControlConn * rollback_handler_conn;
25 extern int active_control_conns;
28 // (1 byte) packet type
29 // (N bytes) additional data (service name, etc)
30 // for STARTSERVICE/STOPSERVICE:
31 // (2 bytes) service name length
32 // (M buyes) service name (without nul terminator)
39 friend void control_conn_cb(struct ev_loop *, ev_io *, int);
43 ServiceSet *service_set;
45 bool bad_conn_close; // close when finished output?
46 bool oom_close; // send final 'out of memory' indicator
48 // The packet length before we need to re-check if the packet is complete.
49 // processPacket() will not be called until the packet reaches this size.
55 template <typename T> using list = std::list<T>;
56 template <typename T> using vector = std::vector<T>;
58 // Buffer for outgoing packets. Each outgoing back is represented as a vector<char>.
59 list<vector<char>> outbuf;
60 // Current index within the first outgoing packet (all previous bytes have been sent).
61 unsigned outpkt_index = 0;
63 // Queue a packet to be sent
64 // Returns: true if the packet was successfully queued, false if otherwise
65 // (eg if out of memory); in the latter case the connection might
66 // no longer be valid (iff there are no outgoing packets queued).
67 bool queuePacket(vector<char> &&v) noexcept;
68 bool queuePacket(const char *pkt, unsigned size) noexcept;
70 // Process a packet. Can cause the ControlConn to be deleted iff there are no
71 // outgoing packets queued.
74 // Notify that data is ready to be read from the socket. Returns true in cases where the
75 // connection was deleted with potentially pending outgoing packets.
76 bool dataReady() noexcept;
78 void sendData() noexcept;
81 ControlConn(struct ev_loop * loop, ServiceSet * service_set, int fd) : loop(loop), service_set(service_set), bufidx(0), chklen(0)
83 iobuf = new char[1024];
85 ev_io_init(&iob, control_conn_cb, fd, EV_READ);
87 ev_io_start(loop, &iob);
89 active_control_conns++;
92 bool rollbackComplete() noexcept;
94 ~ControlConn() noexcept;
98 static void control_conn_cb(struct ev_loop * loop, ev_io * w, int revents)
100 ControlConn *conn = (ControlConn *) w->data;
101 if (revents & EV_READ) {
102 if (conn->dataReady()) {
103 // ControlConn was deleted
107 if (revents & EV_WRITE) {