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 "gsocketinputstream.h"
30 #include <gio/gsimpleasyncresult.h>
31 #include <gio/gcancellable.h>
33 #define g_socket_input_stream_get_type _g_socket_input_stream_get_type
34 G_DEFINE_TYPE (GSocketInputStream, g_socket_input_stream, G_TYPE_INPUT_STREAM);
42 struct _GSocketInputStreamPrivate
46 /* pending operation metadata */
47 GSimpleAsyncResult *result;
48 GCancellable *cancellable;
49 gboolean from_mainloop;
55 g_socket_input_stream_get_property (GObject *object,
60 GSocketInputStream *stream = G_SOCKET_INPUT_STREAM (object);
65 g_value_set_object (value, stream->priv->socket);
69 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
74 g_socket_input_stream_set_property (GObject *object,
79 GSocketInputStream *stream = G_SOCKET_INPUT_STREAM (object);
84 stream->priv->socket = g_value_dup_object (value);
88 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
93 g_socket_input_stream_finalize (GObject *object)
95 GSocketInputStream *stream = G_SOCKET_INPUT_STREAM (object);
97 if (stream->priv->socket)
98 g_object_unref (stream->priv->socket);
100 if (G_OBJECT_CLASS (g_socket_input_stream_parent_class)->finalize)
101 (*G_OBJECT_CLASS (g_socket_input_stream_parent_class)->finalize) (object);
105 g_socket_input_stream_read (GInputStream *stream,
108 GCancellable *cancellable,
111 GSocketInputStream *input_stream = G_SOCKET_INPUT_STREAM (stream);
113 if (!g_socket_condition_wait (input_stream->priv->socket,
114 G_IO_IN, cancellable, error))
117 return g_socket_receive (input_stream->priv->socket, buffer, count, error);
121 g_socket_input_stream_read_ready (GSocket *socket,
122 GIOCondition condition,
123 GSocketInputStream *stream)
125 GSimpleAsyncResult *simple;
126 GError *error = NULL;
128 simple = stream->priv->result;
129 stream->priv->result = NULL;
131 if (!g_cancellable_set_error_if_cancelled (stream->priv->cancellable,
136 result = g_socket_receive (stream->priv->socket,
137 stream->priv->buffer,
141 g_simple_async_result_set_op_res_gssize (simple, result);
146 g_simple_async_result_set_from_error (simple, error);
147 g_error_free (error);
150 if (stream->priv->cancellable)
151 g_object_unref (stream->priv->cancellable);
153 if (stream->priv->from_mainloop)
154 g_simple_async_result_complete (simple);
156 g_simple_async_result_complete_in_idle (simple);
158 g_object_unref (simple);
164 g_socket_input_stream_read_async (GInputStream *stream,
168 GCancellable *cancellable,
169 GAsyncReadyCallback callback,
172 GSocketInputStream *input_stream = G_SOCKET_INPUT_STREAM (stream);
174 g_assert (input_stream->priv->result == NULL);
176 input_stream->priv->result =
177 g_simple_async_result_new (G_OBJECT (stream), callback, user_data,
178 g_socket_input_stream_read_async);
180 g_object_ref (cancellable);
181 input_stream->priv->cancellable = cancellable;
182 input_stream->priv->buffer = buffer;
183 input_stream->priv->count = count;
185 if (!g_socket_condition_check (input_stream->priv->socket, G_IO_IN))
189 input_stream->priv->from_mainloop = TRUE;
190 source = g_socket_create_source (input_stream->priv->socket,
191 G_IO_IN | G_IO_HUP | G_IO_ERR,
193 g_source_set_callback (source,
194 (GSourceFunc) g_socket_input_stream_read_ready,
195 g_object_ref (input_stream), g_object_unref);
196 g_source_attach (source, NULL);
197 g_source_unref (source);
201 input_stream->priv->from_mainloop = FALSE;
202 g_socket_input_stream_read_ready (input_stream->priv->socket, G_IO_IN, input_stream);
207 g_socket_input_stream_read_finish (GInputStream *stream,
208 GAsyncResult *result,
211 GSimpleAsyncResult *simple;
214 g_return_val_if_fail (G_IS_SOCKET_INPUT_STREAM (stream), -1);
216 simple = G_SIMPLE_ASYNC_RESULT (result);
218 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_socket_input_stream_read_async);
220 count = g_simple_async_result_get_op_res_gssize (simple);
226 g_socket_input_stream_class_init (GSocketInputStreamClass *klass)
228 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
229 GInputStreamClass *ginputstream_class = G_INPUT_STREAM_CLASS (klass);
231 g_type_class_add_private (klass, sizeof (GSocketInputStreamPrivate));
233 gobject_class->finalize = g_socket_input_stream_finalize;
234 gobject_class->get_property = g_socket_input_stream_get_property;
235 gobject_class->set_property = g_socket_input_stream_set_property;
237 ginputstream_class->read_fn = g_socket_input_stream_read;
238 ginputstream_class->read_async = g_socket_input_stream_read_async;
239 ginputstream_class->read_finish = g_socket_input_stream_read_finish;
241 g_object_class_install_property (gobject_class, PROP_SOCKET,
242 g_param_spec_object ("socket",
244 P_("The socket that this stream wraps"),
245 G_TYPE_SOCKET, G_PARAM_CONSTRUCT_ONLY |
246 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
250 g_socket_input_stream_init (GSocketInputStream *stream)
252 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream, G_TYPE_SOCKET_INPUT_STREAM, GSocketInputStreamPrivate);
256 _g_socket_input_stream_new (GSocket *socket)
258 return G_SOCKET_INPUT_STREAM (g_object_new (G_TYPE_SOCKET_INPUT_STREAM, "socket", socket, NULL));