tiny_thrift 1.0.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (90) hide show
  1. checksums.yaml +7 -0
  2. data/README +43 -0
  3. data/benchmark/Benchmark.thrift +24 -0
  4. data/benchmark/benchmark.rb +271 -0
  5. data/benchmark/client.rb +74 -0
  6. data/benchmark/server.rb +82 -0
  7. data/benchmark/thin_server.rb +44 -0
  8. data/ext/binary_protocol_accelerated.c +460 -0
  9. data/ext/binary_protocol_accelerated.h +20 -0
  10. data/ext/bytes.c +36 -0
  11. data/ext/bytes.h +31 -0
  12. data/ext/compact_protocol.c +635 -0
  13. data/ext/compact_protocol.h +20 -0
  14. data/ext/constants.h +96 -0
  15. data/ext/extconf.rb +32 -0
  16. data/ext/macros.h +41 -0
  17. data/ext/memory_buffer.c +134 -0
  18. data/ext/memory_buffer.h +20 -0
  19. data/ext/protocol.c +0 -0
  20. data/ext/protocol.h +0 -0
  21. data/ext/strlcpy.c +41 -0
  22. data/ext/strlcpy.h +34 -0
  23. data/ext/struct.c +688 -0
  24. data/ext/struct.h +25 -0
  25. data/ext/thrift_native.c +195 -0
  26. data/lib/thrift.rb +66 -0
  27. data/lib/thrift/bytes.rb +131 -0
  28. data/lib/thrift/client.rb +62 -0
  29. data/lib/thrift/core_ext.rb +23 -0
  30. data/lib/thrift/core_ext/fixnum.rb +29 -0
  31. data/lib/thrift/exceptions.rb +87 -0
  32. data/lib/thrift/processor.rb +57 -0
  33. data/lib/thrift/protocol/base_protocol.rb +377 -0
  34. data/lib/thrift/protocol/binary_protocol.rb +237 -0
  35. data/lib/thrift/protocol/binary_protocol_accelerated.rb +39 -0
  36. data/lib/thrift/protocol/compact_protocol.rb +434 -0
  37. data/lib/thrift/protocol/json_protocol.rb +769 -0
  38. data/lib/thrift/serializer/deserializer.rb +33 -0
  39. data/lib/thrift/serializer/serializer.rb +34 -0
  40. data/lib/thrift/server/base_server.rb +31 -0
  41. data/lib/thrift/server/mongrel_http_server.rb +60 -0
  42. data/lib/thrift/server/nonblocking_server.rb +305 -0
  43. data/lib/thrift/server/simple_server.rb +43 -0
  44. data/lib/thrift/server/thin_http_server.rb +91 -0
  45. data/lib/thrift/server/thread_pool_server.rb +75 -0
  46. data/lib/thrift/server/threaded_server.rb +47 -0
  47. data/lib/thrift/struct.rb +237 -0
  48. data/lib/thrift/struct_union.rb +192 -0
  49. data/lib/thrift/thrift_native.rb +24 -0
  50. data/lib/thrift/transport/base_server_transport.rb +37 -0
  51. data/lib/thrift/transport/base_transport.rb +109 -0
  52. data/lib/thrift/transport/buffered_transport.rb +114 -0
  53. data/lib/thrift/transport/framed_transport.rb +117 -0
  54. data/lib/thrift/transport/http_client_transport.rb +56 -0
  55. data/lib/thrift/transport/io_stream_transport.rb +39 -0
  56. data/lib/thrift/transport/memory_buffer_transport.rb +125 -0
  57. data/lib/thrift/transport/server_socket.rb +63 -0
  58. data/lib/thrift/transport/socket.rb +139 -0
  59. data/lib/thrift/transport/unix_server_socket.rb +60 -0
  60. data/lib/thrift/transport/unix_socket.rb +40 -0
  61. data/lib/thrift/types.rb +101 -0
  62. data/lib/thrift/union.rb +179 -0
  63. data/lib/tiny_thrift.rb +1 -0
  64. data/spec/ThriftSpec.thrift +183 -0
  65. data/spec/base_protocol_spec.rb +217 -0
  66. data/spec/base_transport_spec.rb +350 -0
  67. data/spec/binary_protocol_accelerated_spec.rb +42 -0
  68. data/spec/binary_protocol_spec.rb +66 -0
  69. data/spec/binary_protocol_spec_shared.rb +455 -0
  70. data/spec/bytes_spec.rb +160 -0
  71. data/spec/client_spec.rb +99 -0
  72. data/spec/compact_protocol_spec.rb +143 -0
  73. data/spec/exception_spec.rb +141 -0
  74. data/spec/http_client_spec.rb +120 -0
  75. data/spec/json_protocol_spec.rb +513 -0
  76. data/spec/nonblocking_server_spec.rb +263 -0
  77. data/spec/processor_spec.rb +80 -0
  78. data/spec/serializer_spec.rb +67 -0
  79. data/spec/server_socket_spec.rb +79 -0
  80. data/spec/server_spec.rb +147 -0
  81. data/spec/socket_spec.rb +61 -0
  82. data/spec/socket_spec_shared.rb +104 -0
  83. data/spec/spec_helper.rb +61 -0
  84. data/spec/struct_nested_containers_spec.rb +191 -0
  85. data/spec/struct_spec.rb +293 -0
  86. data/spec/thin_http_server_spec.rb +141 -0
  87. data/spec/types_spec.rb +115 -0
  88. data/spec/union_spec.rb +203 -0
  89. data/spec/unix_socket_spec.rb +107 -0
  90. metadata +313 -0
