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 "gsocketoutputstream.h"
37 #include "gcancellable.h"
38 #include "gsimpleasyncresult.h"
39 #include "gasynchelper.h"
43 G_DEFINE_TYPE (GSocketOutputStream, g_socket_output_stream, G_TYPE_OUTPUT_STREAM);
46 struct _GSocketOutputStreamPrivate {
48 gboolean close_fd_at_close;
51 static gssize g_socket_output_stream_write (GOutputStream *stream,
54 GCancellable *cancellable,
56 static gboolean g_socket_output_stream_close (GOutputStream *stream,
57 GCancellable *cancellable,
59 static void g_socket_output_stream_write_async (GOutputStream *stream,
63 GCancellable *cancellable,
64 GAsyncReadyCallback callback,
66 static gssize g_socket_output_stream_write_finish (GOutputStream *stream,
69 static void g_socket_output_stream_close_async (GOutputStream *stream,
71 GCancellable *cancellable,
72 GAsyncReadyCallback callback,
74 static gboolean g_socket_output_stream_close_finish (GOutputStream *stream,
80 g_socket_output_stream_finalize (GObject *object)
82 GSocketOutputStream *stream;
84 stream = G_SOCKET_OUTPUT_STREAM (object);
86 if (G_OBJECT_CLASS (g_socket_output_stream_parent_class)->finalize)
87 (*G_OBJECT_CLASS (g_socket_output_stream_parent_class)->finalize) (object);
91 g_socket_output_stream_class_init (GSocketOutputStreamClass *klass)
93 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
94 GOutputStreamClass *stream_class = G_OUTPUT_STREAM_CLASS (klass);
96 g_type_class_add_private (klass, sizeof (GSocketOutputStreamPrivate));
98 gobject_class->finalize = g_socket_output_stream_finalize;
100 stream_class->write = g_socket_output_stream_write;
101 stream_class->close = g_socket_output_stream_close;
102 stream_class->write_async = g_socket_output_stream_write_async;
103 stream_class->write_finish = g_socket_output_stream_write_finish;
104 stream_class->close_async = g_socket_output_stream_close_async;
105 stream_class->close_finish = g_socket_output_stream_close_finish;
109 g_socket_output_stream_init (GSocketOutputStream *socket)
111 socket->priv = G_TYPE_INSTANCE_GET_PRIVATE (socket,
112 G_TYPE_SOCKET_OUTPUT_STREAM,
113 GSocketOutputStreamPrivate);
118 * g_socket_output_stream_new:
119 * @fd: file descriptor.
120 * @close_fd_at_close: boolean value.
122 * Returns: #GOutputStream. If @close_fd_at_close is %TRUE, then
123 * @fd will be closed when the #GOutputStream is closed.
126 g_socket_output_stream_new (int fd,
127 gboolean close_fd_at_close)
129 GSocketOutputStream *stream;
131 g_return_val_if_fail (fd != -1, NULL);
133 stream = g_object_new (G_TYPE_SOCKET_OUTPUT_STREAM, NULL);
135 stream->priv->fd = fd;
136 stream->priv->close_fd_at_close = close_fd_at_close;
138 return G_OUTPUT_STREAM (stream);
142 g_socket_output_stream_write (GOutputStream *stream,
145 GCancellable *cancellable,
148 GSocketOutputStream *socket_stream;
150 struct pollfd poll_fds[2];
154 socket_stream = G_SOCKET_OUTPUT_STREAM (stream);
156 cancel_fd = g_cancellable_get_fd (cancellable);
161 poll_fds[0].events = POLLOUT;
162 poll_fds[0].fd = socket_stream->priv->fd;
163 poll_fds[1].events = POLLIN;
164 poll_fds[1].fd = cancel_fd;
165 poll_ret = poll (poll_fds, 2, -1);
167 while (poll_ret == -1 && errno == EINTR);
171 g_set_error (error, G_IO_ERROR,
172 g_io_error_from_errno (errno),
173 _("Error writing to socket: %s"),
181 if (g_cancellable_set_error_if_cancelled (cancellable, error))
184 res = write (socket_stream->priv->fd, buffer, count);
190 g_set_error (error, G_IO_ERROR,
191 g_io_error_from_errno (errno),
192 _("Error writing to socket: %s"),
203 g_socket_output_stream_close (GOutputStream *stream,
204 GCancellable *cancellable,
207 GSocketOutputStream *socket_stream;
210 socket_stream = G_SOCKET_OUTPUT_STREAM (stream);
212 if (!socket_stream->priv->close_fd_at_close)
217 /* This might block during the close. Doesn't seem to be a way to avoid it though. */
218 res = close (socket_stream->priv->fd);
221 g_set_error (error, G_IO_ERROR,
222 g_io_error_from_errno (errno),
223 _("Error closing socket: %s"),
235 GAsyncReadyCallback callback;
237 GCancellable *cancellable;
238 GSocketOutputStream *stream;
242 write_async_cb (WriteAsyncData *data,
243 GIOCondition condition,
246 GSimpleAsyncResult *simple;
247 GError *error = NULL;
248 gssize count_written;
252 if (g_cancellable_set_error_if_cancelled (data->cancellable, &error))
258 count_written = write (data->stream->priv->fd, data->buffer, data->count);
259 if (count_written == -1)
264 g_set_error (&error, G_IO_ERROR,
265 g_io_error_from_errno (errno),
266 _("Error reading from socket: %s"),
272 simple = g_simple_async_result_new (G_OBJECT (data->stream),
275 g_socket_output_stream_write_async);
277 g_simple_async_result_set_op_res_gssize (simple, count_written);
279 if (count_written == -1)
281 g_simple_async_result_set_from_error (simple, error);
282 g_error_free (error);
285 /* Complete immediately, not in idle, since we're already in a mainloop callout */
286 g_simple_async_result_complete (simple);
287 g_object_unref (simple);
293 g_socket_output_stream_write_async (GOutputStream *stream,
297 GCancellable *cancellable,
298 GAsyncReadyCallback callback,
302 GSocketOutputStream *socket_stream;
303 WriteAsyncData *data;
305 socket_stream = G_SOCKET_OUTPUT_STREAM (stream);
307 data = g_new0 (WriteAsyncData, 1);
309 data->buffer = buffer;
310 data->callback = callback;
311 data->user_data = user_data;
312 data->cancellable = cancellable;
313 data->stream = socket_stream;
315 source = _g_fd_source_new (socket_stream->priv->fd,
319 g_source_set_callback (source, (GSourceFunc)write_async_cb, data, g_free);
320 g_source_attach (source, NULL);
322 g_source_unref (source);
326 g_socket_output_stream_write_finish (GOutputStream *stream,
327 GAsyncResult *result,
330 GSimpleAsyncResult *simple;
333 simple = G_SIMPLE_ASYNC_RESULT (result);
334 g_assert (g_simple_async_result_get_source_tag (simple) == g_socket_output_stream_write_async);
336 nwritten = g_simple_async_result_get_op_res_gssize (simple);
341 GOutputStream *stream;
342 GAsyncReadyCallback callback;
347 close_async_cb (CloseAsyncData *data)
349 GSocketOutputStream *socket_stream;
350 GSimpleAsyncResult *simple;
351 GError *error = NULL;
355 socket_stream = G_SOCKET_OUTPUT_STREAM (data->stream);
357 if (!socket_stream->priv->close_fd_at_close)
365 res = close (socket_stream->priv->fd);
368 g_set_error (&error, G_IO_ERROR,
369 g_io_error_from_errno (errno),
370 _("Error closing socket: %s"),
379 simple = g_simple_async_result_new (G_OBJECT (data->stream),
382 g_socket_output_stream_close_async);
386 g_simple_async_result_set_from_error (simple, error);
387 g_error_free (error);
390 /* Complete immediately, not in idle, since we're already in a mainloop callout */
391 g_simple_async_result_complete (simple);
392 g_object_unref (simple);
398 g_socket_output_stream_close_async (GOutputStream *stream,
400 GCancellable *cancellable,
401 GAsyncReadyCallback callback,
405 CloseAsyncData *data;
407 data = g_new0 (CloseAsyncData, 1);
409 data->stream = stream;
410 data->callback = callback;
411 data->user_data = user_data;
413 idle = g_idle_source_new ();
414 g_source_set_callback (idle, (GSourceFunc)close_async_cb, data, g_free);
415 g_source_attach (idle, NULL);
416 g_source_unref (idle);
420 g_socket_output_stream_close_finish (GOutputStream *stream,
421 GAsyncResult *result,
424 /* Failures handled in generic close_finish code */