[gnio/connection-factory] Update to the new GSocket API for with protocol as int



commit 09ac1a1dd702545651f19653d9bcb52d0a060722
Author: Alexander Larsson <alexl redhat com>
Date:   Fri May 15 09:12:56 2009 +0200

    Update to the new GSocket API for with protocol as int
    
    Also change the connection factory to use int protocols.
---
 gio/gsocketclient.c     |    6 ++++--
 gio/gsocketconnection.c |   37 +++++++++++--------------------------
 gio/gsocketconnection.h |    4 ++--
 gio/gsocketlistener.c   |    3 ++-
 gio/gtcpconnection.c    |    8 ++++----
 gio/gunixconnection.c   |    2 +-
 test/server.c           |    2 +-
 7 files changed, 25 insertions(+), 37 deletions(-)

diff --git a/gio/gsocketclient.c b/gio/gsocketclient.c
index b121b4c..3fefd90 100644
--- a/gio/gsocketclient.c
+++ b/gio/gsocketclient.c
@@ -66,6 +66,7 @@ create_socket (GSocketClient  *client,
 {
   GSocketFamily family;
   GSocket *socket;
+  int proto;
 
   family = client->priv->family;
   if (family == G_SOCKET_FAMILY_INVALID &&
@@ -74,9 +75,10 @@ create_socket (GSocketClient  *client,
   if (family == G_SOCKET_FAMILY_INVALID)
     family = g_socket_address_get_family (dest_address);
 
+  proto = g_socket_protocol_id_lookup_by_name (client->priv->protocol);
   socket = g_socket_new (family,
 			 client->priv->type,
-			 client->priv->protocol,
+			 proto,
 			 error);
   if (socket == NULL)
     return NULL;
@@ -222,7 +224,7 @@ g_socket_client_class_init (GSocketClientClass *class)
   g_object_class_install_property (gobject_class, PROP_PROTOCOL,
                                    g_param_spec_string ("protocol",
 							P_("Socket protocol"),
-							P_("The protocol to use for socket construction"),
+							P_("The protocol to use for socket construction, or %NULL for default"),
 							NULL,
 							G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
 
diff --git a/gio/gsocketconnection.c b/gio/gsocketconnection.c
index 9c75fe6..4ec8045 100644
--- a/gio/gsocketconnection.c
+++ b/gio/gsocketconnection.c
@@ -253,7 +253,7 @@ g_socket_connection_close_finish (GIOStream  *stream,
 typedef struct {
   GSocketFamily socket_family;
   GSocketType socket_type;
-  char *protocol;
+  int protocol;
   GType implementation;
 } ConnectionFactory;
 
@@ -263,13 +263,11 @@ connection_factory_hash (gconstpointer key)
   const ConnectionFactory *factory = key;
   guint h;
 
-  h = factory->socket_family ^ (factory->socket_type << 4);
+  h = factory->socket_family ^ (factory->socket_type << 4) ^ (factory->protocol << 8);
   /* This is likely to be small, so spread over whole
      hash space to get some distribution */
   h = h ^ (h << 8) ^ (h << 16) ^ (h << 24);
 
-  if (factory->protocol)
-    h ^= g_str_hash (factory->protocol);
   return h;
 }
 
@@ -287,24 +285,11 @@ connection_factory_equal (gconstpointer _a,
     return FALSE;
 
   if (a->protocol != b->protocol)
-    {
-      if (a->protocol == NULL || b->protocol == NULL)
-	return FALSE;
-
-      if (strcmp (a->protocol, b->protocol) != 0)
-	return FALSE;
-    }
+    return FALSE;
 
   return TRUE;
 }
 
-static void
-connection_factory_free (ConnectionFactory *factory)
-{
-  g_free (factory->protocol);
-  g_free (factory);
-}
-
 static GHashTable *connection_factories = NULL;
 G_LOCK_DEFINE_STATIC(connection_factories);
 
@@ -312,7 +297,7 @@ void
 g_socket_connection_factory_register_type (GType g_type,
 					   GSocketFamily family,
 					   GSocketType type,
-					   const char *protocol)
+					   gint protocol)
 {
   ConnectionFactory *factory;
 
@@ -321,13 +306,13 @@ g_socket_connection_factory_register_type (GType g_type,
   if (connection_factories == NULL)
     connection_factories = g_hash_table_new_full (connection_factory_hash,
 						  connection_factory_equal,
-						  (GDestroyNotify)connection_factory_free,
+						  (GDestroyNotify)g_free,
 						  NULL);
 
   factory = g_new0 (ConnectionFactory, 1);
   factory->socket_family = family;
   factory->socket_type = type;
-  factory->protocol = g_strdup (protocol);
+  factory->protocol = protocol;
   factory->implementation = g_type;
 
   g_hash_table_insert (connection_factories,
@@ -347,9 +332,9 @@ init_builtin_types (void)
 }
 
 GType
-g_socket_connection_factory_lookup_type (GSocketFamily  family,
-					 GSocketType    type,
-					 const char    *protocol)
+g_socket_connection_factory_lookup_type (GSocketFamily family,
+					 GSocketType type,
+					 gint protocol_id)
 {
   ConnectionFactory *factory, key;
   GType g_type;
@@ -364,7 +349,7 @@ g_socket_connection_factory_lookup_type (GSocketFamily  family,
     {
       key.socket_family = family;
       key.socket_type = type;
-      key.protocol = (char *)protocol;
+      key.protocol = protocol_id;
 
       factory = g_hash_table_lookup (connection_factories, &key);
       if (factory)
@@ -383,6 +368,6 @@ g_socket_connection_factory_create_connection (GSocket *socket)
 
   type = g_socket_connection_factory_lookup_type (g_socket_get_family (socket),
 						  g_socket_get_socket_type (socket),
-						  g_socket_get_protocol (socket));
+						  g_socket_get_protocol_id (socket));
   return g_object_new (type, "socket", socket, NULL);
 }
diff --git a/gio/gsocketconnection.h b/gio/gsocketconnection.h
index 96481a1..ecbcccd 100644
--- a/gio/gsocketconnection.h
+++ b/gio/gsocketconnection.h
@@ -61,10 +61,10 @@ GSocket           *g_socket_connection_get_socket                (GSocketConnect
 void               g_socket_connection_factory_register_type     (GType          g_type,
 								  GSocketFamily  family,
 								  GSocketType    type,
-								  const char    *protocol);
+								  gint           protocol);
 GType              g_socket_connection_factory_lookup_type       (GSocketFamily  family,
 								  GSocketType    type,
-								  const char    *protocol);
+								  gint           protocol);
 GSocketConnection *g_socket_connection_factory_create_connection (GSocket       *socket);
 
 G_END_DECLS
diff --git a/gio/gsocketlistener.c b/gio/gsocketlistener.c
index 9ac7299..340ac1a 100644
--- a/gio/gsocketlistener.c
+++ b/gio/gsocketlistener.c
@@ -203,7 +203,8 @@ g_socket_listener_add_address (GSocketListener *listener,
     return FALSE;
 
   family = g_socket_address_get_family (address);
-  socket = g_socket_new (family, type, protocol, error);
+  socket = g_socket_new (family, type,
+			 g_socket_protocol_id_lookup_by_name (protocol), error);
   if (socket == NULL)
     return FALSE;
 
diff --git a/gio/gtcpconnection.c b/gio/gtcpconnection.c
index a0edce7..c04b5c8 100644
--- a/gio/gtcpconnection.c
+++ b/gio/gtcpconnection.c
@@ -33,19 +33,19 @@ G_DEFINE_TYPE_WITH_CODE (GTcpConnection, g_tcp_connection,
   g_socket_connection_factory_register_type (g_define_type_id,
 					     G_SOCKET_FAMILY_IPV4,
 					     G_SOCKET_TYPE_STREAM,
-					     NULL);
+					     0);
   g_socket_connection_factory_register_type (g_define_type_id,
 					     G_SOCKET_FAMILY_IPV6,
 					     G_SOCKET_TYPE_STREAM,
-					     NULL);
+					     0);
   g_socket_connection_factory_register_type (g_define_type_id,
 					     G_SOCKET_FAMILY_IPV4,
 					     G_SOCKET_TYPE_STREAM,
-					     "tcp");
+					     g_socket_protocol_id_lookup_by_name ("tcp"));
   g_socket_connection_factory_register_type (g_define_type_id,
 					     G_SOCKET_FAMILY_IPV6,
 					     G_SOCKET_TYPE_STREAM,
-					     "tcp");
+					     g_socket_protocol_id_lookup_by_name ("tcp"));
 			 );
 
 static void
diff --git a/gio/gunixconnection.c b/gio/gunixconnection.c
index 02c0747..0f8a6fe 100644
--- a/gio/gunixconnection.c
+++ b/gio/gunixconnection.c
@@ -23,7 +23,7 @@ G_DEFINE_TYPE_WITH_CODE (GUnixConnection, g_unix_connection,
   g_socket_connection_factory_register_type (g_define_type_id,
 					     G_SOCKET_FAMILY_UNIX,
 					     G_SOCKET_TYPE_STREAM,
-					     NULL);
+					     0);
 			 );
 
 gboolean
diff --git a/test/server.c b/test/server.c
index 0733af5..dca34c1 100644
--- a/test/server.c
+++ b/test/server.c
@@ -143,7 +143,7 @@ main (int argc,
   else
     socket_type = G_SOCKET_TYPE_STREAM;
 
-  socket = g_socket_new (G_SOCKET_FAMILY_IPV4, socket_type, NULL, &error);
+  socket = g_socket_new (G_SOCKET_FAMILY_IPV4, socket_type, 0, &error);
 
   if (socket == NULL)
     {



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