Tizen 2.1 base
[platform/upstream/glib2.0.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 "gsimpleasyncresult.h"
31 #include "gcancellable.h"
32 #include "gpollableinputstream.h"
33 #include "gioerror.h"
34 #include "gfiledescriptorbased.h"
35
36 static void g_socket_input_stream_pollable_iface_init (GPollableInputStreamInterface *iface);
37 #ifdef G_OS_UNIX
38 static void g_socket_input_stream_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface);
39 #endif
40
41 #define g_socket_input_stream_get_type _g_socket_input_stream_get_type
42
43 #ifdef G_OS_UNIX
44 G_DEFINE_TYPE_WITH_CODE (GSocketInputStream, g_socket_input_stream, G_TYPE_INPUT_STREAM,
45                          G_IMPLEMENT_INTERFACE (G_TYPE_POLLABLE_INPUT_STREAM, g_socket_input_stream_pollable_iface_init)
46                          G_IMPLEMENT_INTERFACE (G_TYPE_FILE_DESCRIPTOR_BASED, g_socket_input_stream_file_descriptor_based_iface_init)
47                          )
48 #else
49 G_DEFINE_TYPE_WITH_CODE (GSocketInputStream, g_socket_input_stream, G_TYPE_INPUT_STREAM,
50                          G_IMPLEMENT_INTERFACE (G_TYPE_POLLABLE_INPUT_STREAM, g_socket_input_stream_pollable_iface_init)
51                          )
52 #endif
53
54 enum
55 {
56   PROP_0,
57   PROP_SOCKET
58 };
59
60 struct _GSocketInputStreamPrivate
61 {
62   GSocket *socket;
63
64   /* pending operation metadata */
65   GSimpleAsyncResult *result;
66   GCancellable *cancellable;
67   gpointer buffer;
68   gsize count;
69 };
70
71 static void
72 g_socket_input_stream_get_property (GObject    *object,
73                                     guint       prop_id,
74                                     GValue     *value,
75                                     GParamSpec *pspec)
76 {
77   GSocketInputStream *stream = G_SOCKET_INPUT_STREAM (object);
78
79   switch (prop_id)
80     {
81       case PROP_SOCKET:
82         g_value_set_object (value, stream->priv->socket);
83         break;
84
85       default:
86         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
87     }
88 }
89
90 static void
91 g_socket_input_stream_set_property (GObject      *object,
92                                     guint         prop_id,
93                                     const GValue *value,
94                                     GParamSpec   *pspec)
95 {
96   GSocketInputStream *stream = G_SOCKET_INPUT_STREAM (object);
97
98   switch (prop_id)
99     {
100       case PROP_SOCKET:
101         stream->priv->socket = g_value_dup_object (value);
102         break;
103
104       default:
105         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
106     }
107 }
108
109 static void
110 g_socket_input_stream_finalize (GObject *object)
111 {
112   GSocketInputStream *stream = G_SOCKET_INPUT_STREAM (object);
113
114   if (stream->priv->socket)
115     g_object_unref (stream->priv->socket);
116
117   if (G_OBJECT_CLASS (g_socket_input_stream_parent_class)->finalize)
118     (*G_OBJECT_CLASS (g_socket_input_stream_parent_class)->finalize) (object);
119 }
120
121 static gssize
122 g_socket_input_stream_read (GInputStream  *stream,
123                             void          *buffer,
124                             gsize          count,
125                             GCancellable  *cancellable,
126                             GError       **error)
127 {
128   GSocketInputStream *input_stream = G_SOCKET_INPUT_STREAM (stream);
129
130   return g_socket_receive_with_blocking (input_stream->priv->socket,
131                                          buffer, count, TRUE,
132                                          cancellable, error);
133 }
134
135 static gboolean
136 g_socket_input_stream_read_ready (GSocket *socket,
137                                   GIOCondition condition,
138                                   GSocketInputStream *stream)
139 {
140   GSimpleAsyncResult *simple;
141   GError *error = NULL;
142   gssize result;
143
144   result = g_socket_receive_with_blocking (stream->priv->socket,
145                                            stream->priv->buffer,
146                                            stream->priv->count,
147                                            FALSE,
148                                            stream->priv->cancellable,
149                                            &error);
150
151   if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK))
152     return TRUE;
153
154   simple = stream->priv->result;
155   stream->priv->result = NULL;
156
157   if (result >= 0)
158     g_simple_async_result_set_op_res_gssize (simple, result);
159
160   if (error)
161     g_simple_async_result_take_error (simple, error);
162
163   if (stream->priv->cancellable)
164     g_object_unref (stream->priv->cancellable);
165
166   g_simple_async_result_complete (simple);
167   g_object_unref (simple);
168
169   return FALSE;
170 }
171
172 static void
173 g_socket_input_stream_read_async (GInputStream        *stream,
174                                   void                *buffer,
175                                   gsize                count,
176                                   gint                 io_priority,
177                                   GCancellable        *cancellable,
178                                   GAsyncReadyCallback  callback,
179                                   gpointer             user_data)
180 {
181   GSocketInputStream *input_stream = G_SOCKET_INPUT_STREAM (stream);
182   GSource *source;
183
184   g_assert (input_stream->priv->result == NULL);
185
186   input_stream->priv->result =
187     g_simple_async_result_new (G_OBJECT (stream), callback, user_data,
188                                g_socket_input_stream_read_async);
189   if (cancellable)
190     g_object_ref (cancellable);
191   input_stream->priv->cancellable = cancellable;
192   input_stream->priv->buffer = buffer;
193   input_stream->priv->count = count;
194
195   source = g_socket_create_source (input_stream->priv->socket,
196                                    G_IO_IN | G_IO_HUP | G_IO_ERR,
197                                    cancellable);
198   g_source_set_callback (source,
199                          (GSourceFunc) g_socket_input_stream_read_ready,
200                          g_object_ref (input_stream), g_object_unref);
201   g_source_attach (source, g_main_context_get_thread_default ());
202   g_source_unref (source);
203 }
204
205 static gssize
206 g_socket_input_stream_read_finish (GInputStream  *stream,
207                                    GAsyncResult  *result,
208                                    GError       **error)
209 {
210   GSimpleAsyncResult *simple;
211   gssize count;
212
213   g_return_val_if_fail (G_IS_SOCKET_INPUT_STREAM (stream), -1);
214
215   simple = G_SIMPLE_ASYNC_RESULT (result);
216
217   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_socket_input_stream_read_async);
218
219   count = g_simple_async_result_get_op_res_gssize (simple);
220
221   return count;
222 }
223
224 static gboolean
225 g_socket_input_stream_pollable_is_readable (GPollableInputStream *pollable)
226 {
227   GSocketInputStream *input_stream = G_SOCKET_INPUT_STREAM (pollable);
228
229   return g_socket_condition_check (input_stream->priv->socket, G_IO_IN);
230 }
231
232 static GSource *
233 g_socket_input_stream_pollable_create_source (GPollableInputStream *pollable,
234                                               GCancellable         *cancellable)
235 {
236   GSocketInputStream *input_stream = G_SOCKET_INPUT_STREAM (pollable);
237   GSource *socket_source, *pollable_source;
238
239   pollable_source = g_pollable_source_new (G_OBJECT (input_stream));
240   socket_source = g_socket_create_source (input_stream->priv->socket,
241                                           G_IO_IN, cancellable);
242   g_source_set_dummy_callback (socket_source);
243   g_source_add_child_source (pollable_source, socket_source);
244   g_source_unref (socket_source);
245
246   return pollable_source;
247 }
248
249 static gssize
250 g_socket_input_stream_pollable_read_nonblocking (GPollableInputStream  *pollable,
251                                                  void                  *buffer,
252                                                  gsize                  size,
253                                                  GError               **error)
254 {
255   GSocketInputStream *input_stream = G_SOCKET_INPUT_STREAM (pollable);
256
257   return g_socket_receive_with_blocking (input_stream->priv->socket,
258                                          buffer, size, FALSE,
259                                          NULL, error);
260 }
261
262 #ifdef G_OS_UNIX
263 static int
264 g_socket_input_stream_get_fd (GFileDescriptorBased *fd_based)
265 {
266   GSocketInputStream *input_stream = G_SOCKET_INPUT_STREAM (fd_based);
267
268   return g_socket_get_fd (input_stream->priv->socket);
269 }
270 #endif
271
272 static void
273 g_socket_input_stream_class_init (GSocketInputStreamClass *klass)
274 {
275   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
276   GInputStreamClass *ginputstream_class = G_INPUT_STREAM_CLASS (klass);
277
278   g_type_class_add_private (klass, sizeof (GSocketInputStreamPrivate));
279
280   gobject_class->finalize = g_socket_input_stream_finalize;
281   gobject_class->get_property = g_socket_input_stream_get_property;
282   gobject_class->set_property = g_socket_input_stream_set_property;
283
284   ginputstream_class->read_fn = g_socket_input_stream_read;
285   ginputstream_class->read_async = g_socket_input_stream_read_async;
286   ginputstream_class->read_finish = g_socket_input_stream_read_finish;
287
288   g_object_class_install_property (gobject_class, PROP_SOCKET,
289                                    g_param_spec_object ("socket",
290                                                         P_("socket"),
291                                                         P_("The socket that this stream wraps"),
292                                                         G_TYPE_SOCKET, G_PARAM_CONSTRUCT_ONLY |
293                                                         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
294 }
295
296 #ifdef G_OS_UNIX
297 static void
298 g_socket_input_stream_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface)
299 {
300   iface->get_fd = g_socket_input_stream_get_fd;
301 }
302 #endif
303
304 static void
305 g_socket_input_stream_pollable_iface_init (GPollableInputStreamInterface *iface)
306 {
307   iface->is_readable = g_socket_input_stream_pollable_is_readable;
308   iface->create_source = g_socket_input_stream_pollable_create_source;
309   iface->read_nonblocking = g_socket_input_stream_pollable_read_nonblocking;
310 }
311
312 static void
313 g_socket_input_stream_init (GSocketInputStream *stream)
314 {
315   stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream, G_TYPE_SOCKET_INPUT_STREAM, GSocketInputStreamPrivate);
316 }
317
318 GSocketInputStream *
319 _g_socket_input_stream_new (GSocket *socket)
320 {
321   return G_SOCKET_INPUT_STREAM (g_object_new (G_TYPE_SOCKET_INPUT_STREAM, "socket", socket, NULL));
322 }