/src/libmanos/manos.c

http://github.com/jacksonh/manos · C · 115 lines · 78 code · 33 blank · 4 comment · 6 complexity · 70d46273f8e97c1c5643bf288b43bafe MD5 · raw file

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include <fcntl.h>
  5. #include <errno.h>
  6. #include <string.h>
  7. #include <sys/ioctl.h>
  8. #include <sys/socket.h>
  9. #include <sys/un.h>
  10. #include <sys/types.h>
  11. #include <arpa/inet.h>
  12. #include <netdb.h>
  13. #include <netinet/in.h>
  14. #include <netinet/tcp.h>
  15. #ifdef HAVE_SYS_SENDFILE_H
  16. #include <sys/sendfile.h>
  17. #endif
  18. #include "manos.h"
  19. typedef struct {
  20. struct ev_loop *loop;
  21. struct ev_idle eio_idle_watcher;
  22. } manos_data_t;
  23. manos_data_t manos_data;
  24. /*
  25. * I want these to be part of the manos_data_t but then how do i get them in
  26. * to eio_want_poll/eio_done_poll ?
  27. */
  28. struct ev_async eio_want_poll_watcher;
  29. struct ev_async eio_done_poll_watcher;
  30. struct ev_idle eio_idle_watcher;
  31. static void
  32. eio_on_idle (EV_P_ ev_idle *watcher, int revents)
  33. {
  34. if (eio_poll () != -1)
  35. ev_idle_stop (EV_A_ watcher);
  36. }
  37. static void
  38. eio_on_want_poll (EV_P_ ev_async *watcher, int revents)
  39. {
  40. manos_data_t *data = (manos_data_t *) watcher->data;
  41. if (eio_poll () == -1)
  42. ev_idle_start (EV_A_ &eio_idle_watcher);
  43. }
  44. static void
  45. eio_on_done_poll (EV_P_ ev_async *watcher, int revents)
  46. {
  47. manos_data_t *data = (manos_data_t *) watcher->data;
  48. if (eio_poll () != -1)
  49. ev_idle_stop (EV_A_ &eio_idle_watcher);
  50. }
  51. static void
  52. eio_want_poll ()
  53. {
  54. ev_async_send (manos_data.loop, &eio_want_poll_watcher);
  55. }
  56. static void
  57. eio_done_poll ()
  58. {
  59. ev_async_send (manos_data.loop, &eio_done_poll_watcher);
  60. }
  61. void
  62. manos_init (struct ev_loop *loop)
  63. {
  64. memset (&manos_data, 0, sizeof (manos_data_t));
  65. manos_data.loop = loop;
  66. ev_idle_init (&eio_idle_watcher, eio_on_idle);
  67. eio_idle_watcher.data = &manos_data;
  68. ev_async_init (&eio_want_poll_watcher, eio_on_want_poll);
  69. ev_async_start (loop, &eio_want_poll_watcher);
  70. eio_want_poll_watcher.data = &manos_data;
  71. ev_async_init (&eio_done_poll_watcher, eio_on_done_poll);
  72. ev_async_start (loop, &eio_done_poll_watcher);
  73. eio_done_poll_watcher.data = &manos_data;
  74. eio_init (eio_want_poll, eio_done_poll);
  75. }
  76. void
  77. manos_shutdown ()
  78. {
  79. ev_async_stop (manos_data.loop, &eio_want_poll_watcher);
  80. ev_async_stop (manos_data.loop, &eio_done_poll_watcher);
  81. ev_idle_stop (manos_data.loop, &eio_idle_watcher);
  82. }