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>
30 # include <netinet/in.h>
31 # include <arpa/inet.h>
39 #include "ginetaddress.h"
40 #include "ginet4address.h"
41 #include "ginet6address.h"
43 #include "gnioerror.h"
44 #include "ginetsocketaddress.h"
46 G_DEFINE_TYPE (GSocket, g_socket, G_TYPE_OBJECT);
48 struct _GSocketPrivate
54 g_socket_finalize (GObject *object)
56 GSocket *socket G_GNUC_UNUSED = G_SOCKET (object);
58 if (G_OBJECT_CLASS (g_socket_parent_class)->finalize)
59 (*G_OBJECT_CLASS (g_socket_parent_class)->finalize) (object);
63 g_socket_dispose (GObject *object)
65 GSocket *socket G_GNUC_UNUSED = G_SOCKET (object);;
67 if (G_OBJECT_CLASS (g_socket_parent_class)->dispose)
68 (*G_OBJECT_CLASS (g_socket_parent_class)->dispose) (object);
72 g_socket_class_init (GSocketClass *klass)
74 GObjectClass *gobject_class G_GNUC_UNUSED = G_OBJECT_CLASS (klass);
76 g_type_class_add_private (klass, sizeof (GSocketPrivate));
78 gobject_class->finalize = g_socket_finalize;
79 gobject_class->dispose = g_socket_dispose;
83 g_socket_init (GSocket *address)
85 address->priv = G_TYPE_INSTANCE_GET_PRIVATE (address, G_TYPE_SOCKET, GSocketPrivate);
91 return G_SOCKET (g_object_new (G_TYPE_SOCKET, NULL));
95 g_socket_new_from_fd (gint fd)
97 return G_SOCKET (g_object_new (G_TYPE_SOCKET, NULL));
101 g_socket_set_blocking (GSocket *socket, gboolean blocking)
105 g_return_if_fail (G_IS_SOCKET (socket));
107 if ((arg = fcntl (socket->priv->fd, F_GETFL, NULL)) < 0)
108 g_warning ("Error getting socket status flags: %s", g_strerror (errno));
110 arg = blocking ? arg | O_NONBLOCK : arg & ~O_NONBLOCK;
112 if (fcntl (socket->priv->fd, F_SETFL, arg) < 0)
113 g_warning ("Error setting socket status flags: %s", g_strerror (errno));
117 g_socket_listen (GSocket *socket, gint backlog)
119 g_return_if_fail (G_IS_SOCKET (socket));
121 listen (socket->priv->fd, backlog);
125 g_socket_connect (GSocket *socket,
126 GSocketAddress *address,
127 GCancellable *cancellable,
130 g_return_val_if_fail (G_IS_SOCKET (socket), FALSE);
134 if (errno == EINPROGRESS)
135 g_set_error (error, G_IO_ERROR, G_IO_ERROR_PENDING, "connection in progress");
137 g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errno), "error connecting: %s", g_strerror (errno));
145 g_socket_connect_async (GSocket *socket,
146 GSocketAddress *address,
147 GCancellable *cancellable,
148 GAsyncReadyCallback *callback,
155 g_socket_connect_finish (GSocket *socket,
156 GAsyncResult *result,