io_plan.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. * // Simple helper to read a single char.
  35. * static int do_readchar(int fd, struct io_plan_arg *arg)
  36. * {
  37. * return read(fd, arg->u1.cp, 1) <= 0 ? -1 : 1;
  38. * }
  39. *
  40. * struct io_plan *io_read_char_(struct io_conn *conn, char *in,
  41. * struct io_plan *(*next)(struct io_conn*,void*),
  42. * void *arg)
  43. * {
  44. * struct io_plan_arg *arg = io_get_plan_arg(conn, IO_IN);
  45. *
  46. * // Store information we need in the plan unions u1 and u2.
  47. * arg->u1.cp = in;
  48. *
  49. * return io_set_plan(conn, IO_IN, do_readchar, next, arg);
  50. * }
  51. */
  52. struct io_plan_arg *io_plan_arg(struct io_conn *conn, enum io_direction dir);
  53. /**
  54. * io_set_plan - set a conn's io_plan.
  55. * @conn: the connection.
  56. * @dir: IO_IN or IO_OUT.
  57. * @io: the IO function to call when the fd is ready.
  58. * @next: the next callback when @io returns 1.
  59. * @next_arg: the argument to @next.
  60. *
  61. * If @conn has debug set, the io function will be called immediately,
  62. * so it's important that this be the last thing in your function!
  63. *
  64. * See also:
  65. * io_get_plan_arg()
  66. */
  67. struct io_plan *io_set_plan(struct io_conn *conn, enum io_direction dir,
  68. int (*io)(int fd, struct io_plan_arg *arg),
  69. struct io_plan *(*next)(struct io_conn *, void *),
  70. void *next_arg);
  71. #endif /* CCAN_IO_PLAN_H */