2007-01-14 12:34:48 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* Generic non-forking server infrastructure.
|
|
|
|
* Intended to make writing telnetd-type servers easier.
|
|
|
|
*
|
2008-03-02 12:53:15 +00:00
|
|
|
* Copyright (C) 2007 Denys Vlasenko
|
2007-01-14 12:34:48 +00:00
|
|
|
*
|
2010-08-16 18:14:46 +00:00
|
|
|
* Licensed under GPLv2, see file LICENSE in this source tree.
|
2007-01-14 12:34:48 +00:00
|
|
|
*/
|
|
|
|
|
2009-04-09 12:35:13 +00:00
|
|
|
PUSH_AND_SET_FUNCTION_VISIBILITY_TO_HIDDEN
|
2008-05-09 17:59:34 +00:00
|
|
|
|
2007-01-14 12:32:30 +00:00
|
|
|
/* opaque structure */
|
|
|
|
struct isrv_state_t;
|
|
|
|
typedef struct isrv_state_t isrv_state_t;
|
|
|
|
|
|
|
|
/* callbacks */
|
|
|
|
void isrv_want_rd(isrv_state_t *state, int fd);
|
|
|
|
void isrv_want_wr(isrv_state_t *state, int fd);
|
|
|
|
void isrv_dont_want_rd(isrv_state_t *state, int fd);
|
|
|
|
void isrv_dont_want_wr(isrv_state_t *state, int fd);
|
|
|
|
int isrv_register_fd(isrv_state_t *state, int peer, int fd);
|
|
|
|
void isrv_close_fd(isrv_state_t *state, int fd);
|
|
|
|
int isrv_register_peer(isrv_state_t *state, void *param);
|
|
|
|
|
2014-01-09 18:58:19 +00:00
|
|
|
/* Driver:
|
|
|
|
*
|
|
|
|
* Select on listen_fd for <linger_timeout> (or forever if 0).
|
|
|
|
*
|
|
|
|
* If we time out and we have no peers, exit.
|
|
|
|
* If we have peers, call do_timeout(peer_param),
|
|
|
|
* if it returns !0, peer is removed.
|
|
|
|
*
|
|
|
|
* If listen_fd is active, accept new connection ("peer"),
|
2014-01-09 19:05:47 +00:00
|
|
|
* call new_peer() on it, and if it returns 0,
|
2014-01-09 19:09:43 +00:00
|
|
|
* add it to fds to select on.
|
2014-01-09 18:58:19 +00:00
|
|
|
* Now, select will wait for <timeout>, not <linger_timeout>
|
2014-01-09 19:09:43 +00:00
|
|
|
* (as long as we have more than zero peers).
|
2014-01-09 18:58:19 +00:00
|
|
|
*
|
|
|
|
* If a peer's fd is active, we call do_rd() on it if read
|
2014-01-09 19:09:43 +00:00
|
|
|
* bit was set, and then do_wr() if write bit was also set.
|
2014-01-09 18:58:19 +00:00
|
|
|
* If either returns !0, peer is removed.
|
|
|
|
* Reaching this place also resets timeout counter for this peer.
|
|
|
|
*
|
|
|
|
* Note that peer must indicate that he wants to be selected
|
|
|
|
* for read and/or write using isrv_want_rd()/isrv_want_wr()
|
|
|
|
* [can be called in new_peer() or in do_rd()/do_wr()].
|
|
|
|
* If it never wants to be selected for write, do_wr()
|
|
|
|
* will never be called (can be NULL).
|
|
|
|
*/
|
2007-01-14 12:32:30 +00:00
|
|
|
void isrv_run(
|
|
|
|
int listen_fd,
|
|
|
|
int (*new_peer)(isrv_state_t *state, int fd),
|
|
|
|
int (*do_rd)(int fd, void **),
|
|
|
|
int (*do_wr)(int fd, void **),
|
|
|
|
int (*do_timeout)(void **),
|
|
|
|
int timeout,
|
|
|
|
int linger_timeout
|
|
|
|
);
|
2008-05-09 17:59:34 +00:00
|
|
|
|
2009-04-09 12:35:13 +00:00
|
|
|
POP_SAVED_FUNCTION_VISIBILITY
|