Skip to content

Commit

Permalink
Low: ipcserver.c: Fix example server's glib mainloop implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
davidvossel committed Jul 24, 2013
1 parent 54ef796 commit 1e1397f
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions examples/ipcserver.c
Expand Up @@ -181,8 +181,9 @@ show_usage(const char *name)
#ifdef HAVE_GLIB
struct gio_to_qb_poll {
gboolean is_used;
GIOChannel *channel;
int32_t events;
int32_t source;
int32_t fd;
void *data;
qb_ipcs_dispatch_fn_t fn;
enum qb_loop_priority p;
Expand All @@ -197,6 +198,16 @@ gio_read_socket(GIOChannel * gio, GIOCondition condition, gpointer data)
return (adaptor->fn(fd, condition, adaptor->data) == 0);
}

static void
gio_poll_destroy(gpointer data)
{
struct gio_to_qb_poll *adaptor = (struct gio_to_qb_poll *)data;

qb_log(LOG_DEBUG, "fd %d adaptor destroyed\n", adaptor->fd);
adaptor->is_used = QB_FALSE;
adaptor->fd = 0;
}

static int32_t
my_g_dispatch_add(enum qb_loop_priority p, int32_t fd, int32_t evts,
void *data, qb_ipcs_dispatch_fn_t fn)
Expand All @@ -218,14 +229,19 @@ my_g_dispatch_add(enum qb_loop_priority p, int32_t fd, int32_t evts,
return -ENOMEM;
}

adaptor->channel = channel;
adaptor->fn = fn;
adaptor->events = evts;
adaptor->data = data;
adaptor->p = p;
adaptor->is_used = TRUE;
adaptor->fd = fd;

adaptor->source = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, evts, gio_read_socket, adaptor, gio_poll_destroy);

/* we are handing the channel off to be managed by mainloop now.
* remove our reference. */
g_io_channel_unref(channel);

g_io_add_watch(channel, evts, gio_read_socket, adaptor);
return 0;
}

Expand All @@ -241,7 +257,7 @@ my_g_dispatch_del(int32_t fd)
{
struct gio_to_qb_poll *adaptor;
if (qb_array_index(gio_map, fd, (void **)&adaptor) == 0) {
g_io_channel_unref(adaptor->channel);
g_source_remove(adaptor->source);
adaptor->is_used = FALSE;
}
return 0;
Expand Down

0 comments on commit 1e1397f

Please sign in to comment.