gio: port networking classes from GSimpleAsyncResult to GTask
[platform/upstream/glib.git] / gio / gsocketoutputstream.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 "goutputstream.h"
28 #include "gsocketoutputstream.h"
29 #include "gsocket.h"
30 #include "glibintl.h"
31
32 #include "gcancellable.h"
33 #include "gpollableinputstream.h"
34 #include "gpollableoutputstream.h"
35 #include "gioerror.h"
36 #include "glibintl.h"
37 #include "gfiledescriptorbased.h"
38
39 static void g_socket_output_stream_pollable_iface_init (GPollableOutputStreamInterface *iface);
40 #ifdef G_OS_UNIX
41 static void g_socket_output_stream_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface);
42 #endif
43
44 #define g_socket_output_stream_get_type _g_socket_output_stream_get_type
45
46 #ifdef G_OS_UNIX
47 G_DEFINE_TYPE_WITH_CODE (GSocketOutputStream, g_socket_output_stream, G_TYPE_OUTPUT_STREAM,
48                          G_IMPLEMENT_INTERFACE (G_TYPE_POLLABLE_OUTPUT_STREAM, g_socket_output_stream_pollable_iface_init)
49                          G_IMPLEMENT_INTERFACE (G_TYPE_FILE_DESCRIPTOR_BASED, g_socket_output_stream_file_descriptor_based_iface_init)
50                          )
51 #else
52 G_DEFINE_TYPE_WITH_CODE (GSocketOutputStream, g_socket_output_stream, G_TYPE_OUTPUT_STREAM,
53                          G_IMPLEMENT_INTERFACE (G_TYPE_POLLABLE_OUTPUT_STREAM, g_socket_output_stream_pollable_iface_init)
54                          )
55 #endif
56
57 enum
58 {
59   PROP_0,
60   PROP_SOCKET
61 };
62
63 struct _GSocketOutputStreamPrivate
64 {
65   GSocket *socket;
66
67   /* pending operation metadata */
68   gconstpointer buffer;
69   gsize count;
70 };
71
72 static void
73 g_socket_output_stream_get_property (GObject    *object,
74                                      guint       prop_id,
75                                      GValue     *value,
76                                      GParamSpec *pspec)
77 {
78   GSocketOutputStream *stream = G_SOCKET_OUTPUT_STREAM (object);
79
80   switch (prop_id)
81     {
82       case PROP_SOCKET:
83         g_value_set_object (value, stream->priv->socket);
84         break;
85
86       default:
87         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
88     }
89 }
90
91 static void
92 g_socket_output_stream_set_property (GObject      *object,
93                                      guint         prop_id,
94                                      const GValue *value,
95                                      GParamSpec   *pspec)
96 {
97   GSocketOutputStream *stream = G_SOCKET_OUTPUT_STREAM (object);
98
99   switch (prop_id)
100     {
101       case PROP_SOCKET:
102         stream->priv->socket = g_value_dup_object (value);
103         break;
104
105       default:
106         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
107     }
108 }
109
110 static void
111 g_socket_output_stream_finalize (GObject *object)
112 {
113   GSocketOutputStream *stream = G_SOCKET_OUTPUT_STREAM (object);
114
115   if (stream->priv->socket)
116     g_object_unref (stream->priv->socket);
117
118   if (G_OBJECT_CLASS (g_socket_output_stream_parent_class)->finalize)
119     (*G_OBJECT_CLASS (g_socket_output_stream_parent_class)->finalize) (object);
120 }
121
122 static gssize
123 g_socket_output_stream_write (GOutputStream  *stream,
124                               const void     *buffer,
125                               gsize           count,
126                               GCancellable   *cancellable,
127                               GError        **error)
128 {
129   GSocketOutputStream *onput_stream = G_SOCKET_OUTPUT_STREAM (stream);
130
131   return g_socket_send_with_blocking (onput_stream->priv->socket,
132                                       buffer, count, TRUE,
133                                       cancellable, error);
134 }
135
136 static gboolean
137 g_socket_output_stream_pollable_is_writable (GPollableOutputStream *pollable)
138 {
139   GSocketOutputStream *output_stream = G_SOCKET_OUTPUT_STREAM (pollable);
140
141   return g_socket_condition_check (output_stream->priv->socket, G_IO_OUT);
142 }
143
144 static gssize
145 g_socket_output_stream_pollable_write_nonblocking (GPollableOutputStream  *pollable,
146                                                    const void             *buffer,
147                                                    gsize                   size,
148                                                    GError                **error)
149 {
150   GSocketOutputStream *output_stream = G_SOCKET_OUTPUT_STREAM (pollable);
151
152   return g_socket_send_with_blocking (output_stream->priv->socket,
153                                       buffer, size, FALSE,
154                                       NULL, error);
155 }
156
157 static GSource *
158 g_socket_output_stream_pollable_create_source (GPollableOutputStream *pollable,
159                                                GCancellable          *cancellable)
160 {
161   GSocketOutputStream *output_stream = G_SOCKET_OUTPUT_STREAM (pollable);
162   GSource *socket_source, *pollable_source;
163
164   pollable_source = g_pollable_source_new (G_OBJECT (output_stream));
165   socket_source = g_socket_create_source (output_stream->priv->socket,
166                                           G_IO_OUT, cancellable);
167   g_source_set_dummy_callback (socket_source);
168   g_source_add_child_source (pollable_source, socket_source);
169   g_source_unref (socket_source);
170
171   return pollable_source;
172 }
173
174 #ifdef G_OS_UNIX
175 static int
176 g_socket_output_stream_get_fd (GFileDescriptorBased *fd_based)
177 {
178   GSocketOutputStream *output_stream = G_SOCKET_OUTPUT_STREAM (fd_based);
179
180   return g_socket_get_fd (output_stream->priv->socket);
181 }
182 #endif
183
184 static void
185 g_socket_output_stream_class_init (GSocketOutputStreamClass *klass)
186 {
187   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
188   GOutputStreamClass *goutputstream_class = G_OUTPUT_STREAM_CLASS (klass);
189
190   g_type_class_add_private (klass, sizeof (GSocketOutputStreamPrivate));
191
192   gobject_class->finalize = g_socket_output_stream_finalize;
193   gobject_class->get_property = g_socket_output_stream_get_property;
194   gobject_class->set_property = g_socket_output_stream_set_property;
195
196   goutputstream_class->write_fn = g_socket_output_stream_write;
197
198   g_object_class_install_property (gobject_class, PROP_SOCKET,
199                                    g_param_spec_object ("socket",
200                                                         P_("socket"),
201                                                         P_("The socket that this stream wraps"),
202                                                         G_TYPE_SOCKET, G_PARAM_CONSTRUCT_ONLY |
203                                                         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
204 }
205
206 #ifdef G_OS_UNIX
207 static void
208 g_socket_output_stream_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface)
209 {
210   iface->get_fd = g_socket_output_stream_get_fd;
211 }
212 #endif
213
214 static void
215 g_socket_output_stream_pollable_iface_init (GPollableOutputStreamInterface *iface)
216 {
217   iface->is_writable = g_socket_output_stream_pollable_is_writable;
218   iface->create_source = g_socket_output_stream_pollable_create_source;
219   iface->write_nonblocking = g_socket_output_stream_pollable_write_nonblocking;
220 }
221
222 static void
223 g_socket_output_stream_init (GSocketOutputStream *stream)
224 {
225   stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream, G_TYPE_SOCKET_OUTPUT_STREAM, GSocketOutputStreamPrivate);
226 }
227
228 GSocketOutputStream *
229 _g_socket_output_stream_new (GSocket *socket)
230 {
231   return G_SOCKET_OUTPUT_STREAM (g_object_new (G_TYPE_SOCKET_OUTPUT_STREAM, "socket", socket, NULL));
232 }