io_plan.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /* Licensed under LGPLv2.1+ - see LICENSE file for details */
  2. #ifndef CCAN_IO_PLAN_H
  3. #define CCAN_IO_PLAN_H
  4. struct io_conn;
  5. /**
  6. * union io_plan_union - type for struct io_plan read/write fns.
  7. */
  8. union io_plan_union {
  9. char *cp;
  10. void *vp;
  11. const void *const_vp;
  12. size_t s;
  13. char c[sizeof(size_t)];
  14. };
  15. /**
  16. * struct io_plan_arg - scratch space for struct io_plan read/write fns.
  17. */
  18. struct io_plan_arg {
  19. union io_plan_union u1, u2;
  20. };
  21. enum io_direction {
  22. IO_IN,
  23. IO_OUT
  24. };
  25. /**
  26. * io_plan_arg - get a conn's io_plan_arg for a given direction.
  27. * @conn: the connection.
  28. * @dir: IO_IN or IO_OUT.
  29. *
  30. * This is how an io helper gets scratch space to store into; you must call
  31. * io_set_plan() when you've initialized it.
  32. *
  33. * Example:
  34. * #include <ccan/io/io_plan.h>
  35. *
  36. * // Simple helper to read a single char.
  37. * static int do_readchar(int fd, struct io_plan_arg *arg)
  38. * {
  39. * return read(fd, arg->u1.cp, 1) <= 0 ? -1 : 1;
  40. * }
  41. *
  42. * static struct io_plan *io_read_char_(struct io_conn *conn, char *in,
  43. * struct io_plan *(*next)(struct io_conn*,void*),
  44. * void *next_arg)
  45. * {
  46. * struct io_plan_arg *arg = io_plan_arg(conn, IO_IN);
  47. *
  48. * // Store information we need in the plan unions u1 and u2.
  49. * arg->u1.cp = in;
  50. *
  51. * return io_set_plan(conn, IO_IN, do_readchar, next, next_arg);
  52. * }
  53. */
  54. struct io_plan_arg *io_plan_arg(struct io_conn *conn, enum io_direction dir);
  55. /**
  56. * io_set_plan - set a conn's io_plan.
  57. * @conn: the connection.
  58. * @dir: IO_IN or IO_OUT.
  59. * @io: the IO function to call when the fd is ready.
  60. * @next: the next callback when @io returns 1.
  61. * @next_arg: the argument to @next.
  62. *
  63. * If @conn has debug set, the io function will be called immediately,
  64. * so it's important that this be the last thing in your function!
  65. *
  66. * See also:
  67. * io_get_plan_arg()
  68. */
  69. struct io_plan *io_set_plan(struct io_conn *conn, enum io_direction dir,
  70. int (*io)(int fd, struct io_plan_arg *arg),
  71. struct io_plan *(*next)(struct io_conn *, void *),
  72. void *next_arg);
  73. #endif /* CCAN_IO_PLAN_H */