@@ -0,0 +1,20 @@
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
+ void Init_binary_protocol_accelerated();
@@ -0,0 +1,36 @@
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 <ruby.h>
21
+ #ifdef HAVE_RUBY_ENCODING_H
22
+ #include <ruby/encoding.h>
23
+ #endif
24
+ #include <constants.h>
25
+
26
+ VALUE force_binary_encoding(VALUE buffer) {
27
+ return rb_funcall(thrift_bytes_module, force_binary_encoding_id, 1, buffer);
28
+ }
29
+
30
+ VALUE convert_to_utf8_byte_buffer(VALUE string) {
31
+ return rb_funcall(thrift_bytes_module, convert_to_utf8_byte_buffer_id, 1, string);
32
+ }
33
+
34
+ VALUE convert_to_string(VALUE utf8_buffer) {
35
+ return rb_funcall(thrift_bytes_module, convert_to_string_id, 1, utf8_buffer);
36
+ }
@@ -0,0 +1,31 @@
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 <ruby.h>
21
+
22
+ /*
23
+ * A collection of utilities for working with bytes and byte buffers.
24
+ *
25
+ * These methods are the native analogies to some of the methods in
26
+ * Thrift::Bytes (thrift/bytes.rb).
27
+ */
28
+
29
+ VALUE force_binary_encoding(VALUE buffer);
30
+ VALUE convert_to_utf8_byte_buffer(VALUE string);
31
+ VALUE convert_to_string(VALUE utf8_buffer);
@@ -0,0 +1,635 @@
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 <ruby.h>
21
+ #include <stdbool.h>
22
+ #include <stdint.h>
23
+ #include <constants.h>
24
+ #include <struct.h>
25
+ #include <macros.h>
26
+ #include <bytes.h>
27
+
28
+ #define LAST_ID(obj) FIX2INT(rb_ary_pop(rb_ivar_get(obj, last_field_id)))
29
+ #define SET_LAST_ID(obj, val) rb_ary_push(rb_ivar_get(obj, last_field_id), val)
30
+
31
+ VALUE rb_thrift_compact_proto_native_qmark(VALUE self) {
32
+ return Qtrue;
33
+ }
34
+
35
+ static ID last_field_id;
36
+ static ID boolean_field_id;
37
+ static ID bool_value_id;
38
+ static ID rbuf_ivar_id;
39
+
40
+ static int VERSION;
41
+ static int VERSION_MASK;
42
+ static int TYPE_MASK;
43
+ static int TYPE_SHIFT_AMOUNT;
44
+ static int PROTOCOL_ID;
45
+
46
+ static VALUE thrift_compact_protocol_class;
47
+
48
+ static int CTYPE_BOOLEAN_TRUE = 0x01;
49
+ static int CTYPE_BOOLEAN_FALSE = 0x02;
50
+ static int CTYPE_BYTE = 0x03;
51
+ static int CTYPE_I16 = 0x04;
52
+ static int CTYPE_I32 = 0x05;
53
+ static int CTYPE_I64 = 0x06;
54
+ static int CTYPE_DOUBLE = 0x07;
55
+ static int CTYPE_BINARY = 0x08;
56
+ static int CTYPE_LIST = 0x09;
57
+ static int CTYPE_SET = 0x0A;
58
+ static int CTYPE_MAP = 0x0B;
59
+ static int CTYPE_STRUCT = 0x0C;
60
+
61
+ VALUE rb_thrift_compact_proto_write_i16(VALUE self, VALUE i16);
62
+
63
+ // TODO: implement this
64
+ static int get_compact_type(VALUE type_value) {
65
+ int type = FIX2INT(type_value);
66
+ if (type == TTYPE_BOOL) {
67
+ return CTYPE_BOOLEAN_TRUE;
68
+ } else if (type == TTYPE_BYTE) {
69
+ return CTYPE_BYTE;
70
+ } else if (type == TTYPE_I16) {
71
+ return CTYPE_I16;
72
+ } else if (type == TTYPE_I32) {
73
+ return CTYPE_I32;
74
+ } else if (type == TTYPE_I64) {
75
+ return CTYPE_I64;
76
+ } else if (type == TTYPE_DOUBLE) {
77
+ return CTYPE_DOUBLE;
78
+ } else if (type == TTYPE_STRING) {
79
+ return CTYPE_BINARY;
80
+ } else if (type == TTYPE_LIST) {
81
+ return CTYPE_LIST;
82
+ } else if (type == TTYPE_SET) {
83
+ return CTYPE_SET;
84
+ } else if (type == TTYPE_MAP) {
85
+ return CTYPE_MAP;
86
+ } else if (type == TTYPE_STRUCT) {
87
+ return CTYPE_STRUCT;
88
+ } else {
89
+ char str[50];
90
+ sprintf(str, "don't know what type: %d", type);
91
+ rb_raise(rb_eStandardError, "%s", str);
92
+ return 0;
93
+ }
94
+ }
95
+
96
+ static void write_byte_direct(VALUE transport, int8_t b) {
97
+ WRITE(transport, (char*)&b, 1);
98
+ }
99
+
100
+ static void write_field_begin_internal(VALUE self, VALUE type, VALUE id_value, VALUE type_override) {
101
+ int id = FIX2INT(id_value);
102
+ int last_id = LAST_ID(self);
103
+ VALUE transport = GET_TRANSPORT(self);
104
+
105
+ // if there's a type override, use that.
106
+ int8_t type_to_write = RTEST(type_override) ? FIX2INT(type_override) : get_compact_type(type);
107
+ // check if we can use delta encoding for the field id
108
+ int diff = id - last_id;
109
+ if (diff > 0 && diff <= 15) {
110
+ // write them together
111
+ write_byte_direct(transport, diff << 4 | (type_to_write & 0x0f));
112
+ } else {
113
+ // write them separate
114
+ write_byte_direct(transport, type_to_write & 0x0f);
115
+ rb_thrift_compact_proto_write_i16(self, id_value);
116
+ }
117
+
118
+ SET_LAST_ID(self, id_value);
119
+ }
120
+
121
+ static int32_t int_to_zig_zag(int32_t n) {
122
+ return (n << 1) ^ (n >> 31);
123
+ }
124
+
125
+ static uint64_t ll_to_zig_zag(int64_t n) {
126
+ return (n << 1) ^ (n >> 63);
127
+ }
128
+
129
+ static void write_varint32(VALUE transport, uint32_t n) {
130
+ while (true) {
131
+ if ((n & ~0x7F) == 0) {
132
+ write_byte_direct(transport, n & 0x7f);
133
+ break;
134
+ } else {
135
+ write_byte_direct(transport, (n & 0x7F) | 0x80);
136
+ n = n >> 7;
137
+ }
138
+ }
139
+ }
140
+
141
+ static void write_varint64(VALUE transport, uint64_t n) {
142
+ while (true) {
143
+ if ((n & ~0x7F) == 0) {
144
+ write_byte_direct(transport, n & 0x7f);
145
+ break;
146
+ } else {
147
+ write_byte_direct(transport, (n & 0x7F) | 0x80);
148
+ n = n >> 7;
149
+ }
150
+ }
151
+ }
152
+
153
+ static void write_collection_begin(VALUE transport, VALUE elem_type, VALUE size_value) {
154
+ int size = FIX2INT(size_value);
155
+ if (size <= 14) {
156
+ write_byte_direct(transport, size << 4 | get_compact_type(elem_type));
157
+ } else {
158
+ write_byte_direct(transport, 0xf0 | get_compact_type(elem_type));
159
+ write_varint32(transport, size);
160
+ }
161
+ }
162
+
163
+
164
+ //--------------------------------
165
+ // interface writing methods
166
+ //--------------------------------
167
+
168
+ VALUE rb_thrift_compact_proto_write_i32(VALUE self, VALUE i32);
169
+ VALUE rb_thrift_compact_proto_write_string(VALUE self, VALUE str);
170
+ VALUE rb_thrift_compact_proto_write_binary(VALUE self, VALUE buf);
171
+
172
+ VALUE rb_thrift_compact_proto_write_message_end(VALUE self) {
173
+ return Qnil;
174
+ }
175
+
176
+ VALUE rb_thrift_compact_proto_write_struct_begin(VALUE self, VALUE name) {
177
+ rb_ary_push(rb_ivar_get(self, last_field_id), INT2FIX(0));
178
+ return Qnil;
179
+ }
180
+
181
+ VALUE rb_thrift_compact_proto_write_struct_end(VALUE self) {
182
+ rb_ary_pop(rb_ivar_get(self, last_field_id));
183
+ return Qnil;
184
+ }
185
+
186
+ VALUE rb_thrift_compact_proto_write_field_end(VALUE self) {
187
+ return Qnil;
188
+ }
189
+
190
+ VALUE rb_thrift_compact_proto_write_map_end(VALUE self) {
191
+ return Qnil;
192
+ }
193
+
194
+ VALUE rb_thrift_compact_proto_write_list_end(VALUE self) {
195
+ return Qnil;
196
+ }
197
+
198
+ VALUE rb_thrift_compact_proto_write_set_end(VALUE self) {
199
+ return Qnil;
200
+ }
201
+
202
+ VALUE rb_thrift_compact_proto_write_message_begin(VALUE self, VALUE name, VALUE type, VALUE seqid) {
203
+ VALUE transport = GET_TRANSPORT(self);
204
+ write_byte_direct(transport, PROTOCOL_ID);
205
+ write_byte_direct(transport, (VERSION & VERSION_MASK) | ((FIX2INT(type) << TYPE_SHIFT_AMOUNT) & TYPE_MASK));
206
+ write_varint32(transport, FIX2INT(seqid));
207
+ rb_thrift_compact_proto_write_string(self, name);
208
+
209
+ return Qnil;
210
+ }
211
+
212
+ VALUE rb_thrift_compact_proto_write_field_begin(VALUE self, VALUE name, VALUE type, VALUE id) {
213
+ if (FIX2INT(type) == TTYPE_BOOL) {
214
+ // we want to possibly include the value, so we'll wait.
215
+ rb_ivar_set(self, boolean_field_id, rb_ary_new3(2, type, id));
216
+ } else {
217
+ write_field_begin_internal(self, type, id, Qnil);
218
+ }
219
+
220
+ return Qnil;
221
+ }
222
+
223
+ VALUE rb_thrift_compact_proto_write_field_stop(VALUE self) {
224
+ write_byte_direct(GET_TRANSPORT(self), TTYPE_STOP);
225
+ return Qnil;
226
+ }
227
+
228
+ VALUE rb_thrift_compact_proto_write_map_begin(VALUE self, VALUE ktype, VALUE vtype, VALUE size_value) {
229
+ int size = FIX2INT(size_value);
230
+ VALUE transport = GET_TRANSPORT(self);
231
+ if (size == 0) {
232
+ write_byte_direct(transport, 0);
233
+ } else {
234
+ write_varint32(transport, size);
235
+ write_byte_direct(transport, get_compact_type(ktype) << 4 | get_compact_type(vtype));
236
+ }
237
+ return Qnil;
238
+ }
239
+
240
+ VALUE rb_thrift_compact_proto_write_list_begin(VALUE self, VALUE etype, VALUE size) {
241
+ write_collection_begin(GET_TRANSPORT(self), etype, size);
242
+ return Qnil;
243
+ }
244
+
245
+ VALUE rb_thrift_compact_proto_write_set_begin(VALUE self, VALUE etype, VALUE size) {
246
+ write_collection_begin(GET_TRANSPORT(self), etype, size);
247
+ return Qnil;
248
+ }
249
+
250
+ VALUE rb_thrift_compact_proto_write_bool(VALUE self, VALUE b) {
251
+ int8_t type = b == Qtrue ? CTYPE_BOOLEAN_TRUE : CTYPE_BOOLEAN_FALSE;
252
+ VALUE boolean_field = rb_ivar_get(self, boolean_field_id);
253
+ if (NIL_P(boolean_field)) {
254
+ // we're not part of a field, so just write the value.
255
+ write_byte_direct(GET_TRANSPORT(self), type);
256
+ } else {
257
+ // we haven't written the field header yet
258
+ write_field_begin_internal(self, rb_ary_entry(boolean_field, 0), rb_ary_entry(boolean_field, 1), INT2FIX(type));
259
+ rb_ivar_set(self, boolean_field_id, Qnil);
260
+ }
261
+ return Qnil;
262
+ }
263
+
264
+ VALUE rb_thrift_compact_proto_write_byte(VALUE self, VALUE byte) {
265
+ CHECK_NIL(byte);
266
+ write_byte_direct(GET_TRANSPORT(self), FIX2INT(byte));
267
+ return Qnil;
268
+ }
269
+
270
+ VALUE rb_thrift_compact_proto_write_i16(VALUE self, VALUE i16) {
271
+ rb_thrift_compact_proto_write_i32(self, i16);
272
+ return Qnil;
273
+ }
274
+
275
+ VALUE rb_thrift_compact_proto_write_i32(VALUE self, VALUE i32) {
276
+ CHECK_NIL(i32);
277
+ write_varint32(GET_TRANSPORT(self), int_to_zig_zag(NUM2INT(i32)));
278
+ return Qnil;
279
+ }
280
+
281
+ VALUE rb_thrift_compact_proto_write_i64(VALUE self, VALUE i64) {
282
+ CHECK_NIL(i64);
283
+ write_varint64(GET_TRANSPORT(self), ll_to_zig_zag(NUM2LL(i64)));
284
+ return Qnil;
285
+ }
286
+
287
+ VALUE rb_thrift_compact_proto_write_double(VALUE self, VALUE dub) {
288
+ CHECK_NIL(dub);
289
+ // Unfortunately, bitwise_cast doesn't work in C. Bad C!
290
+ union {
291
+ double f;
292
+ int64_t l;
293
+ } transfer;
294
+ transfer.f = RFLOAT_VALUE(rb_Float(dub));
295
+ char buf[8];
296
+ buf[0] = transfer.l & 0xff;
297
+ buf[1] = (transfer.l >> 8) & 0xff;
298
+ buf[2] = (transfer.l >> 16) & 0xff;
299
+ buf[3] = (transfer.l >> 24) & 0xff;
300
+ buf[4] = (transfer.l >> 32) & 0xff;
301
+ buf[5] = (transfer.l >> 40) & 0xff;
302
+ buf[6] = (transfer.l >> 48) & 0xff;
303
+ buf[7] = (transfer.l >> 56) & 0xff;
304
+ WRITE(GET_TRANSPORT(self), buf, 8);
305
+ return Qnil;
306
+ }
307
+
308
+ VALUE rb_thrift_compact_proto_write_string(VALUE self, VALUE str) {
309
+ str = convert_to_utf8_byte_buffer(str);
310
+ rb_thrift_compact_proto_write_binary(self, str);
311
+ return Qnil;
312
+ }
313
+
314
+ VALUE rb_thrift_compact_proto_write_binary(VALUE self, VALUE buf) {
315
+ buf = force_binary_encoding(buf);
316
+ VALUE transport = GET_TRANSPORT(self);
317
+ write_varint32(transport, RSTRING_LEN(buf));
318
+ WRITE(transport, StringValuePtr(buf), RSTRING_LEN(buf));
319
+ return Qnil;
320
+ }
321
+
322
+ //---------------------------------------
323
+ // interface reading methods
324
+ //---------------------------------------
325
+
326
+ #define is_bool_type(ctype) (((ctype) & 0x0F) == CTYPE_BOOLEAN_TRUE || ((ctype) & 0x0F) == CTYPE_BOOLEAN_FALSE)
327
+
328
+ VALUE rb_thrift_compact_proto_read_string(VALUE self);
329
+ VALUE rb_thrift_compact_proto_read_binary(VALUE self);
330
+ VALUE rb_thrift_compact_proto_read_byte(VALUE self);
331
+ VALUE rb_thrift_compact_proto_read_i32(VALUE self);
332
+ VALUE rb_thrift_compact_proto_read_i16(VALUE self);
333
+
334
+ static int8_t get_ttype(int8_t ctype) {
335
+ if (ctype == TTYPE_STOP) {
336
+ return TTYPE_STOP;
337
+ } else if (ctype == CTYPE_BOOLEAN_TRUE || ctype == CTYPE_BOOLEAN_FALSE) {
338
+ return TTYPE_BOOL;
339
+ } else if (ctype == CTYPE_BYTE) {
340
+ return TTYPE_BYTE;
341
+ } else if (ctype == CTYPE_I16) {
342
+ return TTYPE_I16;
343
+ } else if (ctype == CTYPE_I32) {
344
+ return TTYPE_I32;
345
+ } else if (ctype == CTYPE_I64) {
346
+ return TTYPE_I64;
347
+ } else if (ctype == CTYPE_DOUBLE) {
348
+ return TTYPE_DOUBLE;
349
+ } else if (ctype == CTYPE_BINARY) {
350
+ return TTYPE_STRING;
351
+ } else if (ctype == CTYPE_LIST) {
352
+ return TTYPE_LIST;
353
+ } else if (ctype == CTYPE_SET) {
354
+ return TTYPE_SET;
355
+ } else if (ctype == CTYPE_MAP) {
356
+ return TTYPE_MAP;
357
+ } else if (ctype == CTYPE_STRUCT) {
358
+ return TTYPE_STRUCT;
359
+ } else {
360
+ char str[50];
361
+ sprintf(str, "don't know what type: %d", ctype);
362
+ rb_raise(rb_eStandardError, "%s", str);
363
+ return 0;
364
+ }
365
+ }
366
+
367
+ static char read_byte_direct(VALUE self) {
368
+ VALUE byte = rb_funcall(GET_TRANSPORT(self), read_byte_method_id, 0);
369
+ return (char)(FIX2INT(byte));
370
+ }
371
+
372
+ static int64_t zig_zag_to_ll(int64_t n) {
373
+ return (((uint64_t)n) >> 1) ^ -(n & 1);
374
+ }
375
+
376
+ static int32_t zig_zag_to_int(int32_t n) {
377
+ return (((uint32_t)n) >> 1) ^ -(n & 1);
378
+ }
379
+
380
+ static int64_t read_varint64(VALUE self) {
381
+ int shift = 0;
382
+ int64_t result = 0;
383
+ while (true) {
384
+ int8_t b = read_byte_direct(self);
385
+ result = result | ((uint64_t)(b & 0x7f) << shift);
386
+ if ((b & 0x80) != 0x80) {
387
+ break;
388
+ }
389
+ shift += 7;
390
+ }
391
+ return result;
392
+ }
393
+
394
+ static int16_t read_i16(VALUE self) {
395
+ return zig_zag_to_int((int32_t)read_varint64(self));
396
+ }
397
+
398
+ static VALUE get_protocol_exception(VALUE code, VALUE message) {
399
+ VALUE args[2];
400
+ args[0] = code;
401
+ args[1] = message;
402
+ return rb_class_new_instance(2, (VALUE*)&args, protocol_exception_class);
403
+ }
404
+
405
+ VALUE rb_thrift_compact_proto_read_message_end(VALUE self) {
406
+ return Qnil;
407
+ }
408
+
409
+ VALUE rb_thrift_compact_proto_read_struct_begin(VALUE self) {
410
+ rb_ary_push(rb_ivar_get(self, last_field_id), INT2FIX(0));
411
+ return Qnil;
412
+ }
413
+
414
+ VALUE rb_thrift_compact_proto_read_struct_end(VALUE self) {
415
+ rb_ary_pop(rb_ivar_get(self, last_field_id));
416
+ return Qnil;
417
+ }
418
+
419
+ VALUE rb_thrift_compact_proto_read_field_end(VALUE self) {
420
+ return Qnil;
421
+ }
422
+
423
+ VALUE rb_thrift_compact_proto_read_map_end(VALUE self) {
424
+ return Qnil;
425
+ }
426
+
427
+ VALUE rb_thrift_compact_proto_read_list_end(VALUE self) {
428
+ return Qnil;
429
+ }
430
+
431
+ VALUE rb_thrift_compact_proto_read_set_end(VALUE self) {
432
+ return Qnil;
433
+ }
434
+
435
+ VALUE rb_thrift_compact_proto_read_message_begin(VALUE self) {
436
+ int8_t protocol_id = read_byte_direct(self);
437
+ if (protocol_id != PROTOCOL_ID) {
438
+ char buf[100];
439
+ int len = sprintf(buf, "Expected protocol id %d but got %d", PROTOCOL_ID, protocol_id);
440
+ buf[len] = 0;
441
+ rb_exc_raise(get_protocol_exception(INT2FIX(-1), rb_str_new2(buf)));
442
+ }
443
+
444
+ int8_t version_and_type = read_byte_direct(self);
445
+ int8_t version = version_and_type & VERSION_MASK;
446
+ if (version != VERSION) {
447
+ char buf[100];
448
+ int len = sprintf(buf, "Expected version id %d but got %d", version, VERSION);
449
+ buf[len] = 0;
450
+ rb_exc_raise(get_protocol_exception(INT2FIX(-1), rb_str_new2(buf)));
451
+ }
452
+
453
+ int8_t type = (version_and_type >> TYPE_SHIFT_AMOUNT) & 0x03;
454
+ int32_t seqid = read_varint64(self);
455
+ VALUE messageName = rb_thrift_compact_proto_read_string(self);
456
+ return rb_ary_new3(3, messageName, INT2FIX(type), INT2NUM(seqid));
457
+ }
458
+
459
+ VALUE rb_thrift_compact_proto_read_field_begin(VALUE self) {
460
+ int8_t type = read_byte_direct(self);
461
+ // if it's a stop, then we can return immediately, as the struct is over.
462
+ if ((type & 0x0f) == TTYPE_STOP) {
463
+ return rb_ary_new3(3, Qnil, INT2FIX(0), INT2FIX(0));
464
+ } else {
465
+ int field_id = 0;
466
+
467
+ // mask off the 4 MSB of the type header. it could contain a field id delta.
468
+ uint8_t modifier = ((type & 0xf0) >> 4);
469
+
470
+ if (modifier == 0) {
471
+ // not a delta. look ahead for the zigzag varint field id.
472
+ (void) LAST_ID(self);
473
+ field_id = read_i16(self);
474
+ } else {
475
+ // has a delta. add the delta to the last read field id.
476
+ field_id = LAST_ID(self) + modifier;
477
+ }
478
+
479
+ // if this happens to be a boolean field, the value is encoded in the type
480
+ if (is_bool_type(type)) {
481
+ // save the boolean value in a special instance variable.
482
+ rb_ivar_set(self, bool_value_id, (type & 0x0f) == CTYPE_BOOLEAN_TRUE ? Qtrue : Qfalse);
483
+ }
484
+
485
+ // push the new field onto the field stack so we can keep the deltas going.
486
+ SET_LAST_ID(self, INT2FIX(field_id));
487
+ return rb_ary_new3(3, Qnil, INT2FIX(get_ttype(type & 0x0f)), INT2FIX(field_id));
488
+ }
489
+ }
490
+
491
+ VALUE rb_thrift_compact_proto_read_map_begin(VALUE self) {
492
+ int32_t size = read_varint64(self);
493
+ uint8_t key_and_value_type = size == 0 ? 0 : read_byte_direct(self);
494
+ return rb_ary_new3(3, INT2FIX(get_ttype(key_and_value_type >> 4)), INT2FIX(get_ttype(key_and_value_type & 0xf)), INT2FIX(size));
495
+ }
496
+
497
+ VALUE rb_thrift_compact_proto_read_list_begin(VALUE self) {
498
+ uint8_t size_and_type = read_byte_direct(self);
499
+ int32_t size = (size_and_type >> 4) & 0x0f;
500
+ if (size == 15) {
501
+ size = read_varint64(self);
502
+ }
503
+ uint8_t type = get_ttype(size_and_type & 0x0f);
504
+ return rb_ary_new3(2, INT2FIX(type), INT2FIX(size));
505
+ }
506
+
507
+ VALUE rb_thrift_compact_proto_read_set_begin(VALUE self) {
508
+ return rb_thrift_compact_proto_read_list_begin(self);
509
+ }
510
+
511
+ VALUE rb_thrift_compact_proto_read_bool(VALUE self) {
512
+ VALUE bool_value = rb_ivar_get(self, bool_value_id);
513
+ if (NIL_P(bool_value)) {
514
+ return read_byte_direct(self) == CTYPE_BOOLEAN_TRUE ? Qtrue : Qfalse;
515
+ } else {
516
+ rb_ivar_set(self, bool_value_id, Qnil);
517
+ return bool_value;
518
+ }
519
+ }
520
+
521
+ VALUE rb_thrift_compact_proto_read_byte(VALUE self) {
522
+ return INT2FIX(read_byte_direct(self));
523
+ }
524
+
525
+ VALUE rb_thrift_compact_proto_read_i16(VALUE self) {
526
+ return INT2FIX(read_i16(self));
527
+ }
528
+
529
+ VALUE rb_thrift_compact_proto_read_i32(VALUE self) {
530
+ return INT2NUM(zig_zag_to_int(read_varint64(self)));
531
+ }
532
+
533
+ VALUE rb_thrift_compact_proto_read_i64(VALUE self) {
534
+ return LL2NUM(zig_zag_to_ll(read_varint64(self)));
535
+ }
536
+
537
+ VALUE rb_thrift_compact_proto_read_double(VALUE self) {
538
+ union {
539
+ double f;
540
+ int64_t l;
541
+ } transfer;
542
+ VALUE rbuf = rb_ivar_get(self, rbuf_ivar_id);
543
+ rb_funcall(GET_TRANSPORT(self), read_into_buffer_method_id, 2, rbuf, INT2FIX(8));
544
+ uint32_t lo = ((uint8_t)(RSTRING_PTR(rbuf)[0]))
545
+ | (((uint8_t)(RSTRING_PTR(rbuf)[1])) << 8)
546
+ | (((uint8_t)(RSTRING_PTR(rbuf)[2])) << 16)
547
+ | (((uint8_t)(RSTRING_PTR(rbuf)[3])) << 24);
548
+ uint64_t hi = (((uint8_t)(RSTRING_PTR(rbuf)[4])))
549
+ | (((uint8_t)(RSTRING_PTR(rbuf)[5])) << 8)
550
+ | (((uint8_t)(RSTRING_PTR(rbuf)[6])) << 16)
551
+ | (((uint8_t)(RSTRING_PTR(rbuf)[7])) << 24);
552
+ transfer.l = (hi << 32) | lo;
553
+
554
+ return rb_float_new(transfer.f);
555
+ }
556
+
557
+ VALUE rb_thrift_compact_proto_read_string(VALUE self) {
558
+ VALUE buffer = rb_thrift_compact_proto_read_binary(self);
559
+ return convert_to_string(buffer);
560
+ }
561
+
562
+ VALUE rb_thrift_compact_proto_read_binary(VALUE self) {
563
+ int64_t size = read_varint64(self);
564
+ return READ(self, size);
565
+ }
566
+
567
+ static void Init_constants() {
568
+ thrift_compact_protocol_class = rb_const_get(thrift_module, rb_intern("CompactProtocol"));
569
+
570
+ VERSION = rb_num2ll(rb_const_get(thrift_compact_protocol_class, rb_intern("VERSION")));
571
+ VERSION_MASK = rb_num2ll(rb_const_get(thrift_compact_protocol_class, rb_intern("VERSION_MASK")));
572
+ TYPE_MASK = rb_num2ll(rb_const_get(thrift_compact_protocol_class, rb_intern("TYPE_MASK")));
573
+ TYPE_SHIFT_AMOUNT = FIX2INT(rb_const_get(thrift_compact_protocol_class, rb_intern("TYPE_SHIFT_AMOUNT")));
574
+ PROTOCOL_ID = FIX2INT(rb_const_get(thrift_compact_protocol_class, rb_intern("PROTOCOL_ID")));
575
+
576
+ last_field_id = rb_intern("@last_field");
577
+ boolean_field_id = rb_intern("@boolean_field");
578
+ bool_value_id = rb_intern("@bool_value");
579
+ rbuf_ivar_id = rb_intern("@rbuf");
580
+ }
581
+
582
+ static void Init_rb_methods() {
583
+ rb_define_method(thrift_compact_protocol_class, "native?", rb_thrift_compact_proto_native_qmark, 0);
584
+
585
+ rb_define_method(thrift_compact_protocol_class, "write_message_begin", rb_thrift_compact_proto_write_message_begin, 3);
586
+ rb_define_method(thrift_compact_protocol_class, "write_field_begin", rb_thrift_compact_proto_write_field_begin, 3);
587
+ rb_define_method(thrift_compact_protocol_class, "write_field_stop", rb_thrift_compact_proto_write_field_stop, 0);
588
+ rb_define_method(thrift_compact_protocol_class, "write_map_begin", rb_thrift_compact_proto_write_map_begin, 3);
589
+ rb_define_method(thrift_compact_protocol_class, "write_list_begin", rb_thrift_compact_proto_write_list_begin, 2);
590
+ rb_define_method(thrift_compact_protocol_class, "write_set_begin", rb_thrift_compact_proto_write_set_begin, 2);
591
+ rb_define_method(thrift_compact_protocol_class, "write_byte", rb_thrift_compact_proto_write_byte, 1);
592
+ rb_define_method(thrift_compact_protocol_class, "write_bool", rb_thrift_compact_proto_write_bool, 1);
593
+ rb_define_method(thrift_compact_protocol_class, "write_i16", rb_thrift_compact_proto_write_i16, 1);
594
+ rb_define_method(thrift_compact_protocol_class, "write_i32", rb_thrift_compact_proto_write_i32, 1);
595
+ rb_define_method(thrift_compact_protocol_class, "write_i64", rb_thrift_compact_proto_write_i64, 1);
596
+ rb_define_method(thrift_compact_protocol_class, "write_double", rb_thrift_compact_proto_write_double, 1);
597
+ rb_define_method(thrift_compact_protocol_class, "write_string", rb_thrift_compact_proto_write_string, 1);
598
+ rb_define_method(thrift_compact_protocol_class, "write_binary", rb_thrift_compact_proto_write_binary, 1);
599
+
600
+ rb_define_method(thrift_compact_protocol_class, "write_message_end", rb_thrift_compact_proto_write_message_end, 0);
601
+ rb_define_method(thrift_compact_protocol_class, "write_struct_begin", rb_thrift_compact_proto_write_struct_begin, 1);
602
+ rb_define_method(thrift_compact_protocol_class, "write_struct_end", rb_thrift_compact_proto_write_struct_end, 0);
603
+ rb_define_method(thrift_compact_protocol_class, "write_field_end", rb_thrift_compact_proto_write_field_end, 0);
604
+ rb_define_method(thrift_compact_protocol_class, "write_map_end", rb_thrift_compact_proto_write_map_end, 0);
605
+ rb_define_method(thrift_compact_protocol_class, "write_list_end", rb_thrift_compact_proto_write_list_end, 0);
606
+ rb_define_method(thrift_compact_protocol_class, "write_set_end", rb_thrift_compact_proto_write_set_end, 0);
607
+
608
+
609
+ rb_define_method(thrift_compact_protocol_class, "read_message_begin", rb_thrift_compact_proto_read_message_begin, 0);
610
+ rb_define_method(thrift_compact_protocol_class, "read_field_begin", rb_thrift_compact_proto_read_field_begin, 0);
611
+ rb_define_method(thrift_compact_protocol_class, "read_map_begin", rb_thrift_compact_proto_read_map_begin, 0);
612
+ rb_define_method(thrift_compact_protocol_class, "read_list_begin", rb_thrift_compact_proto_read_list_begin, 0);
613
+ rb_define_method(thrift_compact_protocol_class, "read_set_begin", rb_thrift_compact_proto_read_set_begin, 0);
614
+ rb_define_method(thrift_compact_protocol_class, "read_byte", rb_thrift_compact_proto_read_byte, 0);
615
+ rb_define_method(thrift_compact_protocol_class, "read_bool", rb_thrift_compact_proto_read_bool, 0);
616
+ rb_define_method(thrift_compact_protocol_class, "read_i16", rb_thrift_compact_proto_read_i16, 0);
617
+ rb_define_method(thrift_compact_protocol_class, "read_i32", rb_thrift_compact_proto_read_i32, 0);
618
+ rb_define_method(thrift_compact_protocol_class, "read_i64", rb_thrift_compact_proto_read_i64, 0);
619
+ rb_define_method(thrift_compact_protocol_class, "read_double", rb_thrift_compact_proto_read_double, 0);
620
+ rb_define_method(thrift_compact_protocol_class, "read_string", rb_thrift_compact_proto_read_string, 0);
621
+ rb_define_method(thrift_compact_protocol_class, "read_binary", rb_thrift_compact_proto_read_binary, 0);
622
+
623
+ rb_define_method(thrift_compact_protocol_class, "read_message_end", rb_thrift_compact_proto_read_message_end, 0);
624
+ rb_define_method(thrift_compact_protocol_class, "read_struct_begin", rb_thrift_compact_proto_read_struct_begin, 0);
625
+ rb_define_method(thrift_compact_protocol_class, "read_struct_end", rb_thrift_compact_proto_read_struct_end, 0);
626
+ rb_define_method(thrift_compact_protocol_class, "read_field_end", rb_thrift_compact_proto_read_field_end, 0);
627
+ rb_define_method(thrift_compact_protocol_class, "read_map_end", rb_thrift_compact_proto_read_map_end, 0);
628
+ rb_define_method(thrift_compact_protocol_class, "read_list_end", rb_thrift_compact_proto_read_list_end, 0);
629
+ rb_define_method(thrift_compact_protocol_class, "read_set_end", rb_thrift_compact_proto_read_set_end, 0);
630
+ }
631
+
632
+ void Init_compact_protocol() {
633
+ Init_constants();
634
+ Init_rb_methods();
635
+ }