sel.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * sel.h: subsystem to manage the grubby details of a select loop,
  3. * buffering data to write, and performing the actual writes and
  4. * reads.
  5. */
  6. #ifndef FIXME_SEL_H
  7. #define FIXME_SEL_H
  8. typedef struct sel sel;
  9. typedef struct sel_wfd sel_wfd;
  10. typedef struct sel_rfd sel_rfd;
  11. /*
  12. * Callback called when some data is written to a wfd. "bufsize"
  13. * is the remaining quantity of data buffered in that wfd.
  14. */
  15. typedef void (*sel_written_fn_t)(sel_wfd *wfd, size_t bufsize);
  16. /*
  17. * Callback called when an error occurs on a wfd, preventing
  18. * further writing to it. "error" is the errno value.
  19. */
  20. typedef void (*sel_writeerr_fn_t)(sel_wfd *wfd, int error);
  21. /*
  22. * Callback called when some data is read from an rfd. On EOF,
  23. * this will be called with len==0.
  24. */
  25. typedef void (*sel_readdata_fn_t)(sel_rfd *rfd, void *data, size_t len);
  26. /*
  27. * Callback called when an error occurs on an rfd, preventing
  28. * further reading from it. "error" is the errno value.
  29. */
  30. typedef void (*sel_readerr_fn_t)(sel_rfd *rfd, int error);
  31. /*
  32. * Create a sel structure, which will oversee a select loop.
  33. *
  34. * "ctx" is user-supplied data stored in the sel structure; it can
  35. * be read and written with sel_get_ctx() and sel_set_ctx().
  36. */
  37. sel *sel_new(void *ctx);
  38. /*
  39. * Add a new fd for writing. Returns a sel_wfd which identifies
  40. * that fd in the sel structure, e.g. for putting data into its
  41. * output buffer.
  42. *
  43. * "ctx" is user-supplied data stored in the sel structure; it can
  44. * be read and written with sel_wfd_get_ctx() and sel_wfd_set_ctx().
  45. *
  46. * "written" and "writeerr" are called from the event loop when
  47. * things happen.
  48. *
  49. * The fd passed in can be -1, in which case it will be assumed to
  50. * be unwritable at all times. An actual fd can be passed in later
  51. * using sel_wfd_setfd.
  52. */
  53. sel_wfd *sel_wfd_add(sel *sel, int fd,
  54. sel_written_fn_t written, sel_writeerr_fn_t writeerr,
  55. void *ctx);
  56. /*
  57. * Add a new fd for reading. Returns a sel_rfd which identifies
  58. * that fd in the sel structure.
  59. *
  60. * "ctx" is user-supplied data stored in the sel structure; it can
  61. * be read and written with sel_rfd_get_ctx() and sel_rfd_set_ctx().
  62. *
  63. * "readdata" and "readerr" are called from the event loop when
  64. * things happen. "ctx" is passed to both of them.
  65. */
  66. sel_rfd *sel_rfd_add(sel *sel, int fd,
  67. sel_readdata_fn_t readdata, sel_readerr_fn_t readerr,
  68. void *ctx);
  69. /*
  70. * Write data into the output buffer of a wfd. Returns the new
  71. * size of the output buffer. (You can call it with len==0 if you
  72. * just want to know the buffer size; in that situation data==NULL
  73. * is also safe.)
  74. */
  75. size_t sel_write(sel_wfd *wfd, const void *data, size_t len);
  76. /*
  77. * Freeze and unfreeze an rfd. When frozen, sel will temporarily
  78. * not attempt to read from it, but all its state is retained so
  79. * it can be conveniently unfrozen later. (You might use this
  80. * facility, for instance, if what you were doing with the
  81. * incoming data could only accept it at a certain rate: freeze
  82. * the rfd when you've got lots of backlog, and unfreeze it again
  83. * when things get calmer.)
  84. */
  85. void sel_rfd_freeze(sel_rfd *rfd);
  86. void sel_rfd_unfreeze(sel_rfd *rfd);
  87. /*
  88. * Delete a wfd structure from its containing sel. Returns the
  89. * underlying fd, which the client may now consider itself to own
  90. * once more.
  91. */
  92. int sel_wfd_delete(sel_wfd *wfd);
  93. /*
  94. * Delete an rfd structure from its containing sel. Returns the
  95. * underlying fd, which the client may now consider itself to own
  96. * once more.
  97. */
  98. int sel_rfd_delete(sel_rfd *rfd);
  99. /*
  100. * NOT IMPLEMENTED YET: useful functions here might be ones which
  101. * enumerated all the wfds/rfds in a sel structure in some
  102. * fashion, so you could go through them and remove them all while
  103. * doing sensible things to them. Or, at the very least, just
  104. * return an arbitrary one of the wfds/rfds.
  105. */
  106. /*
  107. * Free a sel structure and all its remaining wfds and rfds.
  108. */
  109. void sel_free(sel *sel);
  110. /*
  111. * Read and write the ctx parameters in sel, sel_wfd and sel_rfd.
  112. */
  113. void *sel_get_ctx(sel *sel);
  114. void sel_set_ctx(sel *sel, void *ctx);
  115. void *sel_wfd_get_ctx(sel_wfd *wfd);
  116. void sel_wfd_set_ctx(sel_wfd *wfd, void *ctx);
  117. void *sel_rfd_get_ctx(sel_rfd *rfd);
  118. void sel_rfd_set_ctx(sel_rfd *rfd, void *ctx);
  119. /*
  120. * Run one iteration of the sel event loop, calling callbacks as
  121. * necessary. Returns zero on success; in the event of a fatal
  122. * error, returns the errno value.
  123. *
  124. * "timeout" is a value in microseconds to limit the length of the
  125. * select call. Less than zero means to wait indefinitely.
  126. */
  127. int sel_iterate(sel *sel, long timeout);
  128. /*
  129. * Change the underlying fd in a wfd. If set to -1, no write
  130. * attempts will take place and the wfd's buffer will simply store
  131. * everything passed to sel_write(). If later set to something
  132. * other than -1, all that buffered data will become eligible for
  133. * real writing.
  134. */
  135. void sel_wfd_setfd(sel_wfd *wfd, int fd);
  136. /*
  137. * Change the underlying fd in a rfd. If set to -1, no read
  138. * attempts will take place.
  139. */
  140. void sel_rfd_setfd(sel_rfd *rfd, int fd);
  141. #endif /* FIXME_SEL_H */