gio: port networking classes from GSimpleAsyncResult to GTask
[platform/upstream/glib.git] / gio / gsocketinputstream.c
1 /* GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright © 2008 Christian Kellner, Samuel Cormier-Iijima
4  *           © 2009 codethink
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General
17  * Public License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
19  * Boston, MA 02111-1307, USA.
20  *
21  * Authors: Christian Kellner <gicmo@gnome.org>
22  *          Samuel Cormier-Iijima <sciyoshi@gmail.com>
23  *          Ryan Lortie <desrt@desrt.ca>
24  */
25
26 #include "config.h"
27 #include "gsocketinputstream.h"
28 #include "glibintl.h"
29
30 #include "gcancellable.h"
31 #include "gpollableinputstream.h"
32 #include "gioerror.h"
33 #include "gfiledescriptorbased.h"
34
35 static void g_socket_input_stream_pollable_iface_init (GPollableInputStreamInterface *iface);
36 #ifdef G_OS_UNIX
37 static void g_socket_input_stream_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface);
38 #endif
39
40 #define g_socket_input_stream_get_type _g_socket_input_stream_get_type
41
42 #ifdef G_OS_UNIX
43 G_DEFINE_TYPE_WITH_CODE (GSocketInputStream, g_socket_input_stream, G_TYPE_INPUT_STREAM,
44                          G_IMPLEMENT_INTERFACE (G_TYPE_POLLABLE_INPUT_STREAM, g_socket_input_stream_pollable_iface_init)
45                          G_IMPLEMENT_INTERFACE (G_TYPE_FILE_DESCRIPTOR_BASED, g_socket_input_stream_file_descriptor_based_iface_init)
46                          )
47 #else
48 G_DEFINE_TYPE_WITH_CODE (GSocketInputStream, g_socket_input_stream, G_TYPE_INPUT_STREAM,
49                          G_IMPLEMENT_INTERFACE (G_TYPE_POLLABLE_INPUT_STREAM, g_socket_input_stream_pollable_iface_init)
50                          )
51 #endif
52
53 enum
54 {
55   PROP_0,
56   PROP_SOCKET
57 };
58
59 struct _GSocketInputStreamPrivate
60 {
61   GSocket *socket;
62
63   /* pending operation metadata */
64   gpointer buffer;
65   gsize count;
66 };
67
68 static void
69 g_socket_input_stream_get_property (GObject    *object,
70                                     guint       prop_id,
71                                     GValue     *value,
72                                     GParamSpec *pspec)
73 {
74   GSocketInputStream *stream = G_SOCKET_INPUT_STREAM (object);
75
76   switch (prop_id)
77     {
78       case PROP_SOCKET:
79         g_value_set_object (value, stream->priv->socket);
80         break;
81
82       default:
83         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
84     }
85 }
86
87 static void
88 g_socket_input_stream_set_property (GObject      *object,
89                                     guint         prop_id,
90                                     const GValue *value,
91                                     GParamSpec   *pspec)
92 {
93   GSocketInputStream *stream = G_SOCKET_INPUT_STREAM (object);
94
95   switch (prop_id)
96     {
97       case PROP_SOCKET:
98         stream->priv->socket = g_value_dup_object (value);
99         break;
100
101       default:
102         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
103     }
104 }
105
106 static void
107 g_socket_input_stream_finalize (GObject *object)
108 {
109   GSocketInputStream *stream = G_SOCKET_INPUT_STREAM (object);
110
111   if (stream->priv->socket)
112     g_object_unref (stream->priv->socket);
113
114   if (G_OBJECT_CLASS (g_socket_input_stream_parent_class)->finalize)
115     (*G_OBJECT_CLASS (g_socket_input_stream_parent_class)->finalize) (object);
116 }
117
118 static gssize
119 g_socket_input_stream_read (GInputStream  *stream,
120                             void          *buffer,
121                             gsize          count,
122                             GCancellable  *cancellable,
123                             GError       **error)
124 {
125   GSocketInputStream *input_stream = G_SOCKET_INPUT_STREAM (stream);
126
127   return g_socket_receive_with_blocking (input_stream->priv->socket,
128                                          buffer, count, TRUE,
129                                          cancellable, error);
130 }
131
132 static gboolean
133 g_socket_input_stream_pollable_is_readable (GPollableInputStream *pollable)
134 {
135   GSocketInputStream *input_stream = G_SOCKET_INPUT_STREAM (pollable);
136
137   return g_socket_condition_check (input_stream->priv->socket, G_IO_IN);
138 }
139
140 static GSource *
141 g_socket_input_stream_pollable_create_source (GPollableInputStream *pollable,
142                                               GCancellable         *cancellable)
143 {
144   GSocketInputStream *input_stream = G_SOCKET_INPUT_STREAM (pollable);
145   GSource *socket_source, *pollable_source;
146
147   pollable_source = g_pollable_source_new (G_OBJECT (input_stream));
148   socket_source = g_socket_create_source (input_stream->priv->socket,
149                                           G_IO_IN, cancellable);
150   g_source_set_dummy_callback (socket_source);
151   g_source_add_child_source (pollable_source, socket_source);
152   g_source_unref (socket_source);
153
154   return pollable_source;
155 }
156
157 static gssize
158 g_socket_input_stream_pollable_read_nonblocking (GPollableInputStream  *pollable,
159                                                  void                  *buffer,
160                                                  gsize                  size,
161                                                  GError               **error)
162 {
163   GSocketInputStream *input_stream = G_SOCKET_INPUT_STREAM (pollable);
164
165   return g_socket_receive_with_blocking (input_stream->priv->socket,
166                                          buffer, size, FALSE,
167                                          NULL, error);
168 }
169
170 #ifdef G_OS_UNIX
171 static int
172 g_socket_input_stream_get_fd (GFileDescriptorBased *fd_based)
173 {
174   GSocketInputStream *input_stream = G_SOCKET_INPUT_STREAM (fd_based);
175
176   return g_socket_get_fd (input_stream->priv->socket);
177 }
178 #endif
179
180 static void
181 g_socket_input_stream_class_init (GSocketInputStreamClass *klass)
182 {
183   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
184   GInputStreamClass *ginputstream_class = G_INPUT_STREAM_CLASS (klass);
185
186   g_type_class_add_private (klass, sizeof (GSocketInputStreamPrivate));
187
188   gobject_class->finalize = g_socket_input_stream_finalize;
189   gobject_class->get_property = g_socket_input_stream_get_property;
190   gobject_class->set_property = g_socket_input_stream_set_property;
191
192   ginputstream_class->read_fn = g_socket_input_stream_read;
193
194   g_object_class_install_property (gobject_class, PROP_SOCKET,
195                                    g_param_spec_object ("socket",
196                                                         P_("socket"),
197                                                         P_("The socket that this stream wraps"),
198                                                         G_TYPE_SOCKET, G_PARAM_CONSTRUCT_ONLY |
199                                                         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
200 }
201
202 #ifdef G_OS_UNIX
203 static void
204 g_socket_input_stream_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface)
205 {
206   iface->get_fd = g_socket_input_stream_get_fd;
207 }
208 #endif
209
210 static void
211 g_socket_input_stream_pollable_iface_init (GPollableInputStreamInterface *iface)
212 {
213   iface->is_readable = g_socket_input_stream_pollable_is_readable;
214   iface->create_source = g_socket_input_stream_pollable_create_source;
215   iface->read_nonblocking = g_socket_input_stream_pollable_read_nonblocking;
216 }
217
218 static void
219 g_socket_input_stream_init (GSocketInputStream *stream)
220 {
221   stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream, G_TYPE_SOCKET_INPUT_STREAM, GSocketInputStreamPrivate);
222 }
223
224 GSocketInputStream *
225 _g_socket_input_stream_new (GSocket *socket)
226 {
227   return G_SOCKET_INPUT_STREAM (g_object_new (G_TYPE_SOCKET_INPUT_STREAM, "socket", socket, NULL));
228 }