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>
32 #include <gio/gioerror.h>
36 #define g_socket_input_stream_get_type _g_socket_input_stream_get_type
37 G_DEFINE_TYPE (GSocketInputStream, g_socket_input_stream, G_TYPE_INPUT_STREAM);
45 struct _GSocketInputStreamPrivate
49 /* pending operation metadata */
50 GSimpleAsyncResult *result;
51 GCancellable *cancellable;
57 g_socket_input_stream_get_property (GObject *object,
62 GSocketInputStream *stream = G_SOCKET_INPUT_STREAM (object);
67 g_value_set_object (value, stream->priv->socket);
71 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
76 g_socket_input_stream_set_property (GObject *object,
81 GSocketInputStream *stream = G_SOCKET_INPUT_STREAM (object);
86 stream->priv->socket = g_value_dup_object (value);
90 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
95 g_socket_input_stream_finalize (GObject *object)
97 GSocketInputStream *stream = G_SOCKET_INPUT_STREAM (object);
99 if (stream->priv->socket)
100 g_object_unref (stream->priv->socket);
102 if (G_OBJECT_CLASS (g_socket_input_stream_parent_class)->finalize)
103 (*G_OBJECT_CLASS (g_socket_input_stream_parent_class)->finalize) (object);
107 g_socket_input_stream_read (GInputStream *stream,
110 GCancellable *cancellable,
113 GSocketInputStream *input_stream = G_SOCKET_INPUT_STREAM (stream);
115 return g_socket_receive (input_stream->priv->socket, buffer, count,
120 g_socket_input_stream_read_ready (GSocket *socket,
121 GIOCondition condition,
122 GSocketInputStream *stream)
124 GSimpleAsyncResult *simple;
125 GError *error = NULL;
128 result = g_socket_receive (stream->priv->socket,
129 stream->priv->buffer,
131 stream->priv->cancellable,
134 if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK))
137 simple = stream->priv->result;
138 stream->priv->result = NULL;
141 g_simple_async_result_set_op_res_gssize (simple, result);
145 g_simple_async_result_set_from_error (simple, error);
146 g_error_free (error);
149 if (stream->priv->cancellable)
150 g_object_unref (stream->priv->cancellable);
152 g_simple_async_result_complete (simple);
153 g_object_unref (simple);
159 g_socket_input_stream_read_async (GInputStream *stream,
163 GCancellable *cancellable,
164 GAsyncReadyCallback callback,
167 GSocketInputStream *input_stream = G_SOCKET_INPUT_STREAM (stream);
170 g_assert (input_stream->priv->result == NULL);
172 input_stream->priv->result =
173 g_simple_async_result_new (G_OBJECT (stream), callback, user_data,
174 g_socket_input_stream_read_async);
176 g_object_ref (cancellable);
177 input_stream->priv->cancellable = cancellable;
178 input_stream->priv->buffer = buffer;
179 input_stream->priv->count = count;
181 source = g_socket_create_source (input_stream->priv->socket,
182 G_IO_IN | G_IO_HUP | G_IO_ERR,
184 g_source_set_callback (source,
185 (GSourceFunc) g_socket_input_stream_read_ready,
186 g_object_ref (input_stream), g_object_unref);
187 g_source_attach (source, g_main_context_get_thread_default ());
188 g_source_unref (source);
192 g_socket_input_stream_read_finish (GInputStream *stream,
193 GAsyncResult *result,
196 GSimpleAsyncResult *simple;
199 g_return_val_if_fail (G_IS_SOCKET_INPUT_STREAM (stream), -1);
201 simple = G_SIMPLE_ASYNC_RESULT (result);
203 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_socket_input_stream_read_async);
205 count = g_simple_async_result_get_op_res_gssize (simple);
211 g_socket_input_stream_class_init (GSocketInputStreamClass *klass)
213 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
214 GInputStreamClass *ginputstream_class = G_INPUT_STREAM_CLASS (klass);
216 g_type_class_add_private (klass, sizeof (GSocketInputStreamPrivate));
218 gobject_class->finalize = g_socket_input_stream_finalize;
219 gobject_class->get_property = g_socket_input_stream_get_property;
220 gobject_class->set_property = g_socket_input_stream_set_property;
222 ginputstream_class->read_fn = g_socket_input_stream_read;
223 ginputstream_class->read_async = g_socket_input_stream_read_async;
224 ginputstream_class->read_finish = g_socket_input_stream_read_finish;
226 g_object_class_install_property (gobject_class, PROP_SOCKET,
227 g_param_spec_object ("socket",
229 P_("The socket that this stream wraps"),
230 G_TYPE_SOCKET, G_PARAM_CONSTRUCT_ONLY |
231 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
235 g_socket_input_stream_init (GSocketInputStream *stream)
237 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream, G_TYPE_SOCKET_INPUT_STREAM, GSocketInputStreamPrivate);
241 _g_socket_input_stream_new (GSocket *socket)
243 return G_SOCKET_INPUT_STREAM (g_object_new (G_TYPE_SOCKET_INPUT_STREAM, "socket", socket, NULL));
246 #define __G_SOCKET_INPUT_STREAM_C__
247 #include "gioaliasdef.c"