Blame gio/gpollableoutputstream.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 "gpollableoutputstream.h"
Packit ae235b
#include "gasynchelper.h"
Packit ae235b
#include "gfiledescriptorbased.h"
Packit ae235b
#include "glibintl.h"
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:gpollableoutputstream
Packit ae235b
 * @short_description: Interface for pollable output streams
Packit ae235b
 * @include: gio/gio.h
Packit ae235b
 * @see_also: #GOutputStream, #GFileDescriptorBased, #GPollableInputStream
Packit ae235b
 *
Packit ae235b
 * #GPollableOutputStream is implemented by #GOutputStreams that
Packit ae235b
 * can be polled for readiness to write. 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 (GPollableOutputStream, g_pollable_output_stream, G_TYPE_OUTPUT_STREAM)
Packit ae235b
Packit ae235b
static gboolean g_pollable_output_stream_default_can_poll          (GPollableOutputStream *stream);
Packit ae235b
static gssize   g_pollable_output_stream_default_write_nonblocking (GPollableOutputStream  *stream,
Packit ae235b
								    const void             *buffer,
Packit ae235b
								    gsize                   count,
Packit ae235b
								    GError                **error);
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_pollable_output_stream_default_init (GPollableOutputStreamInterface *iface)
Packit ae235b
{
Packit ae235b
  iface->can_poll          = g_pollable_output_stream_default_can_poll;
Packit ae235b
  iface->write_nonblocking = g_pollable_output_stream_default_write_nonblocking;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_pollable_output_stream_default_can_poll (GPollableOutputStream *stream)
Packit ae235b
{
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_pollable_output_stream_can_poll:
Packit ae235b
 * @stream: a #GPollableOutputStream.
Packit ae235b
 *
Packit ae235b
 * Checks if @stream is actually pollable. Some classes may implement
Packit ae235b
 * #GPollableOutputStream but have only certain instances of that
Packit ae235b
 * class be pollable. If this method returns %FALSE, then the behavior
Packit ae235b
 * of other #GPollableOutputStream 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_output_stream_can_poll (GPollableOutputStream *stream)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_POLLABLE_OUTPUT_STREAM (stream), FALSE);
Packit ae235b
Packit ae235b
  return G_POLLABLE_OUTPUT_STREAM_GET_INTERFACE (stream)->can_poll (stream);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_pollable_output_stream_is_writable:
Packit ae235b
 * @stream: a #GPollableOutputStream.
Packit ae235b
 *
Packit ae235b
 * Checks if @stream can be written.
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_output_stream_write()
Packit ae235b
 * after this returns %TRUE would still block. To guarantee
Packit ae235b
 * non-blocking behavior, you should always use
Packit ae235b
 * g_pollable_output_stream_write_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 writable, %FALSE if not. If an error
Packit ae235b
 *   has occurred on @stream, this will result in
Packit ae235b
 *   g_pollable_output_stream_is_writable() returning %TRUE, and the
Packit ae235b
 *   next attempt to write will return the error.
Packit ae235b
 *
Packit ae235b
 * Since: 2.28
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_pollable_output_stream_is_writable (GPollableOutputStream *stream)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_POLLABLE_OUTPUT_STREAM (stream), FALSE);
Packit ae235b
Packit ae235b
  return G_POLLABLE_OUTPUT_STREAM_GET_INTERFACE (stream)->is_writable (stream);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_pollable_output_stream_create_source:
Packit ae235b
 * @stream: a #GPollableOutputStream.
Packit ae235b
 * @cancellable: (nullable): a #GCancellable, or %NULL
Packit ae235b
 *
Packit ae235b
 * Creates a #GSource that triggers when @stream can be written, 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_output_stream_is_writable(), it is possible that
Packit ae235b
 * the stream may not actually be writable even after the source
Packit ae235b
 * triggers, so you should use g_pollable_output_stream_write_nonblocking()
Packit ae235b
 * rather than g_output_stream_write() 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_output_stream_create_source (GPollableOutputStream *stream,
Packit ae235b
					GCancellable          *cancellable)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_POLLABLE_OUTPUT_STREAM (stream), NULL);
Packit ae235b
Packit ae235b
  return G_POLLABLE_OUTPUT_STREAM_GET_INTERFACE (stream)->
Packit ae235b
	  create_source (stream, cancellable);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gssize
Packit ae235b
g_pollable_output_stream_default_write_nonblocking (GPollableOutputStream  *stream,
Packit ae235b
						    const void             *buffer,
Packit ae235b
						    gsize                   count,
Packit ae235b
						    GError                **error)
Packit ae235b
{
Packit ae235b
  if (!g_pollable_output_stream_is_writable (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_OUTPUT_STREAM_GET_CLASS (stream)->
Packit ae235b
    write_fn (G_OUTPUT_STREAM (stream), buffer, count, NULL, error);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_pollable_output_stream_write_nonblocking:
Packit ae235b
 * @stream: a #GPollableOutputStream
Packit ae235b
 * @buffer: (array length=count) (element-type guint8): a buffer to write
Packit ae235b
 *     data from
Packit ae235b
 * @count: the number of bytes you want to write
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 write up to @count bytes from @buffer to @stream, as
Packit ae235b
 * with g_output_stream_write(). If @stream is not currently writable,
Packit ae235b
 * this will immediately return %G_IO_ERROR_WOULD_BLOCK, and you can
Packit ae235b
 * use g_pollable_output_stream_create_source() to create a #GSource
Packit ae235b
 * that will be triggered when @stream is writable.
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
 * Also note that if %G_IO_ERROR_WOULD_BLOCK is returned some underlying
Packit ae235b
 * transports like D/TLS require that you send the same @buffer and @count.
Packit ae235b
 *
Packit ae235b
 * Virtual: write_nonblocking
Packit ae235b
 * Returns: the number of bytes written, or -1 on error (including
Packit ae235b
 *   %G_IO_ERROR_WOULD_BLOCK).
Packit ae235b
 */
Packit ae235b
gssize
Packit ae235b
g_pollable_output_stream_write_nonblocking (GPollableOutputStream  *stream,
Packit ae235b
					    const 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_OUTPUT_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_OUTPUT_STREAM_GET_INTERFACE (stream)->
Packit ae235b
    write_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
}