Denis Vlasenko | 844ff09 | 2007-01-14 12:34:48 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Generic non-forking server infrastructure. |
| 4 | * Intended to make writing telnetd-type servers easier. |
| 5 | * |
Denis Vlasenko | d18f52b | 2008-03-02 12:53:15 +0000 | [diff] [blame] | 6 | * Copyright (C) 2007 Denys Vlasenko |
Denis Vlasenko | 844ff09 | 2007-01-14 12:34:48 +0000 | [diff] [blame] | 7 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 8 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Denis Vlasenko | 844ff09 | 2007-01-14 12:34:48 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Denis Vlasenko | f81e8db | 2009-04-09 12:35:13 +0000 | [diff] [blame] | 11 | PUSH_AND_SET_FUNCTION_VISIBILITY_TO_HIDDEN |
Denis Vlasenko | 98636eb | 2008-05-09 17:59:34 +0000 | [diff] [blame] | 12 | |
Denis Vlasenko | fdf7aec | 2007-01-14 12:32:30 +0000 | [diff] [blame] | 13 | /* opaque structure */ |
| 14 | struct isrv_state_t; |
| 15 | typedef struct isrv_state_t isrv_state_t; |
| 16 | |
| 17 | /* callbacks */ |
| 18 | void isrv_want_rd(isrv_state_t *state, int fd); |
| 19 | void isrv_want_wr(isrv_state_t *state, int fd); |
| 20 | void isrv_dont_want_rd(isrv_state_t *state, int fd); |
| 21 | void isrv_dont_want_wr(isrv_state_t *state, int fd); |
| 22 | int isrv_register_fd(isrv_state_t *state, int peer, int fd); |
| 23 | void isrv_close_fd(isrv_state_t *state, int fd); |
| 24 | int isrv_register_peer(isrv_state_t *state, void *param); |
| 25 | |
Denys Vlasenko | 3ea93e8 | 2014-01-09 19:58:19 +0100 | [diff] [blame] | 26 | /* Driver: |
| 27 | * |
| 28 | * Select on listen_fd for <linger_timeout> (or forever if 0). |
| 29 | * |
| 30 | * If we time out and we have no peers, exit. |
| 31 | * If we have peers, call do_timeout(peer_param), |
| 32 | * if it returns !0, peer is removed. |
| 33 | * |
| 34 | * If listen_fd is active, accept new connection ("peer"), |
Denys Vlasenko | abaf910 | 2014-01-09 20:05:47 +0100 | [diff] [blame] | 35 | * call new_peer() on it, and if it returns 0, |
Denys Vlasenko | 3deabea | 2014-01-09 20:09:43 +0100 | [diff] [blame] | 36 | * add it to fds to select on. |
Denys Vlasenko | 3ea93e8 | 2014-01-09 19:58:19 +0100 | [diff] [blame] | 37 | * Now, select will wait for <timeout>, not <linger_timeout> |
Denys Vlasenko | 3deabea | 2014-01-09 20:09:43 +0100 | [diff] [blame] | 38 | * (as long as we have more than zero peers). |
Denys Vlasenko | 3ea93e8 | 2014-01-09 19:58:19 +0100 | [diff] [blame] | 39 | * |
| 40 | * If a peer's fd is active, we call do_rd() on it if read |
Denys Vlasenko | 3deabea | 2014-01-09 20:09:43 +0100 | [diff] [blame] | 41 | * bit was set, and then do_wr() if write bit was also set. |
Denys Vlasenko | 3ea93e8 | 2014-01-09 19:58:19 +0100 | [diff] [blame] | 42 | * If either returns !0, peer is removed. |
| 43 | * Reaching this place also resets timeout counter for this peer. |
| 44 | * |
| 45 | * Note that peer must indicate that he wants to be selected |
| 46 | * for read and/or write using isrv_want_rd()/isrv_want_wr() |
| 47 | * [can be called in new_peer() or in do_rd()/do_wr()]. |
| 48 | * If it never wants to be selected for write, do_wr() |
| 49 | * will never be called (can be NULL). |
| 50 | */ |
Denis Vlasenko | fdf7aec | 2007-01-14 12:32:30 +0000 | [diff] [blame] | 51 | void isrv_run( |
| 52 | int listen_fd, |
| 53 | int (*new_peer)(isrv_state_t *state, int fd), |
| 54 | int (*do_rd)(int fd, void **), |
| 55 | int (*do_wr)(int fd, void **), |
| 56 | int (*do_timeout)(void **), |
| 57 | int timeout, |
| 58 | int linger_timeout |
| 59 | ); |
Denis Vlasenko | 98636eb | 2008-05-09 17:59:34 +0000 | [diff] [blame] | 60 | |
Denis Vlasenko | f81e8db | 2009-04-09 12:35:13 +0000 | [diff] [blame] | 61 | POP_SAVED_FUNCTION_VISIBILITY |