backend.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* Licensed under LGPLv2.1+ - see LICENSE file for details */
  2. #ifndef CCAN_IO_BACKEND_H
  3. #define CCAN_IO_BACKEND_H
  4. #include <stdbool.h>
  5. #include <ccan/timer/timer.h>
  6. struct fd {
  7. int fd;
  8. bool listener;
  9. size_t backend_info;
  10. };
  11. /* Listeners create connections. */
  12. struct io_listener {
  13. struct fd fd;
  14. /* These are for connections we create. */
  15. struct io_plan *(*next)(struct io_conn *, void *arg);
  16. void (*finish)(struct io_conn *, void *arg);
  17. void *conn_arg;
  18. };
  19. enum io_state {
  20. /* These wait for something to input */
  21. READ,
  22. READPART,
  23. /* These wait for room to output */
  24. WRITE,
  25. WRITEPART,
  26. NEXT, /* eg starting, woken from idle, return from io_break. */
  27. IDLE,
  28. FINISHED,
  29. PROCESSING /* We expect them to change this now. */
  30. };
  31. static inline enum io_state from_ioplan(struct io_plan *op)
  32. {
  33. return (enum io_state)(long)op;
  34. }
  35. struct io_state_read {
  36. char *buf;
  37. size_t len;
  38. };
  39. struct io_state_write {
  40. const char *buf;
  41. size_t len;
  42. };
  43. struct io_state_readpart {
  44. char *buf;
  45. size_t *lenp;
  46. };
  47. struct io_state_writepart {
  48. const char *buf;
  49. size_t *lenp;
  50. };
  51. struct io_timeout {
  52. struct timer timer;
  53. struct io_conn *conn;
  54. struct io_plan *(*next)(struct io_conn *, void *arg);
  55. void *next_arg;
  56. };
  57. /* One connection per client. */
  58. struct io_conn {
  59. struct fd fd;
  60. struct io_plan *(*next)(struct io_conn *, void *arg);
  61. void *next_arg;
  62. void (*finish)(struct io_conn *, void *arg);
  63. void *finish_arg;
  64. struct io_conn *duplex;
  65. struct io_timeout *timeout;
  66. int pollflag; /* 0, POLLIN or POLLOUT */
  67. enum io_state state;
  68. union {
  69. struct io_state_read read;
  70. struct io_state_write write;
  71. struct io_state_readpart readpart;
  72. struct io_state_writepart writepart;
  73. } u;
  74. };
  75. static inline bool timeout_active(const struct io_conn *conn)
  76. {
  77. return conn->timeout && conn->timeout->conn;
  78. }
  79. extern void *io_loop_return;
  80. bool add_listener(struct io_listener *l);
  81. bool add_conn(struct io_conn *c);
  82. bool add_duplex(struct io_conn *c);
  83. void del_listener(struct io_listener *l);
  84. void backend_set_state(struct io_conn *conn, struct io_plan *op);
  85. void backend_add_timeout(struct io_conn *conn, struct timespec ts);
  86. void backend_del_timeout(struct io_conn *conn);
  87. struct io_plan *do_ready(struct io_conn *conn);
  88. #endif /* CCAN_IO_BACKEND_H */