1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2010 Red Hat, Inc.
5 * SPDX-License-Identifier: LGPL-2.1-or-later
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
25 #include "gpollableinputstream.h"
26 #include "gasynchelper.h"
30 * SECTION:gpollableinputstream
31 * @short_description: Interface for pollable input streams
33 * @see_also: #GInputStream, #GPollableOutputStream, #GFileDescriptorBased
35 * #GPollableInputStream is implemented by #GInputStreams that
36 * can be polled for readiness to read. This can be used when
37 * interfacing with a non-GIO API that expects
38 * UNIX-file-descriptor-style asynchronous I/O rather than GIO-style.
40 * Some classes may implement #GPollableInputStream but have only certain
41 * instances of that class be pollable. If g_pollable_input_stream_can_poll()
42 * returns %FALSE, then the behavior of other #GPollableInputStream methods is
48 G_DEFINE_INTERFACE (GPollableInputStream, g_pollable_input_stream, G_TYPE_INPUT_STREAM)
50 static gboolean g_pollable_input_stream_default_can_poll (GPollableInputStream *stream);
51 static gssize g_pollable_input_stream_default_read_nonblocking (GPollableInputStream *stream,
57 g_pollable_input_stream_default_init (GPollableInputStreamInterface *iface)
59 iface->can_poll = g_pollable_input_stream_default_can_poll;
60 iface->read_nonblocking = g_pollable_input_stream_default_read_nonblocking;
64 g_pollable_input_stream_default_can_poll (GPollableInputStream *stream)
70 * g_pollable_input_stream_can_poll:
71 * @stream: a #GPollableInputStream.
73 * Checks if @stream is actually pollable. Some classes may implement
74 * #GPollableInputStream but have only certain instances of that class
75 * be pollable. If this method returns %FALSE, then the behavior of
76 * other #GPollableInputStream 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_input_stream_can_poll (GPollableInputStream *stream)
88 g_return_val_if_fail (G_IS_POLLABLE_INPUT_STREAM (stream), FALSE);
90 return G_POLLABLE_INPUT_STREAM_GET_INTERFACE (stream)->can_poll (stream);
94 * g_pollable_input_stream_is_readable:
95 * @stream: a #GPollableInputStream.
97 * Checks if @stream can be read.
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_input_stream_read()
101 * after this returns %TRUE would still block. To guarantee
102 * non-blocking behavior, you should always use
103 * g_pollable_input_stream_read_nonblocking(), which will return a
104 * %G_IO_ERROR_WOULD_BLOCK error rather than blocking.
106 * The behaviour of this method is undefined if
107 * g_pollable_input_stream_can_poll() returns %FALSE for @stream.
109 * Returns: %TRUE if @stream is readable, %FALSE if not. If an error
110 * has occurred on @stream, this will result in
111 * g_pollable_input_stream_is_readable() returning %TRUE, and the
112 * next attempt to read will return the error.
117 g_pollable_input_stream_is_readable (GPollableInputStream *stream)
119 g_return_val_if_fail (G_IS_POLLABLE_INPUT_STREAM (stream), FALSE);
121 return G_POLLABLE_INPUT_STREAM_GET_INTERFACE (stream)->is_readable (stream);
125 * g_pollable_input_stream_create_source:
126 * @stream: a #GPollableInputStream.
127 * @cancellable: (nullable): a #GCancellable, or %NULL
129 * Creates a #GSource that triggers when @stream can be read, or
130 * @cancellable is triggered or an error occurs. The callback on the
131 * source is of the #GPollableSourceFunc type.
133 * As with g_pollable_input_stream_is_readable(), it is possible that
134 * the stream may not actually be readable even after the source
135 * triggers, so you should use g_pollable_input_stream_read_nonblocking()
136 * rather than g_input_stream_read() from the callback.
138 * The behaviour of this method is undefined if
139 * g_pollable_input_stream_can_poll() returns %FALSE for @stream.
141 * Returns: (transfer full): a new #GSource
146 g_pollable_input_stream_create_source (GPollableInputStream *stream,
147 GCancellable *cancellable)
149 g_return_val_if_fail (G_IS_POLLABLE_INPUT_STREAM (stream), NULL);
151 return G_POLLABLE_INPUT_STREAM_GET_INTERFACE (stream)->
152 create_source (stream, cancellable);
156 g_pollable_input_stream_default_read_nonblocking (GPollableInputStream *stream,
161 if (!g_pollable_input_stream_is_readable (stream))
163 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK,
164 g_strerror (EAGAIN));
168 return G_INPUT_STREAM_GET_CLASS (stream)->
169 read_fn (G_INPUT_STREAM (stream), buffer, count, NULL, error);
173 * g_pollable_input_stream_read_nonblocking:
174 * @stream: a #GPollableInputStream
175 * @buffer: (array length=count) (element-type guint8) (out caller-allocates): a
176 * buffer to read data into (which should be at least @count bytes long).
177 * @count: (in): the number of bytes you want to read
178 * @cancellable: (nullable): a #GCancellable, or %NULL
179 * @error: #GError for error reporting, or %NULL to ignore.
181 * Attempts to read up to @count bytes from @stream into @buffer, as
182 * with g_input_stream_read(). If @stream is not currently readable,
183 * this will immediately return %G_IO_ERROR_WOULD_BLOCK, and you can
184 * use g_pollable_input_stream_create_source() to create a #GSource
185 * that will be triggered when @stream is readable.
187 * Note that since this method never blocks, you cannot actually
188 * use @cancellable to cancel it. However, it will return an error
189 * if @cancellable has already been cancelled when you call, which
190 * may happen if you call this method after a source triggers due
191 * to having been cancelled.
193 * The behaviour of this method is undefined if
194 * g_pollable_input_stream_can_poll() returns %FALSE for @stream.
196 * Virtual: read_nonblocking
197 * Returns: the number of bytes read, or -1 on error (including
198 * %G_IO_ERROR_WOULD_BLOCK).
201 g_pollable_input_stream_read_nonblocking (GPollableInputStream *stream,
204 GCancellable *cancellable,
209 g_return_val_if_fail (G_IS_POLLABLE_INPUT_STREAM (stream), -1);
210 g_return_val_if_fail (buffer != NULL, 0);
212 if (g_cancellable_set_error_if_cancelled (cancellable, error))
218 if (((gssize) count) < 0)
220 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
221 _("Too large count value passed to %s"), G_STRFUNC);
226 g_cancellable_push_current (cancellable);
228 res = G_POLLABLE_INPUT_STREAM_GET_INTERFACE (stream)->
229 read_nonblocking (stream, buffer, count, error);
232 g_cancellable_pop_current (cancellable);