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