Blame gio/gpollableinputstream.c

Packit ae235b
/* GIO - GLib Input, Output and Streaming Library
Packit ae235b
 *
Packit ae235b
 * Copyright (C) 2010 Red Hat, Inc.
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General
Packit ae235b
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include <errno.h>
Packit ae235b
Packit ae235b
#include "gpollableinputstream.h"
Packit ae235b
#include "gasynchelper.h"
Packit ae235b
#include "glibintl.h"
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:gpollableinputstream
Packit ae235b
 * @short_description: Interface for pollable input streams
Packit ae235b
 * @include: gio/gio.h
Packit ae235b
 * @see_also: #GInputStream, #GPollableOutputStream, #GFileDescriptorBased
Packit ae235b
 *
Packit ae235b
 * #GPollableInputStream is implemented by #GInputStreams that
Packit ae235b
 * can be polled for readiness to read. This can be used when
Packit ae235b
 * interfacing with a non-GIO API that expects
Packit ae235b
 * UNIX-file-descriptor-style asynchronous I/O rather than GIO-style.
Packit ae235b
 *
Packit ae235b
 * Since: 2.28
Packit ae235b
 */
Packit ae235b
Packit ae235b
G_DEFINE_INTERFACE (GPollableInputStream, g_pollable_input_stream, G_TYPE_INPUT_STREAM)
Packit ae235b
Packit ae235b
static gboolean g_pollable_input_stream_default_can_poll         (GPollableInputStream *stream);
Packit ae235b
static gssize   g_pollable_input_stream_default_read_nonblocking (GPollableInputStream  *stream,
Packit ae235b
								  void                  *buffer,
Packit ae235b
								  gsize                  count,
Packit ae235b
								  GError               **error);
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_pollable_input_stream_default_init (GPollableInputStreamInterface *iface)
Packit ae235b
{
Packit ae235b
  iface->can_poll         = g_pollable_input_stream_default_can_poll;
Packit ae235b
  iface->read_nonblocking = g_pollable_input_stream_default_read_nonblocking;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_pollable_input_stream_default_can_poll (GPollableInputStream *stream)
Packit ae235b
{
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_pollable_input_stream_can_poll:
Packit ae235b
 * @stream: a #GPollableInputStream.
Packit ae235b
 *
Packit ae235b
 * Checks if @stream is actually pollable. Some classes may implement
Packit ae235b
 * #GPollableInputStream but have only certain instances of that class
Packit ae235b
 * be pollable. If this method returns %FALSE, then the behavior of
Packit ae235b
 * other #GPollableInputStream methods is undefined.
Packit ae235b
 *
Packit ae235b
 * For any given stream, the value returned by this method is constant;
Packit ae235b
 * a stream cannot switch from pollable to non-pollable or vice versa.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if @stream is pollable, %FALSE if not.
Packit ae235b
 *
Packit ae235b
 * Since: 2.28
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_pollable_input_stream_can_poll (GPollableInputStream *stream)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_POLLABLE_INPUT_STREAM (stream), FALSE);
Packit ae235b
Packit ae235b
  return G_POLLABLE_INPUT_STREAM_GET_INTERFACE (stream)->can_poll (stream);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_pollable_input_stream_is_readable:
Packit ae235b
 * @stream: a #GPollableInputStream.
Packit ae235b
 *
Packit ae235b
 * Checks if @stream can be read.
Packit ae235b
 *
Packit ae235b
 * Note that some stream types may not be able to implement this 100%
Packit ae235b
 * reliably, and it is possible that a call to g_input_stream_read()
Packit ae235b
 * after this returns %TRUE would still block. To guarantee
Packit ae235b
 * non-blocking behavior, you should always use
Packit ae235b
 * g_pollable_input_stream_read_nonblocking(), which will return a
Packit ae235b
 * %G_IO_ERROR_WOULD_BLOCK error rather than blocking.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if @stream is readable, %FALSE if not. If an error
Packit ae235b
 *   has occurred on @stream, this will result in
Packit ae235b
 *   g_pollable_input_stream_is_readable() returning %TRUE, and the
Packit ae235b
 *   next attempt to read will return the error.
Packit ae235b
 *
Packit ae235b
 * Since: 2.28
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_pollable_input_stream_is_readable (GPollableInputStream *stream)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_POLLABLE_INPUT_STREAM (stream), FALSE);
Packit ae235b
Packit ae235b
  return G_POLLABLE_INPUT_STREAM_GET_INTERFACE (stream)->is_readable (stream);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_pollable_input_stream_create_source:
Packit ae235b
 * @stream: a #GPollableInputStream.
Packit ae235b
 * @cancellable: (nullable): a #GCancellable, or %NULL
Packit ae235b
 *
Packit ae235b
 * Creates a #GSource that triggers when @stream can be read, or
