gio: use GPollable* to implement fallback read_async/write_async
[platform/upstream/glib.git] / gio / gsocketinputstream.c
1 /* GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright © 2008 Christian Kellner, Samuel Cormier-Iijima
4  *           © 2009 codethink
5  *
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.
10  *
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.
15  *
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.
20  *
21  * Authors: Christian Kellner <gicmo@gnome.org>
22  *          Samuel Cormier-Iijima <sciyoshi@gmail.com>
23  *          Ryan Lortie <desrt@desrt.ca>
24  */
25
26 #include "config.h"
27 #include "gsocketinputstream.h"
28 #include "glibintl.h"
29
30 #include "gsimpleasyncresult.h"
31 #include "gcancellable.h"
32 #include "gpollableinputstream.h"
33 #include "gioerror.h"
34 #include "gfiledescriptorbased.h"
35
36 static void g_socket_input_stream_pollable_iface_init (GPollableInputStreamInterface *iface);
37 #ifdef G_OS_UNIX
38 static void g_socket_input_stream_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface);
39 #endif
40
41 #define g_socket_input_stream_get_type _g_socket_input_stream_get_type
42
43 #ifdef G_OS_UNIX
44 G_DEFINE_TYPE_WITH_CODE (GSocketInputStream, g_socket_input_stream, G_TYPE_INPUT_STREAM,
45                          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)
47                          )
48 #else
49 G_DEFINE_TYPE_WITH_CODE (GSocketInputStream, g_socket_input_stream, G_TYPE_INPUT_STREAM,
50                          G_IMPLEMENT_INTERFACE (G_TYPE_POLLABLE_INPUT_STREAM, g_socket_input_stream_pollable_iface_init)
51                          )
52 #endif
53
54 enum
55 {
56   PROP_0,
57   PROP_SOCKET
58 };
59
60 struct _GSocketInputStreamPrivate
61 {
62   GSocket *socket;
63
64   /* pending operation metadata */
65   GSimpleAsyncResult *result;
66   GCancellable *cancellable;
67   gpointer buffer;
68   gsize count;
69 };
70
71 static void
72 g_socket_input_stream_get_property (GObject    *object,
73                                     guint       prop_id,
74                                     GValue     *value,
75                                     GParamSpec *pspec)
76 {
77   GSocketInputStream *stream = G_SOCKET_INPUT_STREAM (object);
78
79   switch (prop_id)
80     {
81       case PROP_SOCKET:
82         g_value_set_object (value, stream->priv->socket);
83         break;
84
85       default:
86         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
87     }
88 }
89
90 static void
91 g_socket_input_stream_set_property (GObject      *object,
92                                     guint         prop_id,
93                                     const GValue *value,
94                                     GParamSpec   *pspec)
95 {
96   GSocketInputStream *stream = G_SOCKET_INPUT_STREAM (object);
97
98   switch (prop_id)
99     {
100       case PROP_SOCKET:
101         stream->priv->socket = g_value_dup_object (value);
102         break;
103
104       default:
105         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
106     }
107 }
108
109 static void
110 g_socket_input_stream_finalize (GObject *object)
111 {
112   GSocketInputStream *stream = G_SOCKET_INPUT_STREAM (object);
113
114   if (stream->priv->socket)
115     g_object_unref (stream->priv->socket);
116
117   if (G_OBJECT_CLASS (g_socket_input_stream_parent_class)->finalize)
118     (*G_OBJECT_CLASS (g_socket_input_stream_parent_class)->finalize) (object);
119 }
120
121 static gssize
122 g_socket_input_stream_read (GInputStream  *stream,
123                             void          *buffer,
124                             gsize          count,
125                             GCancellable  *cancellable,
126                             GError       **error)
127 {
128   GSocketInputStream *input_stream = G_SOCKET_INPUT_STREAM (stream);
129
130   return g_socket_receive_with_blocking (input_stream->priv->socket,
131                                          buffer, count, TRUE,
132                                          cancellable, error);
133 }
134
135 static gboolean
136 g_socket_input_stream_pollable_is_readable (GPollableInputStream *pollable)
137 {
138   GSocketInputStream *input_stream = G_SOCKET_INPUT_STREAM (pollable);
139
140   return g_socket_condition_check (input_stream->priv->socket, G_IO_IN);
141 }
142
143 static GSource *
144 g_socket_input_stream_pollable_create_source (GPollableInputStream *pollable,
145                                               GCancellable         *cancellable)
146 {
147   GSocketInputStream *input_stream = G_SOCKET_INPUT_STREAM (pollable);
148   GSource *socket_source, *pollable_source;
149
150   pollable_source = g_pollable_source_new (G_OBJECT (input_stream));
151   socket_source = g_socket_create_source (input_stream->priv->socket,
152                                           G_IO_IN, cancellable);
153   g_source_set_dummy_callback (socket_source);
154   g_source_add_child_source (pollable_source, socket_source);
155   g_source_unref (socket_source);
156
157   return pollable_source;
158 }
159
160 static gssize
161 g_socket_input_stream_pollable_read_nonblocking (GPollableInputStream  *pollable,
162                                                  void                  *buffer,
163                                                  gsize                  size,
164                                                  GError               **error)
165 {
166   GSocketInputStream *input_stream = G_SOCKET_INPUT_STREAM (pollable);
167
168   return g_socket_receive_with_blocking (input_stream->priv->socket,
169                                          buffer, size, FALSE,
170                                          NULL, error);
171 }
172
173 #ifdef G_OS_UNIX
174 static int
175 g_socket_input_stream_get_fd (GFileDescriptorBased *fd_based)
176 {
177   GSocketInputStream *input_stream = G_SOCKET_INPUT_STREAM (fd_based);
178
179   return g_socket_get_fd (input_stream->priv->socket);
180 }
181 #endif
182
183 static void
184 g_socket_input_stream_class_init (GSocketInputStreamClass *klass)
185 {
186   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
187   GInputStreamClass *ginputstream_class = G_INPUT_STREAM_CLASS (klass);
188
189   g_type_class_add_private (klass, sizeof (GSocketInputStreamPrivate));
190
191   gobject_class->finalize = g_socket_input_stream_finalize;
192   gobject_class->get_property = g_socket_input_stream_get_property;
193   gobject_class->set_property = g_socket_input_stream_set_property;
194
195   ginputstream_class->read_fn = g_socket_input_stream_read;
196
197   g_object_class_install_property (gobject_class, PROP_SOCKET,
198                                    g_param_spec_object ("socket",
199                                                         P_("socket"),
200                                                         P_("The socket that this stream wraps"),
201                                                         G_TYPE_SOCKET, G_PARAM_CONSTRUCT_ONLY |
202                                                         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
203 }
204
205 #ifdef G_OS_UNIX
206 static void
207 g_socket_input_stream_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface)
208 {
209   iface->get_fd = g_socket_input_stream_get_fd;
210 }
211 #endif
212
213 static void
214 g_socket_input_stream_pollable_iface_init (GPollableInputStreamInterface *iface)
215 {
216   iface->is_readable = g_socket_input_stream_pollable_is_readable;
217   iface->create_source = g_socket_input_stream_pollable_create_source;
218   iface->read_nonblocking = g_socket_input_stream_pollable_read_nonblocking;
219 }
220
221 static void
222 g_socket_input_stream_init (GSocketInputStream *stream)
223 {
224   stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream, G_TYPE_SOCKET_INPUT_STREAM, GSocketInputStreamPrivate);
225 }
226
227 GSocketInputStream *
228 _g_socket_input_stream_new (GSocket *socket)
229 {
230   return G_SOCKET_INPUT_STREAM (g_object_new (G_TYPE_SOCKET_INPUT_STREAM, "socket", socket, NULL));
231 }