nexmon – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /* GIO - GLib Input, Output and Streaming Library |
2 | * |
||
3 | * Copyright (C) 2010 Red Hat, Inc. |
||
4 | * |
||
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. |
||
9 | * |
||
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. |
||
14 | * |
||
15 | * You should have received a copy of the GNU Lesser General |
||
16 | * Public License along with this library; if not, see <http://www.gnu.org/licenses/>. |
||
17 | */ |
||
18 | |||
19 | #include "config.h" |
||
20 | |||
21 | #include <errno.h> |
||
22 | |||
23 | #include "gpollableinputstream.h" |
||
24 | #include "gasynchelper.h" |
||
25 | #include "glibintl.h" |
||
26 | |||
27 | /** |
||
28 | * SECTION:gpollableutils |
||
29 | * @short_description: Utilities for pollable streams |
||
30 | * @include: gio/gio.h |
||
31 | * |
||
32 | * Utility functions for #GPollableInputStream and |
||
33 | * #GPollableOutputStream implementations. |
||
34 | */ |
||
35 | |||
36 | typedef struct { |
||
37 | GSource source; |
||
38 | |||
39 | GObject *stream; |
||
40 | } GPollableSource; |
||
41 | |||
42 | static gboolean |
||
43 | pollable_source_dispatch (GSource *source, |
||
44 | GSourceFunc callback, |
||
45 | gpointer user_data) |
||
46 | { |
||
47 | GPollableSourceFunc func = (GPollableSourceFunc)callback; |
||
48 | GPollableSource *pollable_source = (GPollableSource *)source; |
||
49 | |||
50 | return (*func) (pollable_source->stream, user_data); |
||
51 | } |
||
52 | |||
53 | static void |
||
54 | pollable_source_finalize (GSource *source) |
||
55 | { |
||
56 | GPollableSource *pollable_source = (GPollableSource *)source; |
||
57 | |||
58 | g_object_unref (pollable_source->stream); |
||
59 | } |
||
60 | |||
61 | static gboolean |
||
62 | pollable_source_closure_callback (GObject *stream, |
||
63 | gpointer data) |
||
64 | { |
||
65 | GClosure *closure = data; |
||
66 | |||
67 | GValue param = G_VALUE_INIT; |
||
68 | GValue result_value = G_VALUE_INIT; |
||
69 | gboolean result; |
||
70 | |||
71 | g_value_init (&result_value, G_TYPE_BOOLEAN); |
||
72 | |||
73 | g_value_init (¶m, G_TYPE_OBJECT); |
||
74 | g_value_set_object (¶m, stream); |
||
75 | |||
76 | g_closure_invoke (closure, &result_value, 1, ¶m, NULL); |
||
77 | |||
78 | result = g_value_get_boolean (&result_value); |
||
79 | g_value_unset (&result_value); |
||
80 | g_value_unset (¶m); |
||
81 | |||
82 | return result; |
||
83 | } |
||
84 | |||
85 | static GSourceFuncs pollable_source_funcs = |
||
86 | { |
||
87 | NULL, |
||
88 | NULL, |
||
89 | pollable_source_dispatch, |
||
90 | pollable_source_finalize, |
||
91 | (GSourceFunc)pollable_source_closure_callback, |
||
92 | }; |
||
93 | |||
94 | /** |
||
95 | * g_pollable_source_new: |
||
96 | * @pollable_stream: the stream associated with the new source |
||
97 | * |
||
98 | * Utility method for #GPollableInputStream and #GPollableOutputStream |
||
99 | * implementations. Creates a new #GSource that expects a callback of |
||
100 | * type #GPollableSourceFunc. The new source does not actually do |
||
101 | * anything on its own; use g_source_add_child_source() to add other |
||
102 | * sources to it to cause it to trigger. |
||
103 | * |
||
104 | * Returns: (transfer full): the new #GSource. |
||
105 | * |
||
106 | * Since: 2.28 |
||
107 | */ |
||
108 | GSource * |
||
109 | g_pollable_source_new (GObject *pollable_stream) |
||
110 | { |
||
111 | GSource *source; |
||
112 | GPollableSource *pollable_source; |
||
113 | |||
114 | g_return_val_if_fail (G_IS_POLLABLE_INPUT_STREAM (pollable_stream) || |
||
115 | G_IS_POLLABLE_OUTPUT_STREAM (pollable_stream), NULL); |
||
116 | |||
117 | source = g_source_new (&pollable_source_funcs, sizeof (GPollableSource)); |
||
118 | g_source_set_name (source, "GPollableSource"); |
||
119 | pollable_source = (GPollableSource *)source; |
||
120 | pollable_source->stream = g_object_ref (pollable_stream); |
||
121 | |||
122 | return source; |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * g_pollable_source_new_full: |
||
127 | * @pollable_stream: (type GObject): the stream associated with the |
||
128 | * new source |
||
129 | * @child_source: (allow-none): optional child source to attach |
||
130 | * @cancellable: (allow-none): optional #GCancellable to attach |
||
131 | * |
||
132 | * Utility method for #GPollableInputStream and #GPollableOutputStream |
||
133 | * implementations. Creates a new #GSource, as with |
||
134 | * g_pollable_source_new(), but also attaching @child_source (with a |
||
135 | * dummy callback), and @cancellable, if they are non-%NULL. |
||
136 | * |
||
137 | * Returns: (transfer full): the new #GSource. |
||
138 | * |
||
139 | * Since: 2.34 |
||
140 | */ |
||
141 | GSource * |
||
142 | g_pollable_source_new_full (gpointer pollable_stream, |
||
143 | GSource *child_source, |
||
144 | GCancellable *cancellable) |
||
145 | { |
||
146 | GSource *source; |
||
147 | |||
148 | g_return_val_if_fail (G_IS_POLLABLE_INPUT_STREAM (pollable_stream) || |
||
149 | G_IS_POLLABLE_OUTPUT_STREAM (pollable_stream), NULL); |
||
150 | |||
151 | source = g_pollable_source_new (pollable_stream); |
||
152 | if (child_source) |
||
153 | { |
||
154 | g_source_set_dummy_callback (child_source); |
||
155 | g_source_add_child_source (source, child_source); |
||
156 | } |
||
157 | if (cancellable) |
||
158 | { |
||
159 | GSource *cancellable_source = g_cancellable_source_new (cancellable); |
||
160 | |||
161 | g_source_set_dummy_callback (cancellable_source); |
||
162 | g_source_add_child_source (source, cancellable_source); |
||
163 | g_source_unref (cancellable_source); |
||
164 | } |
||
165 | |||
166 | return source; |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * g_pollable_stream_read: |
||
171 | * @stream: a #GInputStream |
||
172 | * @buffer: (array length=count) (element-type guint8): a buffer to |
||
173 | * read data into |
||
174 | * @count: the number of bytes to read |
||
175 | * @blocking: whether to do blocking I/O |
||
176 | * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore. |
||
177 | * @error: location to store the error occurring, or %NULL to ignore |
||
178 | * |
||
179 | * Tries to read from @stream, as with g_input_stream_read() (if |
||
180 | * @blocking is %TRUE) or g_pollable_input_stream_read_nonblocking() |
||
181 | * (if @blocking is %FALSE). This can be used to more easily share |
||
182 | * code between blocking and non-blocking implementations of a method. |
||
183 | * |
||
184 | * If @blocking is %FALSE, then @stream must be a |
||
185 | * #GPollableInputStream for which g_pollable_input_stream_can_poll() |
||
186 | * returns %TRUE, or else the behavior is undefined. If @blocking is |
||
187 | * %TRUE, then @stream does not need to be a #GPollableInputStream. |
||
188 | * |
||
189 | * Returns: the number of bytes read, or -1 on error. |
||
190 | * |
||
191 | * Since: 2.34 |
||
192 | */ |
||
193 | gssize |
||
194 | g_pollable_stream_read (GInputStream *stream, |
||
195 | void *buffer, |
||
196 | gsize count, |
||
197 | gboolean blocking, |
||
198 | GCancellable *cancellable, |
||
199 | GError **error) |
||
200 | { |
||
201 | if (blocking) |
||
202 | { |
||
203 | return g_input_stream_read (stream, |
||
204 | buffer, count, |
||
205 | cancellable, error); |
||
206 | } |
||
207 | else |
||
208 | { |
||
209 | return g_pollable_input_stream_read_nonblocking (G_POLLABLE_INPUT_STREAM (stream), |
||
210 | buffer, count, |
||
211 | cancellable, error); |
||
212 | } |
||
213 | } |
||
214 | |||
215 | /** |
||
216 | * g_pollable_stream_write: |
||
217 | * @stream: a #GOutputStream. |
||
218 | * @buffer: (array length=count) (element-type guint8): the buffer |
||
219 | * containing the data to write. |
||
220 | * @count: the number of bytes to write |
||
221 | * @blocking: whether to do blocking I/O |
||
222 | * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore. |
||
223 | * @error: location to store the error occurring, or %NULL to ignore |
||
224 | * |
||
225 | * Tries to write to @stream, as with g_output_stream_write() (if |
||
226 | * @blocking is %TRUE) or g_pollable_output_stream_write_nonblocking() |
||
227 | * (if @blocking is %FALSE). This can be used to more easily share |
||
228 | * code between blocking and non-blocking implementations of a method. |
||
229 | * |
||
230 | * If @blocking is %FALSE, then @stream must be a |
||
231 | * #GPollableOutputStream for which |
||
232 | * g_pollable_output_stream_can_poll() returns %TRUE or else the |
||
233 | * behavior is undefined. If @blocking is %TRUE, then @stream does not |
||
234 | * need to be a #GPollableOutputStream. |
||
235 | * |
||
236 | * Returns: the number of bytes written, or -1 on error. |
||
237 | * |
||
238 | * Since: 2.34 |
||
239 | */ |
||
240 | gssize |
||
241 | g_pollable_stream_write (GOutputStream *stream, |
||
242 | const void *buffer, |
||
243 | gsize count, |
||
244 | gboolean blocking, |
||
245 | GCancellable *cancellable, |
||
246 | GError **error) |
||
247 | { |
||
248 | if (blocking) |
||
249 | { |
||
250 | return g_output_stream_write (stream, |
||
251 | buffer, count, |
||
252 | cancellable, error); |
||
253 | } |
||
254 | else |
||
255 | { |
||
256 | return g_pollable_output_stream_write_nonblocking (G_POLLABLE_OUTPUT_STREAM (stream), |
||
257 | buffer, count, |
||
258 | cancellable, error); |
||
259 | } |
||
260 | } |
||
261 | |||
262 | /** |
||
263 | * g_pollable_stream_write_all: |
||
264 | * @stream: a #GOutputStream. |
||
265 | * @buffer: (array length=count) (element-type guint8): the buffer |
||
266 | * containing the data to write. |
||
267 | * @count: the number of bytes to write |
||
268 | * @blocking: whether to do blocking I/O |
||
269 | * @bytes_written: (out): location to store the number of bytes that was |
||
270 | * written to the stream |
||
271 | * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore. |
||
272 | * @error: location to store the error occurring, or %NULL to ignore |
||
273 | * |
||
274 | * Tries to write @count bytes to @stream, as with |
||
275 | * g_output_stream_write_all(), but using g_pollable_stream_write() |
||
276 | * rather than g_output_stream_write(). |
||
277 | * |
||
278 | * On a successful write of @count bytes, %TRUE is returned, and |
||
279 | * @bytes_written is set to @count. |
||
280 | * |
||
281 | * If there is an error during the operation (including |
||
282 | * %G_IO_ERROR_WOULD_BLOCK in the non-blocking case), %FALSE is |
||
283 | * returned and @error is set to indicate the error status, |
||
284 | * @bytes_written is updated to contain the number of bytes written |
||
285 | * into the stream before the error occurred. |
||
286 | * |
||
287 | * As with g_pollable_stream_write(), if @blocking is %FALSE, then |
||
288 | * @stream must be a #GPollableOutputStream for which |
||
289 | * g_pollable_output_stream_can_poll() returns %TRUE or else the |
||
290 | * behavior is undefined. If @blocking is %TRUE, then @stream does not |
||
291 | * need to be a #GPollableOutputStream. |
||
292 | * |
||
293 | * Returns: %TRUE on success, %FALSE if there was an error |
||
294 | * |
||
295 | * Since: 2.34 |
||
296 | */ |
||
297 | gboolean |
||
298 | g_pollable_stream_write_all (GOutputStream *stream, |
||
299 | const void *buffer, |
||
300 | gsize count, |
||
301 | gboolean blocking, |
||
302 | gsize *bytes_written, |
||
303 | GCancellable *cancellable, |
||
304 | GError **error) |
||
305 | { |
||
306 | gsize _bytes_written; |
||
307 | gssize res; |
||
308 | |||
309 | _bytes_written = 0; |
||
310 | while (_bytes_written < count) |
||
311 | { |
||
312 | res = g_pollable_stream_write (stream, |
||
313 | (char *)buffer + _bytes_written, |
||
314 | count - _bytes_written, |
||
315 | blocking, |
||
316 | cancellable, error); |
||
317 | if (res == -1) |
||
318 | { |
||
319 | if (bytes_written) |
||
320 | *bytes_written = _bytes_written; |
||
321 | return FALSE; |
||
322 | } |
||
323 | |||
324 | if (res == 0) |
||
325 | g_warning ("Write returned zero without error"); |
||
326 | |||
327 | _bytes_written += res; |
||
328 | } |
||
329 | |||
330 | if (bytes_written) |
||
331 | *bytes_written = _bytes_written; |
||
332 | |||
333 | return TRUE; |
||
334 | } |