Add pollable input/output streams
[platform/upstream/glib.git] / gio / gpollableoutputstream.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 "gpollableoutputstream.h"
26 #include "gasynchelper.h"
27 #include "gfiledescriptorbased.h"
28 #include "gio-marshal.h"
29 #include "glibintl.h"
30
31 /**
32  * SECTION:gpollableoutputstream
33  * @short_description: Interface for pollable output streams
34  * @include: gio/gio.h
35  * @see_also: #GOutputStream, #GFileDescriptorBased, #GPollableInputStream
36  *
37  * #GPollableOutputStream is implemented by #GOutputStream<!-- -->s that
38  * can be polled for readiness to write. This can be used when
39  * interfacing with a non-gio API that expects
40  * unix-file-descriptor-style asynchronous I/O rather than gio-style.
41  *
42  * Since: 2.28
43  */
44
45 G_DEFINE_INTERFACE (GPollableOutputStream, g_pollable_output_stream, G_TYPE_OUTPUT_STREAM)
46
47 static gboolean g_pollable_output_stream_default_can_poll          (GPollableOutputStream *stream);
48 static gssize   g_pollable_output_stream_default_write_nonblocking (GPollableOutputStream  *stream,
49                                                                     const void             *buffer,
50                                                                     gsize                   size,
51                                                                     GError                **error);
52
53 static void
54 g_pollable_output_stream_default_init (GPollableOutputStreamInterface *iface)
55 {
56   iface->can_poll          = g_pollable_output_stream_default_can_poll;
57   iface->write_nonblocking = g_pollable_output_stream_default_write_nonblocking;
58 }
59
60 static gboolean
61 g_pollable_output_stream_default_can_poll (GPollableOutputStream *stream)
62 {
63   return TRUE;
64 }
65
66 /**
67  * g_pollable_output_stream_can_poll:
68  * @stream: a #GPollableOutputStream.
69  *
70  * Checks if @stream is actually pollable. Some classes may implement
71  * #GPollableOutputStream but have only certain instances of that
72  * class be pollable. If this method returns %FALSE, then the behavior
73  * of other #GPollableOutputStream methods is undefined.
74  *
75  * For any given stream, the value returned by this method is constant;
76  * a stream cannot switch from pollable to non-pollable or vice versa.
77  *
78  * Returns: %TRUE if @stream is pollable, %FALSE if not.
79  *
80  * Since: 2.28
81  */
82 gboolean
83 g_pollable_output_stream_can_poll (GPollableOutputStream *stream)
84 {
85   g_return_val_if_fail (G_IS_POLLABLE_OUTPUT_STREAM (stream), FALSE);
86
87   return G_POLLABLE_OUTPUT_STREAM_GET_INTERFACE (stream)->can_poll (stream);
88 }
89
90 /**
91  * g_pollable_output_stream_is_writable:
92  * @stream: a #GPollableOutputStream.
93  *
94  * Checks if @stream can be written.
95  *
96  * Note that some stream types may not be able to implement this 100%
97  * reliably, and it is possible that a call to g_output_stream_write()
98  * after this returns %TRUE would still block. To guarantee
99  * non-blocking behavior, you should always use
100  * g_pollable_output_stream_write_nonblocking(), which will return a
101  * %G_IO_ERROR_WOULD_BLOCK error rather than blocking.
102  *
103  * Returns: %TRUE if @stream is writable, %FALSE if not. If an error
104  *   has occurred on @stream, this will result in
105  *   g_pollable_output_stream_is_writable() returning %TRUE, and the
106  *   next attempt to write will return the error.
107  *
108  * Since: 2.28
109  */
110 gboolean
111 g_pollable_output_stream_is_writable (GPollableOutputStream *stream)
112 {
113   g_return_val_if_fail (G_IS_POLLABLE_OUTPUT_STREAM (stream), FALSE);
114
115   return G_POLLABLE_OUTPUT_STREAM_GET_INTERFACE (stream)->is_writable (stream);
116 }
117
118 /**
119  * g_pollable_output_stream_create_source:
120  * @stream: a #GPollableOutputStream.
121  * @cancellable: a #GCancellable, or %NULL
122  *
123  * Creates a #GSource that triggers when @stream can be written, or
124  * @cancellable is triggered or an error occurs. The callback on the
125  * source is of the #GPollableSourceFunc type.
126  *
127  * As with g_pollable_output_stream_is_writable(), it is possible that
128  * the stream may not actually be writable even after the source
129  * triggers, so you should use
130  * g_pollable_output_stream_write_nonblocking() rather than
131  * g_output_stream_write() from the callback.
132  *
133  * Returns: a new #GSource
134  *
135  * Since: 2.28
136  */
137 GSource *
138 g_pollable_output_stream_create_source (GPollableOutputStream *stream,
139                                         GCancellable          *cancellable)
140 {
141   g_return_val_if_fail (G_IS_POLLABLE_OUTPUT_STREAM (stream), NULL);
142
143   return G_POLLABLE_OUTPUT_STREAM_GET_INTERFACE (stream)->
144           create_source (stream, cancellable);
145 }
146
147 static gssize
148 g_pollable_output_stream_default_write_nonblocking (GPollableOutputStream  *stream,
149                                                     const void             *buffer,
150                                                     gsize                   size,
151                                                     GError                **error)
152 {
153   if (!g_pollable_output_stream_is_writable (stream))
154     {
155       g_set_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK,
156                    g_strerror (EAGAIN));
157       return -1;
158     }
159
160   return g_output_stream_write (G_OUTPUT_STREAM (stream), buffer, size,
161                                 NULL, error);
162 }
163
164 /**
165  * g_pollable_output_stream_write_nonblocking:
166  * @stream: a #GPollableOutputStream
167  * @buffer: a buffer to write data from
168  * @size: the number of bytes you want to write
169  * @cancellable: a #GCancellable, or %NULL
170  * @error: #GError for error reporting, or %NULL to ignore.
171  *
172  * Attempts to write up to @size bytes from @buffer to @stream, as
173  * with g_output_stream_write(). If @stream is not currently writable,
174  * this will immediately return %G_IO_ERROR_WOULD_BLOCK, and you can
175  * use g_pollable_output_stream_create_source() to create a #GSource
176  * that will be triggered when @stream is writable.
177  *
178  * Note that since this method never blocks, you cannot actually
179  * use @cancellable to cancel it. However, it will return an error
180  * if @cancellable has already been cancelled when you call, which
181  * may happen if you call this method after a source triggers due
182  * to having been cancelled.
183  *
184  * Return value: the number of bytes written, or -1 on error (including
185  *   %G_IO_ERROR_WOULD_BLOCK).
186  */
187 gssize
188 g_pollable_output_stream_write_nonblocking (GPollableOutputStream  *stream,
189                                             const void             *buffer,
190                                             gsize                   size,
191                                             GCancellable           *cancellable,
192                                             GError                **error)
193 {
194   g_return_val_if_fail (G_IS_POLLABLE_OUTPUT_STREAM (stream), -1);
195
196   if (g_cancellable_set_error_if_cancelled (cancellable, error))
197     return -1;
198
199   return G_POLLABLE_OUTPUT_STREAM_GET_INTERFACE (stream)->
200     write_nonblocking (stream, buffer, size, error);
201 }