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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
26 #include <glib/gstdio.h>
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>
32 /* object properties */
33 enum _ThriftFDTransportProperties
36 PROP_THRIFT_FD_TRANSPORT_FD
39 G_DEFINE_TYPE (ThriftFDTransport
, thrift_fd_transport
, THRIFT_TYPE_TRANSPORT
)
41 /* implements thrift_transport_is_open */
43 thrift_fd_transport_is_open (ThriftTransport
*transport
)
46 t
= THRIFT_FD_TRANSPORT (transport
);
47 return t
->fd
>= 0 && ! (fcntl (t
->fd
, F_GETFL
) == -1 && errno
== EBADF
);
50 /* implements thrift_transport_open */
52 thrift_fd_transport_open (ThriftTransport
*transport
, GError
**error
)
54 THRIFT_UNUSED_VAR (error
);
55 return thrift_fd_transport_is_open (transport
);
58 /* implements thrift_transport_close */
60 thrift_fd_transport_close (ThriftTransport
*transport
, GError
**error
)
63 t
= THRIFT_FD_TRANSPORT (transport
);
65 #if GLIB_CHECK_VERSION (2, 36, 0)
66 return g_close (t
->fd
, error
);
68 if (close (t
->fd
) == 0) {
69 g_clear_error (error
);
73 THRIFT_TRANSPORT_ERROR
,
74 THRIFT_TRANSPORT_ERROR_CLOSE
,
81 /* implements thrift_transport_read */
83 thrift_fd_transport_read (ThriftTransport
*transport
, gpointer buf
,
84 guint32 len
, GError
**error
)
89 t
= THRIFT_FD_TRANSPORT (transport
);
90 n
= read (t
->fd
, (guint8
*) buf
, len
);
93 THRIFT_TRANSPORT_ERROR
,
94 THRIFT_TRANSPORT_ERROR_RECEIVE
,
95 "Failed to read from fd: %s",
102 /* implements thrift_transport_read_end
103 * called when write is complete. nothing to do on our end. */
105 thrift_fd_transport_read_end (ThriftTransport
*transport
, GError
**error
)
108 THRIFT_UNUSED_VAR (transport
);
109 THRIFT_UNUSED_VAR (error
);
113 /* implements thrift_transport_write */
115 thrift_fd_transport_write (ThriftTransport
*transport
,
117 const guint32 len
, GError
**error
)
119 ThriftFDTransport
*t
;
124 t
= THRIFT_FD_TRANSPORT (transport
);
125 _buf
= (guint8
*) buf
;
128 n
= write (t
->fd
, _buf
, _len
);
131 THRIFT_TRANSPORT_ERROR
,
132 THRIFT_TRANSPORT_ERROR_SEND
,
133 "Failed to write from fd: %s",
144 /* implements thrift_transport_write_end
145 * called when write is complete. nothing to do on our end. */
147 thrift_fd_transport_write_end (ThriftTransport
*transport
, GError
**error
)
149 THRIFT_UNUSED_VAR (transport
);
150 THRIFT_UNUSED_VAR (error
);
154 /* implements thrift_transport_flush */
156 thrift_fd_transport_flush (ThriftTransport
*transport
, GError
**error
)
158 ThriftFDTransport
*t
;
159 t
= THRIFT_FD_TRANSPORT (transport
);
160 if (fsync (t
->fd
) == -1) {
162 THRIFT_TRANSPORT_ERROR
,
163 THRIFT_TRANSPORT_ERROR_UNKNOWN
,
164 "Failed to flush fd: %s",
172 /* initializes the instance */
174 thrift_fd_transport_init (ThriftFDTransport
*transport
)
181 thrift_fd_transport_finalize (GObject
*object
)
183 THRIFT_UNUSED_VAR (object
);
186 /* property accessor */
188 thrift_fd_transport_get_property (GObject
*object
, guint property_id
,
189 GValue
*value
, GParamSpec
*pspec
)
191 ThriftFDTransport
*t
;
193 THRIFT_UNUSED_VAR (pspec
);
195 t
= THRIFT_FD_TRANSPORT (object
);
197 switch (property_id
) {
198 case PROP_THRIFT_FD_TRANSPORT_FD
:
199 g_value_set_int (value
, t
->fd
);
202 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, property_id
, pspec
);
207 /* property mutator */
209 thrift_fd_transport_set_property (GObject
*object
, guint property_id
,
210 const GValue
*value
, GParamSpec
*pspec
)
212 ThriftFDTransport
*t
;
214 THRIFT_UNUSED_VAR (pspec
);
216 t
= THRIFT_FD_TRANSPORT (object
);
218 switch (property_id
) {
219 case PROP_THRIFT_FD_TRANSPORT_FD
:
220 t
->fd
= g_value_get_int (value
);
223 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, property_id
, pspec
);
228 /* initializes the class */
230 thrift_fd_transport_class_init (ThriftFDTransportClass
*cls
)
232 ThriftTransportClass
*ttc
;
233 GObjectClass
*gobject_class
;
234 GParamSpec
*param_spec
;
236 ttc
= THRIFT_TRANSPORT_CLASS (cls
);
237 gobject_class
= G_OBJECT_CLASS (cls
);
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
;
244 param_spec
= g_param_spec_int ("fd",
245 "file descriptor (construct)",
246 "Set the file descriptor",
248 INT_MAX
, /* max, 1024*1024 */
249 -1, /* default value */
250 G_PARAM_CONSTRUCT_ONLY
|
252 g_object_class_install_property (gobject_class
,
253 PROP_THRIFT_FD_TRANSPORT_FD
,
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
;