nexmon – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 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, see <http://www.gnu.org/licenses/>. |
||
18 | * |
||
19 | * Authors: Christian Kellner <gicmo@gnome.org> |
||
20 | * Samuel Cormier-Iijima <sciyoshi@gmail.com> |
||
21 | * Ryan Lortie <desrt@desrt.ca> |
||
22 | */ |
||
23 | |||
24 | #include "config.h" |
||
25 | #include "goutputstream.h" |
||
26 | #include "gsocketoutputstream.h" |
||
27 | #include "gsocket.h" |
||
28 | #include "glibintl.h" |
||
29 | |||
30 | #include "gcancellable.h" |
||
31 | #include "gpollableinputstream.h" |
||
32 | #include "gpollableoutputstream.h" |
||
33 | #include "gioerror.h" |
||
34 | #include "glibintl.h" |
||
35 | #include "gfiledescriptorbased.h" |
||
36 | |||
37 | struct _GSocketOutputStreamPrivate |
||
38 | { |
||
39 | GSocket *socket; |
||
40 | |||
41 | /* pending operation metadata */ |
||
42 | gconstpointer buffer; |
||
43 | gsize count; |
||
44 | }; |
||
45 | |||
46 | static void g_socket_output_stream_pollable_iface_init (GPollableOutputStreamInterface *iface); |
||
47 | #ifdef G_OS_UNIX |
||
48 | static void g_socket_output_stream_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface); |
||
49 | #endif |
||
50 | |||
51 | #define g_socket_output_stream_get_type _g_socket_output_stream_get_type |
||
52 | |||
53 | #ifdef G_OS_UNIX |
||
54 | G_DEFINE_TYPE_WITH_CODE (GSocketOutputStream, g_socket_output_stream, G_TYPE_OUTPUT_STREAM, |
||
55 | G_ADD_PRIVATE (GSocketOutputStream) |
||
56 | G_IMPLEMENT_INTERFACE (G_TYPE_POLLABLE_OUTPUT_STREAM, g_socket_output_stream_pollable_iface_init) |
||
57 | G_IMPLEMENT_INTERFACE (G_TYPE_FILE_DESCRIPTOR_BASED, g_socket_output_stream_file_descriptor_based_iface_init) |
||
58 | ) |
||
59 | #else |
||
60 | G_DEFINE_TYPE_WITH_CODE (GSocketOutputStream, g_socket_output_stream, G_TYPE_OUTPUT_STREAM, |
||
61 | G_ADD_PRIVATE (GSocketOutputStream) |
||
62 | G_IMPLEMENT_INTERFACE (G_TYPE_POLLABLE_OUTPUT_STREAM, g_socket_output_stream_pollable_iface_init) |
||
63 | ) |
||
64 | #endif |
||
65 | |||
66 | enum |
||
67 | { |
||
68 | PROP_0, |
||
69 | PROP_SOCKET |
||
70 | }; |
||
71 | |||
72 | static void |
||
73 | g_socket_output_stream_get_property (GObject *object, |
||
74 | guint prop_id, |
||
75 | GValue *value, |
||
76 | GParamSpec *pspec) |
||
77 | { |
||
78 | GSocketOutputStream *stream = G_SOCKET_OUTPUT_STREAM (object); |
||
79 | |||
80 | switch (prop_id) |
||
81 | { |
||
82 | case PROP_SOCKET: |
||
83 | g_value_set_object (value, stream->priv->socket); |
||
84 | break; |
||
85 | |||
86 | default: |
||
87 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
||
88 | } |
||
89 | } |
||
90 | |||
91 | static void |
||
92 | g_socket_output_stream_set_property (GObject *object, |
||
93 | guint prop_id, |
||
94 | const GValue *value, |
||
95 | GParamSpec *pspec) |
||
96 | { |
||
97 | GSocketOutputStream *stream = G_SOCKET_OUTPUT_STREAM (object); |
||
98 | |||
99 | switch (prop_id) |
||
100 | { |
||
101 | case PROP_SOCKET: |
||
102 | stream->priv->socket = g_value_dup_object (value); |
||
103 | break; |
||
104 | |||
105 | default: |
||
106 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
||
107 | } |
||
108 | } |
||
109 | |||
110 | static void |
||
111 | g_socket_output_stream_finalize (GObject *object) |
||
112 | { |
||
113 | GSocketOutputStream *stream = G_SOCKET_OUTPUT_STREAM (object); |
||
114 | |||
115 | if (stream->priv->socket) |
||
116 | g_object_unref (stream->priv->socket); |
||
117 | |||
118 | G_OBJECT_CLASS (g_socket_output_stream_parent_class)->finalize (object); |
||
119 | } |
||
120 | |||
121 | static gssize |
||
122 | g_socket_output_stream_write (GOutputStream *stream, |
||
123 | const void *buffer, |
||
124 | gsize count, |
||
125 | GCancellable *cancellable, |
||
126 | GError **error) |
||
127 | { |
||
128 | GSocketOutputStream *onput_stream = G_SOCKET_OUTPUT_STREAM (stream); |
||
129 | |||
130 | return g_socket_send_with_blocking (onput_stream->priv->socket, |
||
131 | buffer, count, TRUE, |
||
132 | cancellable, error); |
||
133 | } |
||
134 | |||
135 | static gboolean |
||
136 | g_socket_output_stream_pollable_is_writable (GPollableOutputStream *pollable) |
||
137 | { |
||
138 | GSocketOutputStream *output_stream = G_SOCKET_OUTPUT_STREAM (pollable); |
||
139 | |||
140 | return g_socket_condition_check (output_stream->priv->socket, G_IO_OUT); |
||
141 | } |
||
142 | |||
143 | static gssize |
||
144 | g_socket_output_stream_pollable_write_nonblocking (GPollableOutputStream *pollable, |
||
145 | const void *buffer, |
||
146 | gsize size, |
||
147 | GError **error) |
||
148 | { |
||
149 | GSocketOutputStream *output_stream = G_SOCKET_OUTPUT_STREAM (pollable); |
||
150 | |||
151 | return g_socket_send_with_blocking (output_stream->priv->socket, |
||
152 | buffer, size, FALSE, |
||
153 | NULL, error); |
||
154 | } |
||
155 | |||
156 | static GSource * |
||
157 | g_socket_output_stream_pollable_create_source (GPollableOutputStream *pollable, |
||
158 | GCancellable *cancellable) |
||
159 | { |
||
160 | GSocketOutputStream *output_stream = G_SOCKET_OUTPUT_STREAM (pollable); |
||
161 | GSource *socket_source, *pollable_source; |
||
162 | |||
163 | pollable_source = g_pollable_source_new (G_OBJECT (output_stream)); |
||
164 | socket_source = g_socket_create_source (output_stream->priv->socket, |
||
165 | G_IO_OUT, cancellable); |
||
166 | g_source_set_dummy_callback (socket_source); |
||
167 | g_source_add_child_source (pollable_source, socket_source); |
||
168 | g_source_unref (socket_source); |
||
169 | |||
170 | return pollable_source; |
||
171 | } |
||
172 | |||
173 | #ifdef G_OS_UNIX |
||
174 | static int |
||
175 | g_socket_output_stream_get_fd (GFileDescriptorBased *fd_based) |
||
176 | { |
||
177 | GSocketOutputStream *output_stream = G_SOCKET_OUTPUT_STREAM (fd_based); |
||
178 | |||
179 | return g_socket_get_fd (output_stream->priv->socket); |
||
180 | } |
||
181 | #endif |
||
182 | |||
183 | static void |
||
184 | g_socket_output_stream_class_init (GSocketOutputStreamClass *klass) |
||
185 | { |
||
186 | GObjectClass *gobject_class = G_OBJECT_CLASS (klass); |
||
187 | GOutputStreamClass *goutputstream_class = G_OUTPUT_STREAM_CLASS (klass); |
||
188 | |||
189 | gobject_class->finalize = g_socket_output_stream_finalize; |
||
190 | gobject_class->get_property = g_socket_output_stream_get_property; |
||
191 | gobject_class->set_property = g_socket_output_stream_set_property; |
||
192 | |||
193 | goutputstream_class->write_fn = g_socket_output_stream_write; |
||
194 | |||
195 | g_object_class_install_property (gobject_class, PROP_SOCKET, |
||
196 | g_param_spec_object ("socket", |
||
197 | P_("socket"), |
||
198 | P_("The socket that this stream wraps"), |
||
199 | G_TYPE_SOCKET, G_PARAM_CONSTRUCT_ONLY | |
||
200 | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); |
||
201 | } |
||
202 | |||
203 | #ifdef G_OS_UNIX |
||
204 | static void |
||
205 | g_socket_output_stream_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface) |
||
206 | { |
||
207 | iface->get_fd = g_socket_output_stream_get_fd; |
||
208 | } |
||
209 | #endif |
||
210 | |||
211 | static void |
||
212 | g_socket_output_stream_pollable_iface_init (GPollableOutputStreamInterface *iface) |
||
213 | { |
||
214 | iface->is_writable = g_socket_output_stream_pollable_is_writable; |
||
215 | iface->create_source = g_socket_output_stream_pollable_create_source; |
||
216 | iface->write_nonblocking = g_socket_output_stream_pollable_write_nonblocking; |
||
217 | } |
||
218 | |||
219 | static void |
||
220 | g_socket_output_stream_init (GSocketOutputStream *stream) |
||
221 | { |
||
222 | stream->priv = g_socket_output_stream_get_instance_private (stream); |
||
223 | } |
||
224 | |||
225 | GSocketOutputStream * |
||
226 | _g_socket_output_stream_new (GSocket *socket) |
||
227 | { |
||
228 | return g_object_new (G_TYPE_SOCKET_OUTPUT_STREAM, "socket", socket, NULL); |
||
229 | } |