1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright © 2008 Christian Kellner, Samuel Cormier-Iijima
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>
27 #include "goutputstream.h"
28 #include "gsocketoutputstream.h"
32 #include "gcancellable.h"
33 #include "gpollableinputstream.h"
34 #include "gpollableoutputstream.h"
37 #include "gfiledescriptorbased.h"
39 struct _GSocketOutputStreamPrivate
43 /* pending operation metadata */
48 static void g_socket_output_stream_pollable_iface_init (GPollableOutputStreamInterface *iface);
50 static void g_socket_output_stream_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface);
53 #define g_socket_output_stream_get_type _g_socket_output_stream_get_type
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)
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)
75 g_socket_output_stream_get_property (GObject *object,
80 GSocketOutputStream *stream = G_SOCKET_OUTPUT_STREAM (object);
85 g_value_set_object (value, stream->priv->socket);
89 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
94 g_socket_output_stream_set_property (GObject *object,
99 GSocketOutputStream *stream = G_SOCKET_OUTPUT_STREAM (object);
104 stream->priv->socket = g_value_dup_object (value);
108 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
113 g_socket_output_stream_finalize (GObject *object)
115 GSocketOutputStream *stream = G_SOCKET_OUTPUT_STREAM (object);
117 if (stream->priv->socket)
118 g_object_unref (stream->priv->socket);
120 G_OBJECT_CLASS (g_socket_output_stream_parent_class)->finalize (object);
124 g_socket_output_stream_write (GOutputStream *stream,
127 GCancellable *cancellable,
130 GSocketOutputStream *onput_stream = G_SOCKET_OUTPUT_STREAM (stream);
132 return g_socket_send_with_blocking (onput_stream->priv->socket,
138 g_socket_output_stream_pollable_is_writable (GPollableOutputStream *pollable)
140 GSocketOutputStream *output_stream = G_SOCKET_OUTPUT_STREAM (pollable);
142 return g_socket_condition_check (output_stream->priv->socket, G_IO_OUT);
146 g_socket_output_stream_pollable_write_nonblocking (GPollableOutputStream *pollable,
151 GSocketOutputStream *output_stream = G_SOCKET_OUTPUT_STREAM (pollable);
153 return g_socket_send_with_blocking (output_stream->priv->socket,
159 g_socket_output_stream_pollable_create_source (GPollableOutputStream *pollable,
160 GCancellable *cancellable)
162 GSocketOutputStream *output_stream = G_SOCKET_OUTPUT_STREAM (pollable);
163 GSource *socket_source, *pollable_source;
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);
172 return pollable_source;
177 g_socket_output_stream_get_fd (GFileDescriptorBased *fd_based)
179 GSocketOutputStream *output_stream = G_SOCKET_OUTPUT_STREAM (fd_based);
181 return g_socket_get_fd (output_stream->priv->socket);
186 g_socket_output_stream_class_init (GSocketOutputStreamClass *klass)
188 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
189 GOutputStreamClass *goutputstream_class = G_OUTPUT_STREAM_CLASS (klass);
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;
195 goutputstream_class->write_fn = g_socket_output_stream_write;
197 g_object_class_install_property (gobject_class, PROP_SOCKET,
198 g_param_spec_object ("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));
207 g_socket_output_stream_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface)
209 iface->get_fd = g_socket_output_stream_get_fd;
214 g_socket_output_stream_pollable_iface_init (GPollableOutputStreamInterface *iface)
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;
222 g_socket_output_stream_init (GSocketOutputStream *stream)
224 stream->priv = g_socket_output_stream_get_instance_private (stream);
227 GSocketOutputStream *
228 _g_socket_output_stream_new (GSocket *socket)
230 return g_object_new (G_TYPE_SOCKET_OUTPUT_STREAM, "socket", socket, NULL);