Documentation and coding style fixups
[platform/upstream/glib.git] / gio / gsocketconnectable.c
1 /* GIO - GLib Input, Output and Streaming Library
2  * 
3  * Copyright (C) 2008 Red Hat, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #include "config.h"
22 #include "gsocketconnectable.h"
23 #include "glibintl.h"
24
25 #include "gioalias.h"
26
27 /**
28  * SECTION:gsocketconnectable
29  * @short_description: Interface for potential socket endpoints.
30  *
31  * Objects that describe one or more potential socket endpoints
32  * implement #GSocketConnectable. Callers can then use
33  * g_socket_connectable_enumerate() to get a #GSocketAddressEnumerator
34  * to try out each socket address in turn until one succeeds, as shown
35  * in the sample code below.
36  *
37  * |[
38  * MyConnectionType *
39  * connect_to_host (const char    *hostname,
40  *                  guint16        port,
41  *                  GCancellable  *cancellable,
42  *                  GError       **error)
43  * {
44  *   MyConnection *conn = NULL;
45  *   GSocketConnectable *addr;
46  *   GSocketAddressEnumerator *enumerator;
47  *   GSocketAddress *sockaddr;
48  *   GError *conn_error = NULL;
49  *
50  *   addr = g_network_address_new ("www.gnome.org", 80);
51  *   enumerator = g_socket_connectable_enumerate (addr);
52  *   g_object_unref (addr);
53  *
54  *   /<!-- -->* Try each sockaddr until we succeed. Record the first
55  *    * connection error, but not any further ones (since they'll probably
56  *    * be basically the same as the first).
57  *    *<!-- -->/
58  *   while (!conn && (sockaddr = g_socket_address_enumerator_next (enumerator, cancellable, error))
59  *     {
60  *       conn = connect_to_sockaddr (sockaddr, conn_error ? NULL : &conn_error);
61  *       g_object_unref (sockaddr);
62  *     }
63  *   g_object_unref (enumerator);
64  *
65  *   if (conn)
66  *     {
67  *       if (conn_error)
68  *         {
69  *           /<!-- -->* We couldn't connect to the first address, but we succeeded
70  *            * in connecting to a later address.
71  *            *<!-- -->/
72  *           g_error_free (conn_error);
73  *         }
74  *       return conn;
75  *     }
76  *   else if (error)
77  *     {
78  *       /<!-- -->* Either the initial lookup failed, or else the caller
79  *        * cancelled us.
80  *        *<!-- -->/
81  *       if (conn_error)
82  *         g_error_free (conn_error);
83  *       return NULL;
84  *     }
85  *   else
86  *     {
87  *       g_error_propagate (error, conn_error);
88  *       return NULL;
89  *     }
90  * }
91  * ]|
92  */
93
94 GType
95 g_socket_connectable_get_type (void)
96 {
97   static volatile gsize g_define_type_id__volatile = 0;
98
99   if (g_once_init_enter (&g_define_type_id__volatile))
100     {
101       const GTypeInfo connectable_info =
102       {
103         sizeof (GSocketConnectableIface), /* class_size */
104         NULL,           /* base_init */
105         NULL,           /* base_finalize */
106         NULL,
107         NULL,           /* class_finalize */
108         NULL,           /* class_data */
109         0,
110         0,              /* n_preallocs */
111         NULL
112       };
113       GType g_define_type_id =
114         g_type_register_static (G_TYPE_INTERFACE, I_("GSocketConnectable"),
115                                 &connectable_info, 0);
116
117       g_type_interface_add_prerequisite (g_define_type_id, G_TYPE_OBJECT);
118
119       g_once_init_leave (&g_define_type_id__volatile, g_define_type_id);
120     }
121
122   return g_define_type_id__volatile;
123 }
124
125 /**
126  * g_socket_connectable_enumerate:
127  * @connectable: a #GSocketConnectable
128  *
129  * Creates a #GSocketAddressEnumerator for @connectable.
130  *
131  * Return value: a new #GSocketAddressEnumerator.
132  *
133  * Since: 2.22
134  */
135 GSocketAddressEnumerator *
136 g_socket_connectable_enumerate (GSocketConnectable *connectable)
137 {
138   GSocketConnectableIface *iface;
139
140   g_return_val_if_fail (G_IS_SOCKET_CONNECTABLE (connectable), NULL);
141
142   iface = G_SOCKET_CONNECTABLE_GET_IFACE (connectable);
143
144   return (* iface->enumerate) (connectable);
145 }
146
147 #define __G_SOCKET_CONNECTABLE_C__
148 #include "gioaliasdef.c"