Merge remote branch 'gvdb/master'
[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
95 typedef GSocketConnectableIface GSocketConnectableInterface;
96 G_DEFINE_INTERFACE (GSocketConnectable, g_socket_connectable, G_TYPE_OBJECT)
97
98 static void
99 g_socket_connectable_default_init (GSocketConnectableInterface *iface)
100 {
101 }
102
103 /**
104  * g_socket_connectable_enumerate:
105  * @connectable: a #GSocketConnectable
106  *
107  * Creates a #GSocketAddressEnumerator for @connectable.
108  *
109  * Return value: a new #GSocketAddressEnumerator.
110  *
111  * Since: 2.22
112  */
113 GSocketAddressEnumerator *
114 g_socket_connectable_enumerate (GSocketConnectable *connectable)
115 {
116   GSocketConnectableIface *iface;
117
118   g_return_val_if_fail (G_IS_SOCKET_CONNECTABLE (connectable), NULL);
119
120   iface = G_SOCKET_CONNECTABLE_GET_IFACE (connectable);
121
122   return (* iface->enumerate) (connectable);
123 }
124
125 #define __G_SOCKET_CONNECTABLE_C__
126 #include "gioaliasdef.c"