control.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #ifndef DINIT_CONTROL_H
  2. #define DINIT_CONTROL_H
  3. #include <unistd.h>
  4. #include <ev++.h>
  5. #include "dinit-log.h"
  6. #include "control-cmds.h"
  7. // Control connection for dinit
  8. // forward-declaration of callback:
  9. static void control_conn_cb(struct ev_loop * loop, ev_io * w, int revents);
  10. class ControlConn;
  11. // Pointer to the control connection that is listening for rollback completion
  12. extern ControlConn * rollback_handler_conn;
  13. extern int active_control_conns;
  14. // "packet" format:
  15. // (1 byte) packet type
  16. // (N bytes) additional data (service name, etc)
  17. // for STARTSERVICE/STOPSERVICE:
  18. // (2 bytes) service name length
  19. // (M buyes) service name (without nul terminator)
  20. class ServiceSet;
  21. class ControlConn
  22. {
  23. struct ev_io iob;
  24. struct ev_loop *loop;
  25. ServiceSet *service_set;
  26. char * iobuf;
  27. int bufidx;
  28. // The packet length before we need to re-check if the packet is complete
  29. int chklen;
  30. public:
  31. ControlConn(struct ev_loop * loop, ServiceSet * service_set, int fd) : loop(loop), service_set(service_set), bufidx(0), chklen(0)
  32. {
  33. iobuf = new char[1024];
  34. ev_io_init(&iob, control_conn_cb, fd, EV_READ);
  35. iob.data = this;
  36. ev_io_start(loop, &iob);
  37. active_control_conns++;
  38. }
  39. void processPacket();
  40. void rollbackComplete();
  41. void dataReady();
  42. ~ControlConn();
  43. };
  44. static void control_conn_cb(struct ev_loop * loop, ev_io * w, int revents)
  45. {
  46. ControlConn *conn = (ControlConn *) w->data;
  47. conn->dataReady();
  48. }
  49. #endif