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
20 #include <thrift/c_glib/thrift.h>
21 #include <thrift/c_glib/protocol/thrift_protocol.h>
22 #include <thrift/c_glib/transport/thrift_transport.h>
24 /* define the GError domain string */
25 #define THRIFT_PROTOCOL_ERROR_DOMAIN "thrift-protocol-error-quark"
27 /* object properties */
28 enum _ThriftProtocolProperties
31 PROP_THRIFT_PROTOCOL_TRANSPORT
34 G_DEFINE_ABSTRACT_TYPE(ThriftProtocol
, thrift_protocol
, G_TYPE_OBJECT
)
37 thrift_protocol_get_property (GObject
*object
, guint property_id
,
38 GValue
*value
, GParamSpec
*pspec
)
40 ThriftProtocol
*protocol
= THRIFT_PROTOCOL (object
);
42 THRIFT_UNUSED_VAR (pspec
);
46 case PROP_THRIFT_PROTOCOL_TRANSPORT
:
47 g_value_set_object (value
, protocol
->transport
);
53 thrift_protocol_set_property (GObject
*object
, guint property_id
,
54 const GValue
*value
, GParamSpec
*pspec
)
57 ThriftProtocol
*protocol
= THRIFT_PROTOCOL (object
);
59 THRIFT_UNUSED_VAR (pspec
);
63 case PROP_THRIFT_PROTOCOL_TRANSPORT
:
64 protocol
->transport
= g_value_dup_object (value
);
71 thrift_protocol_write_message_begin (ThriftProtocol
*protocol
,
73 const ThriftMessageType message_type
,
74 const gint32 seqid
, GError
**error
)
76 return THRIFT_PROTOCOL_GET_CLASS (protocol
)->write_message_begin
83 thrift_protocol_write_message_end (ThriftProtocol
*protocol
, GError
**error
)
85 return THRIFT_PROTOCOL_GET_CLASS (protocol
)->write_message_end (protocol
,
90 thrift_protocol_write_struct_begin (ThriftProtocol
*protocol
, const gchar
*name
,
93 return THRIFT_PROTOCOL_GET_CLASS (protocol
)->write_struct_begin (protocol
,
98 thrift_protocol_write_struct_end (ThriftProtocol
*protocol
, GError
**error
)
100 return THRIFT_PROTOCOL_GET_CLASS (protocol
)->write_struct_end (protocol
,
105 thrift_protocol_write_field_begin (ThriftProtocol
*protocol
,
107 const ThriftType field_type
,
108 const gint16 field_id
,
111 return THRIFT_PROTOCOL_GET_CLASS (protocol
)->write_field_begin (protocol
,
117 thrift_protocol_write_field_end (ThriftProtocol
*protocol
, GError
**error
)
119 return THRIFT_PROTOCOL_GET_CLASS (protocol
)->write_field_end (protocol
,
124 thrift_protocol_write_field_stop (ThriftProtocol
*protocol
, GError
**error
)
126 return THRIFT_PROTOCOL_GET_CLASS (protocol
)->write_field_stop (protocol
,
131 thrift_protocol_write_map_begin (ThriftProtocol
*protocol
,
132 const ThriftType key_type
,
133 const ThriftType value_type
,
134 const guint32 size
, GError
**error
)
136 return THRIFT_PROTOCOL_GET_CLASS (protocol
)->write_map_begin (protocol
,
137 key_type
, value_type
,
142 thrift_protocol_write_map_end (ThriftProtocol
*protocol
, GError
**error
)
144 return THRIFT_PROTOCOL_GET_CLASS (protocol
)->write_map_end (protocol
,
149 thrift_protocol_write_list_begin (ThriftProtocol
*protocol
,
150 const ThriftType element_type
,
151 const guint32 size
, GError
**error
)
153 return THRIFT_PROTOCOL_GET_CLASS (protocol
)->write_list_begin (protocol
,
159 thrift_protocol_write_list_end (ThriftProtocol
*protocol
, GError
**error
)
161 return THRIFT_PROTOCOL_GET_CLASS (protocol
)->write_list_end (protocol
,
166 thrift_protocol_write_set_begin (ThriftProtocol
*protocol
,
167 const ThriftType element_type
,
168 const guint32 size
, GError
**error
)
170 return THRIFT_PROTOCOL_GET_CLASS (protocol
)->write_set_begin (protocol
,
176 thrift_protocol_write_set_end (ThriftProtocol
*protocol
, GError
**error
)
178 return THRIFT_PROTOCOL_GET_CLASS (protocol
)->write_set_end (protocol
,
183 thrift_protocol_write_bool (ThriftProtocol
*protocol
,
184 const gboolean value
, GError
**error
)
186 return THRIFT_PROTOCOL_GET_CLASS (protocol
)->write_bool (protocol
, value
,
191 thrift_protocol_write_byte (ThriftProtocol
*protocol
, const gint8 value
,
194 return THRIFT_PROTOCOL_GET_CLASS (protocol
)->write_byte (protocol
, value
,
199 thrift_protocol_write_i16 (ThriftProtocol
*protocol
, const gint16 value
,
202 return THRIFT_PROTOCOL_GET_CLASS (protocol
)->write_i16 (protocol
, value
,
207 thrift_protocol_write_i32 (ThriftProtocol
*protocol
, const gint32 value
,
210 return THRIFT_PROTOCOL_GET_CLASS (protocol
)->write_i32 (protocol
, value
,
215 thrift_protocol_write_i64 (ThriftProtocol
*protocol
, const gint64 value
,
218 return THRIFT_PROTOCOL_GET_CLASS (protocol
)->write_i64 (protocol
, value
,
223 thrift_protocol_write_double (ThriftProtocol
*protocol
,
224 const gdouble value
, GError
**error
)
226 return THRIFT_PROTOCOL_GET_CLASS (protocol
)->write_double (protocol
,
231 thrift_protocol_write_string (ThriftProtocol
*protocol
,
232 const gchar
*str
, GError
**error
)
234 return THRIFT_PROTOCOL_GET_CLASS (protocol
)->write_string (protocol
, str
,
239 thrift_protocol_write_binary (ThriftProtocol
*protocol
, const gpointer buf
,
240 const guint32 len
, GError
**error
)
242 return THRIFT_PROTOCOL_GET_CLASS (protocol
)->write_binary (protocol
, buf
,
247 thrift_protocol_read_message_begin (ThriftProtocol
*protocol
,
249 ThriftMessageType
*message_type
,
250 gint32
*seqid
, GError
**error
)
252 return THRIFT_PROTOCOL_GET_CLASS (protocol
)->read_message_begin (protocol
,
258 thrift_protocol_read_message_end (ThriftProtocol
*protocol
,
261 return THRIFT_PROTOCOL_GET_CLASS (protocol
)->read_message_end (protocol
,
266 thrift_protocol_read_struct_begin (ThriftProtocol
*protocol
,
270 return THRIFT_PROTOCOL_GET_CLASS (protocol
)->read_struct_begin (protocol
,
276 thrift_protocol_read_struct_end (ThriftProtocol
*protocol
, GError
**error
)
278 return THRIFT_PROTOCOL_GET_CLASS (protocol
)->read_struct_end (protocol
,
283 thrift_protocol_read_field_begin (ThriftProtocol
*protocol
,
285 ThriftType
*field_type
,
289 return THRIFT_PROTOCOL_GET_CLASS (protocol
)->read_field_begin (protocol
,
297 thrift_protocol_read_field_end (ThriftProtocol
*protocol
,
300 return THRIFT_PROTOCOL_GET_CLASS (protocol
)->read_field_end (protocol
,
305 thrift_protocol_read_map_begin (ThriftProtocol
*protocol
,
306 ThriftType
*key_type
,
307 ThriftType
*value_type
, guint32
*size
,
310 return THRIFT_PROTOCOL_GET_CLASS (protocol
)->read_map_begin (protocol
,
318 thrift_protocol_read_map_end (ThriftProtocol
*protocol
, GError
**error
)
320 return THRIFT_PROTOCOL_GET_CLASS (protocol
)->read_map_end (protocol
,
325 thrift_protocol_read_list_begin (ThriftProtocol
*protocol
,
326 ThriftType
*element_type
,
327 guint32
*size
, GError
**error
)
329 return THRIFT_PROTOCOL_GET_CLASS (protocol
)->read_list_begin (protocol
,
335 thrift_protocol_read_list_end (ThriftProtocol
*protocol
, GError
**error
)
337 return THRIFT_PROTOCOL_GET_CLASS (protocol
)->read_list_end (protocol
,
342 thrift_protocol_read_set_begin (ThriftProtocol
*protocol
,
343 ThriftType
*element_type
,
344 guint32
*size
, GError
**error
)
346 return THRIFT_PROTOCOL_GET_CLASS (protocol
)->read_set_begin (protocol
,
352 thrift_protocol_read_set_end (ThriftProtocol
*protocol
, GError
**error
)
354 return THRIFT_PROTOCOL_GET_CLASS (protocol
)->read_set_end (protocol
,
359 thrift_protocol_read_bool (ThriftProtocol
*protocol
, gboolean
*value
,
362 return THRIFT_PROTOCOL_GET_CLASS (protocol
)->read_bool (protocol
, value
,
367 thrift_protocol_read_byte (ThriftProtocol
*protocol
, gint8
*value
,
370 return THRIFT_PROTOCOL_GET_CLASS (protocol
)->read_byte (protocol
, value
,
375 thrift_protocol_read_i16 (ThriftProtocol
*protocol
, gint16
*value
,
378 return THRIFT_PROTOCOL_GET_CLASS (protocol
)->read_i16 (protocol
, value
,
383 thrift_protocol_read_i32 (ThriftProtocol
*protocol
, gint32
*value
,
386 return THRIFT_PROTOCOL_GET_CLASS (protocol
)->read_i32 (protocol
, value
,
391 thrift_protocol_read_i64 (ThriftProtocol
*protocol
, gint64
*value
,
394 return THRIFT_PROTOCOL_GET_CLASS (protocol
)->read_i64 (protocol
, value
,
399 thrift_protocol_read_double (ThriftProtocol
*protocol
,
400 gdouble
*value
, GError
**error
)
402 return THRIFT_PROTOCOL_GET_CLASS (protocol
)->read_double (protocol
, value
,
407 thrift_protocol_read_string (ThriftProtocol
*protocol
,
408 gchar
**str
, GError
**error
)
410 return THRIFT_PROTOCOL_GET_CLASS (protocol
)->read_string (protocol
, str
,
415 thrift_protocol_read_binary (ThriftProtocol
*protocol
, gpointer
*buf
,
416 guint32
*len
, GError
**error
)
418 return THRIFT_PROTOCOL_GET_CLASS (protocol
)->read_binary (protocol
, buf
,
422 #define THRIFT_SKIP_RESULT_OR_RETURN(_RES, _CALL) \
424 gint32 _x = (_CALL); \
425 if (_x < 0) { return _x; } \
430 thrift_protocol_skip (ThriftProtocol
*protocol
, ThriftType type
, GError
**error
)
437 return thrift_protocol_read_bool (protocol
, &boolv
, error
);
442 return thrift_protocol_read_byte (protocol
, &bytev
, error
);
448 return thrift_protocol_read_i16 (protocol
, &i16
, error
);
453 return thrift_protocol_read_i32 (protocol
, &i32
, error
);
458 return thrift_protocol_read_i64 (protocol
, &i64
, error
);
463 return thrift_protocol_read_double (protocol
, &dub
, error
);
469 gint32 ret
= thrift_protocol_read_binary (protocol
, &data
, &len
, error
);
479 THRIFT_SKIP_RESULT_OR_RETURN(result
,
480 thrift_protocol_read_struct_begin (protocol
, &name
, error
))
483 THRIFT_SKIP_RESULT_OR_RETURN(result
,
484 thrift_protocol_read_field_begin (protocol
, &name
, &ftype
,
490 THRIFT_SKIP_RESULT_OR_RETURN(result
,
491 thrift_protocol_skip (protocol
, ftype
, error
))
492 THRIFT_SKIP_RESULT_OR_RETURN(result
,
493 thrift_protocol_read_field_end (protocol
, error
))
495 THRIFT_SKIP_RESULT_OR_RETURN(result
,
496 thrift_protocol_read_struct_end (protocol
, error
))
502 ThriftType elem_type
;
504 THRIFT_SKIP_RESULT_OR_RETURN(result
,
505 thrift_protocol_read_set_begin (protocol
, &elem_type
, &size
,
507 for (i
= 0; i
< size
; i
++)
509 THRIFT_SKIP_RESULT_OR_RETURN(result
,
510 thrift_protocol_skip (protocol
, elem_type
, error
))
512 THRIFT_SKIP_RESULT_OR_RETURN(result
,
513 thrift_protocol_read_set_end (protocol
, error
))
519 ThriftType elem_type
;
522 THRIFT_SKIP_RESULT_OR_RETURN(result
,
523 thrift_protocol_read_map_begin (protocol
, &key_type
, &elem_type
, &size
,
525 for (i
= 0; i
< size
; i
++)
527 THRIFT_SKIP_RESULT_OR_RETURN(result
,
528 thrift_protocol_skip (protocol
, key_type
, error
))
529 THRIFT_SKIP_RESULT_OR_RETURN(result
,
530 thrift_protocol_skip (protocol
, elem_type
, error
))
532 THRIFT_SKIP_RESULT_OR_RETURN(result
,
533 thrift_protocol_read_map_end (protocol
, error
))
539 ThriftType elem_type
;
541 THRIFT_SKIP_RESULT_OR_RETURN(result
,
542 thrift_protocol_read_list_begin (protocol
, &elem_type
, &size
,
544 for (i
= 0; i
< size
; i
++)
546 THRIFT_SKIP_RESULT_OR_RETURN(result
,
547 thrift_protocol_skip (protocol
, elem_type
, error
))
549 THRIFT_SKIP_RESULT_OR_RETURN(result
,
550 thrift_protocol_read_list_end (protocol
, error
))
557 g_set_error (error
, THRIFT_PROTOCOL_ERROR
,
558 THRIFT_PROTOCOL_ERROR_INVALID_DATA
,
559 "unrecognized type");
563 /* define the GError domain for Thrift protocols */
565 thrift_protocol_error_quark (void)
567 return g_quark_from_static_string (THRIFT_PROTOCOL_ERROR_DOMAIN
);
572 thrift_protocol_init (ThriftProtocol
*protocol
)
574 protocol
->transport
= NULL
;
578 thrift_protocol_dispose (GObject
*gobject
)
580 ThriftProtocol
*self
= THRIFT_PROTOCOL (gobject
);
582 g_clear_object(&self
->transport
);
584 /* Always chain up to the parent class; there is no need to check if
585 * the parent class implements the dispose() virtual function: it is
586 * always guaranteed to do so
588 G_OBJECT_CLASS (thrift_protocol_parent_class
)->dispose(gobject
);
592 thrift_protocol_class_init (ThriftProtocolClass
*cls
)
594 GObjectClass
*gobject_class
= G_OBJECT_CLASS (cls
);
596 gobject_class
->get_property
= thrift_protocol_get_property
;
597 gobject_class
->set_property
= thrift_protocol_set_property
;
598 gobject_class
->dispose
= thrift_protocol_dispose
;
600 g_object_class_install_property (gobject_class
,
601 PROP_THRIFT_PROTOCOL_TRANSPORT
,
602 g_param_spec_object ("transport", "Transport", "Thrift Transport",
603 THRIFT_TYPE_TRANSPORT
,
604 G_PARAM_READWRITE
| G_PARAM_CONSTRUCT_ONLY
));
606 cls
->write_message_begin
= thrift_protocol_write_message_begin
;
607 cls
->write_message_end
= thrift_protocol_write_message_end
;
608 cls
->write_struct_begin
= thrift_protocol_write_struct_begin
;
609 cls
->write_struct_end
= thrift_protocol_write_struct_end
;
610 cls
->write_field_begin
= thrift_protocol_write_field_begin
;
611 cls
->write_field_end
= thrift_protocol_write_field_end
;
612 cls
->write_field_stop
= thrift_protocol_write_field_stop
;
613 cls
->write_map_begin
= thrift_protocol_write_map_begin
;
614 cls
->write_map_end
= thrift_protocol_write_map_end
;
615 cls
->write_list_begin
= thrift_protocol_write_list_begin
;
616 cls
->write_list_end
= thrift_protocol_write_list_end
;
617 cls
->write_set_begin
= thrift_protocol_write_set_begin
;
618 cls
->write_set_end
= thrift_protocol_write_set_end
;
619 cls
->write_bool
= thrift_protocol_write_bool
;
620 cls
->write_byte
= thrift_protocol_write_byte
;
621 cls
->write_i16
= thrift_protocol_write_i16
;
622 cls
->write_i32
= thrift_protocol_write_i32
;
623 cls
->write_i64
= thrift_protocol_write_i64
;
624 cls
->write_double
= thrift_protocol_write_double
;
625 cls
->write_string
= thrift_protocol_write_string
;
626 cls
->write_binary
= thrift_protocol_write_binary
;
627 cls
->read_message_begin
= thrift_protocol_read_message_begin
;
628 cls
->read_message_end
= thrift_protocol_read_message_end
;
629 cls
->read_struct_begin
= thrift_protocol_read_struct_begin
;
630 cls
->read_struct_end
= thrift_protocol_read_struct_end
;
631 cls
->read_field_begin
= thrift_protocol_read_field_begin
;
632 cls
->read_field_end
= thrift_protocol_read_field_end
;
633 cls
->read_map_begin
= thrift_protocol_read_map_begin
;
634 cls
->read_map_end
= thrift_protocol_read_map_end
;
635 cls
->read_list_begin
= thrift_protocol_read_list_begin
;
636 cls
->read_set_begin
= thrift_protocol_read_set_begin
;
637 cls
->read_set_end
= thrift_protocol_read_set_end
;
638 cls
->read_bool
= thrift_protocol_read_bool
;
639 cls
->read_byte
= thrift_protocol_read_byte
;
640 cls
->read_i16
= thrift_protocol_read_i16
;
641 cls
->read_i32
= thrift_protocol_read_i32
;
642 cls
->read_i64
= thrift_protocol_read_i64
;
643 cls
->read_double
= thrift_protocol_read_double
;
644 cls
->read_string
= thrift_protocol_read_string
;
645 cls
->read_binary
= thrift_protocol_read_binary
;