1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright © 2008 Christian Kellner, Samuel Cormier-Iijima
6 * SPDX-License-Identifier: LGPL-2.1-or-later
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General
19 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
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"
38 #include "gioprivate.h"
40 struct _GSocketOutputStreamPrivate
44 /* pending operation metadata */
49 static void g_socket_output_stream_pollable_iface_init (GPollableOutputStreamInterface *iface);
51 static void g_socket_output_stream_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface);
54 #define g_socket_output_stream_get_type _g_socket_output_stream_get_type
57 G_DEFINE_TYPE_WITH_CODE (GSocketOutputStream, g_socket_output_stream, G_TYPE_OUTPUT_STREAM,
58 G_ADD_PRIVATE (GSocketOutputStream)
59 G_IMPLEMENT_INTERFACE (G_TYPE_POLLABLE_OUTPUT_STREAM, g_socket_output_stream_pollable_iface_init)
60 G_IMPLEMENT_INTERFACE (G_TYPE_FILE_DESCRIPTOR_BASED, g_socket_output_stream_file_descriptor_based_iface_init)
63 G_DEFINE_TYPE_WITH_CODE (GSocketOutputStream, g_socket_output_stream, G_TYPE_OUTPUT_STREAM,
64 G_ADD_PRIVATE (GSocketOutputStream)
65 G_IMPLEMENT_INTERFACE (G_TYPE_POLLABLE_OUTPUT_STREAM, g_socket_output_stream_pollable_iface_init)
76 g_socket_output_stream_get_property (GObject *object,
81 GSocketOutputStream *stream = G_SOCKET_OUTPUT_STREAM (object);
86 g_value_set_object (value, stream->priv->socket);
90 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
95 g_socket_output_stream_set_property (GObject *object,
100 GSocketOutputStream *stream = G_SOCKET_OUTPUT_STREAM (object);
105 stream->priv->socket = g_value_dup_object (value);
109 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
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 G_OBJECT_CLASS (g_socket_output_stream_parent_class)->finalize (object);
125 g_socket_output_stream_write (GOutputStream *stream,
128 GCancellable *cancellable,
131 GSocketOutputStream *output_stream = G_SOCKET_OUTPUT_STREAM (stream);
133 return g_socket_send_with_blocking (output_stream->priv->socket,
139 g_socket_output_stream_writev (GOutputStream *stream,
140 const GOutputVector *vectors,
142 gsize *bytes_written,
143 GCancellable *cancellable,
146 GSocketOutputStream *output_stream = G_SOCKET_OUTPUT_STREAM (stream);
149 /* Clamp the number of vectors if more given than we can write in one go.
150 * The caller has to handle short writes anyway.
152 if (n_vectors > G_IOV_MAX)
153 n_vectors = G_IOV_MAX;
155 res = g_socket_send_message_with_timeout (output_stream->priv->socket, NULL,
157 NULL, 0, G_SOCKET_MSG_NONE,
161 /* we have a non-zero timeout so this can't happen */
162 g_assert (res != G_POLLABLE_RETURN_WOULD_BLOCK);
164 return res == G_POLLABLE_RETURN_OK;
168 g_socket_output_stream_pollable_is_writable (GPollableOutputStream *pollable)
170 GSocketOutputStream *output_stream = G_SOCKET_OUTPUT_STREAM (pollable);
172 return g_socket_condition_check (output_stream->priv->socket, G_IO_OUT);
176 g_socket_output_stream_pollable_write_nonblocking (GPollableOutputStream *pollable,
181 GSocketOutputStream *output_stream = G_SOCKET_OUTPUT_STREAM (pollable);
183 return g_socket_send_with_blocking (output_stream->priv->socket,
188 static GPollableReturn
189 g_socket_output_stream_pollable_writev_nonblocking (GPollableOutputStream *pollable,
190 const GOutputVector *vectors,
192 gsize *bytes_written,
195 GSocketOutputStream *output_stream = G_SOCKET_OUTPUT_STREAM (pollable);
197 /* Clamp the number of vectors if more given than we can write in one go.
198 * The caller has to handle short writes anyway.
200 if (n_vectors > G_IOV_MAX)
201 n_vectors = G_IOV_MAX;
203 return g_socket_send_message_with_timeout (output_stream->priv->socket,
204 NULL, vectors, n_vectors,
205 NULL, 0, G_SOCKET_MSG_NONE, 0,
206 bytes_written, NULL, error);
210 g_socket_output_stream_pollable_create_source (GPollableOutputStream *pollable,
211 GCancellable *cancellable)
213 GSocketOutputStream *output_stream = G_SOCKET_OUTPUT_STREAM (pollable);
214 GSource *socket_source, *pollable_source;
216 pollable_source = g_pollable_source_new (G_OBJECT (output_stream));
217 socket_source = g_socket_create_source (output_stream->priv->socket,
218 G_IO_OUT, cancellable);
219 g_source_set_dummy_callback (socket_source);
220 g_source_add_child_source (pollable_source, socket_source);
221 g_source_unref (socket_source);
223 return pollable_source;
228 g_socket_output_stream_get_fd (GFileDescriptorBased *fd_based)
230 GSocketOutputStream *output_stream = G_SOCKET_OUTPUT_STREAM (fd_based);
232 return g_socket_get_fd (output_stream->priv->socket);
237 g_socket_output_stream_class_init (GSocketOutputStreamClass *klass)
239 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
240 GOutputStreamClass *goutputstream_class = G_OUTPUT_STREAM_CLASS (klass);
242 gobject_class->finalize = g_socket_output_stream_finalize;
243 gobject_class->get_property = g_socket_output_stream_get_property;
244 gobject_class->set_property = g_socket_output_stream_set_property;
246 goutputstream_class->write_fn = g_socket_output_stream_write;
247 goutputstream_class->writev_fn = g_socket_output_stream_writev;
249 g_object_class_install_property (gobject_class, PROP_SOCKET,
250 g_param_spec_object ("socket", NULL, NULL,
251 G_TYPE_SOCKET, G_PARAM_CONSTRUCT_ONLY |
252 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
257 g_socket_output_stream_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface)
259 iface->get_fd = g_socket_output_stream_get_fd;
264 g_socket_output_stream_pollable_iface_init (GPollableOutputStreamInterface *iface)
266 iface->is_writable = g_socket_output_stream_pollable_is_writable;
267 iface->create_source = g_socket_output_stream_pollable_create_source;
268 iface->write_nonblocking = g_socket_output_stream_pollable_write_nonblocking;
269 iface->writev_nonblocking = g_socket_output_stream_pollable_writev_nonblocking;
273 g_socket_output_stream_init (GSocketOutputStream *stream)
275 stream->priv = g_socket_output_stream_get_instance_private (stream);
278 GSocketOutputStream *
279 _g_socket_output_stream_new (GSocket *socket)
281 return g_object_new (G_TYPE_SOCKET_OUTPUT_STREAM, "socket", socket, NULL);