1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright © 2008 Christian Kellner, Samuel Cormier-Iijima
6 * SPDX-License-Identifier: LGPL-2.1-or-later
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General
19 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
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 "gcancellable.h"
31 #include "gpollableinputstream.h"
33 #include "gfiledescriptorbased.h"
35 struct _GSocketInputStreamPrivate
39 /* pending operation metadata */
44 static void g_socket_input_stream_pollable_iface_init (GPollableInputStreamInterface *iface);
46 static void g_socket_input_stream_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface);
49 #define g_socket_input_stream_get_type _g_socket_input_stream_get_type
52 G_DEFINE_TYPE_WITH_CODE (GSocketInputStream, g_socket_input_stream, G_TYPE_INPUT_STREAM,
53 G_ADD_PRIVATE (GSocketInputStream)
54 G_IMPLEMENT_INTERFACE (G_TYPE_POLLABLE_INPUT_STREAM, g_socket_input_stream_pollable_iface_init)
55 G_IMPLEMENT_INTERFACE (G_TYPE_FILE_DESCRIPTOR_BASED, g_socket_input_stream_file_descriptor_based_iface_init)
58 G_DEFINE_TYPE_WITH_CODE (GSocketInputStream, g_socket_input_stream, G_TYPE_INPUT_STREAM,
59 G_ADD_PRIVATE (GSocketInputStream)
60 G_IMPLEMENT_INTERFACE (G_TYPE_POLLABLE_INPUT_STREAM, g_socket_input_stream_pollable_iface_init)
71 g_socket_input_stream_get_property (GObject *object,
76 GSocketInputStream *stream = G_SOCKET_INPUT_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_input_stream_set_property (GObject *object,
95 GSocketInputStream *stream = G_SOCKET_INPUT_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_input_stream_finalize (GObject *object)
111 GSocketInputStream *stream = G_SOCKET_INPUT_STREAM (object);
113 if (stream->priv->socket)
114 g_object_unref (stream->priv->socket);
116 G_OBJECT_CLASS (g_socket_input_stream_parent_class)->finalize (object);
120 g_socket_input_stream_read (GInputStream *stream,
123 GCancellable *cancellable,
126 GSocketInputStream *input_stream = G_SOCKET_INPUT_STREAM (stream);
128 return g_socket_receive_with_blocking (input_stream->priv->socket,
134 g_socket_input_stream_pollable_is_readable (GPollableInputStream *pollable)
136 GSocketInputStream *input_stream = G_SOCKET_INPUT_STREAM (pollable);
138 return g_socket_condition_check (input_stream->priv->socket, G_IO_IN);
142 g_socket_input_stream_pollable_create_source (GPollableInputStream *pollable,
143 GCancellable *cancellable)
145 GSocketInputStream *input_stream = G_SOCKET_INPUT_STREAM (pollable);
146 GSource *socket_source, *pollable_source;
148 pollable_source = g_pollable_source_new (G_OBJECT (input_stream));
149 socket_source = g_socket_create_source (input_stream->priv->socket,
150 G_IO_IN, cancellable);
151 g_source_set_dummy_callback (socket_source);
152 g_source_add_child_source (pollable_source, socket_source);
153 g_source_unref (socket_source);
155 return pollable_source;
159 g_socket_input_stream_pollable_read_nonblocking (GPollableInputStream *pollable,
164 GSocketInputStream *input_stream = G_SOCKET_INPUT_STREAM (pollable);
166 return g_socket_receive_with_blocking (input_stream->priv->socket,
173 g_socket_input_stream_get_fd (GFileDescriptorBased *fd_based)
175 GSocketInputStream *input_stream = G_SOCKET_INPUT_STREAM (fd_based);
177 return g_socket_get_fd (input_stream->priv->socket);
182 g_socket_input_stream_class_init (GSocketInputStreamClass *klass)
184 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
185 GInputStreamClass *ginputstream_class = G_INPUT_STREAM_CLASS (klass);
187 gobject_class->finalize = g_socket_input_stream_finalize;
188 gobject_class->get_property = g_socket_input_stream_get_property;
189 gobject_class->set_property = g_socket_input_stream_set_property;
191 ginputstream_class->read_fn = g_socket_input_stream_read;
193 g_object_class_install_property (gobject_class, PROP_SOCKET,
194 g_param_spec_object ("socket", NULL, NULL,
195 G_TYPE_SOCKET, G_PARAM_CONSTRUCT_ONLY |
196 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
201 g_socket_input_stream_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface)
203 iface->get_fd = g_socket_input_stream_get_fd;
208 g_socket_input_stream_pollable_iface_init (GPollableInputStreamInterface *iface)
210 iface->is_readable = g_socket_input_stream_pollable_is_readable;
211 iface->create_source = g_socket_input_stream_pollable_create_source;
212 iface->read_nonblocking = g_socket_input_stream_pollable_read_nonblocking;
216 g_socket_input_stream_init (GSocketInputStream *stream)
218 stream->priv = g_socket_input_stream_get_instance_private (stream);
222 _g_socket_input_stream_new (GSocket *socket)
224 return g_object_new (G_TYPE_SOCKET_INPUT_STREAM, "socket", socket, NULL);