1 #ifndef DINIT_CONTROL_H
2 #define DINIT_CONTROL_H
10 #include "control-cmds.h"
12 // Control connection for dinit
15 // forward-declaration of callback:
16 static void control_conn_cb(struct ev_loop * loop, ev_io * w, int revents);
20 // Pointer to the control connection that is listening for rollback completion
21 extern ControlConn * rollback_handler_conn;
23 extern int active_control_conns;
26 // (1 byte) packet type
27 // (N bytes) additional data (service name, etc)
28 // for STARTSERVICE/STOPSERVICE:
29 // (2 bytes) service name length
30 // (M buyes) service name (without nul terminator)
39 ServiceSet *service_set;
43 bool bad_conn_close; // close when finished output?
44 //bool bad_conn_wrerr; // write error has occurred
45 bool oom_close; // send final 'out of memory' indicator
47 template <typename T> using list = std::list<T>;
48 template <typename T> using vector = std::vector<T>;
50 // Buffer for outgoing packets. Each outgoing back is represented as a vector<char>.
51 list<vector<char>> outbuf;
52 unsigned outpkt_index = 0;
54 // The packet length before we need to re-check if the packet is complete
57 // Queue a packet to be sent
58 // Returns: true if the packet was successfully queued, false if otherwise
59 // (eg if out of memory); in the latter case the connection might
60 // no longer be valid (iff there are no outgoing packets queued).
61 bool queuePacket(vector<char> &&v) noexcept;
62 bool queuePacket(const char *pkt, unsigned size) noexcept;
64 // Process a packet. Can cause the ControlConn to be deleted iff there are no
65 // outgoing packets queued.
69 ControlConn(struct ev_loop * loop, ServiceSet * service_set, int fd) : loop(loop), service_set(service_set), bufidx(0), chklen(0)
71 iobuf = new char[1024];
73 ev_io_init(&iob, control_conn_cb, fd, EV_READ);
75 ev_io_start(loop, &iob);
77 active_control_conns++;
80 void rollbackComplete() noexcept;
81 // Notify that data is ready to be read from the socket.
82 void dataReady() noexcept;
83 void sendData() noexcept;
86 ~ControlConn() noexcept;
90 static void control_conn_cb(struct ev_loop * loop, ev_io * w, int revents)
92 ControlConn *conn = (ControlConn *) w->data;
93 if (revents & EV_READ) {
96 // TODO issue here: what if above deletes the connection?
97 if (revents & EV_WRITE) {