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>
31 #include "gtcpclient.h"
32 #include "gasynchelper.h"
34 G_DEFINE_TYPE (GTcpClient, g_tcp_client, G_TYPE_OBJECT);
44 struct _GTcpClientPrivate
46 GInetSocketAddress *address;
52 g_tcp_client_constructed (GObject *object)
54 GTcpClient *client = G_TCP_CLIENT (object);
56 if (client->priv->address)
58 // we've been constructed with an address, extract hostname+port
59 client->priv->hostname = g_inet_address_to_string (g_inet_socket_address_get_address (client->priv->address));
60 client->priv->port = g_inet_socket_address_get_port (client->priv->address);
66 g_tcp_client_get_property (GObject *object,
71 GTcpClient *client = G_TCP_CLIENT (object);
76 g_value_set_object (value, client->priv->address);
80 g_value_set_string (value, client->priv->hostname);
84 g_value_set_uint (value, client->priv->port);
88 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
93 g_tcp_client_set_property (GObject *object,
98 GTcpClient *client = G_TCP_CLIENT (object);
103 // sink the address' floating reference
104 client->priv->address = G_INET_SOCKET_ADDRESS (g_object_ref_sink (g_value_get_object (value)));
108 client->priv->hostname = g_value_dup_string (value);
112 client->priv->port = g_value_get_uint (value);
116 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
121 g_tcp_client_finalize (GObject *object)
123 GTcpClient *client = G_TCP_CLIENT (object);
125 g_object_unref (client->priv->address);
127 if (G_OBJECT_CLASS (g_tcp_client_parent_class)->finalize)
128 (*G_OBJECT_CLASS (g_tcp_client_parent_class)->finalize) (object);
132 g_tcp_client_dispose (GObject *object)
134 GTcpClient *client = G_TCP_CLIENT (object);
136 g_free (client->priv->hostname);
138 if (G_OBJECT_CLASS (g_tcp_client_parent_class)->dispose)
139 (*G_OBJECT_CLASS (g_tcp_client_parent_class)->dispose) (object);
143 g_tcp_client_class_init (GTcpClientClass *klass)
145 GObjectClass *gobject_class G_GNUC_UNUSED = G_OBJECT_CLASS (klass);
147 g_type_class_add_private (klass, sizeof (GTcpClientPrivate));
149 gobject_class->finalize = g_tcp_client_finalize;
150 gobject_class->dispose = g_tcp_client_dispose;
151 gobject_class->constructed = g_tcp_client_constructed;
152 gobject_class->set_property = g_tcp_client_set_property;
153 gobject_class->get_property = g_tcp_client_get_property;
155 g_object_class_install_property (gobject_class, PROP_ADDRESS,
156 g_param_spec_object ("address",
158 "the remote address the socket will connect to",
159 G_TYPE_INET_SOCKET_ADDRESS,
160 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
162 g_object_class_install_property (gobject_class, PROP_HOSTNAME,
163 g_param_spec_string ("hostname",
165 "the hostname of the remote address the socket will connect to",
167 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
169 g_object_class_install_property (gobject_class, PROP_PORT,
170 g_param_spec_uint ("port",
172 "the remote port the socket will connect to",
176 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_BLURB | G_PARAM_STATIC_NICK));
180 g_tcp_client_init (GTcpClient *client)
182 client->priv = G_TYPE_INSTANCE_GET_PRIVATE (client, G_TYPE_TCP_CLIENT, GTcpClientPrivate);
184 client->priv->address = NULL;
185 client->priv->hostname = NULL;
186 client->priv->port = 0;
190 g_tcp_client_new (const gchar *hostname,
193 return G_TCP_CLIENT (g_object_new (G_TYPE_TCP_CLIENT, "hostname", hostname, "port", port, NULL));
197 g_tcp_client_new_from_address (GInetSocketAddress *address)
199 return G_TCP_CLIENT (g_object_new (G_TYPE_TCP_CLIENT, "address", address, NULL));
203 g_tcp_client_connect (GTcpClient *client,
204 GCancellable *cancellable,
211 GAsyncReadyCallback callback;
212 GCancellable *cancellable;
215 gchar address_buffer[256];
216 gsize address_length;
220 connect_callback (ConnectData *data,
221 GIOCondition condition,
228 g_tcp_client_connect_async (GTcpClient *client,
229 GCancellable *cancellable,
230 GAsyncReadyCallback callback,
237 g_tcp_client_connect_finish (GTcpClient *client,
238 GAsyncResult *result,
241 GSimpleAsyncResult *simple;
243 g_return_val_if_fail (G_IS_TCP_CLIENT (client), FALSE);
245 simple = G_SIMPLE_ASYNC_RESULT (result);
247 if (g_simple_async_result_propagate_error (simple, error))
250 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_tcp_client_connect_async);
256 g_tcp_client_close (GTcpClient *tcp_client)
258 g_return_if_fail (G_IS_TCP_CLIENT (tcp_client));