1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 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.
20 * Author: Alexander Larsson <alexl@redhat.com>
25 #include <sys/types.h>
34 #include <glib/gstdio.h>
36 #include "gsimpleasyncresult.h"
37 #include "gsocketinputstream.h"
38 #include "gcancellable.h"
39 #include "gasynchelper.h"
43 G_DEFINE_TYPE (GSocketInputStream, g_socket_input_stream, G_TYPE_INPUT_STREAM);
45 struct _GSocketInputStreamPrivate {
47 gboolean close_fd_at_close;
50 static gssize g_socket_input_stream_read (GInputStream *stream,
53 GCancellable *cancellable,
55 static gboolean g_socket_input_stream_close (GInputStream *stream,
56 GCancellable *cancellable,
58 static void g_socket_input_stream_read_async (GInputStream *stream,
62 GCancellable *cancellable,
63 GAsyncReadyCallback callback,
65 static gssize g_socket_input_stream_read_finish (GInputStream *stream,
68 static void g_socket_input_stream_skip_async (GInputStream *stream,
71 GCancellable *cancellable,
72 GAsyncReadyCallback callback,
74 static gssize g_socket_input_stream_skip_finish (GInputStream *stream,
77 static void g_socket_input_stream_close_async (GInputStream *stream,
79 GCancellable *cancellable,
80 GAsyncReadyCallback callback,
82 static gboolean g_socket_input_stream_close_finish (GInputStream *stream,
87 g_socket_input_stream_finalize (GObject *object)
89 GSocketInputStream *stream;
91 stream = G_SOCKET_INPUT_STREAM (object);
93 if (G_OBJECT_CLASS (g_socket_input_stream_parent_class)->finalize)
94 (*G_OBJECT_CLASS (g_socket_input_stream_parent_class)->finalize) (object);
98 g_socket_input_stream_class_init (GSocketInputStreamClass *klass)
100 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
101 GInputStreamClass *stream_class = G_INPUT_STREAM_CLASS (klass);
103 g_type_class_add_private (klass, sizeof (GSocketInputStreamPrivate));
105 gobject_class->finalize = g_socket_input_stream_finalize;
107 stream_class->read = g_socket_input_stream_read;
108 stream_class->close = g_socket_input_stream_close;
109 stream_class->read_async = g_socket_input_stream_read_async;
110 stream_class->read_finish = g_socket_input_stream_read_finish;
113 /* TODO: Implement instead of using fallbacks */
114 stream_class->skip_async = g_socket_input_stream_skip_async;
115 stream_class->skip_finish = g_socket_input_stream_skip_finish;
117 stream_class->close_async = g_socket_input_stream_close_async;
118 stream_class->close_finish = g_socket_input_stream_close_finish;
122 g_socket_input_stream_init (GSocketInputStream *socket)
124 socket->priv = G_TYPE_INSTANCE_GET_PRIVATE (socket,
125 G_TYPE_SOCKET_INPUT_STREAM,
126 GSocketInputStreamPrivate);
130 * g_socket_input_stream_new:
131 * @fd: file descriptor.
132 * @close_fd_at_close: boolean value
135 * Returns: new #GInputStream. If @close_fd_at_close is %TRUE,
136 * @fd will be closed when the #GInputStream is closed.
139 g_socket_input_stream_new (int fd,
140 gboolean close_fd_at_close)
142 GSocketInputStream *stream;
144 g_return_val_if_fail (fd != -1, NULL);
146 stream = g_object_new (G_TYPE_SOCKET_INPUT_STREAM, NULL);
148 stream->priv->fd = fd;
149 stream->priv->close_fd_at_close = close_fd_at_close;
151 return G_INPUT_STREAM (stream);
155 g_socket_input_stream_read (GInputStream *stream,
158 GCancellable *cancellable,
161 GSocketInputStream *socket_stream;
163 struct pollfd poll_fds[2];
167 socket_stream = G_SOCKET_INPUT_STREAM (stream);
169 cancel_fd = g_cancellable_get_fd (cancellable);
174 poll_fds[0].events = POLLIN;
175 poll_fds[0].fd = socket_stream->priv->fd;
176 poll_fds[1].events = POLLIN;
177 poll_fds[1].fd = cancel_fd;
178 poll_ret = poll (poll_fds, 2, -1);
180 while (poll_ret == -1 && errno == EINTR);
184 g_set_error (error, G_IO_ERROR,
185 g_io_error_from_errno (errno),
186 _("Error reading from socket: %s"),
194 if (g_cancellable_set_error_if_cancelled (cancellable, error))
196 res = read (socket_stream->priv->fd, buffer, count);
202 g_set_error (error, G_IO_ERROR,
203 g_io_error_from_errno (errno),
204 _("Error reading from socket: %s"),
215 g_socket_input_stream_close (GInputStream *stream,
216 GCancellable *cancellable,
219 GSocketInputStream *socket_stream;
222 socket_stream = G_SOCKET_INPUT_STREAM (stream);
224 if (!socket_stream->priv->close_fd_at_close)
229 /* This might block during the close. Doesn't seem to be a way to avoid it though. */
230 res = close (socket_stream->priv->fd);
233 g_set_error (error, G_IO_ERROR,
234 g_io_error_from_errno (errno),
235 _("Error closing socket: %s"),
247 GAsyncReadyCallback callback;
249 GCancellable *cancellable;
250 GSocketInputStream *stream;
254 read_async_cb (ReadAsyncData *data,
255 GIOCondition condition,
258 GSimpleAsyncResult *simple;
259 GError *error = NULL;
262 /* We know that we can read from fd once without blocking */
265 if (g_cancellable_set_error_if_cancelled (data->cancellable, &error))
270 count_read = read (data->stream->priv->fd, data->buffer, data->count);
271 if (count_read == -1)
276 g_set_error (&error, G_IO_ERROR,
277 g_io_error_from_errno (errno),
278 _("Error reading from socket: %s"),
284 simple = g_simple_async_result_new (G_OBJECT (data->stream),
287 g_socket_input_stream_read_async);
289 g_simple_async_result_set_op_res_gssize (simple, count_read);
291 if (count_read == -1)
293 g_simple_async_result_set_from_error (simple, error);
294 g_error_free (error);
297 /* Complete immediately, not in idle, since we're already in a mainloop callout */
298 g_simple_async_result_complete (simple);
299 g_object_unref (simple);
305 g_socket_input_stream_read_async (GInputStream *stream,
309 GCancellable *cancellable,
310 GAsyncReadyCallback callback,
314 GSocketInputStream *socket_stream;
317 socket_stream = G_SOCKET_INPUT_STREAM (stream);
319 data = g_new0 (ReadAsyncData, 1);
321 data->buffer = buffer;
322 data->callback = callback;
323 data->user_data = user_data;
324 data->cancellable = cancellable;
325 data->stream = socket_stream;
327 source = _g_fd_source_new (socket_stream->priv->fd,
331 g_source_set_callback (source, (GSourceFunc)read_async_cb, data, g_free);
332 g_source_attach (source, NULL);
334 g_source_unref (source);
338 g_socket_input_stream_read_finish (GInputStream *stream,
339 GAsyncResult *result,
342 GSimpleAsyncResult *simple;
345 simple = G_SIMPLE_ASYNC_RESULT (result);
346 g_assert (g_simple_async_result_get_source_tag (simple) == g_socket_input_stream_read_async);
348 nread = g_simple_async_result_get_op_res_gssize (simple);
353 g_socket_input_stream_skip_async (GInputStream *stream,
356 GCancellable *cancellable,
357 GAsyncReadyCallback callback,
360 g_assert_not_reached ();
361 /* TODO: Not implemented */
365 g_socket_input_stream_skip_finish (GInputStream *stream,
366 GAsyncResult *result,
369 g_assert_not_reached ();
370 /* TODO: Not implemented */
375 GInputStream *stream;
376 GAsyncReadyCallback callback;
381 close_async_data_free (gpointer _data)
383 CloseAsyncData *data = _data;
389 close_async_cb (CloseAsyncData *data)
391 GSocketInputStream *socket_stream;
392 GSimpleAsyncResult *simple;
393 GError *error = NULL;
397 socket_stream = G_SOCKET_INPUT_STREAM (data->stream);
399 if (!socket_stream->priv->close_fd_at_close)
407 res = close (socket_stream->priv->fd);
410 g_set_error (&error, G_IO_ERROR,
411 g_io_error_from_errno (errno),
412 _("Error closing socket: %s"),
421 simple = g_simple_async_result_new (G_OBJECT (data->stream),
424 g_socket_input_stream_close_async);
428 g_simple_async_result_set_from_error (simple, error);
429 g_error_free (error);
432 /* Complete immediately, not in idle, since we're already in a mainloop callout */
433 g_simple_async_result_complete (simple);
434 g_object_unref (simple);
440 g_socket_input_stream_close_async (GInputStream *stream,
442 GCancellable *cancellable,
443 GAsyncReadyCallback callback,
447 CloseAsyncData *data;
449 data = g_new0 (CloseAsyncData, 1);
451 data->stream = stream;
452 data->callback = callback;
453 data->user_data = user_data;
455 idle = g_idle_source_new ();
456 g_source_set_callback (idle, (GSourceFunc)close_async_cb, data, close_async_data_free);
457 g_source_attach (idle, NULL);
458 g_source_unref (idle);
462 g_socket_input_stream_close_finish (GInputStream *stream,
463 GAsyncResult *result,
466 /* Failures handled in generic close_finish code */