backend.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. struct io_op *(*next)(struct io_conn *, void *arg);
  11. void *next_arg;
  12. void (*finish)(struct io_conn *, void *arg);
  13. void *finish_arg;
  14. };
  15. /* Listeners create connections. */
  16. struct io_listener {
  17. struct fd fd;
  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_ioop(struct io_op *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_op *(*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_conn *duplex;
  61. struct io_timeout *timeout;
  62. enum io_state state;
  63. union {
  64. struct io_state_read read;
  65. struct io_state_write write;
  66. struct io_state_readpart readpart;
  67. struct io_state_writepart writepart;
  68. } u;
  69. };
  70. static inline bool timeout_active(const struct io_conn *conn)
  71. {
  72. return conn->timeout && conn->timeout->conn;
  73. }
  74. extern void *io_loop_return;
  75. bool add_listener(struct io_listener *l);
  76. bool add_conn(struct io_conn *c);
  77. bool add_duplex(struct io_conn *c);
  78. void del_listener(struct io_listener *l);
  79. void backend_set_state(struct io_conn *conn, struct io_op *op);
  80. void backend_add_timeout(struct io_conn *conn, struct timespec ts);
  81. void backend_del_timeout(struct io_conn *conn);
  82. struct io_op *do_ready(struct io_conn *conn);
  83. #endif /* CCAN_IO_BACKEND_H */