1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2010 Red Hat, Inc.
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.1 of the License, or (at your option) any later version.
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.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
23 #include "gpollableoutputstream.h"
24 #include "gasynchelper.h"
25 #include "gfiledescriptorbased.h"
29 * SECTION:gpollableoutputstream
30 * @short_description: Interface for pollable output streams
32 * @see_also: #GOutputStream, #GFileDescriptorBased, #GPollableInputStream
34 * #GPollableOutputStream is implemented by #GOutputStreams that
35 * can be polled for readiness to write. This can be used when
36 * interfacing with a non-GIO API that expects
37 * UNIX-file-descriptor-style asynchronous I/O rather than GIO-style.
42 G_DEFINE_INTERFACE (GPollableOutputStream, g_pollable_output_stream, G_TYPE_OUTPUT_STREAM)
44 static gboolean g_pollable_output_stream_default_can_poll (GPollableOutputStream *stream);
45 static gssize g_pollable_output_stream_default_write_nonblocking (GPollableOutputStream *stream,
49 static GPollableReturn g_pollable_output_stream_default_writev_nonblocking (GPollableOutputStream *stream,
50 const GOutputVector *vectors,
56 g_pollable_output_stream_default_init (GPollableOutputStreamInterface *iface)
58 iface->can_poll = g_pollable_output_stream_default_can_poll;
59 iface->write_nonblocking = g_pollable_output_stream_default_write_nonblocking;
60 iface->writev_nonblocking = g_pollable_output_stream_default_writev_nonblocking;
64 g_pollable_output_stream_default_can_poll (GPollableOutputStream *stream)
70 * g_pollable_output_stream_can_poll:
71 * @stream: a #GPollableOutputStream.
73 * Checks if @stream is actually pollable. Some classes may implement
74 * #GPollableOutputStream but have only certain instances of that
75 * class be pollable. If this method returns %FALSE, then the behavior
76 * of other #GPollableOutputStream methods is undefined.
78 * For any given stream, the value returned by this method is constant;
79 * a stream cannot switch from pollable to non-pollable or vice versa.
81 * Returns: %TRUE if @stream is pollable, %FALSE if not.
86 g_pollable_output_stream_can_poll (GPollableOutputStream *stream)
88 g_return_val_if_fail (G_IS_POLLABLE_OUTPUT_STREAM (stream), FALSE);
90 return G_POLLABLE_OUTPUT_STREAM_GET_INTERFACE (stream)->can_poll (stream);
94 * g_pollable_output_stream_is_writable:
95 * @stream: a #GPollableOutputStream.
97 * Checks if @stream can be written.
99 * Note that some stream types may not be able to implement this 100%
100 * reliably, and it is possible that a call to g_output_stream_write()
101 * after this returns %TRUE would still block. To guarantee
102 * non-blocking behavior, you should always use
103 * g_pollable_output_stream_write_nonblocking(), which will return a
104 * %G_IO_ERROR_WOULD_BLOCK error rather than blocking.
106 * Returns: %TRUE if @stream is writable, %FALSE if not. If an error
107 * has occurred on @stream, this will result in
108 * g_pollable_output_stream_is_writable() returning %TRUE, and the
109 * next attempt to write will return the error.
114 g_pollable_output_stream_is_writable (GPollableOutputStream *stream)
116 g_return_val_if_fail (G_IS_POLLABLE_OUTPUT_STREAM (stream), FALSE);
118 return G_POLLABLE_OUTPUT_STREAM_GET_INTERFACE (stream)->is_writable (stream);
122 * g_pollable_output_stream_create_source:
123 * @stream: a #GPollableOutputStream.
124 * @cancellable: (nullable): a #GCancellable, or %NULL
126 * Creates a #GSource that triggers when @stream can be written, or
127 * @cancellable is triggered or an error occurs. The callback on the
128 * source is of the #GPollableSourceFunc type.
130 * As with g_pollable_output_stream_is_writable(), it is possible that
131 * the stream may not actually be writable even after the source
132 * triggers, so you should use g_pollable_output_stream_write_nonblocking()
133 * rather than g_output_stream_write() from the callback.
135 * Returns: (transfer full): a new #GSource
140 g_pollable_output_stream_create_source (GPollableOutputStream *stream,
141 GCancellable *cancellable)
143 g_return_val_if_fail (G_IS_POLLABLE_OUTPUT_STREAM (stream), NULL);
145 return G_POLLABLE_OUTPUT_STREAM_GET_INTERFACE (stream)->
146 create_source (stream, cancellable);
150 g_pollable_output_stream_default_write_nonblocking (GPollableOutputStream *stream,
155 if (!g_pollable_output_stream_is_writable (stream))
157 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK,
158 g_strerror (EAGAIN));
162 return G_OUTPUT_STREAM_GET_CLASS (stream)->
163 write_fn (G_OUTPUT_STREAM (stream), buffer, count, NULL, error);
166 static GPollableReturn
167 g_pollable_output_stream_default_writev_nonblocking (GPollableOutputStream *stream,
168 const GOutputVector *vectors,
170 gsize *bytes_written,
173 gsize _bytes_written = 0;
174 GPollableOutputStreamInterface *iface = G_POLLABLE_OUTPUT_STREAM_GET_INTERFACE (stream);
178 for (i = 0; i < n_vectors; i++)
182 /* Would we overflow here? In that case simply return and let the caller
183 * handle this like a short write */
184 if (_bytes_written > G_MAXSIZE - vectors[i].size)
187 res = iface->write_nonblocking (stream, vectors[i].buffer, vectors[i].size, &err);
191 *bytes_written = _bytes_written;
193 /* If something was written already we handle this like a short
194 * write and assume that the next call would either give the same
195 * error again or successfully finish writing without errors or data
198 if (_bytes_written > 0)
200 g_clear_error (&err);
201 return G_POLLABLE_RETURN_OK;
203 else if (g_error_matches (err, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK))
205 g_clear_error (&err);
206 return G_POLLABLE_RETURN_WOULD_BLOCK;
210 g_propagate_error (error, err);
211 return G_POLLABLE_RETURN_FAILED;
215 _bytes_written += res;
216 /* if we had a short write break the loop here */
217 if (res < vectors[i].size)
222 *bytes_written = _bytes_written;
224 return G_POLLABLE_RETURN_OK;
228 * g_pollable_output_stream_write_nonblocking:
229 * @stream: a #GPollableOutputStream
230 * @buffer: (array length=count) (element-type guint8): a buffer to write
232 * @count: the number of bytes you want to write
233 * @cancellable: (nullable): a #GCancellable, or %NULL
234 * @error: #GError for error reporting, or %NULL to ignore.
236 * Attempts to write up to @count bytes from @buffer to @stream, as
237 * with g_output_stream_write(). If @stream is not currently writable,
238 * this will immediately return %G_IO_ERROR_WOULD_BLOCK, and you can
239 * use g_pollable_output_stream_create_source() to create a #GSource
240 * that will be triggered when @stream is writable.
242 * Note that since this method never blocks, you cannot actually
243 * use @cancellable to cancel it. However, it will return an error
244 * if @cancellable has already been cancelled when you call, which
245 * may happen if you call this method after a source triggers due
246 * to having been cancelled.
248 * Also note that if %G_IO_ERROR_WOULD_BLOCK is returned some underlying
249 * transports like D/TLS require that you re-send the same @buffer and
250 * @count in the next write call.
252 * Virtual: write_nonblocking
253 * Returns: the number of bytes written, or -1 on error (including
254 * %G_IO_ERROR_WOULD_BLOCK).
257 g_pollable_output_stream_write_nonblocking (GPollableOutputStream *stream,
260 GCancellable *cancellable,
265 g_return_val_if_fail (G_IS_POLLABLE_OUTPUT_STREAM (stream), -1);
266 g_return_val_if_fail (buffer != NULL, 0);
268 if (g_cancellable_set_error_if_cancelled (cancellable, error))
274 if (((gssize) count) < 0)
276 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
277 _("Too large count value passed to %s"), G_STRFUNC);
282 g_cancellable_push_current (cancellable);
284 res = G_POLLABLE_OUTPUT_STREAM_GET_INTERFACE (stream)->
285 write_nonblocking (stream, buffer, count, error);
288 g_cancellable_pop_current (cancellable);
294 * g_pollable_output_stream_writev_nonblocking:
295 * @stream: a #GPollableOutputStream
296 * @vectors: (array length=n_vectors): the buffer containing the #GOutputVectors to write.
297 * @n_vectors: the number of vectors to write
298 * @bytes_written: (out) (optional): location to store the number of bytes that were
299 * written to the stream
300 * @cancellable: (nullable): a #GCancellable, or %NULL
301 * @error: #GError for error reporting, or %NULL to ignore.
303 * Attempts to write the bytes contained in the @n_vectors @vectors to @stream,
304 * as with g_output_stream_writev(). If @stream is not currently writable,
305 * this will immediately return %@G_POLLABLE_RETURN_WOULD_BLOCK, and you can
306 * use g_pollable_output_stream_create_source() to create a #GSource
307 * that will be triggered when @stream is writable. @error will *not* be
310 * Note that since this method never blocks, you cannot actually
311 * use @cancellable to cancel it. However, it will return an error
312 * if @cancellable has already been cancelled when you call, which
313 * may happen if you call this method after a source triggers due
314 * to having been cancelled.
316 * Also note that if %G_POLLABLE_RETURN_WOULD_BLOCK is returned some underlying
317 * transports like D/TLS require that you re-send the same @vectors and
318 * @n_vectors in the next write call.
320 * Virtual: writev_nonblocking
322 * Returns: %@G_POLLABLE_RETURN_OK on success, %G_POLLABLE_RETURN_WOULD_BLOCK
323 * if the stream is not currently writable (and @error is *not* set), or
324 * %G_POLLABLE_RETURN_FAILED if there was an error in which case @error will
330 g_pollable_output_stream_writev_nonblocking (GPollableOutputStream *stream,
331 const GOutputVector *vectors,
333 gsize *bytes_written,
334 GCancellable *cancellable,
337 GPollableOutputStreamInterface *iface;
339 gsize _bytes_written = 0;
344 g_return_val_if_fail (G_IS_POLLABLE_OUTPUT_STREAM (stream), G_POLLABLE_RETURN_FAILED);
345 g_return_val_if_fail (vectors != NULL || n_vectors == 0, G_POLLABLE_RETURN_FAILED);
346 g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), G_POLLABLE_RETURN_FAILED);
347 g_return_val_if_fail (error == NULL || *error == NULL, G_POLLABLE_RETURN_FAILED);
349 if (g_cancellable_set_error_if_cancelled (cancellable, error))
350 return G_POLLABLE_RETURN_FAILED;
353 return G_POLLABLE_RETURN_OK;
355 iface = G_POLLABLE_OUTPUT_STREAM_GET_INTERFACE (stream);
356 g_return_val_if_fail (iface->writev_nonblocking != NULL, G_POLLABLE_RETURN_FAILED);
359 g_cancellable_push_current (cancellable);
362 writev_nonblocking (stream, vectors, n_vectors, &_bytes_written, error);
365 g_cancellable_pop_current (cancellable);
367 if (res == G_POLLABLE_RETURN_FAILED)
368 g_warn_if_fail (error == NULL || (*error != NULL && !g_error_matches (*error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK)));
369 else if (res == G_POLLABLE_RETURN_WOULD_BLOCK)
370 g_warn_if_fail (error == NULL || *error == NULL);
372 /* in case of not-OK nothing must've been written */
373 g_warn_if_fail (res == G_POLLABLE_RETURN_OK || _bytes_written == 0);
376 *bytes_written = _bytes_written;