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