Packit ae235b
 * @cancellable is triggered or an error occurs. The callback on the
Packit ae235b
 * source is of the #GPollableSourceFunc type.
Packit ae235b
 *
Packit ae235b
 * As with g_pollable_input_stream_is_readable(), it is possible that
Packit ae235b
 * the stream may not actually be readable even after the source
Packit ae235b
 * triggers, so you should use g_pollable_input_stream_read_nonblocking()
Packit ae235b
 * rather than g_input_stream_read() from the callback.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer full): a new #GSource
Packit ae235b
 *
Packit ae235b
 * Since: 2.28
Packit ae235b
 */
Packit ae235b
GSource *
Packit ae235b
g_pollable_input_stream_create_source (GPollableInputStream *stream,
Packit ae235b
				       GCancellable         *cancellable)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_POLLABLE_INPUT_STREAM (stream), NULL);
Packit ae235b
Packit ae235b
  return G_POLLABLE_INPUT_STREAM_GET_INTERFACE (stream)->
Packit ae235b
	  create_source (stream, cancellable);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gssize
Packit ae235b
g_pollable_input_stream_default_read_nonblocking (GPollableInputStream  *stream,
Packit ae235b
						  void                  *buffer,
Packit ae235b
						  gsize                  count,
Packit ae235b
						  GError               **error)
Packit ae235b
{
Packit ae235b
  if (!g_pollable_input_stream_is_readable (stream))
Packit ae235b
    {
Packit ae235b
      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK,
Packit ae235b
                           g_strerror (EAGAIN));
Packit ae235b
      return -1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return G_INPUT_STREAM_GET_CLASS (stream)->
Packit ae235b
    read_fn (G_INPUT_STREAM (stream), buffer, count, NULL, error);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_pollable_input_stream_read_nonblocking:
Packit ae235b
 * @stream: a #GPollableInputStream
Packit ae235b
 * @buffer: (array length=count) (element-type guint8): a buffer to
Packit ae235b
 *     read data into (which should be at least @count bytes long).
Packit ae235b
 * @count: the number of bytes you want to read
Packit ae235b
 * @cancellable: (nullable): a #GCancellable, or %NULL
Packit ae235b
 * @error: #GError for error reporting, or %NULL to ignore.
Packit ae235b
 *
Packit ae235b
 * Attempts to read up to @count bytes from @stream into @buffer, as
Packit ae235b
 * with g_input_stream_read(). If @stream is not currently readable,
Packit ae235b
 * this will immediately return %G_IO_ERROR_WOULD_BLOCK, and you can
Packit ae235b
 * use g_pollable_input_stream_create_source() to create a #GSource
Packit ae235b
 * that will be triggered when @stream is readable.
Packit ae235b
 *
Packit ae235b
 * Note that since this method never blocks, you cannot actually
Packit ae235b
 * use @cancellable to cancel it. However, it will return an error
Packit ae235b
 * if @cancellable has already been cancelled when you call, which
Packit ae235b
 * may happen if you call this method after a source triggers due
Packit ae235b
 * to having been cancelled.
Packit ae235b
 *
Packit ae235b
 * Virtual: read_nonblocking
Packit ae235b
 * Returns: the number of bytes read, or -1 on error (including
Packit ae235b
 *   %G_IO_ERROR_WOULD_BLOCK).
Packit ae235b
 */
Packit ae235b
gssize
Packit ae235b
g_pollable_input_stream_read_nonblocking (GPollableInputStream  *stream,
Packit ae235b
					  void                  *buffer,
Packit ae235b
					  gsize                  count,
Packit ae235b
					  GCancellable          *cancellable,
Packit ae235b
					  GError               **error)
Packit ae235b
{
Packit ae235b
  gssize res;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_POLLABLE_INPUT_STREAM (stream), -1);
Packit ae235b
  g_return_val_if_fail (buffer != NULL, 0);
Packit ae235b
Packit ae235b
  if (g_cancellable_set_error_if_cancelled (cancellable, error))
Packit ae235b
    return -1;
Packit ae235b
Packit ae235b
  if (count == 0)
Packit ae235b
    return 0;
Packit ae235b
Packit ae235b
  if (((gssize) count) < 0)
Packit ae235b
    {
Packit ae235b
      g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
Packit ae235b
		   _("Too large count value passed to %s"), G_STRFUNC);
Packit ae235b
      return -1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (cancellable)
Packit ae235b
    g_cancellable_push_current (cancellable);
Packit ae235b
Packit ae235b
  res = G_POLLABLE_INPUT_STREAM_GET_INTERFACE (stream)->
Packit ae235b
    read_nonblocking (stream, buffer, count, error);
Packit ae235b
Packit ae235b
  if (cancellable)
Packit ae235b
    g_cancellable_pop_current (cancellable);
Packit ae235b
Packit ae235b
  return res;
Packit ae235b
}