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 #ifndef _THRIFT_PROTOCOL_H
21 #define _THRIFT_PROTOCOL_H
22 
23 #include <glib-object.h>
24 
25 #include <thrift/c_glib/transport/thrift_transport.h>
26 
27 G_BEGIN_DECLS
28 
29 /*! \file thrift_protocol.h
30  *  \brief Abstract class for Thrift protocol implementations.
31  */
32 
33 /**
34  * Enumerated definition of the types that the Thrift protocol supports.
35  * Take special note of the T_END type which is used specifically to mark
36  * the end of a sequence of fields.
37  */
38 typedef enum {
39   T_STOP   = 0,
40   T_VOID   = 1,
41   T_BOOL   = 2,
42   T_BYTE   = 3,
43   T_I08    = 3,
44   T_I16    = 6,
45   T_I32    = 8,
46   T_U64    = 9,
47   T_I64    = 10,
48   T_DOUBLE = 4,
49   T_STRING = 11,
50   T_UTF7   = 11,
51   T_STRUCT = 12,
52   T_MAP    = 13,
53   T_SET    = 14,
54   T_LIST   = 15,
55   T_UTF8   = 16,
56   T_UTF16  = 17
57 } ThriftType;
58 
59 /**
60  * Enumerated definition of the message types that the Thrift protocol
61  * supports.
62  */
63 typedef enum {
64   T_CALL      = 1,
65   T_REPLY     = 2,
66   T_EXCEPTION = 3,
67   T_ONEWAY    = 4
68 } ThriftMessageType;
69 
70 /* type macros */
71 #define THRIFT_TYPE_PROTOCOL (thrift_protocol_get_type ())
72 #define THRIFT_PROTOCOL(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), THRIFT_TYPE_PROTOCOL, ThriftProtocol))
73 #define THRIFT_IS_PROTOCOL(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), THRIFT_TYPE_PROTOCOL))
74 #define THRIFT_PROTOCOL_CLASS(c) (G_TYPE_CHECK_CLASS_CAST ((c), THRIFT_TYPE_PROTOCOL, ThriftProtocolClass))
75 #define THRIFT_IS_PROTOCOL_CLASS(c) (G_TYPE_CHECK_CLASS_TYPE ((c), THRIFT_TYPE_PROTOCOL))
76 #define THRIFT_PROTOCOL_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), THRIFT_TYPE_PROTOCOL, ThriftProtocolClass))
77 
78 typedef struct _ThriftProtocol ThriftProtocol;
79 
80 /*!
81  * Thrift Protocol object
82  */
83 struct _ThriftProtocol
84 {
85   GObject parent;
86 
87   /* protected */
88   ThriftTransport *transport;
89 
90 };
91 
92 typedef struct _ThriftProtocolClass ThriftProtocolClass;
93 
94 /*!
95  * Thrift Protocol class
96  */
97 struct _ThriftProtocolClass
98 {
99   GObjectClass parent;
100 
101   gint32 (*write_message_begin) (ThriftProtocol *protocol, const gchar *name,
102                                  const ThriftMessageType message_type,
103                                  const gint32 seqid, GError **error);
104   gint32 (*write_message_end) (ThriftProtocol *protocol, GError **error);
105   gint32 (*write_struct_begin) (ThriftProtocol *protocol, const gchar *name,
106                                 GError **error);
107   gint32 (*write_struct_end) (ThriftProtocol *protocol, GError **error);
108   gint32 (*write_field_begin) (ThriftProtocol *protocol, const gchar *name,
109                                const ThriftType field_type,
110                                const gint16 field_id, GError **error);
111   gint32 (*write_field_end) (ThriftProtocol *protocol, GError **error);
112   gint32 (*write_field_stop) (ThriftProtocol *protocol, GError **error);
113   gint32 (*write_map_begin) (ThriftProtocol *protocol,
114                              const ThriftType key_type,
115                              const ThriftType value_type,
116                              const guint32 size, GError **error);
117   gint32 (*write_map_end) (ThriftProtocol *protocol, GError **error);
118   gint32 (*write_list_begin) (ThriftProtocol *protocol,
119                               const ThriftType element_type,
120                               const guint32 size, GError **error);
121   gint32 (*write_list_end) (ThriftProtocol *protocol, GError **error);
122   gint32 (*write_set_begin) (ThriftProtocol *protocol,
123                              const ThriftType element_type,
124                              const guint32 size, GError **error);
125   gint32 (*write_set_end) (ThriftProtocol *protocol, GError **error);
126   gint32 (*write_bool) (ThriftProtocol *protocol, const gboolean value,
127                         GError **error);
128   gint32 (*write_byte) (ThriftProtocol *protocol, const gint8 value,
129                         GError **error);
130   gint32 (*write_i16) (ThriftProtocol *protocol, const gint16 value,
131                        GError **error);
132   gint32 (*write_i32) (ThriftProtocol *protocol, const gint32 value,
133                        GError **error);
134   gint32 (*write_i64) (ThriftProtocol *protocol, const gint64 value,
135                        GError **error);
136   gint32 (*write_double) (ThriftProtocol *protocol, const gdouble value,
137                           GError **error);
138   gint32 (*write_string) (ThriftProtocol *protocol, const gchar *str,
139                           GError **error);
140   gint32 (*write_binary) (ThriftProtocol *protocol, const gpointer buf,
141                           const guint32 len, GError **error);
142 
143   gint32 (*read_message_begin) (ThriftProtocol *thrift_protocol, gchar **name,
144                                 ThriftMessageType *message_type,
145                                 gint32 *seqid, GError **error);
146   gint32 (*read_message_end) (ThriftProtocol *protocol, GError **error);
147   gint32 (*read_struct_begin) (ThriftProtocol *protocol, gchar **name,
148                                GError **error);
149   gint32 (*read_struct_end) (ThriftProtocol *protocol, GError **error);
150   gint32 (*read_field_begin) (ThriftProtocol *protocol, gchar **name,
151                               ThriftType *field_type, gint16 *field_id,
152                               GError **error);
153   gint32 (*read_field_end) (ThriftProtocol *protocol, GError **error);
154   gint32 (*read_map_begin) (ThriftProtocol *protocol, ThriftType *key_type,
155                             ThriftType *value_type, guint32 *size,
156                             GError **error);
157   gint32 (*read_map_end) (ThriftProtocol *protocol, GError **error);
158   gint32 (*read_list_begin) (ThriftProtocol *protocol, ThriftType *element_type,
159                              guint32 *size, GError **error);
160   gint32 (*read_list_end) (ThriftProtocol *protocol, GError **error);
161   gint32 (*read_set_begin) (ThriftProtocol *protocol, ThriftType *element_type,
162                             guint32 *size, GError **error);
163   gint32 (*read_set_end) (ThriftProtocol *protocol, GError **error);
164   gint32 (*read_bool) (ThriftProtocol *protocol, gboolean *value,
165                        GError **error);
166   gint32 (*read_byte) (ThriftProtocol *protocol, gint8 *value, GError **error);
167   gint32 (*read_i16) (ThriftProtocol *protocol, gint16 *value, GError **error);
168   gint32 (*read_i32) (ThriftProtocol *protocol, gint32 *value, GError **error);
169   gint32 (*read_i64) (ThriftProtocol *protocol, gint64 *value, GError **error);
170   gint32 (*read_double) (ThriftProtocol *protocol, gdouble *value,
171                          GError **error);
172   gint32 (*read_string) (ThriftProtocol *protocol, gchar **str, GError **error);
173   gint32 (*read_binary) (ThriftProtocol *protocol, gpointer *buf,
174                          guint32 *len, GError **error);
175   gint (*get_min_serialized_size) (ThriftProtocol *protocol, ThriftType type, GError **error);
176 };
177 
178 /* used by THRIFT_TYPE_PROTOCOL */
179 GType thrift_protocol_get_type (void);
180 
181 /* virtual public methods */
182 gint32 thrift_protocol_write_message_begin (ThriftProtocol *protocol,
183            const gchar *name, const ThriftMessageType message_type,
184            const gint32 seqid, GError **error);
185 
186 gint32 thrift_protocol_write_message_end (ThriftProtocol *protocol,
187                                           GError **error);
188 
189 gint32 thrift_protocol_write_struct_begin (ThriftProtocol *protocol,
190                                            const gchar *name,
191                                            GError **error);
192 
193 gint32 thrift_protocol_write_struct_end (ThriftProtocol *protocol,
194                                          GError **error);
195 
196 gint32 thrift_protocol_write_field_begin (ThriftProtocol *protocol,
197                                           const gchar *name,
198                                           const ThriftType field_type,
199                                           const gint16 field_id,
200                                           GError **error);
201 
202 gint32 thrift_protocol_write_field_end (ThriftProtocol *protocol,
203                                         GError **error);
204 
205 gint32 thrift_protocol_write_field_stop (ThriftProtocol *protocol,
206                                          GError **error);
207 
208 gint32 thrift_protocol_write_map_begin (ThriftProtocol *protocol,
209                                         const ThriftType key_type,
210                                         const ThriftType value_type,
211                                         const guint32 size, GError **error);
212 
213 gint32 thrift_protocol_write_map_end (ThriftProtocol *protocol, GError **error);
214 
215 gint32 thrift_protocol_write_list_begin (ThriftProtocol *protocol,
216                                          const ThriftType element_type,
217                                          const guint32 size, GError **error);
218 
219 gint32 thrift_protocol_write_list_end (ThriftProtocol *protocol,
220                                        GError **error);
221 
222 gint32 thrift_protocol_write_set_begin (ThriftProtocol *protocol,
223                                         const ThriftType element_type,
224                                         const guint32 size, GError **error);
225 
226 gint32 thrift_protocol_write_set_end (ThriftProtocol *protocol,
227                                       GError **error);
228 
229 gint32 thrift_protocol_write_bool (ThriftProtocol *protocol,
230                                    const gboolean value, GError **error);
231 
232 gint32 thrift_protocol_write_byte (ThriftProtocol *protocol, const gint8 value,
233                                    GError **error);
234 
235 gint32 thrift_protocol_write_i16 (ThriftProtocol *protocol, const gint16 value,
236                                   GError **error);
237 
238 gint32 thrift_protocol_write_i32 (ThriftProtocol *protocol, const gint32 value,
239                                   GError **error);
240 
241 gint32 thrift_protocol_write_i64 (ThriftProtocol *protocol, const gint64 value,
242                                   GError **error);
243 
244 gint32 thrift_protocol_write_double (ThriftProtocol *protocol,
245                                      const gdouble value, GError **error);
246 
247 gint32 thrift_protocol_write_string (ThriftProtocol *protocol,
248                                      const gchar *str, GError **error);
249 
250 gint32 thrift_protocol_write_binary (ThriftProtocol *protocol,
251                                      const gpointer buf,
252                                      const guint32 len, GError **error);
253 
254 gint32 thrift_protocol_read_message_begin (ThriftProtocol *thrift_protocol,
255                                            gchar **name,
256                                            ThriftMessageType *message_type,
257                                            gint32 *seqid, GError **error);
258 
259 gint32 thrift_protocol_read_message_end (ThriftProtocol *protocol,
260                                          GError **error);
261 
262 gint32 thrift_protocol_read_struct_begin (ThriftProtocol *protocol,
263                                           gchar **name,
264                                           GError **error);
265 
266 gint32 thrift_protocol_read_struct_end (ThriftProtocol *protocol,
267                                         GError **error);
268 
269 gint32 thrift_protocol_read_field_begin (ThriftProtocol *protocol,
270                                          gchar **name,
271                                          ThriftType *field_type,
272                                          gint16 *field_id,
273                                          GError **error);
274 
275 gint32 thrift_protocol_read_field_end (ThriftProtocol *protocol,
276                                        GError **error);
277 
278 gint32 thrift_protocol_read_map_begin (ThriftProtocol *protocol,
279                                        ThriftType *key_type,
280                                        ThriftType *value_type, guint32 *size,
281                                        GError **error);
282 
283 gint32 thrift_protocol_read_map_end (ThriftProtocol *protocol, GError **error);
284 
285 gint32 thrift_protocol_read_list_begin (ThriftProtocol *protocol,
286                                         ThriftType *element_type,
287                                         guint32 *size, GError **error);
288 
289 gint32 thrift_protocol_read_list_end (ThriftProtocol *protocol, GError **error);
290 
291 gint32 thrift_protocol_read_set_begin (ThriftProtocol *protocol,
292                                        ThriftType *element_type,
293                                        guint32 *size, GError **error);
294 
295 gint32 thrift_protocol_read_set_end (ThriftProtocol *protocol, GError **error);
296 
297 gint32 thrift_protocol_read_bool (ThriftProtocol *protocol, gboolean *value,
298                                   GError **error);
299 
300 gint32 thrift_protocol_read_byte (ThriftProtocol *protocol, gint8 *value,
301                                   GError **error);
302 
303 gint32 thrift_protocol_read_i16 (ThriftProtocol *protocol, gint16 *value,
304                                  GError **error);
305 
306 gint32 thrift_protocol_read_i32 (ThriftProtocol *protocol, gint32 *value,
307                                  GError **error);
308 
309 gint32 thrift_protocol_read_i64 (ThriftProtocol *protocol, gint64 *value,
310                                  GError **error);
311 
312 gint32 thrift_protocol_read_double (ThriftProtocol *protocol,
313                                     gdouble *value, GError **error);
314 
315 gint32 thrift_protocol_read_string (ThriftProtocol *protocol,
316                                     gchar **str, GError **error);
317 
318 gint32 thrift_protocol_read_binary (ThriftProtocol *protocol,
319                                     gpointer *buf, guint32 *len,
320                                     GError **error);
321 
322 gint thrift_protocol_get_min_serialized_size (ThriftProtocol *protocol,
323 		                              ThriftType type, GError **error);
324 
325 gint32 thrift_protocol_skip (ThriftProtocol *protocol, ThriftType type,
326                              GError **error);
327 
328 /* define error types */
329 typedef enum
330 {
331   THRIFT_PROTOCOL_ERROR_UNKNOWN,
332   THRIFT_PROTOCOL_ERROR_INVALID_DATA,
333   THRIFT_PROTOCOL_ERROR_NEGATIVE_SIZE,
334   THRIFT_PROTOCOL_ERROR_SIZE_LIMIT,
335   THRIFT_PROTOCOL_ERROR_BAD_VERSION,
336   THRIFT_PROTOCOL_ERROR_NOT_IMPLEMENTED,
337   THRIFT_PROTOCOL_ERROR_DEPTH_LIMIT
338 } ThriftProtocolError;
339 
340 /* define an error domain for GError to use */
341 GQuark thrift_protocol_error_quark (void);
342 #define THRIFT_PROTOCOL_ERROR (thrift_protocol_error_quark ())
343 
344 G_END_DECLS
345 
346 #endif /* _THRIFT_PROTOCOL_H */
347