Fix make check
[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 <gio/gsimpleasyncresult.h>
31 #include <gio/gcancellable.h>
32
33 #include "gioalias.h"
34
35 #define g_socket_input_stream_get_type _g_socket_input_stream_get_type
36 G_DEFINE_TYPE (GSocketInputStream, g_socket_input_stream, G_TYPE_INPUT_STREAM);
37
38 enum
39 {
40   PROP_0,
41   PROP_SOCKET
42 };
43
44 struct _GSocketInputStreamPrivate
45 {
46   GSocket *socket;
47
48   /* pending operation metadata */
49   GSimpleAsyncResult *result;
50   GCancellable *cancellable;
51   gboolean from_mainloop;
52   gpointer buffer;
53   gsize count;
54 };
55
56 static void
57 g_socket_input_stream_get_property (GObject    *object,
58                                     guint       prop_id,
59                                     GValue     *value,
60                                     GParamSpec *pspec)
61 {
62   GSocketInputStream *stream = G_SOCKET_INPUT_STREAM (object);
63
64   switch (prop_id)
65     {
66       case PROP_SOCKET:
67         g_value_set_object (value, stream->priv->socket);
68         break;
69
70       default:
71         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
72     }
73 }
74
75 static void
76 g_socket_input_stream_set_property (GObject      *object,
77                                     guint         prop_id,
78                                     const GValue *value,
79                                     GParamSpec   *pspec)
80 {
81   GSocketInputStream *stream = G_SOCKET_INPUT_STREAM (object);
82
83   switch (prop_id)
84     {
85       case PROP_SOCKET:
86         stream->priv->socket = g_value_dup_object (value);
87         break;
88
89       default:
90         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
91     }
92 }
93
94 static void
95 g_socket_input_stream_finalize (GObject *object)
96 {
97   GSocketInputStream *stream = G_SOCKET_INPUT_STREAM (object);
98
99   if (stream->priv->socket)
100     g_object_unref (stream->priv->socket);
101
102   if (G_OBJECT_CLASS (g_socket_input_stream_parent_class)->finalize)
103     (*G_OBJECT_CLASS (g_socket_input_stream_parent_class)->finalize) (object);
104 }
105
106 static gssize
107 g_socket_input_stream_read (GInputStream  *stream,
108                             void          *buffer,
109                             gsize          count,
110                             GCancellable  *cancellable,
111                             GError       **error)
112 {
113   GSocketInputStream *input_stream = G_SOCKET_INPUT_STREAM (stream);
114
115   if (!g_socket_condition_wait (input_stream->priv->socket,
116                                 G_IO_IN, cancellable, error))
117     return -1;
118
119   return g_socket_receive (input_stream->priv->socket, buffer, count, error);
120 }
121
122 static gboolean
123 g_socket_input_stream_read_ready (GSocket *socket,
124                                   GIOCondition condition,
125                                   GSocketInputStream *stream)
126 {
127   GSimpleAsyncResult *simple;
128   GError *error = NULL;
129
130   simple = stream->priv->result;
131   stream->priv->result = NULL;
132
133   if (!g_cancellable_set_error_if_cancelled (stream->priv->cancellable,
134                                              &error))
135     {
136       gssize result;
137
138       result = g_socket_receive (stream->priv->socket,
139                                  stream->priv->buffer,
140                                  stream->priv->count,
141                                  &error);
142       if (result >= 0)
143         g_simple_async_result_set_op_res_gssize (simple, result);
144     }
145
146   if (error)
147     {
148       g_simple_async_result_set_from_error (simple, error);
149       g_error_free (error);
150     }
151
152   if (stream->priv->cancellable)
153     g_object_unref (stream->priv->cancellable);
154
155   if (stream->priv->from_mainloop)
156     g_simple_async_result_complete (simple);
157   else
158     g_simple_async_result_complete_in_idle (simple);
159
160   g_object_unref (simple);
161
162   return FALSE;
163 }
164
165 static void
166 g_socket_input_stream_read_async (GInputStream        *stream,
167                                   void                *buffer,
168                                   gsize                count,
169                                   gint                 io_priority,
170                                   GCancellable        *cancellable,
171                                   GAsyncReadyCallback  callback,
172                                   gpointer             user_data)
173 {
174   GSocketInputStream *input_stream = G_SOCKET_INPUT_STREAM (stream);
175
176   g_assert (input_stream->priv->result == NULL);
177
178   input_stream->priv->result =
179     g_simple_async_result_new (G_OBJECT (stream), callback, user_data,
180                                g_socket_input_stream_read_async);
181   if (cancellable)
182     g_object_ref (cancellable);
183   input_stream->priv->cancellable = cancellable;
184   input_stream->priv->buffer = buffer;
185   input_stream->priv->count = count;
186
187   if (!g_socket_condition_check (input_stream->priv->socket, G_IO_IN))
188     {
189       GSource *source;
190
191       input_stream->priv->from_mainloop = TRUE;
192       source = g_socket_create_source (input_stream->priv->socket,
193                                        G_IO_IN | G_IO_HUP | G_IO_ERR,
194                                        cancellable);
195       g_source_set_callback (source,
196                              (GSourceFunc) g_socket_input_stream_read_ready,
197                              g_object_ref (input_stream), g_object_unref);
198       g_source_attach (source, NULL);
199       g_source_unref (source);
200     }
201   else
202     {
203       input_stream->priv->from_mainloop = FALSE;
204       g_socket_input_stream_read_ready (input_stream->priv->socket, G_IO_IN, input_stream);
205     }
206 }
207
208 static gssize
209 g_socket_input_stream_read_finish (GInputStream  *stream,
210                                    GAsyncResult  *result,
211                                    GError       **error)
212 {
213   GSimpleAsyncResult *simple;
214   gssize count;
215
216   g_return_val_if_fail (G_IS_SOCKET_INPUT_STREAM (stream), -1);
217
218   simple = G_SIMPLE_ASYNC_RESULT (result);
219
220   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_socket_input_stream_read_async);
221
222   count = g_simple_async_result_get_op_res_gssize (simple);
223
224   return count;
225 }
226
227 static void
228 g_socket_input_stream_class_init (GSocketInputStreamClass *klass)
229 {
230   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
231   GInputStreamClass *ginputstream_class = G_INPUT_STREAM_CLASS (klass);
232
233   g_type_class_add_private (klass, sizeof (GSocketInputStreamPrivate));
234
235   gobject_class->finalize = g_socket_input_stream_finalize;
236   gobject_class->get_property = g_socket_input_stream_get_property;
237   gobject_class->set_property = g_socket_input_stream_set_property;
238
239   ginputstream_class->read_fn = g_socket_input_stream_read;
240   ginputstream_class->read_async = g_socket_input_stream_read_async;
241   ginputstream_class->read_finish = g_socket_input_stream_read_finish;
242
243   g_object_class_install_property (gobject_class, PROP_SOCKET,
244                                    g_param_spec_object ("socket",
245                                                         P_("socket"),
246                                                         P_("The socket that this stream wraps"),
247                                                         G_TYPE_SOCKET, G_PARAM_CONSTRUCT_ONLY |
248                                                         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
249 }
250
251 static void
252 g_socket_input_stream_init (GSocketInputStream *stream)
253 {
254   stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream, G_TYPE_SOCKET_INPUT_STREAM, GSocketInputStreamPrivate);
255 }
256
257 GSocketInputStream *
258 _g_socket_input_stream_new (GSocket *socket)
259 {
260   return G_SOCKET_INPUT_STREAM (g_object_new (G_TYPE_SOCKET_INPUT_STREAM, "socket", socket, NULL));
261 }
262
263 #define __G_SOCKET_INPUT_STREAM_C__
264 #include "gioaliasdef.c"