PageRenderTime 52ms CodeModel.GetById 40ms app.highlight 5ms RepoModel.GetById 5ms app.codeStats 0ms

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