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 * SECTION:gsocketoutputstream
44 * @short_description: Socket Output Stream
45 * @see_also: #GOutputStream.
47 * #GSocketOutputStream implements #GOutputStream for writing to a socket, including
48 * asynchronous operations.
51 G_DEFINE_TYPE (GSocketOutputStream, g_socket_output_stream, G_TYPE_OUTPUT_STREAM);
54 struct _GSocketOutputStreamPrivate {
56 gboolean close_fd_at_close;
59 static gssize g_socket_output_stream_write (GOutputStream *stream,
62 GCancellable *cancellable,
64 static gboolean g_socket_output_stream_close (GOutputStream *stream,
65 GCancellable *cancellable,
67 static void g_socket_output_stream_write_async (GOutputStream *stream,
71 GCancellable *cancellable,
72 GAsyncReadyCallback callback,
74 static gssize g_socket_output_stream_write_finish (GOutputStream *stream,
77 static void g_socket_output_stream_close_async (GOutputStream *stream,
79 GCancellable *cancellable,
80 GAsyncReadyCallback callback,
82 static gboolean g_socket_output_stream_close_finish (GOutputStream *stream,
88 g_socket_output_stream_finalize (GObject *object)
90 GSocketOutputStream *stream;
92 stream = G_SOCKET_OUTPUT_STREAM (object);
94 if (G_OBJECT_CLASS (g_socket_output_stream_parent_class)->finalize)
95 (*G_OBJECT_CLASS (g_socket_output_stream_parent_class)->finalize) (object);
99 g_socket_output_stream_class_init (GSocketOutputStreamClass *klass)
101 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
102 GOutputStreamClass *stream_class = G_OUTPUT_STREAM_CLASS (klass);
104 g_type_class_add_private (klass, sizeof (GSocketOutputStreamPrivate));
106 gobject_class->finalize = g_socket_output_stream_finalize;
108 stream_class->write = g_socket_output_stream_write;
109 stream_class->close = g_socket_output_stream_close;
110 stream_class->write_async = g_socket_output_stream_write_async;
111 stream_class->write_finish = g_socket_output_stream_write_finish;
112 stream_class->close_async = g_socket_output_stream_close_async;
113 stream_class->close_finish = g_socket_output_stream_close_finish;
117 g_socket_output_stream_init (GSocketOutputStream *socket)
119 socket->priv = G_TYPE_INSTANCE_GET_PRIVATE (socket,
120 G_TYPE_SOCKET_OUTPUT_STREAM,
121 GSocketOutputStreamPrivate);
126 * g_socket_output_stream_new:
127 * @fd: socket's file descriptor.
128 * @close_fd_at_close: a #gboolean.
130 * Creates a new socket output stream for @fd. If @close_fd_at_close
131 * is %TRUE, the socket will be closed when the output stream is destroyed.
133 * Returns: #GOutputStream. If @close_fd_at_close is %TRUE, then
134 * @fd will be closed when the #GOutputStream is closed.
137 g_socket_output_stream_new (int fd,
138 gboolean close_fd_at_close)
140 GSocketOutputStream *stream;
142 g_return_val_if_fail (fd != -1, NULL);
144 stream = g_object_new (G_TYPE_SOCKET_OUTPUT_STREAM, NULL);
146 stream->priv->fd = fd;
147 stream->priv->close_fd_at_close = close_fd_at_close;
149 return G_OUTPUT_STREAM (stream);
153 g_socket_output_stream_write (GOutputStream *stream,
156 GCancellable *cancellable,
159 GSocketOutputStream *socket_stream;
161 struct pollfd poll_fds[2];
165 socket_stream = G_SOCKET_OUTPUT_STREAM (stream);
167 cancel_fd = g_cancellable_get_fd (cancellable);
172 poll_fds[0].events = POLLOUT;
173 poll_fds[0].fd = socket_stream->priv->fd;
174 poll_fds[1].events = POLLIN;
175 poll_fds[1].fd = cancel_fd;
176 poll_ret = poll (poll_fds, 2, -1);
178 while (poll_ret == -1 && errno == EINTR);
182 g_set_error (error, G_IO_ERROR,
183 g_io_error_from_errno (errno),
184 _("Error writing to socket: %s"),
192 if (g_cancellable_set_error_if_cancelled (cancellable, error))
195 res = write (socket_stream->priv->fd, buffer, count);
201 g_set_error (error, G_IO_ERROR,
202 g_io_error_from_errno (errno),
203 _("Error writing to socket: %s"),
214 g_socket_output_stream_close (GOutputStream *stream,
215 GCancellable *cancellable,
218 GSocketOutputStream *socket_stream;
221 socket_stream = G_SOCKET_OUTPUT_STREAM (stream);
223 if (!socket_stream->priv->close_fd_at_close)
228 /* This might block during the close. Doesn't seem to be a way to avoid it though. */
229 res = close (socket_stream->priv->fd);
232 g_set_error (error, G_IO_ERROR,
233 g_io_error_from_errno (errno),
234 _("Error closing socket: %s"),
246 GAsyncReadyCallback callback;
248 GCancellable *cancellable;
249 GSocketOutputStream *stream;
253 write_async_cb (WriteAsyncData *data,
254 GIOCondition condition,
257 GSimpleAsyncResult *simple;
258 GError *error = NULL;
259 gssize count_written;
263 if (g_cancellable_set_error_if_cancelled (data->cancellable, &error))
269 count_written = write (data->stream->priv->fd, data->buffer, data->count);
270 if (count_written == -1)
275 g_set_error (&error, G_IO_ERROR,
276 g_io_error_from_errno (errno),
277 _("Error reading from socket: %s"),
283 simple = g_simple_async_result_new (G_OBJECT (data->stream),
286 g_socket_output_stream_write_async);
288 g_simple_async_result_set_op_res_gssize (simple, count_written);
290 if (count_written == -1)
292 g_simple_async_result_set_from_error (simple, error);
293 g_error_free (error);
296 /* Complete immediately, not in idle, since we're already in a mainloop callout */
297 g_simple_async_result_complete (simple);
298 g_object_unref (simple);
304 g_socket_output_stream_write_async (GOutputStream *stream,
308 GCancellable *cancellable,
309 GAsyncReadyCallback callback,
313 GSocketOutputStream *socket_stream;
314 WriteAsyncData *data;
316 socket_stream = G_SOCKET_OUTPUT_STREAM (stream);
318 data = g_new0 (WriteAsyncData, 1);
320 data->buffer = buffer;
321 data->callback = callback;
322 data->user_data = user_data;
323 data->cancellable = cancellable;
324 data->stream = socket_stream;
326 source = _g_fd_source_new (socket_stream->priv->fd,
330 g_source_set_callback (source, (GSourceFunc)write_async_cb, data, g_free);
331 g_source_attach (source, NULL);
333 g_source_unref (source);
337 g_socket_output_stream_write_finish (GOutputStream *stream,
338 GAsyncResult *result,
341 GSimpleAsyncResult *simple;
344 simple = G_SIMPLE_ASYNC_RESULT (result);
345 g_assert (g_simple_async_result_get_source_tag (simple) == g_socket_output_stream_write_async);
347 nwritten = g_simple_async_result_get_op_res_gssize (simple);
352 GOutputStream *stream;
353 GAsyncReadyCallback callback;
358 close_async_cb (CloseAsyncData *data)
360 GSocketOutputStream *socket_stream;
361 GSimpleAsyncResult *simple;
362 GError *error = NULL;
366 socket_stream = G_SOCKET_OUTPUT_STREAM (data->stream);
368 if (!socket_stream->priv->close_fd_at_close)
376 res = close (socket_stream->priv->fd);
379 g_set_error (&error, G_IO_ERROR,
380 g_io_error_from_errno (errno),
381 _("Error closing socket: %s"),
390 simple = g_simple_async_result_new (G_OBJECT (data->stream),
393 g_socket_output_stream_close_async);
397 g_simple_async_result_set_from_error (simple, error);
398 g_error_free (error);
401 /* Complete immediately, not in idle, since we're already in a mainloop callout */
402 g_simple_async_result_complete (simple);
403 g_object_unref (simple);
409 g_socket_output_stream_close_async (GOutputStream *stream,
411 GCancellable *cancellable,
412 GAsyncReadyCallback callback,
416 CloseAsyncData *data;
418 data = g_new0 (CloseAsyncData, 1);
420 data->stream = stream;
421 data->callback = callback;
422 data->user_data = user_data;
424 idle = g_idle_source_new ();
425 g_source_set_callback (idle, (GSourceFunc)close_async_cb, data, g_free);
426 g_source_attach (idle, NULL);
427 g_source_unref (idle);
431 g_socket_output_stream_close_finish (GOutputStream *stream,
432 GAsyncResult *result,
435 /* Failures handled in generic close_finish code */