isrv.h 1.9 KB

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