[gnio/connection-factory] Pass source GSocket to GSocketSourceFunc



commit eb87cc188d88cc108d91aa6907cfe3d559511e39
Author: Alexander Larsson <alexl redhat com>
Date:   Sat May 9 20:44:31 2009 +0200

    Pass source GSocket to GSocketSourceFunc
    
    This is very useful when you have several sources and want to
    know which originating socket fired.
---
 gio/gasynchelper.c        |   36 ++++++++++++++++++++++++++++--------
 gio/gasynchelper.h        |   14 +++++++++++---
 gio/gsocket.c             |    5 +++--
 gio/gsocket.h             |    5 +++--
 gio/gsocketclient.c       |    5 +++--
 gio/gsocketinputstream.c  |    7 ++++---
 gio/gsocketlistener.c     |    7 ++++---
 gio/gsocketoutputstream.c |    7 ++++---
 8 files changed, 60 insertions(+), 26 deletions(-)

diff --git a/gio/gasynchelper.c b/gio/gasynchelper.c
index 8d376d4..18b6634 100644
--- a/gio/gasynchelper.c
+++ b/gio/gasynchelper.c
@@ -77,6 +77,7 @@ typedef struct
   GPollFD pollfd;
   GCancellable *cancellable;
   gulong cancelled_tag;
+  GObject *object;
 } FDSource;
 
 static gboolean 
@@ -89,7 +90,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;
@@ -106,14 +107,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;
@@ -124,6 +129,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 = {
@@ -143,9 +151,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;
@@ -155,7 +164,10 @@ _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);
@@ -166,6 +178,14 @@ _g_fd_source_new (int           fd,
 			     (GCallback)fd_source_cancelled_cb,
 			     NULL, NULL,
 			     0);
-  
+
   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 <gio/gio.h>
+#include <glib-object.h>
 
 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/gsocket.c b/gio/gsocket.c
index 2e3e538..5f0cfc9 100644
--- a/gio/gsocket.c
+++ b/gio/gsocket.c
@@ -2129,8 +2129,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
diff --git a/gio/gsocket.h b/gio/gsocket.h
index c39f5b0..15755da 100644
--- a/gio/gsocket.h
+++ b/gio/gsocket.h
@@ -140,8 +140,9 @@ typedef struct
  *
  * Since: 2.22
  */
-typedef gboolean (*GSocketSourceFunc) (gpointer user_data,
-				       GIOCondition condition);
+typedef gboolean (*GSocketSourceFunc) (GSocket *socket,
+				       GIOCondition condition,
+				       gpointer user_data);
 
 GType                  g_socket_get_type                (void);
 GSocket *              g_socket_new                     (GSocketFamily            family,
diff --git a/gio/gsocketclient.c b/gio/gsocketclient.c
index 9601ab4..a0e6f08 100644
--- a/gio/gsocketclient.c
+++ b/gio/gsocketclient.c
@@ -376,8 +376,9 @@ set_last_error (GSocketClientAsyncConnectData *data,
 }
 
 static gboolean
-g_socket_client_socket_callback (GSocketClientAsyncConnectData *data,
-                                 GIOCondition  condition)
+g_socket_client_socket_callback (GSocket *socket,
+                                 GIOCondition condition,
+				 GSocketClientAsyncConnectData *data)
 {
   GError *error = NULL;
 
diff --git a/gio/gsocketinputstream.c b/gio/gsocketinputstream.c
index 908cbbc..0f9f848 100644
--- a/gio/gsocketinputstream.c
+++ b/gio/gsocketinputstream.c
@@ -115,8 +115,9 @@ g_socket_input_stream_read (GInputStream  *stream,
 }
 
 static gboolean
-g_socket_input_stream_read_ready (GSocketInputStream *stream,
-                                  GIOCondition        condition)
+g_socket_input_stream_read_ready (GSocket *socket,
+                                  GIOCondition condition,
+				  GSocketInputStream *stream)
 {
   GSimpleAsyncResult *simple;
   GError *error = NULL;
@@ -195,7 +196,7 @@ g_socket_input_stream_read_async (GInputStream        *stream,
   else
     {
       input_stream->priv->from_mainloop = FALSE;
-      g_socket_input_stream_read_ready (input_stream, G_IO_IN);
+      g_socket_input_stream_read_ready (input_stream->priv->socket, G_IO_IN, input_stream);
     }
 }
 
diff --git a/gio/gsocketlistener.c b/gio/gsocketlistener.c
index 53304d3..a0fe37b 100644
--- a/gio/gsocketlistener.c
+++ b/gio/gsocketlistener.c
@@ -124,8 +124,9 @@ g_socket_listener_accept (GSocketListener  *listener,
 }
 
 static gboolean
-g_socket_listener_accept_ready (GSocketListener *listener,
-                                GIOCondition     condition)
+g_socket_listener_accept_ready (GSocket *socket,
+                                GIOCondition     condition,
+				GSocketListener *listener)
 {
   GSimpleAsyncResult *simple;
   GError *error = NULL;
@@ -205,7 +206,7 @@ g_socket_listener_accept_async (GSocketListener     *listener,
   else
     {
       listener->priv->from_mainloop = FALSE;
-      g_socket_listener_accept_ready (listener, G_IO_IN);
+      g_socket_listener_accept_ready (listener->priv->socket, G_IO_IN, listener);
     }
 }
 
diff --git a/gio/gsocketoutputstream.c b/gio/gsocketoutputstream.c
index 20cddce..2bb55d6 100644
--- a/gio/gsocketoutputstream.c
+++ b/gio/gsocketoutputstream.c
@@ -115,8 +115,9 @@ g_socket_output_stream_write (GOutputStream  *stream,
 }
 
 static gboolean
-g_socket_output_stream_write_ready (GSocketOutputStream *stream,
-                                    GIOCondition        condition)
+g_socket_output_stream_write_ready (GSocket *socket,
+                                    GIOCondition condition,
+				    GSocketOutputStream *stream)
 {
   GSimpleAsyncResult *simple;
   GError *error = NULL;
@@ -195,7 +196,7 @@ g_socket_output_stream_write_async (GOutputStream        *stream,
   else
     {
       input_stream->priv->from_mainloop = FALSE;
-      g_socket_output_stream_write_ready (input_stream, G_IO_OUT);
+      g_socket_output_stream_write_ready (input_stream->priv->socket, G_IO_OUT, input_stream);
     }
 }
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]