It is 'registered', not 'registred'
[glib.git] / gio / gsocketoutputstream.c
blob145f0098693f3ee32358f7b96cbf6bffb59c7c71
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 "goutputstream.h"
28 #include "gsocketoutputstream.h"
29 #include "gsocket.h"
30 #include "glibintl.h"
32 #include "gsimpleasyncresult.h"
33 #include "gcancellable.h"
34 #include "gpollableinputstream.h"
35 #include "gpollableoutputstream.h"
36 #include "gioerror.h"
37 #include "glibintl.h"
38 #include "gfiledescriptorbased.h"
40 static void g_socket_output_stream_pollable_iface_init (GPollableOutputStreamInterface *iface);
41 #ifdef G_OS_UNIX
42 static void g_socket_output_stream_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface);
43 #endif
45 #define g_socket_output_stream_get_type _g_socket_output_stream_get_type
47 #ifdef G_OS_UNIX
48 G_DEFINE_TYPE_WITH_CODE (GSocketOutputStream, g_socket_output_stream, G_TYPE_OUTPUT_STREAM,
49 G_IMPLEMENT_INTERFACE (G_TYPE_POLLABLE_OUTPUT_STREAM, g_socket_output_stream_pollable_iface_init)
50 G_IMPLEMENT_INTERFACE (G_TYPE_FILE_DESCRIPTOR_BASED, g_socket_output_stream_file_descriptor_based_iface_init)
52 #else
53 G_DEFINE_TYPE_WITH_CODE (GSocketOutputStream, g_socket_output_stream, G_TYPE_OUTPUT_STREAM,
54 G_IMPLEMENT_INTERFACE (G_TYPE_POLLABLE_OUTPUT_STREAM, g_socket_output_stream_pollable_iface_init)
56 #endif
58 enum
60 PROP_0,
61 PROP_SOCKET
64 struct _GSocketOutputStreamPrivate
66 GSocket *socket;
68 /* pending operation metadata */
69 GSimpleAsyncResult *result;
70 GCancellable *cancellable;
71 gconstpointer buffer;
72 gsize count;
75 static void
76 g_socket_output_stream_get_property (GObject *object,
77 guint prop_id,
78 GValue *value,
79 GParamSpec *pspec)
81 GSocketOutputStream *stream = G_SOCKET_OUTPUT_STREAM (object);
83 switch (prop_id)
85 case PROP_SOCKET:
86 g_value_set_object (value, stream->priv->socket);
87 break;
89 default:
90 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
94 static void
95 g_socket_output_stream_set_property (GObject *object,
96 guint prop_id,
97 const GValue *value,
98 GParamSpec *pspec)
100 GSocketOutputStream *stream = G_SOCKET_OUTPUT_STREAM (object);
102 switch (prop_id)
104 case PROP_SOCKET:
105 stream->priv->socket = g_value_dup_object (value);
106 break;
108 default:
109 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
113 static void
114 g_socket_output_stream_finalize (GObject *object)
116 GSocketOutputStream *stream = G_SOCKET_OUTPUT_STREAM (object);
118 if (stream->priv->socket)
119 g_object_unref (stream->priv->socket);
121 if (G_OBJECT_CLASS (g_socket_output_stream_parent_class)->finalize)
122 (*G_OBJECT_CLASS (g_socket_output_stream_parent_class)->finalize) (object);
125 static gssize
126 g_socket_output_stream_write (GOutputStream *stream,
127 const void *buffer,
128 gsize count,
129 GCancellable *cancellable,
130 GError **error)
132 GSocketOutputStream *onput_stream = G_SOCKET_OUTPUT_STREAM (stream);
134 return g_socket_send_with_blocking (onput_stream->priv->socket,
135 buffer, count, TRUE,
136 cancellable, error);
139 static gboolean
140 g_socket_output_stream_pollable_is_writable (GPollableOutputStream *pollable)
142 GSocketOutputStream *output_stream = G_SOCKET_OUTPUT_STREAM (pollable);
144 return g_socket_condition_check (output_stream->priv->socket, G_IO_OUT);
147 static gssize
148 g_socket_output_stream_pollable_write_nonblocking (GPollableOutputStream *pollable,
149 const void *buffer,
150 gsize size,
151 GError **error)
153 GSocketOutputStream *output_stream = G_SOCKET_OUTPUT_STREAM (pollable);
155 return g_socket_send_with_blocking (output_stream->priv->socket,
156 buffer, size, FALSE,
157 NULL, error);
160 static GSource *
161 g_socket_output_stream_pollable_create_source (GPollableOutputStream *pollable,
162 GCancellable *cancellable)
164 GSocketOutputStream *output_stream = G_SOCKET_OUTPUT_STREAM (pollable);
165 GSource *socket_source, *pollable_source;
167 pollable_source = g_pollable_source_new (G_OBJECT (output_stream));
168 socket_source = g_socket_create_source (output_stream->priv->socket,
169 G_IO_OUT, cancellable);
170 g_source_set_dummy_callback (socket_source);
171 g_source_add_child_source (pollable_source, socket_source);
172 g_source_unref (socket_source);
174 return pollable_source;
177 #ifdef G_OS_UNIX
178 static int
179 g_socket_output_stream_get_fd (GFileDescriptorBased *fd_based)
181 GSocketOutputStream *output_stream = G_SOCKET_OUTPUT_STREAM (fd_based);
183 return g_socket_get_fd (output_stream->priv->socket);
185 #endif
187 static void
188 g_socket_output_stream_class_init (GSocketOutputStreamClass *klass)
190 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
191 GOutputStreamClass *goutputstream_class = G_OUTPUT_STREAM_CLASS (klass);
193 g_type_class_add_private (klass, sizeof (GSocketOutputStreamPrivate));
195 gobject_class->finalize = g_socket_output_stream_finalize;
196 gobject_class->get_property = g_socket_output_stream_get_property;
197 gobject_class->set_property = g_socket_output_stream_set_property;
199 goutputstream_class->write_fn = g_socket_output_stream_write;
201 g_object_class_install_property (gobject_class, PROP_SOCKET,
202 g_param_spec_object ("socket",
203 P_("socket"),
204 P_("The socket that this stream wraps"),
205 G_TYPE_SOCKET, G_PARAM_CONSTRUCT_ONLY |
206 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
209 #ifdef G_OS_UNIX
210 static void
211 g_socket_output_stream_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface)
213 iface->get_fd = g_socket_output_stream_get_fd;
215 #endif
217 static void
218 g_socket_output_stream_pollable_iface_init (GPollableOutputStreamInterface *iface)
220 iface->is_writable = g_socket_output_stream_pollable_is_writable;
221 iface->create_source = g_socket_output_stream_pollable_create_source;
222 iface->write_nonblocking = g_socket_output_stream_pollable_write_nonblocking;
225 static void
226 g_socket_output_stream_init (GSocketOutputStream *stream)
228 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream, G_TYPE_SOCKET_OUTPUT_STREAM, GSocketOutputStreamPrivate);
231 GSocketOutputStream *
232 _g_socket_output_stream_new (GSocket *socket)
234 return G_SOCKET_OUTPUT_STREAM (g_object_new (G_TYPE_SOCKET_OUTPUT_STREAM, "socket", socket, NULL));