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 "gsimpleasyncresult.h"
31 #include "gcancellable.h"
32 #include "gpollableinputstream.h"
34 #include "gfiledescriptorbased.h"
36 static void g_socket_input_stream_pollable_iface_init (GPollableInputStreamInterface *iface);
38 static void g_socket_input_stream_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface);
41 #define g_socket_input_stream_get_type _g_socket_input_stream_get_type
42 G_DEFINE_TYPE_WITH_CODE (GSocketInputStream, g_socket_input_stream, G_TYPE_INPUT_STREAM,
43 G_IMPLEMENT_INTERFACE (G_TYPE_POLLABLE_INPUT_STREAM, g_socket_input_stream_pollable_iface_init)
46 G_IMPLEMENT_INTERFACE (G_TYPE_FILE_DESCRIPTOR_BASED, g_socket_input_stream_file_descriptor_based_iface_init)
56 struct _GSocketInputStreamPrivate
60 /* pending operation metadata */
61 GSimpleAsyncResult *result;
62 GCancellable *cancellable;
68 g_socket_input_stream_get_property (GObject *object,
73 GSocketInputStream *stream = G_SOCKET_INPUT_STREAM (object);
78 g_value_set_object (value, stream->priv->socket);
82 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
87 g_socket_input_stream_set_property (GObject *object,
92 GSocketInputStream *stream = G_SOCKET_INPUT_STREAM (object);
97 stream->priv->socket = g_value_dup_object (value);
101 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
106 g_socket_input_stream_finalize (GObject *object)
108 GSocketInputStream *stream = G_SOCKET_INPUT_STREAM (object);
110 if (stream->priv->socket)
111 g_object_unref (stream->priv->socket);
113 if (G_OBJECT_CLASS (g_socket_input_stream_parent_class)->finalize)
114 (*G_OBJECT_CLASS (g_socket_input_stream_parent_class)->finalize) (object);
118 g_socket_input_stream_read (GInputStream *stream,
121 GCancellable *cancellable,
124 GSocketInputStream *input_stream = G_SOCKET_INPUT_STREAM (stream);
126 return g_socket_receive_with_blocking (input_stream->priv->socket,
132 g_socket_input_stream_read_ready (GSocket *socket,
133 GIOCondition condition,
134 GSocketInputStream *stream)
136 GSimpleAsyncResult *simple;
137 GError *error = NULL;
140 result = g_socket_receive_with_blocking (stream->priv->socket,
141 stream->priv->buffer,
144 stream->priv->cancellable,
147 if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK))
150 simple = stream->priv->result;
151 stream->priv->result = NULL;
154 g_simple_async_result_set_op_res_gssize (simple, result);
157 g_simple_async_result_take_error (simple, error);
159 if (stream->priv->cancellable)
160 g_object_unref (stream->priv->cancellable);
162 g_simple_async_result_complete (simple);
163 g_object_unref (simple);
169 g_socket_input_stream_read_async (GInputStream *stream,
173 GCancellable *cancellable,
174 GAsyncReadyCallback callback,
177 GSocketInputStream *input_stream = G_SOCKET_INPUT_STREAM (stream);
180 g_assert (input_stream->priv->result == NULL);
182 input_stream->priv->result =
183 g_simple_async_result_new (G_OBJECT (stream), callback, user_data,
184 g_socket_input_stream_read_async);
186 g_object_ref (cancellable);
187 input_stream->priv->cancellable = cancellable;
188 input_stream->priv->buffer = buffer;
189 input_stream->priv->count = count;
191 source = g_socket_create_source (input_stream->priv->socket,
192 G_IO_IN | G_IO_HUP | G_IO_ERR,
194 g_source_set_callback (source,
195 (GSourceFunc) g_socket_input_stream_read_ready,
196 g_object_ref (input_stream), g_object_unref);
197 g_source_attach (source, g_main_context_get_thread_default ());
198 g_source_unref (source);
202 g_socket_input_stream_read_finish (GInputStream *stream,
203 GAsyncResult *result,
206 GSimpleAsyncResult *simple;
209 g_return_val_if_fail (G_IS_SOCKET_INPUT_STREAM (stream), -1);
211 simple = G_SIMPLE_ASYNC_RESULT (result);
213 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_socket_input_stream_read_async);
215 count = g_simple_async_result_get_op_res_gssize (simple);
221 g_socket_input_stream_pollable_is_readable (GPollableInputStream *pollable)
223 GSocketInputStream *input_stream = G_SOCKET_INPUT_STREAM (pollable);
225 return g_socket_condition_check (input_stream->priv->socket, G_IO_IN);
229 g_socket_input_stream_pollable_create_source (GPollableInputStream *pollable,
230 GCancellable *cancellable)
232 GSocketInputStream *input_stream = G_SOCKET_INPUT_STREAM (pollable);
233 GSource *socket_source, *pollable_source;
235 pollable_source = g_pollable_source_new (G_OBJECT (input_stream));
236 socket_source = g_socket_create_source (input_stream->priv->socket,
237 G_IO_IN, cancellable);
238 g_source_set_dummy_callback (socket_source);
239 g_source_add_child_source (pollable_source, socket_source);
240 g_source_unref (socket_source);
242 return pollable_source;
246 g_socket_input_stream_pollable_read_nonblocking (GPollableInputStream *pollable,
251 GSocketInputStream *input_stream = G_SOCKET_INPUT_STREAM (pollable);
253 return g_socket_receive_with_blocking (input_stream->priv->socket,
260 g_socket_input_stream_get_fd (GFileDescriptorBased *fd_based)
262 GSocketInputStream *input_stream = G_SOCKET_INPUT_STREAM (fd_based);
264 return g_socket_get_fd (input_stream->priv->socket);
269 g_socket_input_stream_class_init (GSocketInputStreamClass *klass)
271 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
272 GInputStreamClass *ginputstream_class = G_INPUT_STREAM_CLASS (klass);
274 g_type_class_add_private (klass, sizeof (GSocketInputStreamPrivate));
276 gobject_class->finalize = g_socket_input_stream_finalize;
277 gobject_class->get_property = g_socket_input_stream_get_property;
278 gobject_class->set_property = g_socket_input_stream_set_property;
280 ginputstream_class->read_fn = g_socket_input_stream_read;
281 ginputstream_class->read_async = g_socket_input_stream_read_async;
282 ginputstream_class->read_finish = g_socket_input_stream_read_finish;
284 g_object_class_install_property (gobject_class, PROP_SOCKET,
285 g_param_spec_object ("socket",
287 P_("The socket that this stream wraps"),
288 G_TYPE_SOCKET, G_PARAM_CONSTRUCT_ONLY |
289 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
294 g_socket_input_stream_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface)
296 iface->get_fd = g_socket_input_stream_get_fd;
301 g_socket_input_stream_pollable_iface_init (GPollableInputStreamInterface *iface)
303 iface->is_readable = g_socket_input_stream_pollable_is_readable;
304 iface->create_source = g_socket_input_stream_pollable_create_source;
305 iface->read_nonblocking = g_socket_input_stream_pollable_read_nonblocking;
309 g_socket_input_stream_init (GSocketInputStream *stream)
311 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream, G_TYPE_SOCKET_INPUT_STREAM, GSocketInputStreamPrivate);
315 _g_socket_input_stream_new (GSocket *socket)
317 return G_SOCKET_INPUT_STREAM (g_object_new (G_TYPE_SOCKET_INPUT_STREAM, "socket", socket, NULL));