gio/: fully remove gioalias hacks
[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
31 #include <gio/gsimpleasyncresult.h>
32 #include <gio/gcancellable.h>
33 #include <gio/gioerror.h>
34 #include "glibintl.h"
35
36
37 #define g_socket_output_stream_get_type _g_socket_output_stream_get_type
38 G_DEFINE_TYPE (GSocketOutputStream, g_socket_output_stream, G_TYPE_OUTPUT_STREAM);
39
40 enum
41 {
42   PROP_0,
43   PROP_SOCKET
44 };
45
46 struct _GSocketOutputStreamPrivate
47 {
48   GSocket *socket;
49
50   /* pending operation metadata */
51   GSimpleAsyncResult *result;
52   GCancellable *cancellable;
53   gconstpointer buffer;
54   gsize count;
55 };
56
57 static void
58 g_socket_output_stream_get_property (GObject    *object,
59                                      guint       prop_id,
60                                      GValue     *value,
61                                      GParamSpec *pspec)
62 {
63   GSocketOutputStream *stream = G_SOCKET_OUTPUT_STREAM (object);
64
65   switch (prop_id)
66     {
67       case PROP_SOCKET:
68         g_value_set_object (value, stream->priv->socket);
69         break;
70
71       default:
72         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
73     }
74 }
75
76 static void
77 g_socket_output_stream_set_property (GObject      *object,
78                                      guint         prop_id,
79                                      const GValue *value,
80                                      GParamSpec   *pspec)
81 {
82   GSocketOutputStream *stream = G_SOCKET_OUTPUT_STREAM (object);
83
84   switch (prop_id)
85     {
86       case PROP_SOCKET:
87         stream->priv->socket = g_value_dup_object (value);
88         break;
89
90       default:
91         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
92     }
93 }
94
95 static void
96 g_socket_output_stream_finalize (GObject *object)
97 {
98   GSocketOutputStream *stream = G_SOCKET_OUTPUT_STREAM (object);
99
100   if (stream->priv->socket)
101     g_object_unref (stream->priv->socket);
102
103   if (G_OBJECT_CLASS (g_socket_output_stream_parent_class)->finalize)
104     (*G_OBJECT_CLASS (g_socket_output_stream_parent_class)->finalize) (object);
105 }
106
107 static gssize
108 g_socket_output_stream_write (GOutputStream  *stream,
109                               const void     *buffer,
110                               gsize           count,
111                               GCancellable   *cancellable,
112                               GError        **error)
113 {
114   GSocketOutputStream *onput_stream = G_SOCKET_OUTPUT_STREAM (stream);
115
116   return g_socket_send (onput_stream->priv->socket, buffer, count,
117                         cancellable, error);
118 }
119
120 static gboolean
121 g_socket_output_stream_write_ready (GSocket *socket,
122                                     GIOCondition condition,
123                                     GSocketOutputStream *stream)
124 {
125   GSimpleAsyncResult *simple;
126   GError *error = NULL;
127   gssize result;
128
129   result = g_socket_send (stream->priv->socket,
130                           stream->priv->buffer,
131                           stream->priv->count,
132                           stream->priv->cancellable,
133                           &error);
134
135   if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK))
136     return TRUE;
137
138   simple = stream->priv->result;
139   stream->priv->result = NULL;
140
141   if (result >= 0)
142     g_simple_async_result_set_op_res_gssize (simple, result);
143
144   if (error)
145     {
146       g_simple_async_result_set_from_error (simple, error);
147       g_error_free (error);
148     }
149
150   if (stream->priv->cancellable)
151     g_object_unref (stream->priv->cancellable);
152
153   g_simple_async_result_complete (simple);
154   g_object_unref (simple);
155
156   return FALSE;
157 }
158
159 static void
160 g_socket_output_stream_write_async (GOutputStream        *stream,
161                                     const void           *buffer,
162                                     gsize                 count,
163                                     gint                  io_priority,
164                                     GCancellable         *cancellable,
165                                     GAsyncReadyCallback   callback,
166                                     gpointer              user_data)
167 {
168   GSocketOutputStream *output_stream = G_SOCKET_OUTPUT_STREAM (stream);
169   GSource *source;
170
171   g_assert (output_stream->priv->result == NULL);
172
173   output_stream->priv->result =
174     g_simple_async_result_new (G_OBJECT (stream), callback, user_data,
175                                g_socket_output_stream_write_async);
176   if (cancellable)
177     g_object_ref (cancellable);
178   output_stream->priv->cancellable = cancellable;
179   output_stream->priv->buffer = buffer;
180   output_stream->priv->count = count;
181
182   source = g_socket_create_source (output_stream->priv->socket,
183                                    G_IO_OUT | G_IO_HUP | G_IO_ERR,
184                                    cancellable);
185   g_source_set_callback (source,
186                          (GSourceFunc) g_socket_output_stream_write_ready,
187                          g_object_ref (output_stream), g_object_unref);
188   g_source_attach (source, g_main_context_get_thread_default ());
189   g_source_unref (source);
190 }
191
192 static gssize
193 g_socket_output_stream_write_finish (GOutputStream  *stream,
194                                      GAsyncResult   *result,
195                                      GError        **error)
196 {
197   GSimpleAsyncResult *simple;
198   gssize count;
199
200   g_return_val_if_fail (G_IS_SOCKET_OUTPUT_STREAM (stream), -1);
201
202   simple = G_SIMPLE_ASYNC_RESULT (result);
203
204   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_socket_output_stream_write_async);
205
206   count = g_simple_async_result_get_op_res_gssize (simple);
207
208   return count;
209 }
210
211 static void
212 g_socket_output_stream_class_init (GSocketOutputStreamClass *klass)
213 {
214   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
215   GOutputStreamClass *goutputstream_class = G_OUTPUT_STREAM_CLASS (klass);
216
217   g_type_class_add_private (klass, sizeof (GSocketOutputStreamPrivate));
218
219   gobject_class->finalize = g_socket_output_stream_finalize;
220   gobject_class->get_property = g_socket_output_stream_get_property;
221   gobject_class->set_property = g_socket_output_stream_set_property;
222
223   goutputstream_class->write_fn = g_socket_output_stream_write;
224   goutputstream_class->write_async = g_socket_output_stream_write_async;
225   goutputstream_class->write_finish = g_socket_output_stream_write_finish;
226
227   g_object_class_install_property (gobject_class, PROP_SOCKET,
228                                    g_param_spec_object ("socket",
229                                                         P_("socket"),
230                                                         P_("The socket that this stream wraps"),
231                                                         G_TYPE_SOCKET, G_PARAM_CONSTRUCT_ONLY |
232                                                         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
233 }
234
235 static void
236 g_socket_output_stream_init (GSocketOutputStream *stream)
237 {
238   stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream, G_TYPE_SOCKET_OUTPUT_STREAM, GSocketOutputStreamPrivate);
239 }
240
241 GSocketOutputStream *
242 _g_socket_output_stream_new (GSocket *socket)
243 {
244   return G_SOCKET_OUTPUT_STREAM (g_object_new (G_TYPE_SOCKET_OUTPUT_STREAM, "socket", socket, NULL));
245 }