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