]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/thrift/lib/c_glib/src/thrift/c_glib/transport/thrift_fd_transport.c
buildsys: switch source download to quincy
[ceph.git] / ceph / src / jaegertracing / thrift / lib / c_glib / src / thrift / c_glib / transport / thrift_fd_transport.c
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20 #include <errno.h>
21 #include <unistd.h>
22 #include <fcntl.h>
23 #include <string.h>
24
25 #include <glib.h>
26 #include <glib/gstdio.h>
27
28 #include <thrift/c_glib/thrift.h>
29 #include <thrift/c_glib/transport/thrift_transport.h>
30 #include <thrift/c_glib/transport/thrift_fd_transport.h>
31
32 /* object properties */
33 enum _ThriftFDTransportProperties
34 {
35 PROP_0,
36 PROP_THRIFT_FD_TRANSPORT_FD
37 };
38
39 G_DEFINE_TYPE (ThriftFDTransport, thrift_fd_transport, THRIFT_TYPE_TRANSPORT)
40
41 /* implements thrift_transport_is_open */
42 gboolean
43 thrift_fd_transport_is_open (ThriftTransport *transport)
44 {
45 ThriftFDTransport *t;
46 t = THRIFT_FD_TRANSPORT (transport);
47 return t->fd >= 0 && ! (fcntl (t->fd, F_GETFL) == -1 && errno == EBADF);
48 }
49
50 /* implements thrift_transport_open */
51 gboolean
52 thrift_fd_transport_open (ThriftTransport *transport, GError **error)
53 {
54 THRIFT_UNUSED_VAR (error);
55 return thrift_fd_transport_is_open (transport);
56 }
57
58 /* implements thrift_transport_close */
59 gboolean
60 thrift_fd_transport_close (ThriftTransport *transport, GError **error)
61 {
62 ThriftFDTransport *t;
63 t = THRIFT_FD_TRANSPORT (transport);
64
65 #if GLIB_CHECK_VERSION (2, 36, 0)
66 return g_close (t->fd, error);
67 #else
68 if (close (t->fd) == 0) {
69 g_clear_error (error);
70 return TRUE;
71 } else {
72 g_set_error (error,
73 THRIFT_TRANSPORT_ERROR,
74 THRIFT_TRANSPORT_ERROR_CLOSE,
75 strerror (errno));
76 return FALSE;
77 }
78 #endif
79 }
80
81 /* implements thrift_transport_read */
82 gint32
83 thrift_fd_transport_read (ThriftTransport *transport, gpointer buf,
84 guint32 len, GError **error)
85 {
86 ThriftFDTransport *t;
87 ssize_t n;
88
89 t = THRIFT_FD_TRANSPORT (transport);
90 n = read (t->fd, (guint8 *) buf, len);
91 if (n == -1) {
92 g_set_error (error,
93 THRIFT_TRANSPORT_ERROR,
94 THRIFT_TRANSPORT_ERROR_RECEIVE,
95 "Failed to read from fd: %s",
96 strerror (errno));
97 return -1;
98 }
99 return n;
100 }
101
102 /* implements thrift_transport_read_end
103 * called when write is complete. nothing to do on our end. */
104 gboolean
105 thrift_fd_transport_read_end (ThriftTransport *transport, GError **error)
106 {
107 /* satisfy -Wall */
108 THRIFT_UNUSED_VAR (transport);
109 THRIFT_UNUSED_VAR (error);
110 return TRUE;
111 }
112
113 /* implements thrift_transport_write */
114 gboolean
115 thrift_fd_transport_write (ThriftTransport *transport,
116 const gpointer buf,
117 const guint32 len, GError **error)
118 {
119 ThriftFDTransport *t;
120 guint8 *_buf;
121 guint32 _len;
122 ssize_t n;
123
124 t = THRIFT_FD_TRANSPORT (transport);
125 _buf = (guint8 *) buf;
126 _len = len;
127 while (_len > 0) {
128 n = write (t->fd, _buf, _len);
129 if (n == -1) {
130 g_set_error (error,
131 THRIFT_TRANSPORT_ERROR,
132 THRIFT_TRANSPORT_ERROR_SEND,
133 "Failed to write from fd: %s",
134 strerror (errno));
135 return FALSE;
136 } else {
137 _buf += n;
138 _len -= n;
139 }
140 }
141 return TRUE;
142 }
143
144 /* implements thrift_transport_write_end
145 * called when write is complete. nothing to do on our end. */
146 gboolean
147 thrift_fd_transport_write_end (ThriftTransport *transport, GError **error)
148 {
149 THRIFT_UNUSED_VAR (transport);
150 THRIFT_UNUSED_VAR (error);
151 return TRUE;
152 }
153
154 /* implements thrift_transport_flush */
155 gboolean
156 thrift_fd_transport_flush (ThriftTransport *transport, GError **error)
157 {
158 ThriftFDTransport *t;
159 t = THRIFT_FD_TRANSPORT (transport);
160 if (fsync (t->fd) == -1) {
161 g_set_error (error,
162 THRIFT_TRANSPORT_ERROR,
163 THRIFT_TRANSPORT_ERROR_UNKNOWN,
164 "Failed to flush fd: %s",
165 strerror (errno));
166 return FALSE;
167 } else {
168 return TRUE;
169 }
170 }
171
172 /* initializes the instance */
173 static void
174 thrift_fd_transport_init (ThriftFDTransport *transport)
175 {
176 transport->fd = -1;
177 }
178
179 /* destructor */
180 static void
181 thrift_fd_transport_finalize (GObject *object)
182 {
183 THRIFT_UNUSED_VAR (object);
184 }
185
186 /* property accessor */
187 void
188 thrift_fd_transport_get_property (GObject *object, guint property_id,
189 GValue *value, GParamSpec *pspec)
190 {
191 ThriftFDTransport *t;
192
193 THRIFT_UNUSED_VAR (pspec);
194
195 t = THRIFT_FD_TRANSPORT (object);
196
197 switch (property_id) {
198 case PROP_THRIFT_FD_TRANSPORT_FD:
199 g_value_set_int (value, t->fd);
200 break;
201 default:
202 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
203 break;
204 }
205 }
206
207 /* property mutator */
208 void
209 thrift_fd_transport_set_property (GObject *object, guint property_id,
210 const GValue *value, GParamSpec *pspec)
211 {
212 ThriftFDTransport *t;
213
214 THRIFT_UNUSED_VAR (pspec);
215
216 t = THRIFT_FD_TRANSPORT (object);
217
218 switch (property_id) {
219 case PROP_THRIFT_FD_TRANSPORT_FD:
220 t->fd = g_value_get_int (value);
221 break;
222 default:
223 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
224 break;
225 }
226 }
227
228 /* initializes the class */
229 static void
230 thrift_fd_transport_class_init (ThriftFDTransportClass *cls)
231 {
232 ThriftTransportClass *ttc;
233 GObjectClass *gobject_class;
234 GParamSpec *param_spec;
235
236 ttc = THRIFT_TRANSPORT_CLASS (cls);
237 gobject_class = G_OBJECT_CLASS (cls);
238 param_spec = NULL;
239
240 /* setup accessors and mutators */
241 gobject_class->get_property = thrift_fd_transport_get_property;
242 gobject_class->set_property = thrift_fd_transport_set_property;
243
244 param_spec = g_param_spec_int ("fd",
245 "file descriptor (construct)",
246 "Set the file descriptor",
247 INT_MIN, /* min */
248 INT_MAX, /* max, 1024*1024 */
249 -1, /* default value */
250 G_PARAM_CONSTRUCT_ONLY |
251 G_PARAM_READWRITE);
252 g_object_class_install_property (gobject_class,
253 PROP_THRIFT_FD_TRANSPORT_FD,
254 param_spec);
255
256 gobject_class->finalize = thrift_fd_transport_finalize;
257 ttc->is_open = thrift_fd_transport_is_open;
258 ttc->open = thrift_fd_transport_open;
259 ttc->close = thrift_fd_transport_close;
260 ttc->read = thrift_fd_transport_read;
261 ttc->read_end = thrift_fd_transport_read_end;
262 ttc->write = thrift_fd_transport_write;
263 ttc->write_end = thrift_fd_transport_write_end;
264 ttc->flush = thrift_fd_transport_flush;
265 }