gio/tests/gsubprocess.c: Fix on Windows
[glib.git] / gio / gpollableoutputstream.c
blobb019e8139416646ee6b9747848512f95ab8ae191
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.
21 #include "config.h"
23 #include <errno.h>
25 #include "gpollableoutputstream.h"
26 #include "gasynchelper.h"
27 #include "gfiledescriptorbased.h"
28 #include "glibintl.h"
30 /**
31 * SECTION:gpollableoutputstream
32 * @short_description: Interface for pollable output streams
33 * @include: gio/gio.h
34 * @see_also: #GOutputStream, #GFileDescriptorBased, #GPollableInputStream
36 * #GPollableOutputStream is implemented by #GOutputStreams that
37 * can be polled for readiness to write. This can be used when
38 * interfacing with a non-GIO API that expects
39 * UNIX-file-descriptor-style asynchronous I/O rather than GIO-style.
41 * Since: 2.28
44 G_DEFINE_INTERFACE (GPollableOutputStream, g_pollable_output_stream, G_TYPE_OUTPUT_STREAM)
46 static gboolean g_pollable_output_stream_default_can_poll (GPollableOutputStream *stream);
47 static gssize g_pollable_output_stream_default_write_nonblocking (GPollableOutputStream *stream,
48 const void *buffer,
49 gsize count,
50 GError **error);
52 static void
53 g_pollable_output_stream_default_init (GPollableOutputStreamInterface *iface)
55 iface->can_poll = g_pollable_output_stream_default_can_poll;
56 iface->write_nonblocking = g_pollable_output_stream_default_write_nonblocking;
59 static gboolean
60 g_pollable_output_stream_default_can_poll (GPollableOutputStream *stream)
62 return TRUE;
65 /**
66 * g_pollable_output_stream_can_poll:
67 * @stream: a #GPollableOutputStream.
69 * Checks if @stream is actually pollable. Some classes may implement
70 * #GPollableOutputStream but have only certain instances of that
71 * class be pollable. If this method returns %FALSE, then the behavior
72 * of other #GPollableOutputStream methods is undefined.
74 * For any given stream, the value returned by this method is constant;
75 * a stream cannot switch from pollable to non-pollable or vice versa.
77 * Returns: %TRUE if @stream is pollable, %FALSE if not.
79 * Since: 2.28
81 gboolean
82 g_pollable_output_stream_can_poll (GPollableOutputStream *stream)
84 g_return_val_if_fail (G_IS_POLLABLE_OUTPUT_STREAM (stream), FALSE);
86 return G_POLLABLE_OUTPUT_STREAM_GET_INTERFACE (stream)->can_poll (stream);
89 /**
90 * g_pollable_output_stream_is_writable:
91 * @stream: a #GPollableOutputStream.
93 * Checks if @stream can be written.
95 * Note that some stream types may not be able to implement this 100%
96 * reliably, and it is possible that a call to g_output_stream_write()
97 * after this returns %TRUE would still block. To guarantee
98 * non-blocking behavior, you should always use
99 * g_pollable_output_stream_write_nonblocking(), which will return a
100 * %G_IO_ERROR_WOULD_BLOCK error rather than blocking.
102 * Returns: %TRUE if @stream is writable, %FALSE if not. If an error
103 * has occurred on @stream, this will result in
104 * g_pollable_output_stream_is_writable() returning %TRUE, and the
105 * next attempt to write will return the error.
107 * Since: 2.28
109 gboolean
110 g_pollable_output_stream_is_writable (GPollableOutputStream *stream)
112 g_return_val_if_fail (G_IS_POLLABLE_OUTPUT_STREAM (stream), FALSE);
114 return G_POLLABLE_OUTPUT_STREAM_GET_INTERFACE (stream)->is_writable (stream);
118 * g_pollable_output_stream_create_source:
119 * @stream: a #GPollableOutputStream.
120 * @cancellable: (allow-none): a #GCancellable, or %NULL
122 * Creates a #GSource that triggers when @stream can be written, or
123 * @cancellable is triggered or an error occurs. The callback on the
124 * source is of the #GPollableSourceFunc type.
126 * As with g_pollable_output_stream_is_writable(), it is possible that
127 * the stream may not actually be writable even after the source
128 * triggers, so you should use g_pollable_output_stream_write_nonblocking()
129 * rather than g_output_stream_write() from the callback.
131 * Returns: (transfer full): a new #GSource
133 * Since: 2.28
135 GSource *
136 g_pollable_output_stream_create_source (GPollableOutputStream *stream,
137 GCancellable *cancellable)
139 g_return_val_if_fail (G_IS_POLLABLE_OUTPUT_STREAM (stream), NULL);
141 return G_POLLABLE_OUTPUT_STREAM_GET_INTERFACE (stream)->
142 create_source (stream, cancellable);
145 static gssize
146 g_pollable_output_stream_default_write_nonblocking (GPollableOutputStream *stream,
147 const void *buffer,
148 gsize count,
149 GError **error)
151 if (!g_pollable_output_stream_is_writable (stream))
153 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK,
154 g_strerror (EAGAIN));
155 return -1;
158 return G_OUTPUT_STREAM_GET_CLASS (stream)->
159 write_fn (G_OUTPUT_STREAM (stream), buffer, count, NULL, error);
163 * g_pollable_output_stream_write_nonblocking:
164 * @stream: a #GPollableOutputStream
165 * @buffer: (array length=count) (element-type guint8): a buffer to write
166 * data from
167 * @count: the number of bytes you want to write
168 * @cancellable: (allow-none): a #GCancellable, or %NULL
169 * @error: #GError for error reporting, or %NULL to ignore.
171 * Attempts to write up to @count bytes from @buffer to @stream, as
172 * with g_output_stream_write(). If @stream is not currently writable,
173 * this will immediately return %G_IO_ERROR_WOULD_BLOCK, and you can
174 * use g_pollable_output_stream_create_source() to create a #GSource
175 * that will be triggered when @stream is writable.
177 * Note that since this method never blocks, you cannot actually
178 * use @cancellable to cancel it. However, it will return an error
179 * if @cancellable has already been cancelled when you call, which
180 * may happen if you call this method after a source triggers due
181 * to having been cancelled.
183 * Virtual: write_nonblocking
184 * Return value: the number of bytes written, or -1 on error (including
185 * %G_IO_ERROR_WOULD_BLOCK).
187 gssize
188 g_pollable_output_stream_write_nonblocking (GPollableOutputStream *stream,
189 const void *buffer,
190 gsize count,
191 GCancellable *cancellable,
192 GError **error)
194 gssize res;
196 g_return_val_if_fail (G_IS_POLLABLE_OUTPUT_STREAM (stream), -1);
197 g_return_val_if_fail (buffer != NULL, 0);
199 if (g_cancellable_set_error_if_cancelled (cancellable, error))
200 return -1;
202 if (count == 0)
203 return 0;
205 if (((gssize) count) < 0)
207 g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT,
208 _("Too large count value passed to %s"), G_STRFUNC);
209 return -1;
212 if (cancellable)
213 g_cancellable_push_current (cancellable);
215 res = G_POLLABLE_OUTPUT_STREAM_GET_INTERFACE (stream)->
216 write_nonblocking (stream, buffer, count, error);
218 if (cancellable)
219 g_cancellable_pop_current (cancellable);
221 return res;