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 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, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
25 #include "gpollableoutputstream.h"
26 #include "gasynchelper.h"
27 #include "gfiledescriptorbased.h"
28 #include "gio-marshal.h"
32 * SECTION:gpollableoutputstream
33 * @short_description: Interface for pollable output streams
35 * @see_also: #GOutputStream, #GFileDescriptorBased, #GPollableInputStream
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.
45 G_DEFINE_INTERFACE (GPollableOutputStream, g_pollable_output_stream, G_TYPE_OUTPUT_STREAM)
47 static gboolean g_pollable_output_stream_default_can_poll (GPollableOutputStream *stream);
48 static gssize g_pollable_output_stream_default_write_nonblocking (GPollableOutputStream *stream,
54 g_pollable_output_stream_default_init (GPollableOutputStreamInterface *iface)
56 iface->can_poll = g_pollable_output_stream_default_can_poll;
57 iface->write_nonblocking = g_pollable_output_stream_default_write_nonblocking;
61 g_pollable_output_stream_default_can_poll (GPollableOutputStream *stream)
67 * g_pollable_output_stream_can_poll:
68 * @stream: a #GPollableOutputStream.
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.
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.
78 * Returns: %TRUE if @stream is pollable, %FALSE if not.
83 g_pollable_output_stream_can_poll (GPollableOutputStream *stream)
85 g_return_val_if_fail (G_IS_POLLABLE_OUTPUT_STREAM (stream), FALSE);
87 return G_POLLABLE_OUTPUT_STREAM_GET_INTERFACE (stream)->can_poll (stream);
91 * g_pollable_output_stream_is_writable:
92 * @stream: a #GPollableOutputStream.
94 * Checks if @stream can be written.
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.
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.
111 g_pollable_output_stream_is_writable (GPollableOutputStream *stream)
113 g_return_val_if_fail (G_IS_POLLABLE_OUTPUT_STREAM (stream), FALSE);
115 return G_POLLABLE_OUTPUT_STREAM_GET_INTERFACE (stream)->is_writable (stream);
119 * g_pollable_output_stream_create_source:
120 * @stream: a #GPollableOutputStream.
121 * @cancellable: a #GCancellable, or %NULL
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.
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.
133 * Returns: a new #GSource
138 g_pollable_output_stream_create_source (GPollableOutputStream *stream,
139 GCancellable *cancellable)
141 g_return_val_if_fail (G_IS_POLLABLE_OUTPUT_STREAM (stream), NULL);
143 return G_POLLABLE_OUTPUT_STREAM_GET_INTERFACE (stream)->
144 create_source (stream, cancellable);
148 g_pollable_output_stream_default_write_nonblocking (GPollableOutputStream *stream,
153 if (!g_pollable_output_stream_is_writable (stream))
155 g_set_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK,
156 g_strerror (EAGAIN));
160 return g_output_stream_write (G_OUTPUT_STREAM (stream), buffer, size,
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.
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.
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.
184 * Return value: the number of bytes written, or -1 on error (including
185 * %G_IO_ERROR_WOULD_BLOCK).
188 g_pollable_output_stream_write_nonblocking (GPollableOutputStream *stream,
191 GCancellable *cancellable,
194 g_return_val_if_fail (G_IS_POLLABLE_OUTPUT_STREAM (stream), -1);
196 if (g_cancellable_set_error_if_cancelled (cancellable, error))
199 return G_POLLABLE_OUTPUT_STREAM_GET_INTERFACE (stream)->
200 write_nonblocking (stream, buffer, size, error);