Various doc tweaks
[platform/upstream/glib.git] / gio / gpollableinputstream.c
1 /* GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright (C) 2010 Red Hat, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #include "config.h"
22
23 #include <errno.h>
24
25 #include "gpollableinputstream.h"
26 #include "gasynchelper.h"
27 #include "gio-marshal.h"
28 #include "glibintl.h"
29
30 /**
31  * SECTION:gpollableinputstream
32  * @short_description: Interface for pollable input streams
33  * @include: gio/gio.h
34  * @see_also: #GInputStream, #GPollableOutputStream, #GFileDescriptorBased
35  *
36  * #GPollableInputStream is implemented by #GInputStream<!-- -->s that
37  * can be polled for readiness to read. This can be used when
38  * interfacing with a non-GIO API that expects
39  * UNIX-file-descriptor-style asynchronous I/O rather than GIO-style.
40  *
41  * Since: 2.28
42  */
43
44 G_DEFINE_INTERFACE (GPollableInputStream, g_pollable_input_stream, G_TYPE_INPUT_STREAM)
45
46 static gboolean g_pollable_input_stream_default_can_poll         (GPollableInputStream *stream);
47 static gssize   g_pollable_input_stream_default_read_nonblocking (GPollableInputStream  *stream,
48                                                                   void                  *buffer,
49                                                                   gsize                  size,
50                                                                   GError               **error);
51
52 static void
53 g_pollable_input_stream_default_init (GPollableInputStreamInterface *iface)
54 {
55   iface->can_poll         = g_pollable_input_stream_default_can_poll;
56   iface->read_nonblocking = g_pollable_input_stream_default_read_nonblocking;
57 }
58
59 static gboolean
60 g_pollable_input_stream_default_can_poll (GPollableInputStream *stream)
61 {
62   return TRUE;
63 }
64
65 /**
66  * g_pollable_input_stream_can_poll:
67  * @stream: a #GPollableInputStream.
68  *
69  * Checks if @stream is actually pollable. Some classes may implement
70  * #GPollableInputStream but have only certain instances of that class
71  * be pollable. If this method returns %FALSE, then the behavior of
72  * other #GPollableInputStream methods is undefined.
73  *
74  * For any given stream, the value returned by this method is constant;
75  * a stream cannot switch from pollable to non-pollable or vice versa.
76  *
77  * Returns: %TRUE if @stream is pollable, %FALSE if not.
78  *
79  * Since: 2.28
80  */
81 gboolean
82 g_pollable_input_stream_can_poll (GPollableInputStream *stream)
83 {
84   g_return_val_if_fail (G_IS_POLLABLE_INPUT_STREAM (stream), FALSE);
85
86   return G_POLLABLE_INPUT_STREAM_GET_INTERFACE (stream)->can_poll (stream);
87 }
88
89 /**
90  * g_pollable_input_stream_is_readable:
91  * @stream: a #GPollableInputStream.
92  *
93  * Checks if @stream can be read.
94  *
95  * Note that some stream types may not be able to implement this 100%
96  * reliably, and it is possible that a call to g_input_stream_read()
97  * after this returns %TRUE would still block. To guarantee
98  * non-blocking behavior, you should always use
99  * g_pollable_input_stream_read_nonblocking(), which will return a
100  * %G_IO_ERROR_WOULD_BLOCK error rather than blocking.
101  *
102  * Returns: %TRUE if @stream is readable, %FALSE if not. If an error
103  *   has occurred on @stream, this will result in
104  *   g_pollable_input_stream_is_readable() returning %TRUE, and the
105  *   next attempt to read will return the error.
106  *
107  * Since: 2.28
108  */
109 gboolean
110 g_pollable_input_stream_is_readable (GPollableInputStream *stream)
111 {
112   g_return_val_if_fail (G_IS_POLLABLE_INPUT_STREAM (stream), FALSE);
113
114   return G_POLLABLE_INPUT_STREAM_GET_INTERFACE (stream)->is_readable (stream);
115 }
116
117 /**
118  * g_pollable_input_stream_create_source:
119  * @stream: a #GPollableInputStream.
120  * @cancellable: a #GCancellable, or %NULL
121  *
122  * Creates a #GSource that triggers when @stream can be read, or
123  * @cancellable is triggered or an error occurs. The callback on the
124  * source is of the #GPollableSourceFunc type.
125  *
126  * As with g_pollable_input_stream_is_readable(), it is possible that
127  * the stream may not actually be readable even after the source
128  * triggers, so you should use g_pollable_input_stream_read_nonblocking()
129  * rather than g_input_stream_read() from the callback.
130  *
131  * Returns: a new #GSource
132  *
133  * Since: 2.28
134  */
135 GSource *
136 g_pollable_input_stream_create_source (GPollableInputStream *stream,
137                                        GCancellable         *cancellable)
138 {
139   g_return_val_if_fail (G_IS_POLLABLE_INPUT_STREAM (stream), NULL);
140
141   return G_POLLABLE_INPUT_STREAM_GET_INTERFACE (stream)->
142           create_source (stream, cancellable);
143 }
144
145 static gssize
146 g_pollable_input_stream_default_read_nonblocking (GPollableInputStream  *stream,
147                                                   void                  *buffer,
148                                                   gsize                  size,
149                                                   GError               **error)
150 {
151   if (!g_pollable_input_stream_is_readable (stream))
152     {
153       g_set_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK,
154                    g_strerror (EAGAIN));
155       return -1;
156     }
157
158   return g_input_stream_read (G_INPUT_STREAM (stream), buffer, size,
159                               NULL, error);
160 }
161
162 /**
163  * g_pollable_input_stream_read_nonblocking:
164  * @stream: a #GPollableInputStream
165  * @buffer: a buffer to read data into (which should be at least @size
166  *     bytes long).
167  * @size: the number of bytes you want to read
168  * @cancellable: a #GCancellable, or %NULL
169  * @error: #GError for error reporting, or %NULL to ignore.
170  *
171  * Attempts to read up to @size bytes from @stream into @buffer, as
172  * with g_input_stream_read(). If @stream is not currently readable,
173  * this will immediately return %G_IO_ERROR_WOULD_BLOCK, and you can
174  * use g_pollable_input_stream_create_source() to create a #GSource
175  * that will be triggered when @stream is readable.
176  *
177  * Note that since this method never blocks, you cannot actually
178  * use @cancellable to cancel it. However, it will return an error
179  * if @cancellable has already been cancelled when you call, which
180  * may happen if you call this method after a source triggers due
181  * to having been cancelled.
182  *
183  * Return value: the number of bytes read, or -1 on error (including
184  *   %G_IO_ERROR_WOULD_BLOCK).
185  */
186 gssize
187 g_pollable_input_stream_read_nonblocking (GPollableInputStream  *stream,
188                                           void                  *buffer,
189                                           gsize                  size,
190                                           GCancellable          *cancellable,
191                                           GError               **error)
192 {
193   g_return_val_if_fail (G_IS_POLLABLE_INPUT_STREAM (stream), -1);
194
195   if (g_cancellable_set_error_if_cancelled (cancellable, error))
196     return -1;
197
198   return G_POLLABLE_INPUT_STREAM_GET_INTERFACE (stream)->
199     read_nonblocking (stream, buffer, size, error);
200 }
201
202 /* GPollableSource */
203
204 typedef struct {
205   GSource       source;
206
207   GObject      *stream;
208 } GPollableSource;
209
210 static gboolean
211 pollable_source_prepare (GSource *source,
212                          gint    *timeout)
213 {
214   *timeout = -1;
215   return FALSE;
216 }
217
218 static gboolean
219 pollable_source_check (GSource *source)
220 {
221   return FALSE;
222 }
223
224 static gboolean
225 pollable_source_dispatch (GSource     *source,
226                           GSourceFunc  callback,
227                           gpointer     user_data)
228 {
229   GPollableSourceFunc func = (GPollableSourceFunc)callback;
230   GPollableSource *pollable_source = (GPollableSource *)source;
231
232   return (*func) (pollable_source->stream, user_data);
233 }
234
235 static void
236 pollable_source_finalize (GSource *source)
237 {
238   GPollableSource *pollable_source = (GPollableSource *)source;
239
240   g_object_unref (pollable_source->stream);
241 }
242
243 static gboolean
244 pollable_source_closure_callback (GObject  *stream,
245                                   gpointer  data)
246 {
247   GClosure *closure = data;
248
249   GValue param = { 0, };
250   GValue result_value = { 0, };
251   gboolean result;
252
253   g_value_init (&result_value, G_TYPE_BOOLEAN);
254
255   g_value_init (&param, G_TYPE_OBJECT);
256   g_value_set_object (&param, stream);
257
258   g_closure_invoke (closure, &result_value, 1, &param, NULL);
259
260   result = g_value_get_boolean (&result_value);
261   g_value_unset (&result_value);
262   g_value_unset (&param);
263
264   return result;
265 }
266
267 static GSourceFuncs pollable_source_funcs =
268 {
269   pollable_source_prepare,
270   pollable_source_check,
271   pollable_source_dispatch,
272   pollable_source_finalize,
273   (GSourceFunc)pollable_source_closure_callback,
274   (GSourceDummyMarshal)_gio_marshal_BOOLEAN__VOID,
275 };
276
277 /**
278  * g_pollable_source_new:
279  * @pollable_stream: the stream associated with the new source
280  *
281  * Utility method for #GPollableInputStream and #GPollableOutputStream
282  * implementations. Creates a new #GSource that expects a callback of
283  * type #GPollableSourceFunc. The new source does not actually do
284  * anything on its own; use g_source_add_child_source() to add other
285  * sources to it to cause it to trigger.
286  *
287  * Return value: the new #GSource.
288  *
289  * Since: 2.28
290  */
291 GSource *
292 g_pollable_source_new (GObject *pollable_stream)
293 {
294   GSource *source;
295   GPollableSource *pollable_source;
296
297   source = g_source_new (&pollable_source_funcs, sizeof (GPollableSource));
298   g_source_set_name (source, "GPollableSource");
299   pollable_source = (GPollableSource *)source;
300   pollable_source->stream = g_object_ref (pollable_stream);
301
302   return source;
303 }