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