1 /* GNIO - GLib Network Layer of GIO
3 * Copyright (C) 2008 Christian Kellner, Samuel Cormier-Iijima
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.
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.
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.
20 * Authors: Christian Kellner <gicmo@gnome.org>
21 * Samuel Cormier-Iijima <sciyoshi@gmail.com>
27 #include <gio/gasynchelper.h>
32 #include "ginetaddress.h"
33 #include "ginet4address.h"
34 #include "ginet6address.h"
36 #include "gtcpclient.h"
37 #include "gnioerror.h"
38 #include "ginetsocketaddress.h"
40 G_DEFINE_TYPE (GTcpClient, g_tcp_client, G_TYPE_OBJECT);
48 struct _GTcpClientPrivate
50 GSocketAddress *address;
54 g_tcp_client_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
56 GTcpClient *client = G_TCP_CLIENT (object);
61 g_value_set_object (value, client->priv->address);
65 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
70 g_tcp_client_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
72 GTcpClient *client = G_TCP_CLIENT (object);
77 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
82 g_tcp_client_finalize (GObject *object)
84 GTcpClient *client = G_TCP_CLIENT (object);
86 if (G_OBJECT_CLASS (g_tcp_client_parent_class)->finalize)
87 (*G_OBJECT_CLASS (g_tcp_client_parent_class)->finalize) (object);
91 g_tcp_client_dispose (GObject *object)
93 GTcpClient *client = G_TCP_CLIENT (object);
95 if (G_OBJECT_CLASS (g_tcp_client_parent_class)->dispose)
96 (*G_OBJECT_CLASS (g_tcp_client_parent_class)->dispose) (object);
100 g_tcp_client_class_init (GTcpClientClass *klass)
102 GObjectClass *gobject_class G_GNUC_UNUSED = G_OBJECT_CLASS (klass);
104 g_type_class_add_private (klass, sizeof (GTcpClientPrivate));
106 gobject_class->finalize = g_tcp_client_finalize;
107 gobject_class->dispose = g_tcp_client_dispose;
108 gobject_class->set_property = g_tcp_client_set_property;
109 gobject_class->get_property = g_tcp_client_get_property;
111 g_object_class_install_property (gobject_class, PROP_ADDRESS,
112 g_param_spec_object ("address",
114 "the remote address the socket will connect to",
115 G_TYPE_SOCKET_ADDRESS,
116 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
120 g_tcp_client_init (GTcpClient *client)
122 client->priv = G_TYPE_INSTANCE_GET_PRIVATE (client, G_TYPE_TCP_CLIENT, GTcpClientPrivate);
126 g_tcp_client_new (const gchar *hostname,
133 g_tcp_client_new_with_address (GInetSocketAddress *address)
139 g_tcp_client_connect (GTcpClient *client,
140 GCancellable *cancellable,
147 GAsyncReadyCallback callback;
148 GCancellable *cancellable;
151 gchar address_buffer[256];
152 gsize address_length;
156 connect_callback (ConnectData *data,
157 GIOCondition condition,
164 g_tcp_client_connect_async (GTcpClient *client,
165 GCancellable *cancellable,
166 GAsyncReadyCallback callback,
173 g_tcp_client_connect_finish (GTcpClient *client,
174 GAsyncResult *result,
177 GSimpleAsyncResult *simple;
179 g_return_val_if_fail (G_IS_TCP_CLIENT (client), FALSE);
181 simple = G_SIMPLE_ASYNC_RESULT (result);
183 if (g_simple_async_result_propagate_error (simple, error))
186 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_tcp_client_connect_async);
192 g_tcp_client_close (GTcpClient *tcp_client)
194 g_return_if_fail (G_IS_TCP_CLIENT (tcp_client));