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"
31 #include <gio/gsimpleasyncresult.h>
32 #include <gio/gcancellable.h>
37 #define g_socket_output_stream_get_type _g_socket_output_stream_get_type
38 G_DEFINE_TYPE (GSocketOutputStream, g_socket_output_stream, G_TYPE_OUTPUT_STREAM);
46 struct _GSocketOutputStreamPrivate
50 /* pending operation metadata */
51 GSimpleAsyncResult *result;
52 GCancellable *cancellable;
53 gboolean from_mainloop;
59 g_socket_output_stream_get_property (GObject *object,
64 GSocketOutputStream *stream = G_SOCKET_OUTPUT_STREAM (object);
69 g_value_set_object (value, stream->priv->socket);
73 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
78 g_socket_output_stream_set_property (GObject *object,
83 GSocketOutputStream *stream = G_SOCKET_OUTPUT_STREAM (object);
88 stream->priv->socket = g_value_dup_object (value);
92 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
97 g_socket_output_stream_finalize (GObject *object)
99 GSocketOutputStream *stream = G_SOCKET_OUTPUT_STREAM (object);
101 if (stream->priv->socket)
102 g_object_unref (stream->priv->socket);
104 if (G_OBJECT_CLASS (g_socket_output_stream_parent_class)->finalize)
105 (*G_OBJECT_CLASS (g_socket_output_stream_parent_class)->finalize) (object);
109 g_socket_output_stream_write (GOutputStream *stream,
112 GCancellable *cancellable,
115 GSocketOutputStream *onput_stream = G_SOCKET_OUTPUT_STREAM (stream);
117 return g_socket_send (onput_stream->priv->socket, buffer, count,
122 g_socket_output_stream_write_ready (GSocket *socket,
123 GIOCondition condition,
124 GSocketOutputStream *stream)
126 GSimpleAsyncResult *simple;
127 GError *error = NULL;
130 simple = stream->priv->result;
131 stream->priv->result = NULL;
133 result = g_socket_send (stream->priv->socket,
134 stream->priv->buffer,
136 stream->priv->cancellable,
139 g_simple_async_result_set_op_res_gssize (simple, result);
143 g_simple_async_result_set_from_error (simple, error);
144 g_error_free (error);
147 if (stream->priv->cancellable)
148 g_object_unref (stream->priv->cancellable);
150 if (stream->priv->from_mainloop)
151 g_simple_async_result_complete (simple);
153 g_simple_async_result_complete_in_idle (simple);
155 g_object_unref (simple);
161 g_socket_output_stream_write_async (GOutputStream *stream,
165 GCancellable *cancellable,
166 GAsyncReadyCallback callback,
169 GSocketOutputStream *output_stream = G_SOCKET_OUTPUT_STREAM (stream);
171 g_assert (output_stream->priv->result == NULL);
173 output_stream->priv->result =
174 g_simple_async_result_new (G_OBJECT (stream), callback, user_data,
175 g_socket_output_stream_write_async);
177 g_object_ref (cancellable);
178 output_stream->priv->cancellable = cancellable;
179 output_stream->priv->buffer = buffer;
180 output_stream->priv->count = count;
182 if (!g_socket_condition_check (output_stream->priv->socket, G_IO_OUT))
186 output_stream->priv->from_mainloop = TRUE;
187 source = g_socket_create_source (output_stream->priv->socket,
188 G_IO_OUT | G_IO_HUP | G_IO_ERR,
190 g_source_set_callback (source,
191 (GSourceFunc) g_socket_output_stream_write_ready,
192 g_object_ref (output_stream), g_object_unref);
193 g_source_attach (source, NULL);
194 g_source_unref (source);
198 output_stream->priv->from_mainloop = FALSE;
199 g_socket_output_stream_write_ready (output_stream->priv->socket, G_IO_OUT, output_stream);
204 g_socket_output_stream_write_finish (GOutputStream *stream,
205 GAsyncResult *result,
208 GSimpleAsyncResult *simple;
211 g_return_val_if_fail (G_IS_SOCKET_OUTPUT_STREAM (stream), -1);
213 simple = G_SIMPLE_ASYNC_RESULT (result);
215 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_socket_output_stream_write_async);
217 count = g_simple_async_result_get_op_res_gssize (simple);
223 g_socket_output_stream_class_init (GSocketOutputStreamClass *klass)
225 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
226 GOutputStreamClass *goutputstream_class = G_OUTPUT_STREAM_CLASS (klass);
228 g_type_class_add_private (klass, sizeof (GSocketOutputStreamPrivate));
230 gobject_class->finalize = g_socket_output_stream_finalize;
231 gobject_class->get_property = g_socket_output_stream_get_property;
232 gobject_class->set_property = g_socket_output_stream_set_property;
234 goutputstream_class->write_fn = g_socket_output_stream_write;
235 goutputstream_class->write_async = g_socket_output_stream_write_async;
236 goutputstream_class->write_finish = g_socket_output_stream_write_finish;
238 g_object_class_install_property (gobject_class, PROP_SOCKET,
239 g_param_spec_object ("socket",
241 P_("The socket that this stream wraps"),
242 G_TYPE_SOCKET, G_PARAM_CONSTRUCT_ONLY |
243 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
247 g_socket_output_stream_init (GSocketOutputStream *stream)
249 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream, G_TYPE_SOCKET_OUTPUT_STREAM, GSocketOutputStreamPrivate);
252 GSocketOutputStream *
253 _g_socket_output_stream_new (GSocket *socket)
255 return G_SOCKET_OUTPUT_STREAM (g_object_new (G_TYPE_SOCKET_OUTPUT_STREAM, "socket", socket, NULL));
258 #define __G_SOCKET_OUTPUT_STREAM_C__
259 #include "gioaliasdef.c"