From bd87df9e73272c72a1f45cc606d010e8c4961363 Mon Sep 17 00:00:00 2001 From: Alexander Larsson Date: Thu, 14 May 2009 16:58:47 +0200 Subject: [PATCH] Make GSocketSourceFunc return the GSocket This is very useful when you have multiple sockets with sources. --- gio/gasynchelper.c | 38 +++++++++++++++++++++++++++++--------- gio/gasynchelper.h | 14 +++++++++++--- gio/giotypes.h | 8 +++++--- gio/gsocket.c | 8 +++++--- 4 files changed, 50 insertions(+), 18 deletions(-) diff --git a/gio/gasynchelper.c b/gio/gasynchelper.c index 3e5fdf8..61ef35a 100644 --- a/gio/gasynchelper.c +++ b/gio/gasynchelper.c @@ -81,6 +81,7 @@ typedef struct GPollFD pollfd; GCancellable *cancellable; gulong cancelled_tag; + GObject *object; } FDSource; static gboolean @@ -93,7 +94,7 @@ fd_source_prepare (GSource *source, return g_cancellable_is_cancelled (fd_source->cancellable); } -static gboolean +static gboolean fd_source_check (GSource *source) { FDSource *fd_source = (FDSource *)source; @@ -110,14 +111,18 @@ fd_source_dispatch (GSource *source, { GFDSourceFunc func = (GFDSourceFunc)callback; + GFDSourceObjectFunc func2 = (GFDSourceObjectFunc)callback; FDSource *fd_source = (FDSource *)source; g_warn_if_fail (func != NULL); - return (*func) (user_data, fd_source->pollfd.revents, fd_source->pollfd.fd); + if (fd_source->object) + return (*func2) (fd_source->object, fd_source->pollfd.revents, user_data); + else + return (*func) (user_data, fd_source->pollfd.revents, fd_source->pollfd.fd); } -static void +static void fd_source_finalize (GSource *source) { FDSource *fd_source = (FDSource *)source; @@ -128,6 +133,9 @@ fd_source_finalize (GSource *source) if (fd_source->cancellable) g_object_unref (fd_source->cancellable); + + if (fd_source->object) + g_object_unref (fd_source->object); } static GSourceFuncs fd_source_funcs = { @@ -147,9 +155,10 @@ fd_source_cancelled_cb (GCancellable *cancellable, } GSource * -_g_fd_source_new (int fd, - gushort events, - GCancellable *cancellable) +_g_fd_source_new_with_object (GObject *object, + int fd, + gushort events, + GCancellable *cancellable) { GSource *source; FDSource *fd_source; @@ -159,16 +168,27 @@ _g_fd_source_new (int fd, if (cancellable) fd_source->cancellable = g_object_ref (cancellable); - + + if (object) + fd_source->object = g_object_ref (object); + fd_source->pollfd.fd = fd; fd_source->pollfd.events = events; g_source_add_poll (source, &fd_source->pollfd); if (cancellable) fd_source->cancelled_tag = - g_cancellable_connect (cancellable, + g_cancellable_connect (cancellable, (GCallback)fd_source_cancelled_cb, NULL, NULL); - + return source; } + +GSource * +_g_fd_source_new (int fd, + gushort events, + GCancellable *cancellable) +{ + return _g_fd_source_new_with_object (NULL, fd, events, cancellable); +} diff --git a/gio/gasynchelper.h b/gio/gasynchelper.h index b4d8e50..e95b405 100644 --- a/gio/gasynchelper.h +++ b/gio/gasynchelper.h @@ -24,6 +24,7 @@ #define __G_ASYNC_HELPER_H__ #include +#include G_BEGIN_DECLS @@ -37,6 +38,9 @@ typedef struct typedef gboolean (*GFDSourceFunc) (gpointer user_data, GIOCondition condition, int fd); +typedef gboolean (*GFDSourceObjectFunc) (GObject *object, + GIOCondition condition, + gpointer user_data); void _g_queue_async_result (GAsyncResultData *result, gpointer async_object, @@ -44,9 +48,13 @@ void _g_queue_async_result (GAsyncResultData *result, gpointer user_data, GSourceFunc source_func); -GSource *_g_fd_source_new (int fd, - gushort events, - GCancellable *cancellable); +GSource *_g_fd_source_new_with_object (GObject *object, + int fd, + gushort events, + GCancellable *cancellable); +GSource *_g_fd_source_new (int fd, + gushort events, + GCancellable *cancellable); G_END_DECLS diff --git a/gio/giotypes.h b/gio/giotypes.h index 9ca4540..70bc054 100644 --- a/gio/giotypes.h +++ b/gio/giotypes.h @@ -223,16 +223,18 @@ typedef void (*GSimpleAsyncThreadFunc) (GSimpleAsyncResult *res, /** * GSocketSourceFunc: - * @user_data: data passed in by the user. + * @socket: the #GSocket * @condition: the current condition at the source fired. + * @user_data: data passed in by the user. * * This is the function type of the callback used for the #GSource * returned by g_socket_create_source(). * * Since: 2.22 */ -typedef gboolean (*GSocketSourceFunc) (gpointer user_data, - GIOCondition condition); +typedef gboolean (*GSocketSourceFunc) (GSocket *socket, + GIOCondition condition, + gpointer user_data); /** * GInputVector: diff --git a/gio/gsocket.c b/gio/gsocket.c index 059f14e..c03cdb7 100644 --- a/gio/gsocket.c +++ b/gio/gsocket.c @@ -2089,8 +2089,9 @@ winsock_dispatch (GSource *source, GSocketSourceFunc func = (GSocketSourceFunc)callback; GWinsockSource *winsock_source = (GWinsockSource *)source; - return (*func) (user_data, - winsock_source->result_condition & winsock_source->condition); + return (*func) (winsock_source->socket, + winsock_source->result_condition & winsock_source->condition, + user_data); } static void @@ -2190,7 +2191,8 @@ g_socket_create_source (GSocket *socket, #ifdef G_OS_WIN32 source = winsock_source_new (socket, condition, cancellable); #else - source =_g_fd_source_new (socket->priv->fd, condition, cancellable); + source =_g_fd_source_new_with_object (G_OBJECT (socket), socket->priv->fd, + condition, cancellable); #endif return source; } -- 2.7.4