/src/glib/gio/gsocketoutputstream.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /*  GIO - GLib Input, Output and Streaming Library  | 
2  |  |  *  | 
3  |  |  * Copyright © 2008 Christian Kellner, Samuel Cormier-Iijima  | 
4  |  |  *           © 2009 codethink  | 
5  |  |  *  | 
6  |  |  * SPDX-License-Identifier: LGPL-2.1-or-later  | 
7  |  |  *  | 
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.  | 
12  |  |  *  | 
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.  | 
17  |  |  *  | 
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/>.  | 
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  |  | #include "gioprivate.h"  | 
39  |  |  | 
40  |  | struct _GSocketOutputStreamPrivate  | 
41  |  | { | 
42  |  |   GSocket *socket;  | 
43  |  |  | 
44  |  |   /* pending operation metadata */  | 
45  |  |   gconstpointer buffer;  | 
46  |  |   gsize count;  | 
47  |  | };  | 
48  |  |  | 
49  |  | static void g_socket_output_stream_pollable_iface_init (GPollableOutputStreamInterface *iface);  | 
50  |  | #ifdef G_OS_UNIX  | 
51  |  | static void g_socket_output_stream_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface);  | 
52  |  | #endif  | 
53  |  |  | 
54  |  | #define g_socket_output_stream_get_type _g_socket_output_stream_get_type  | 
55  |  |  | 
56  |  | #ifdef G_OS_UNIX  | 
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)  | 
61  |  |        )  | 
62  |  | #else  | 
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)  | 
66  |  |        )  | 
67  |  | #endif  | 
68  |  |  | 
69  |  | enum  | 
70  |  | { | 
71  |  |   PROP_0,  | 
72  |  |   PROP_SOCKET  | 
73  |  | };  | 
74  |  |  | 
75  |  | static void  | 
76  |  | g_socket_output_stream_get_property (GObject    *object,  | 
77  |  |                                      guint       prop_id,  | 
78  |  |                                      GValue     *value,  | 
79  |  |                                      GParamSpec *pspec)  | 
80  | 0  | { | 
81  | 0  |   GSocketOutputStream *stream = G_SOCKET_OUTPUT_STREAM (object);  | 
82  |  | 
  | 
83  | 0  |   switch (prop_id)  | 
84  | 0  |     { | 
85  | 0  |       case PROP_SOCKET:  | 
86  | 0  |         g_value_set_object (value, stream->priv->socket);  | 
87  | 0  |         break;  | 
88  |  |  | 
89  | 0  |       default:  | 
90  | 0  |         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);  | 
91  | 0  |     }  | 
92  | 0  | }  | 
93  |  |  | 
94  |  | static void  | 
95  |  | g_socket_output_stream_set_property (GObject      *object,  | 
96  |  |                                      guint         prop_id,  | 
97  |  |                                      const GValue *value,  | 
98  |  |                                      GParamSpec   *pspec)  | 
99  | 0  | { | 
100  | 0  |   GSocketOutputStream *stream = G_SOCKET_OUTPUT_STREAM (object);  | 
101  |  | 
  | 
102  | 0  |   switch (prop_id)  | 
103  | 0  |     { | 
104  | 0  |       case PROP_SOCKET:  | 
105  | 0  |         stream->priv->socket = g_value_dup_object (value);  | 
106  | 0  |         break;  | 
107  |  |  | 
108  | 0  |       default:  | 
109  | 0  |         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);  | 
110  | 0  |     }  | 
111  | 0  | }  | 
112  |  |  | 
113  |  | static void  | 
114  |  | g_socket_output_stream_finalize (GObject *object)  | 
115  | 0  | { | 
116  | 0  |   GSocketOutputStream *stream = G_SOCKET_OUTPUT_STREAM (object);  | 
117  |  | 
  | 
118  | 0  |   if (stream->priv->socket)  | 
119  | 0  |     g_object_unref (stream->priv->socket);  | 
120  |  | 
  | 
121  | 0  |   G_OBJECT_CLASS (g_socket_output_stream_parent_class)->finalize (object);  | 
122  | 0  | }  | 
123  |  |  | 
124  |  | static gssize  | 
125  |  | g_socket_output_stream_write (GOutputStream  *stream,  | 
126  |  |                               const void     *buffer,  | 
127  |  |                               gsize           count,  | 
128  |  |                               GCancellable   *cancellable,  | 
129  |  |                               GError        **error)  | 
130  | 0  | { | 
131  | 0  |   GSocketOutputStream *output_stream = G_SOCKET_OUTPUT_STREAM (stream);  | 
132  |  | 
  | 
133  | 0  |   return g_socket_send_with_blocking (output_stream->priv->socket,  | 
134  | 0  |               buffer, count, TRUE,  | 
135  | 0  |               cancellable, error);  | 
136  | 0  | }  | 
137  |  |  | 
138  |  | static gboolean  | 
139  |  | g_socket_output_stream_writev (GOutputStream        *stream,  | 
140  |  |                                const GOutputVector  *vectors,  | 
141  |  |                                gsize                 n_vectors,  | 
142  |  |                                gsize                *bytes_written,  | 
143  |  |                                GCancellable         *cancellable,  | 
144  |  |                                GError              **error)  | 
145  | 0  | { | 
146  | 0  |   GSocketOutputStream *output_stream = G_SOCKET_OUTPUT_STREAM (stream);  | 
147  | 0  |   GPollableReturn res;  | 
148  |  |  | 
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.  | 
151  |  |    */  | 
152  | 0  |   if (n_vectors > G_IOV_MAX)  | 
153  | 0  |     n_vectors = G_IOV_MAX;  | 
154  |  | 
  | 
155  | 0  |   res = g_socket_send_message_with_timeout (output_stream->priv->socket, NULL,  | 
156  | 0  |                                             vectors, n_vectors,  | 
157  | 0  |                                             NULL, 0, G_SOCKET_MSG_NONE,  | 
158  | 0  |                                             -1, bytes_written,  | 
159  | 0  |                                             cancellable, error);  | 
160  |  |  | 
161  |  |   /* we have a non-zero timeout so this can't happen */  | 
162  | 0  |   g_assert (res != G_POLLABLE_RETURN_WOULD_BLOCK);  | 
163  |  |  | 
164  | 0  |   return res == G_POLLABLE_RETURN_OK;  | 
165  | 0  | }  | 
166  |  |  | 
167  |  | static gboolean  | 
168  |  | g_socket_output_stream_pollable_is_writable (GPollableOutputStream *pollable)  | 
169  | 0  | { | 
170  | 0  |   GSocketOutputStream *output_stream = G_SOCKET_OUTPUT_STREAM (pollable);  | 
171  |  | 
  | 
172  | 0  |   return g_socket_condition_check (output_stream->priv->socket, G_IO_OUT);  | 
173  | 0  | }  | 
174  |  |  | 
175  |  | static gssize  | 
176  |  | g_socket_output_stream_pollable_write_nonblocking (GPollableOutputStream  *pollable,  | 
177  |  |                const void             *buffer,  | 
178  |  |                gsize                   size,  | 
179  |  |                GError                **error)  | 
180  | 0  | { | 
181  | 0  |   GSocketOutputStream *output_stream = G_SOCKET_OUTPUT_STREAM (pollable);  | 
182  |  | 
  | 
183  | 0  |   return g_socket_send_with_blocking (output_stream->priv->socket,  | 
184  | 0  |               buffer, size, FALSE,  | 
185  | 0  |               NULL, error);  | 
186  | 0  | }  | 
187  |  |  | 
188  |  | static GPollableReturn  | 
189  |  | g_socket_output_stream_pollable_writev_nonblocking (GPollableOutputStream  *pollable,  | 
190  |  |                                                     const GOutputVector    *vectors,  | 
191  |  |                                                     gsize                   n_vectors,  | 
192  |  |                                                     gsize                  *bytes_written,  | 
193  |  |                                                     GError                **error)  | 
194  | 0  | { | 
195  | 0  |   GSocketOutputStream *output_stream = G_SOCKET_OUTPUT_STREAM (pollable);  | 
196  |  |  | 
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.  | 
199  |  |    */  | 
200  | 0  |   if (n_vectors > G_IOV_MAX)  | 
201  | 0  |     n_vectors = G_IOV_MAX;  | 
202  |  | 
  | 
203  | 0  |   return g_socket_send_message_with_timeout (output_stream->priv->socket,  | 
204  | 0  |                                              NULL, vectors, n_vectors,  | 
205  | 0  |                                              NULL, 0, G_SOCKET_MSG_NONE, 0,  | 
206  | 0  |                                              bytes_written, NULL, error);  | 
207  | 0  | }  | 
208  |  |  | 
209  |  | static GSource *  | 
210  |  | g_socket_output_stream_pollable_create_source (GPollableOutputStream *pollable,  | 
211  |  |                  GCancellable          *cancellable)  | 
212  | 0  | { | 
213  | 0  |   GSocketOutputStream *output_stream = G_SOCKET_OUTPUT_STREAM (pollable);  | 
214  | 0  |   GSource *socket_source, *pollable_source;  | 
215  |  | 
  | 
216  | 0  |   pollable_source = g_pollable_source_new (G_OBJECT (output_stream));  | 
217  | 0  |   socket_source = g_socket_create_source (output_stream->priv->socket,  | 
218  | 0  |             G_IO_OUT, cancellable);  | 
219  | 0  |   g_source_set_dummy_callback (socket_source);  | 
220  | 0  |   g_source_add_child_source (pollable_source, socket_source);  | 
221  | 0  |   g_source_unref (socket_source);  | 
222  |  | 
  | 
223  | 0  |   return pollable_source;  | 
224  | 0  | }  | 
225  |  |  | 
226  |  | #ifdef G_OS_UNIX  | 
227  |  | static int  | 
228  |  | g_socket_output_stream_get_fd (GFileDescriptorBased *fd_based)  | 
229  | 0  | { | 
230  | 0  |   GSocketOutputStream *output_stream = G_SOCKET_OUTPUT_STREAM (fd_based);  | 
231  |  | 
  | 
232  | 0  |   return g_socket_get_fd (output_stream->priv->socket);  | 
233  | 0  | }  | 
234  |  | #endif  | 
235  |  |  | 
236  |  | static void  | 
237  |  | g_socket_output_stream_class_init (GSocketOutputStreamClass *klass)  | 
238  | 0  | { | 
239  | 0  |   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);  | 
240  | 0  |   GOutputStreamClass *goutputstream_class = G_OUTPUT_STREAM_CLASS (klass);  | 
241  |  | 
  | 
