Make mdns_avahi.c fully compliant with libevent 2, so it doesn't

complain in the logs about missing event base for event_del_
This commit is contained in:
ejurgensen 2014-03-16 23:11:21 +01:00
parent 065bec6eee
commit ae70bf58fc
1 changed files with 113 additions and 19 deletions

View File

@ -34,7 +34,11 @@
#include <arpa/inet.h>
#include <net/if.h>
#include <event.h>
#ifdef HAVE_LIBEVENT2
# include <event2/event.h>
#else
# include <event.h>
#endif
#include <avahi-common/watch.h>
#include <avahi-common/malloc.h>
@ -56,7 +60,7 @@ static AvahiEntryGroup *mdns_group = NULL;
struct AvahiWatch
{
struct event ev;
struct event *ev;
AvahiWatchCallback cb;
void *userdata;
@ -66,7 +70,7 @@ struct AvahiWatch
struct AvahiTimeout
{
struct event ev;
struct event *ev;
AvahiTimeoutCallback cb;
void *userdata;
@ -93,7 +97,7 @@ evcb_watch(int fd, short ev_events, void *arg)
if (ev_events & EV_WRITE)
a_events |= AVAHI_WATCH_OUT;
event_add(&w->ev, NULL);
event_add(w->ev, NULL);
w->cb(w, fd, a_events, w->userdata);
}
@ -121,10 +125,32 @@ _ev_watch_add(AvahiWatch *w, int fd, AvahiWatchEvent a_events)
if (a_events & AVAHI_WATCH_OUT)
ev_events |= EV_WRITE;
event_set(&w->ev, fd, ev_events, evcb_watch, w);
event_base_set(evbase_main, &w->ev);
#ifdef HAVE_LIBEVENT2
if (w->ev)
event_free(w->ev);
return event_add(&w->ev, NULL);
w->ev = event_new(evbase_main, fd, ev_events, evcb_watch, w);
if (!w->ev)
{
DPRINTF(E_LOG, L_MDNS, "Could not make new event in _ev_watch_add\n");
return -1;
}
#else
if (w->ev)
free(w->ev);
w->ev = (struct event *)malloc(sizeof(struct event));
if (!w->ev)
{
DPRINTF(E_LOG, L_MDNS, "Out of memory in _ev_watch_add\n");
return -1;
}
event_set(w->ev, fd, ev_events, evcb_watch, w);
event_base_set(evbase_main, w->ev);
#endif
return event_add(w->ev, NULL);
}
static AvahiWatch *
@ -158,9 +184,15 @@ ev_watch_new(const AvahiPoll *api, int fd, AvahiWatchEvent a_events, AvahiWatchC
static void
ev_watch_update(AvahiWatch *w, AvahiWatchEvent a_events)
{
event_del(&w->ev);
DPRINTF(E_DBG, L_MDNS, "DEBUG ev_watch_update\n");
if (w->ev)
event_del(w->ev);
_ev_watch_add(w, EVENT_FD(&w->ev), a_events);
#ifdef HAVE_LIBEVENT2
_ev_watch_add(w, (int)event_get_fd(w->ev), a_events);
#else
_ev_watch_add(w, EVENT_FD(w->ev), a_events);
#endif
}
static AvahiWatchEvent
@ -170,9 +202,9 @@ ev_watch_get_events(AvahiWatch *w)
a_events = 0;
if (event_pending(&w->ev, EV_READ, NULL))
if (event_pending(w->ev, EV_READ, NULL))
a_events |= AVAHI_WATCH_IN;
if (event_pending(&w->ev, EV_WRITE, NULL))
if (event_pending(w->ev, EV_WRITE, NULL))
a_events |= AVAHI_WATCH_OUT;
return a_events;
@ -184,7 +216,17 @@ ev_watch_free(AvahiWatch *w)
AvahiWatch *prev;
AvahiWatch *cur;
event_del(&w->ev);
DPRINTF(E_DBG, L_MDNS, "DEBUG ev_watch_free\n");
if (w->ev)
{
event_del(w->ev);
#ifdef HAVE_LIBEVENT2
event_free(w->ev);
#else
free(w->ev);
#endif
w->ev = NULL;
}
prev = NULL;
for (cur = all_w; cur; prev = cur, cur = cur->next)
@ -211,8 +253,30 @@ _ev_timeout_add(AvahiTimeout *t, const struct timeval *tv)
struct timeval now;
int ret;
evtimer_set(&t->ev, evcb_timeout, t);
event_base_set(evbase_main, &t->ev);
#ifdef HAVE_LIBEVENT2
if (t->ev)
event_free(t->ev);
t->ev = evtimer_new(evbase_main, evcb_timeout, t);
if (!t->ev)
{
DPRINTF(E_LOG, L_MDNS, "Could not make event in _ev_timeout_add - out of memory?\n");
return -1;
}
#else
if (t->ev)
free(t->ev);
t->ev = (struct event *)malloc(sizeof(struct event));
if (!t->ev)
{
DPRINTF(E_LOG, L_MDNS, "Out of memory in _ev_timeout_add\n");
return -1;
}
evtimer_set(t->ev, evcb_timeout, t);
event_base_set(evbase_main, t->ev);
#endif
if ((tv->tv_sec == 0) && (tv->tv_usec == 0))
{
@ -227,7 +291,7 @@ _ev_timeout_add(AvahiTimeout *t, const struct timeval *tv)
evutil_timersub(tv, &now, &e_tv);
}
return evtimer_add(&t->ev, &e_tv);
return evtimer_add(t->ev, &e_tv);
}
static AvahiTimeout *
@ -265,7 +329,9 @@ ev_timeout_new(const AvahiPoll *api, const struct timeval *tv, AvahiTimeoutCallb
static void
ev_timeout_update(AvahiTimeout *t, const struct timeval *tv)
{
event_del(&t->ev);
DPRINTF(E_DBG, L_MDNS, "DEBUG ev_timeout_update\n");
if (t->ev)
event_del(t->ev);
if (tv)
_ev_timeout_add(t, tv);
@ -277,7 +343,17 @@ ev_timeout_free(AvahiTimeout *t)
AvahiTimeout *prev;
AvahiTimeout *cur;
event_del(&t->ev);
DPRINTF(E_DBG, L_MDNS, "DEBUG ev_timeout_free\n");
if (t->ev)
{
event_del(t->ev);
#ifdef HAVE_LIBEVENT2
event_free(t->ev);
#else
free(t->ev);
#endif
t->ev = NULL;
}
prev = NULL;
for (cur = all_t; cur; prev = cur, cur = cur->next)
@ -954,10 +1030,28 @@ mdns_deinit(void)
AvahiTimeout *t;
for (t = all_t; t; t = t->next)
event_del(&t->ev);
if (t->ev)
{
event_del(t->ev);
#ifdef HAVE_LIBEVENT2
event_free(t->ev);
#else
free(t->ev);
#endif
t->ev = NULL;
}
for (w = all_w; w; w = w->next)
event_del(&w->ev);
if (w->ev)
{
event_del(w->ev);
#ifdef HAVE_LIBEVENT2
event_free(w->ev);
#else
free(w->ev);
#endif
w->ev = NULL;
}
for (ge = group_entries; group_entries; ge = group_entries)
{