2 * sel.h: subsystem to manage the grubby details of a select loop,
\r
3 * buffering data to write, and performing the actual writes and
\r
10 typedef struct sel sel;
\r
11 typedef struct sel_wfd sel_wfd;
\r
12 typedef struct sel_rfd sel_rfd;
\r
15 * Callback called when some data is written to a wfd. "bufsize"
\r
16 * is the remaining quantity of data buffered in that wfd.
\r
18 typedef void (*sel_written_fn_t)(sel_wfd *wfd, size_t bufsize);
\r
21 * Callback called when an error occurs on a wfd, preventing
\r
22 * further writing to it. "error" is the errno value.
\r
24 typedef void (*sel_writeerr_fn_t)(sel_wfd *wfd, int error);
\r
27 * Callback called when some data is read from an rfd. On EOF,
\r
28 * this will be called with len==0.
\r
30 typedef void (*sel_readdata_fn_t)(sel_rfd *rfd, void *data, size_t len);
\r
33 * Callback called when an error occurs on an rfd, preventing
\r
34 * further reading from it. "error" is the errno value.
\r
36 typedef void (*sel_readerr_fn_t)(sel_rfd *rfd, int error);
\r
39 * Create a sel structure, which will oversee a select loop.
\r
41 * "ctx" is user-supplied data stored in the sel structure; it can
\r
42 * be read and written with sel_get_ctx() and sel_set_ctx().
\r
44 sel *sel_new(void *ctx);
\r
47 * Add a new fd for writing. Returns a sel_wfd which identifies
\r
48 * that fd in the sel structure, e.g. for putting data into its
\r
51 * "ctx" is user-supplied data stored in the sel structure; it can
\r
52 * be read and written with sel_wfd_get_ctx() and sel_wfd_set_ctx().
\r
54 * "written" and "writeerr" are called from the event loop when
\r
57 * The fd passed in can be -1, in which case it will be assumed to
\r
58 * be unwritable at all times. An actual fd can be passed in later
\r
59 * using sel_wfd_setfd.
\r
61 sel_wfd *sel_wfd_add(sel *sel, int fd,
\r
62 sel_written_fn_t written, sel_writeerr_fn_t writeerr,
\r
66 * Add a new fd for reading. Returns a sel_rfd which identifies
\r
67 * that fd in the sel structure.
\r
69 * "ctx" is user-supplied data stored in the sel structure; it can
\r
70 * be read and written with sel_rfd_get_ctx() and sel_rfd_set_ctx().
\r
72 * "readdata" and "readerr" are called from the event loop when
\r
73 * things happen. "ctx" is passed to both of them.
\r
75 sel_rfd *sel_rfd_add(sel *sel, int fd,
\r
76 sel_readdata_fn_t readdata, sel_readerr_fn_t readerr,
\r
80 * Write data into the output buffer of a wfd. Returns the new
\r
81 * size of the output buffer. (You can call it with len==0 if you
\r
82 * just want to know the buffer size; in that situation data==NULL
\r
85 size_t sel_write(sel_wfd *wfd, const void *data, size_t len);
\r
88 * Freeze and unfreeze an rfd. When frozen, sel will temporarily
\r
89 * not attempt to read from it, but all its state is retained so
\r
90 * it can be conveniently unfrozen later. (You might use this
\r
91 * facility, for instance, if what you were doing with the
\r
92 * incoming data could only accept it at a certain rate: freeze
\r
93 * the rfd when you've got lots of backlog, and unfreeze it again
\r
94 * when things get calmer.)
\r
96 void sel_rfd_freeze(sel_rfd *rfd);
\r
97 void sel_rfd_unfreeze(sel_rfd *rfd);
\r
100 * Delete a wfd structure from its containing sel. Returns the
\r
101 * underlying fd, which the client may now consider itself to own
\r
104 int sel_wfd_delete(sel_wfd *wfd);
\r
107 * Delete an rfd structure from its containing sel. Returns the
\r
108 * underlying fd, which the client may now consider itself to own
\r
111 int sel_rfd_delete(sel_rfd *rfd);
\r
114 * NOT IMPLEMENTED YET: useful functions here might be ones which
\r
115 * enumerated all the wfds/rfds in a sel structure in some
\r
116 * fashion, so you could go through them and remove them all while
\r
117 * doing sensible things to them. Or, at the very least, just
\r
118 * return an arbitrary one of the wfds/rfds.
\r
122 * Free a sel structure and all its remaining wfds and rfds.
\r
124 void sel_free(sel *sel);
\r
127 * Read and write the ctx parameters in sel, sel_wfd and sel_rfd.
\r
129 void *sel_get_ctx(sel *sel);
\r
130 void sel_set_ctx(sel *sel, void *ctx);
\r
131 void *sel_wfd_get_ctx(sel_wfd *wfd);
\r
132 void sel_wfd_set_ctx(sel_wfd *wfd, void *ctx);
\r
133 void *sel_rfd_get_ctx(sel_rfd *rfd);
\r
134 void sel_rfd_set_ctx(sel_rfd *rfd, void *ctx);
\r
137 * Run one iteration of the sel event loop, calling callbacks as
\r
138 * necessary. Returns zero on success; in the event of a fatal
\r
139 * error, returns the errno value.
\r
141 * "timeout" is a value in microseconds to limit the length of the
\r
142 * select call. Less than zero means to wait indefinitely.
\r
144 int sel_iterate(sel *sel, long timeout);
\r
147 * Change the underlying fd in a wfd. If set to -1, no write
\r
148 * attempts will take place and the wfd's buffer will simply store
\r
149 * everything passed to sel_write(). If later set to something
\r
150 * other than -1, all that buffered data will become eligible for
\r
153 void sel_wfd_setfd(sel_wfd *wfd, int fd);
\r
156 * Change the underlying fd in a rfd. If set to -1, no read
\r
157 * attempts will take place.
\r
159 void sel_rfd_setfd(sel_rfd *rfd, int fd);
\r
161 #endif /* FIXME_SEL_H */
\r