242  | 0  |   gobject_class->finalize = g_socket_output_stream_finalize;  | 
243  | 0  |   gobject_class->get_property = g_socket_output_stream_get_property;  | 
244  | 0  |   gobject_class->set_property = g_socket_output_stream_set_property;  | 
245  |  | 
  | 
246  | 0  |   goutputstream_class->write_fn = g_socket_output_stream_write;  | 
247  | 0  |   goutputstream_class->writev_fn = g_socket_output_stream_writev;  | 
248  |  | 
  | 
249  | 0  |   g_object_class_install_property (gobject_class, PROP_SOCKET,  | 
250  | 0  |            g_param_spec_object ("socket", | 
251  | 0  |               P_("socket"), | 
252  | 0  |               P_("The socket that this stream wraps"), | 
253  | 0  |               G_TYPE_SOCKET, G_PARAM_CONSTRUCT_ONLY |  | 
254  | 0  |               G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));  | 
255  | 0  | }  | 
256  |  |  | 
257  |  | #ifdef G_OS_UNIX  | 
258  |  | static void  | 
259  |  | g_socket_output_stream_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface)  | 
260  | 0  | { | 
261  | 0  |   iface->get_fd = g_socket_output_stream_get_fd;  | 
262  | 0  | }  | 
263  |  | #endif  | 
264  |  |  | 
265  |  | static void  | 
266  |  | g_socket_output_stream_pollable_iface_init (GPollableOutputStreamInterface *iface)  | 
267  | 0  | { | 
268  | 0  |   iface->is_writable = g_socket_output_stream_pollable_is_writable;  | 
269  | 0  |   iface->create_source = g_socket_output_stream_pollable_create_source;  | 
270  | 0  |   iface->write_nonblocking = g_socket_output_stream_pollable_write_nonblocking;  | 
271  | 0  |   iface->writev_nonblocking = g_socket_output_stream_pollable_writev_nonblocking;  | 
272  | 0  | }  | 
273  |  |  | 
274  |  | static void  | 
275  |  | g_socket_output_stream_init (GSocketOutputStream *stream)  | 
276  | 0  | { | 
277  | 0  |   stream->priv = g_socket_output_stream_get_instance_private (stream);  | 
278  | 0  | }  | 
279  |  |  | 
280  |  | GSocketOutputStream *  | 
281  |  | _g_socket_output_stream_new (GSocket *socket)  | 
282  | 0  | { | 
283  | 0  |   return g_object_new (G_TYPE_SOCKET_OUTPUT_STREAM, "socket", socket, NULL);  | 
284  | 0  | }  |