design_pattern_for_c  V 1.00
event_if_libevent.c
Go to the documentation of this file.
1 #include "tpool_event_if.h"
2 #include "dp_debug.h"
3 #include <event2/event.h>
4 #include <unistd.h>
5 
8  void *arg;
9  struct event * event;
10 };
11 
13 
14 static inline short convert_etpoll_eveid2own(int eventflag) {
15  short ret_eveflag=EV_PERSIST;
16  if(eventflag&EV_TPOOL_READ) ret_eveflag |= EV_READ;
17  if(eventflag&EV_TPOOL_WRITE) ret_eveflag |= EV_WRITE;
18 #ifdef EV_CLOSED
19  if(eventflag&EV_TPOOL_HUNGUP) ret_eveflag |= EV_CLOSED;
20 #endif
21  return ret_eveflag;
22 }
23 
24 static inline int convert_etpoll_own2eveid(short eventflag) {
25  int ret_eveflag=0;
26  if(eventflag&EV_READ) ret_eveflag |= EV_TPOOL_READ;
27  if(eventflag&EV_WRITE) ret_eveflag |= EV_TPOOL_WRITE;
28 #ifdef EV_CLOSED
29  if(eventflag&EV_CLOSED) ret_eveflag |= EV_TPOOL_HUNGUP;
30 #endif
31  return ret_eveflag;
32 }
33 
34 static void event_if_libevent_callback(evutil_socket_t fd, short eventflag, void * arg) {
36  int eflag = convert_etpoll_own2eveid(eventflag);
37  instance->subscriber.event_callback(fd, eflag, instance->arg);
38 }
39 
44  return event_base_new();
45 }
46 
49 
50  struct event_base * base = (struct event_base *)this;
51 
52  /*add event*/
53  EventLibeventHandler instance = (EventLibeventHandler)calloc(1, sizeof(*instance));
54  if(!instance) return NULL;
55 
56  memcpy(&instance->subscriber, subscriber, sizeof(instance->subscriber));
57  instance->arg = arg;
58  instance->event = event_new(base, subscriber->fd, convert_etpoll_eveid2own(subscriber->eventflag), event_if_libevent_callback, instance);
59  if(!instance->event) {
60  DEBUG_ERRPRINT("Failed to new event!\n" );
61  goto err;
62  }
63 
64  if(event_add(instance->event, NULL) == -1) {
65  DEBUG_ERRPRINT("Failed to add event!\n" );
66  goto err;
67  }
68 
69  return instance;
70 err:
71  if(instance->event) event_free(instance->event);
72  free(instance);
73  return NULL;
74 }
75 
77 void * event_if_update(EventInstance this, EventHandler handler, EventSubscriber subscriber, void *arg) {
78  struct event_base * event_base = (struct event_base *)this;
79  EventLibeventHandler eventinfo = (EventLibeventHandler)handler;
80 
81  /*delete and add to update*/
82  event_if_del(event_base, eventinfo);
83  eventinfo = event_if_add(this, subscriber, arg);
84 
85 #if 0
86  /*This function support after 2.1.2-alpha*/
87  /*restart loop and reload settings*/
88  event_base_loopcontinue(event_base);
89 #endif
90  return eventinfo;
91 }
92 
95  EventLibeventHandler instance = (EventLibeventHandler)handler;
96  event_del(instance->event);
97  event_free(instance->event);
98  free(instance);
99 }
100 
102  EventLibeventHandler instance = (EventLibeventHandler)handler;
103  return event_get_fd(instance->event);
104 }
105 
108  event_base_dispatch((struct event_base *)this);
109  return 0;
110 }
111 
114  event_base_loopbreak((struct event_base *)this);
115 }
116 
119  struct event_base * base = (struct event_base *)this;
120  if(event_base_got_break(base)) {
121  event_base_loopbreak(base);
122  }
123 }
124 
127  event_base_free((struct event_base *)this);
128 }
#define EV_TPOOL_READ
struct event_libevent_handler_t * EventLibeventHandler
EventHandler event_if_add(EventInstance this, EventSubscriber subscriber, void *arg)
add new event
static void event_if_libevent_callback(evutil_socket_t fd, short eventflag, void *arg)
void event_if_del(EventInstance this, EventHandler handler)
delete event
static int convert_etpoll_own2eveid(short eventflag)
void(* free)(EventInstance this)
Definition: event_thread.c:39
void event_if_loopbreak(EventInstance this)
break event
static short convert_etpoll_eveid2own(int eventflag)
#define EV_TPOOL_WRITE
void(* event_callback)(int socketfd, int eventflag, void *event_arg)
void * EventHandler
Event handler related to fd.
EventSubscriber class instance definition, this is storaged in any threads.
event_subscriber_t subscriber
int eventflag
OR value of EV_TPOOL_XXX definition.
#define EV_TPOOL_HUNGUP
For using debug log.
int fd
file descripter of this subscriber
void * EventInstance
Event management instance which get from event_if_new.
void event_if_exit(EventInstance this)
exit after main loop
void * event_if_update(EventInstance this, EventHandler handler, EventSubscriber subscriber, void *arg)
update registered event
int event_if_getfd(EventHandler handler)
get fd related to handler
#define DEBUG_ERRPRINT(...)
Definition: dp_debug.h:69
EventInstance event_if_new(void)
event new
int event_if_loop(EventInstance this)
main loop of this event
void event_if_free(EventInstance this)
free event if instance