GSeekable: document seek-past-end semantics
[glib.git] / gio / gsocketinputstream.c
blob85295bf1e9592f6f816697a260f523942fac90c4
1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright © 2008 Christian Kellner, Samuel Cormier-Iijima
4 * © 2009 codethink
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>
26 #include "config.h"
27 #include "gsocketinputstream.h"
28 #include "glibintl.h"
30 #include "gcancellable.h"
31 #include "gpollableinputstream.h"
32 #include "gioerror.h"
33 #include "gfiledescriptorbased.h"
35 struct _GSocketInputStreamPrivate
37 GSocket *socket;
39 /* pending operation metadata */
40 gpointer buffer;
41 gsize count;
44 static void g_socket_input_stream_pollable_iface_init (GPollableInputStreamInterface *iface);
45 #ifdef G_OS_UNIX
46 static void g_socket_input_stream_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface);
47 #endif
49 #define g_socket_input_stream_get_type _g_socket_input_stream_get_type
51 #ifdef G_OS_UNIX
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)
57 #else
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)
62 #endif
64 enum
66 PROP_0,
67 PROP_SOCKET
70 static void
71 g_socket_input_stream_get_property (GObject *object,
72 guint prop_id,
73 GValue *value,
74 GParamSpec *pspec)
76 GSocketInputStream *stream = G_SOCKET_INPUT_STREAM (object);
78 switch (prop_id)
80 case PROP_SOCKET:
81 g_value_set_object (value, stream->priv->socket);
82 break;
84 default:
85 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
89 static void
90 g_socket_input_stream_set_property (GObject *object,
91 guint prop_id,
92 const GValue *value,
93 GParamSpec *pspec)
95 GSocketInputStream *stream = G_SOCKET_INPUT_STREAM (object);
97 switch (prop_id)
99 case PROP_SOCKET:
100 stream->priv->socket = g_value_dup_object (value);
101 break;
103 default:
104 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
108 static void
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);
119 static gssize
120 g_socket_input_stream_read (GInputStream *stream,
121 void *buffer,
122 gsize count,
123 GCancellable *cancellable,
124 GError **error)
126 GSocketInputStream *input_stream = G_SOCKET_INPUT_STREAM (stream);
128 return g_socket_receive_with_blocking (input_stream->priv->socket,
129 buffer, count, TRUE,
130 cancellable, error);
133 static gboolean
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);
141 static GSource *
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;
158 static gssize
159 g_socket_input_stream_pollable_read_nonblocking (GPollableInputStream *pollable,
160 void *buffer,
161 gsize size,
162 GError **error)
164 GSocketInputStream *input_stream = G_SOCKET_INPUT_STREAM (pollable);
166 return g_socket_receive_with_blocking (input_stream->priv->socket,
167 buffer, size, FALSE,
168 NULL, error);
171 #ifdef G_OS_UNIX
172 static int
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);
179 #endif
181 static void
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",
195 P_("socket"),
196 P_("The socket that this stream wraps"),
197 G_TYPE_SOCKET, G_PARAM_CONSTRUCT_ONLY |
198 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
201 #ifdef G_OS_UNIX
202 static void
203 g_socket_input_stream_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface)
205 iface->get_fd = g_socket_input_stream_get_fd;
207 #endif
209 static void
210 g_socket_input_stream_pollable_iface_init (GPollableInputStreamInterface *iface)
212 iface->is_readable = g_socket_input_stream_pollable_is_readable;
213 iface->create_source = g_socket_input_stream_pollable_create_source;
214 iface->read_nonblocking = g_socket_input_stream_pollable_read_nonblocking;
217 static void
218 g_socket_input_stream_init (GSocketInputStream *stream)
220 stream->priv = g_socket_input_stream_get_instance_private (stream);
223 GSocketInputStream *
224 _g_socket_input_stream_new (GSocket *socket)
226 return g_object_new (G_TYPE_SOCKET_INPUT_STREAM, "socket", socket, NULL);