1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright © 2008 Christian Kellner, Samuel Cormier-Iijima
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General
17 * Public License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
19 * Boston, MA 02111-1307, USA.
21 * Authors: Christian Kellner <gicmo@gnome.org>
22 * Samuel Cormier-Iijima <sciyoshi@gmail.com>
23 * Ryan Lortie <desrt@desrt.ca>
27 #include "goutputstream.h"
28 #include "gsocketoutputstream.h"
32 #include "gsimpleasyncresult.h"
33 #include "gcancellable.h"
34 #include "gpollableinputstream.h"
35 #include "gpollableoutputstream.h"
38 #include "gfiledescriptorbased.h"
40 static void g_socket_output_stream_pollable_iface_init (GPollableOutputStreamInterface *iface);
42 static void g_socket_output_stream_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface);
45 #define g_socket_output_stream_get_type _g_socket_output_stream_get_type
46 G_DEFINE_TYPE_WITH_CODE (GSocketOutputStream, g_socket_output_stream, G_TYPE_OUTPUT_STREAM,
47 G_IMPLEMENT_INTERFACE (G_TYPE_POLLABLE_OUTPUT_STREAM, g_socket_output_stream_pollable_iface_init)
49 G_IMPLEMENT_INTERFACE (G_TYPE_FILE_DESCRIPTOR_BASED, g_socket_output_stream_file_descriptor_based_iface_init)
59 struct _GSocketOutputStreamPrivate
63 /* pending operation metadata */
64 GSimpleAsyncResult *result;
65 GCancellable *cancellable;
71 g_socket_output_stream_get_property (GObject *object,
76 GSocketOutputStream *stream = G_SOCKET_OUTPUT_STREAM (object);
81 g_value_set_object (value, stream->priv->socket);
85 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
90 g_socket_output_stream_set_property (GObject *object,
95 GSocketOutputStream *stream = G_SOCKET_OUTPUT_STREAM (object);
100 stream->priv->socket = g_value_dup_object (value);
104 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
109 g_socket_output_stream_finalize (GObject *object)
111 GSocketOutputStream *stream = G_SOCKET_OUTPUT_STREAM (object);
113 if (stream->priv->socket)
114 g_object_unref (stream->priv->socket);
116 if (G_OBJECT_CLASS (g_socket_output_stream_parent_class)->finalize)
117 (*G_OBJECT_CLASS (g_socket_output_stream_parent_class)->finalize) (object);
121 g_socket_output_stream_write (GOutputStream *stream,
124 GCancellable *cancellable,
127 GSocketOutputStream *onput_stream = G_SOCKET_OUTPUT_STREAM (stream);
129 return g_socket_send_with_blocking (onput_stream->priv->socket,
135 g_socket_output_stream_write_ready (GSocket *socket,
136 GIOCondition condition,
137 GSocketOutputStream *stream)
139 GSimpleAsyncResult *simple;
140 GError *error = NULL;
143 result = g_socket_send_with_blocking (stream->priv->socket,
144 stream->priv->buffer,
147 stream->priv->cancellable,
150 if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK))
153 simple = stream->priv->result;
154 stream->priv->result = NULL;
157 g_simple_async_result_set_op_res_gssize (simple, result);
160 g_simple_async_result_take_error (simple, error);
162 if (stream->priv->cancellable)
163 g_object_unref (stream->priv->cancellable);
165 g_simple_async_result_complete (simple);
166 g_object_unref (simple);
172 g_socket_output_stream_write_async (GOutputStream *stream,
176 GCancellable *cancellable,
177 GAsyncReadyCallback callback,
180 GSocketOutputStream *output_stream = G_SOCKET_OUTPUT_STREAM (stream);
183 g_assert (output_stream->priv->result == NULL);
185 output_stream->priv->result =
186 g_simple_async_result_new (G_OBJECT (stream), callback, user_data,
187 g_socket_output_stream_write_async);
189 g_object_ref (cancellable);
190 output_stream->priv->cancellable = cancellable;
191 output_stream->priv->buffer = buffer;
192 output_stream->priv->count = count;
194 source = g_socket_create_source (output_stream->priv->socket,
195 G_IO_OUT | G_IO_HUP | G_IO_ERR,
197 g_source_set_callback (source,
198 (GSourceFunc) g_socket_output_stream_write_ready,
199 g_object_ref (output_stream), g_object_unref);
200 g_source_attach (source, g_main_context_get_thread_default ());
201 g_source_unref (source);
205 g_socket_output_stream_write_finish (GOutputStream *stream,
206 GAsyncResult *result,
209 GSimpleAsyncResult *simple;
212 g_return_val_if_fail (G_IS_SOCKET_OUTPUT_STREAM (stream), -1);
214 simple = G_SIMPLE_ASYNC_RESULT (result);
216 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_socket_output_stream_write_async);
218 count = g_simple_async_result_get_op_res_gssize (simple);
224 g_socket_output_stream_pollable_is_writable (GPollableOutputStream *pollable)
226 GSocketOutputStream *output_stream = G_SOCKET_OUTPUT_STREAM (pollable);
228 return g_socket_condition_check (output_stream->priv->socket, G_IO_OUT);
232 g_socket_output_stream_pollable_create_source (GPollableOutputStream *pollable,
233 GCancellable *cancellable)
235 GSocketOutputStream *output_stream = G_SOCKET_OUTPUT_STREAM (pollable);
236 GSource *socket_source, *pollable_source;
238 pollable_source = g_pollable_source_new (G_OBJECT (output_stream));
239 socket_source = g_socket_create_source (output_stream->priv->socket,
240 G_IO_OUT, cancellable);
241 g_source_set_dummy_callback (socket_source);
242 g_source_add_child_source (pollable_source, socket_source);
243 g_source_unref (socket_source);
245 return pollable_source;
249 g_socket_output_stream_pollable_write_nonblocking (GPollableOutputStream *pollable,
254 GSocketOutputStream *output_stream = G_SOCKET_OUTPUT_STREAM (pollable);
256 return g_socket_send_with_blocking (output_stream->priv->socket,
263 g_socket_output_stream_get_fd (GFileDescriptorBased *fd_based)
265 GSocketOutputStream *output_stream = G_SOCKET_OUTPUT_STREAM (fd_based);
267 return g_socket_get_fd (output_stream->priv->socket);
272 g_socket_output_stream_class_init (GSocketOutputStreamClass *klass)
274 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
275 GOutputStreamClass *goutputstream_class = G_OUTPUT_STREAM_CLASS (klass);
277 g_type_class_add_private (klass, sizeof (GSocketOutputStreamPrivate));
279 gobject_class->finalize = g_socket_output_stream_finalize;
280 gobject_class->get_property = g_socket_output_stream_get_property;
281 gobject_class->set_property = g_socket_output_stream_set_property;
283 goutputstream_class->write_fn = g_socket_output_stream_write;
284 goutputstream_class->write_async = g_socket_output_stream_write_async;
285 goutputstream_class->write_finish = g_socket_output_stream_write_finish;
287 g_object_class_install_property (gobject_class, PROP_SOCKET,
288 g_param_spec_object ("socket",
290 P_("The socket that this stream wraps"),
291 G_TYPE_SOCKET, G_PARAM_CONSTRUCT_ONLY |
292 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
297 g_socket_output_stream_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface)
299 iface->get_fd = g_socket_output_stream_get_fd;
304 g_socket_output_stream_pollable_iface_init (GPollableOutputStreamInterface *iface)
306 iface->is_writable = g_socket_output_stream_pollable_is_writable;
307 iface->create_source = g_socket_output_stream_pollable_create_source;
308 iface->write_nonblocking = g_socket_output_stream_pollable_write_nonblocking;
312 g_socket_output_stream_init (GSocketOutputStream *stream)
314 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream, G_TYPE_SOCKET_OUTPUT_STREAM, GSocketOutputStreamPrivate);
317 GSocketOutputStream *
318 _g_socket_output_stream_new (GSocket *socket)
320 return G_SOCKET_OUTPUT_STREAM (g_object_new (G_TYPE_SOCKET_OUTPUT_STREAM, "socket", socket, NULL));