backend.h 2.1 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_result {
  20. RESULT_AGAIN,
  21. RESULT_FINISHED,
  22. RESULT_CLOSE
  23. };
  24. enum io_state {
  25. IO_IO,
  26. IO_NEXT, /* eg starting, woken from idle, return from io_break. */
  27. IO_IDLE,
  28. IO_FINISHED
  29. };
  30. static inline enum io_state from_ioplan(struct io_plan *op)
  31. {
  32. return (enum io_state)(long)op;
  33. }
  34. struct io_state_read {
  35. char *buf;
  36. size_t len;
  37. };
  38. struct io_state_write {
  39. const char *buf;
  40. size_t len;
  41. };
  42. struct io_state_readpart {
  43. char *buf;
  44. size_t *lenp;
  45. };
  46. struct io_state_writepart {
  47. const char *buf;
  48. size_t *lenp;
  49. };
  50. struct io_timeout {
  51. struct timer timer;
  52. struct io_conn *conn;
  53. struct io_plan *(*next)(struct io_conn *, void *arg);
  54. void *next_arg;
  55. };
  56. /* One connection per client. */
  57. struct io_conn {
  58. struct fd fd;
  59. struct io_plan *(*next)(struct io_conn *, void *arg);
  60. void *next_arg;
  61. void (*finish)(struct io_conn *, void *arg);
  62. void *finish_arg;
  63. struct io_conn *duplex;
  64. struct io_timeout *timeout;
  65. enum io_result (*io)(struct io_conn *conn);
  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_wakeup(struct io_conn *conn);
  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 */