thrift-mavericks 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (95) hide show
  1. checksums.yaml +15 -0
  2. data/CHANGELOG +1 -0
  3. data/README +43 -0
  4. data/benchmark/Benchmark.thrift +24 -0
  5. data/benchmark/benchmark.rb +271 -0
  6. data/benchmark/client.rb +74 -0
  7. data/benchmark/gen-rb/benchmark_constants.rb +11 -0
  8. data/benchmark/gen-rb/benchmark_service.rb +80 -0
  9. data/benchmark/gen-rb/benchmark_types.rb +10 -0
  10. data/benchmark/server.rb +82 -0
  11. data/benchmark/thin_server.rb +44 -0
  12. data/ext/binary_protocol_accelerated.c +441 -0
  13. data/ext/binary_protocol_accelerated.h +20 -0
  14. data/ext/compact_protocol.c +618 -0
  15. data/ext/compact_protocol.h +20 -0
  16. data/ext/constants.h +96 -0
  17. data/ext/extconf.rb +30 -0
  18. data/ext/macros.h +41 -0
  19. data/ext/memory_buffer.c +131 -0
  20. data/ext/memory_buffer.h +20 -0
  21. data/ext/protocol.c +185 -0
  22. data/ext/protocol.h +20 -0
  23. data/ext/strlcpy.c +41 -0
  24. data/ext/strlcpy.h +32 -0
  25. data/ext/struct.c +691 -0
  26. data/ext/struct.h +25 -0
  27. data/ext/thrift_native.c +196 -0
  28. data/lib/thrift.rb +64 -0
  29. data/lib/thrift/client.rb +62 -0
  30. data/lib/thrift/core_ext.rb +23 -0
  31. data/lib/thrift/core_ext/fixnum.rb +29 -0
  32. data/lib/thrift/exceptions.rb +84 -0
  33. data/lib/thrift/processor.rb +57 -0
  34. data/lib/thrift/protocol/base_protocol.rb +290 -0
  35. data/lib/thrift/protocol/binary_protocol.rb +229 -0
  36. data/lib/thrift/protocol/binary_protocol_accelerated.rb +39 -0
  37. data/lib/thrift/protocol/compact_protocol.rb +426 -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 +58 -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/thread_pool_server.rb +75 -0
  45. data/lib/thrift/server/threaded_server.rb +47 -0
  46. data/lib/thrift/struct.rb +237 -0
  47. data/lib/thrift/struct_union.rb +192 -0
  48. data/lib/thrift/thrift_native.rb +24 -0
  49. data/lib/thrift/transport/base_server_transport.rb +37 -0
  50. data/lib/thrift/transport/base_transport.rb +107 -0
  51. data/lib/thrift/transport/buffered_transport.rb +108 -0
  52. data/lib/thrift/transport/framed_transport.rb +116 -0
  53. data/lib/thrift/transport/http_client_transport.rb +51 -0
  54. data/lib/thrift/transport/io_stream_transport.rb +39 -0
  55. data/lib/thrift/transport/memory_buffer_transport.rb +125 -0
  56. data/lib/thrift/transport/server_socket.rb +63 -0
  57. data/lib/thrift/transport/socket.rb +137 -0
  58. data/lib/thrift/transport/unix_server_socket.rb +60 -0
  59. data/lib/thrift/transport/unix_socket.rb +40 -0
  60. data/lib/thrift/types.rb +101 -0
  61. data/lib/thrift/union.rb +179 -0
  62. data/spec/ThriftSpec.thrift +132 -0
  63. data/spec/base_protocol_spec.rb +160 -0
  64. data/spec/base_transport_spec.rb +351 -0
  65. data/spec/binary_protocol_accelerated_spec.rb +46 -0
  66. data/spec/binary_protocol_spec.rb +61 -0
  67. data/spec/binary_protocol_spec_shared.rb +375 -0
  68. data/spec/client_spec.rb +100 -0
  69. data/spec/compact_protocol_spec.rb +144 -0
  70. data/spec/exception_spec.rb +142 -0
  71. data/spec/gen-rb/nonblocking_service.rb +272 -0
  72. data/spec/gen-rb/thrift_spec_constants.rb +11 -0
  73. data/spec/gen-rb/thrift_spec_types.rb +346 -0
  74. data/spec/http_client_spec.rb +64 -0
  75. data/spec/mongrel_http_server_spec.rb +117 -0
  76. data/spec/nonblocking_server_spec.rb +265 -0
  77. data/spec/processor_spec.rb +83 -0
  78. data/spec/serializer_spec.rb +69 -0
  79. data/spec/server_socket_spec.rb +80 -0
  80. data/spec/server_spec.rb +159 -0
  81. data/spec/socket_spec.rb +61 -0
  82. data/spec/socket_spec_shared.rb +104 -0
  83. data/spec/spec_helper.rb +58 -0
  84. data/spec/struct_spec.rb +295 -0
  85. data/spec/types_spec.rb +116 -0
  86. data/spec/union_spec.rb +193 -0
  87. data/spec/unix_socket_spec.rb +108 -0
  88. data/test/debug_proto/gen-rb/debug_proto_test_constants.rb +274 -0
  89. data/test/debug_proto/gen-rb/debug_proto_test_types.rb +761 -0
  90. data/test/debug_proto/gen-rb/empty_service.rb +24 -0
  91. data/test/debug_proto/gen-rb/inherited.rb +79 -0
  92. data/test/debug_proto/gen-rb/reverse_order_service.rb +82 -0
  93. data/test/debug_proto/gen-rb/service_for_exception_with_a_map.rb +81 -0
  94. data/test/debug_proto/gen-rb/srv.rb +330 -0
  95. metadata +281 -0
@@ -0,0 +1,80 @@
1
+ #
2
+ # Autogenerated by Thrift Compiler (0.9.0)
3
+ #
4
+ # DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
5
+ #
6
+
7
+ require 'thrift'
8
+ require 'benchmark_types'
9
+
10
+ module ThriftBenchmark
11
+ module BenchmarkService
12
+ class Client
13
+ include ::Thrift::Client
14
+
15
+ def fibonacci(n)
16
+ send_fibonacci(n)
17
+ return recv_fibonacci()
18
+ end
19
+
20
+ def send_fibonacci(n)
21
+ send_message('fibonacci', Fibonacci_args, :n => n)
22
+ end
23
+
24
+ def recv_fibonacci()
25
+ result = receive_message(Fibonacci_result)
26
+ return result.success unless result.success.nil?
27
+ raise ::Thrift::ApplicationException.new(::Thrift::ApplicationException::MISSING_RESULT, 'fibonacci failed: unknown result')
28
+ end
29
+
30
+ end
31
+
32
+ class Processor
33
+ include ::Thrift::Processor
34
+
35
+ def process_fibonacci(seqid, iprot, oprot)
36
+ args = read_args(iprot, Fibonacci_args)
37
+ result = Fibonacci_result.new()
38
+ result.success = @handler.fibonacci(args.n)
39
+ write_result(result, oprot, 'fibonacci', seqid)
40
+ end
41
+
42
+ end
43
+
44
+ # HELPER FUNCTIONS AND STRUCTURES
45
+
46
+ class Fibonacci_args
47
+ include ::Thrift::Struct, ::Thrift::Struct_Union
48
+ N = 1
49
+
50
+ FIELDS = {
51
+ N => {:type => ::Thrift::Types::BYTE, :name => 'n'}
52
+ }
53
+
54
+ def struct_fields; FIELDS; end
55
+
56
+ def validate
57
+ end
58
+
59
+ ::Thrift::Struct.generate_accessors self
60
+ end
61
+
62
+ class Fibonacci_result
63
+ include ::Thrift::Struct, ::Thrift::Struct_Union
64
+ SUCCESS = 0
65
+
66
+ FIELDS = {
67
+ SUCCESS => {:type => ::Thrift::Types::I32, :name => 'success'}
68
+ }
69
+
70
+ def struct_fields; FIELDS; end
71
+
72
+ def validate
73
+ end
74
+
75
+ ::Thrift::Struct.generate_accessors self
76
+ end
77
+
78
+ end
79
+
80
+ end
@@ -0,0 +1,10 @@
1
+ #
2
+ # Autogenerated by Thrift Compiler (0.9.0)
3
+ #
4
+ # DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
5
+ #
6
+
7
+ require 'thrift'
8
+
9
+ module ThriftBenchmark
10
+ end
@@ -0,0 +1,82 @@
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
+ $:.unshift File.dirname(__FILE__) + '/../lib'
21
+ require 'thrift'
22
+ $:.unshift File.dirname(__FILE__) + "/gen-rb"
23
+ require 'benchmark_service'
24
+
25
+ module Server
26
+ include Thrift
27
+
28
+ class BenchmarkHandler
29
+ # 1-based index into the fibonacci sequence
30
+ def fibonacci(n)
31
+ seq = [1, 1]
32
+ 3.upto(n) do
33
+ seq << seq[-1] + seq[-2]
34
+ end
35
+ seq[n-1] # n is 1-based
36
+ end
37
+ end
38
+
39
+ def self.start_server(host, port, serverClass)
40
+ handler = BenchmarkHandler.new
41
+ processor = ThriftBenchmark::BenchmarkService::Processor.new(handler)
42
+ transport = ServerSocket.new(host, port)
43
+ transport_factory = FramedTransportFactory.new
44
+ args = [processor, transport, transport_factory, nil, 20]
45
+ if serverClass == NonblockingServer
46
+ logger = Logger.new(STDERR)
47
+ logger.level = Logger::WARN
48
+ args << logger
49
+ end
50
+ server = serverClass.new(*args)
51
+ @server_thread = Thread.new do
52
+ server.serve
53
+ end
54
+ @server = server
55
+ end
56
+
57
+ def self.shutdown
58
+ return if @server.nil?
59
+ if @server.respond_to? :shutdown
60
+ @server.shutdown
61
+ else
62
+ @server_thread.kill
63
+ end
64
+ end
65
+ end
66
+
67
+ def resolve_const(const)
68
+ const and const.split('::').inject(Object) { |k,c| k.const_get(c) }
69
+ end
70
+
71
+ host, port, serverklass = ARGV
72
+
73
+ Server.start_server(host, port.to_i, resolve_const(serverklass))
74
+
75
+ # let our host know that the interpreter has started
76
+ # ideally we'd wait until the server was serving, but we don't have a hook for that
77
+ Marshal.dump(:started, STDOUT)
78
+ STDOUT.flush
79
+
80
+ Marshal.load(STDIN) # wait until we're instructed to shut down
81
+
82
+ Server.shutdown
@@ -0,0 +1,44 @@
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
+ $:.unshift File.dirname(__FILE__) + '/../lib'
21
+ require 'thrift'
22
+ $:.unshift File.dirname(__FILE__) + "/gen-rb"
23
+ require 'benchmark_service'
24
+ HOST = 'localhost'
25
+ PORT = 42587
26
+
27
+ class BenchmarkHandler
28
+ # 1-based index into the fibonacci sequence
29
+ def fibonacci(n)
30
+ seq = [1, 1]
31
+ 3.upto(n) do
32
+ seq << seq[-1] + seq[-2]
33
+ end
34
+ seq[n-1] # n is 1-based
35
+ end
36
+ end
37
+
38
+ handler = BenchmarkHandler.new
39
+ processor = ThriftBenchmark::BenchmarkService::Processor.new(handler)
40
+ transport = Thrift::ServerSocket.new(HOST, PORT)
41
+ transport_factory = Thrift::FramedTransportFactory.new
42
+ logger = Logger.new(STDERR)
43
+ logger.level = Logger::WARN
44
+ Thrift::NonblockingServer.new(processor, transport, transport_factory, nil, 20, logger).serve
@@ -0,0 +1,441 @@
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
+
27
+ VALUE rb_thrift_binary_proto_native_qmark(VALUE self) {
28
+ return Qtrue;
29
+ }
30
+
31
+
32
+
33
+ static int VERSION_1;
34
+ static int VERSION_MASK;
35
+ static int TYPE_MASK;
36
+ static int BAD_VERSION;
37
+ static ID rbuf_ivar_id;
38
+
39
+ static void write_byte_direct(VALUE trans, int8_t b) {
40
+ WRITE(trans, (char*)&b, 1);
41
+ }
42
+
43
+ static void write_i16_direct(VALUE trans, int16_t value) {
44
+ char data[2];
45
+
46
+ data[1] = value;
47
+ data[0] = (value >> 8);
48
+
49
+ WRITE(trans, data, 2);
50
+ }
51
+
52
+ static void write_i32_direct(VALUE trans, int32_t value) {
53
+ char data[4];
54
+
55
+ data[3] = value;
56
+ data[2] = (value >> 8);
57
+ data[1] = (value >> 16);
58
+ data[0] = (value >> 24);
59
+
60
+ WRITE(trans, data, 4);
61
+ }
62
+
63
+
64
+ static void write_i64_direct(VALUE trans, int64_t value) {
65
+ char data[8];
66
+
67
+ data[7] = value;
68
+ data[6] = (value >> 8);
69
+ data[5] = (value >> 16);
70
+ data[4] = (value >> 24);
71
+ data[3] = (value >> 32);
72
+ data[2] = (value >> 40);
73
+ data[1] = (value >> 48);
74
+ data[0] = (value >> 56);
75
+
76
+ WRITE(trans, data, 8);
77
+ }
78
+
79
+ static void write_string_direct(VALUE trans, VALUE str) {
80
+ if (TYPE(str) != T_STRING) {
81
+ rb_raise(rb_eStandardError, "Value should be a string");
82
+ }
83
+ write_i32_direct(trans, RSTRING_LEN(str));
84
+ rb_funcall(trans, write_method_id, 1, str);
85
+ }
86
+
87
+ //--------------------------------
88
+ // interface writing methods
89
+ //--------------------------------
90
+
91
+ VALUE rb_thrift_binary_proto_write_message_end(VALUE self) {
92
+ return Qnil;
93
+ }
94
+
95
+ VALUE rb_thrift_binary_proto_write_struct_begin(VALUE self, VALUE name) {
96
+ return Qnil;
97
+ }
98
+
99
+ VALUE rb_thrift_binary_proto_write_struct_end(VALUE self) {
100
+ return Qnil;
101
+ }
102
+
103
+ VALUE rb_thrift_binary_proto_write_field_end(VALUE self) {
104
+ return Qnil;
105
+ }
106
+
107
+ VALUE rb_thrift_binary_proto_write_map_end(VALUE self) {
108
+ return Qnil;
109
+ }
110
+
111
+ VALUE rb_thrift_binary_proto_write_list_end(VALUE self) {
112
+ return Qnil;
113
+ }
114
+
115
+ VALUE rb_thrift_binary_proto_write_set_end(VALUE self) {
116
+ return Qnil;
117
+ }
118
+
119
+ VALUE rb_thrift_binary_proto_write_message_begin(VALUE self, VALUE name, VALUE type, VALUE seqid) {
120
+ VALUE trans = GET_TRANSPORT(self);
121
+ VALUE strict_write = GET_STRICT_WRITE(self);
122
+
123
+ if (strict_write == Qtrue) {
124
+ write_i32_direct(trans, VERSION_1 | FIX2INT(type));
125
+ write_string_direct(trans, name);
126
+ write_i32_direct(trans, FIX2INT(seqid));
127
+ } else {
128
+ write_string_direct(trans, name);
129
+ write_byte_direct(trans, FIX2INT(type));
130
+ write_i32_direct(trans, FIX2INT(seqid));
131
+ }
132
+
133
+ return Qnil;
134
+ }
135
+
136
+ VALUE rb_thrift_binary_proto_write_field_begin(VALUE self, VALUE name, VALUE type, VALUE id) {
137
+ VALUE trans = GET_TRANSPORT(self);
138
+ write_byte_direct(trans, FIX2INT(type));
139
+ write_i16_direct(trans, FIX2INT(id));
140
+
141
+ return Qnil;
142
+ }
143
+
144
+ VALUE rb_thrift_binary_proto_write_field_stop(VALUE self) {
145
+ write_byte_direct(GET_TRANSPORT(self), TTYPE_STOP);
146
+ return Qnil;
147
+ }
148
+
149
+ VALUE rb_thrift_binary_proto_write_map_begin(VALUE self, VALUE ktype, VALUE vtype, VALUE size) {
150
+ VALUE trans = GET_TRANSPORT(self);
151
+ write_byte_direct(trans, FIX2INT(ktype));
152
+ write_byte_direct(trans, FIX2INT(vtype));
153
+ write_i32_direct(trans, FIX2INT(size));
154
+
155
+ return Qnil;
156
+ }
157
+
158
+ VALUE rb_thrift_binary_proto_write_list_begin(VALUE self, VALUE etype, VALUE size) {
159
+ VALUE trans = GET_TRANSPORT(self);
160
+ write_byte_direct(trans, FIX2INT(etype));
161
+ write_i32_direct(trans, FIX2INT(size));
162
+
163
+ return Qnil;
164
+ }
165
+
166
+ VALUE rb_thrift_binary_proto_write_set_begin(VALUE self, VALUE etype, VALUE size) {
167
+ rb_thrift_binary_proto_write_list_begin(self, etype, size);
168
+ return Qnil;
169
+ }
170
+
171
+ VALUE rb_thrift_binary_proto_write_bool(VALUE self, VALUE b) {
172
+ write_byte_direct(GET_TRANSPORT(self), RTEST(b) ? 1 : 0);
173
+ return Qnil;
174
+ }
175
+
176
+ VALUE rb_thrift_binary_proto_write_byte(VALUE self, VALUE byte) {
177
+ CHECK_NIL(byte);
178
+ write_byte_direct(GET_TRANSPORT(self), NUM2INT(byte));
179
+ return Qnil;
180
+ }
181
+
182
+ VALUE rb_thrift_binary_proto_write_i16(VALUE self, VALUE i16) {
183
+ CHECK_NIL(i16);
184
+ write_i16_direct(GET_TRANSPORT(self), FIX2INT(i16));
185
+ return Qnil;
186
+ }
187
+
188
+ VALUE rb_thrift_binary_proto_write_i32(VALUE self, VALUE i32) {
189
+ CHECK_NIL(i32);
190
+ write_i32_direct(GET_TRANSPORT(self), NUM2INT(i32));
191
+ return Qnil;
192
+ }
193
+
194
+ VALUE rb_thrift_binary_proto_write_i64(VALUE self, VALUE i64) {
195
+ CHECK_NIL(i64);
196
+ write_i64_direct(GET_TRANSPORT(self), NUM2LL(i64));
197
+ return Qnil;
198
+ }
199
+
200
+ VALUE rb_thrift_binary_proto_write_double(VALUE self, VALUE dub) {
201
+ CHECK_NIL(dub);
202
+ // Unfortunately, bitwise_cast doesn't work in C. Bad C!
203
+ union {
204
+ double f;
205
+ int64_t t;
206
+ } transfer;
207
+ transfer.f = RFLOAT_VALUE(rb_Float(dub));
208
+ write_i64_direct(GET_TRANSPORT(self), transfer.t);
209
+
210
+ return Qnil;
211
+ }
212
+
213
+ VALUE rb_thrift_binary_proto_write_string(VALUE self, VALUE str) {
214
+ CHECK_NIL(str);
215
+ VALUE trans = GET_TRANSPORT(self);
216
+ write_string_direct(trans, str);
217
+ return Qnil;
218
+ }
219
+
220
+ //---------------------------------------
221
+ // interface reading methods
222
+ //---------------------------------------
223
+
224
+ VALUE rb_thrift_binary_proto_read_string(VALUE self);
225
+ VALUE rb_thrift_binary_proto_read_byte(VALUE self);
226
+ VALUE rb_thrift_binary_proto_read_i32(VALUE self);
227
+ VALUE rb_thrift_binary_proto_read_i16(VALUE self);
228
+
229
+ static char read_byte_direct(VALUE self) {
230
+ VALUE byte = rb_funcall(GET_TRANSPORT(self), read_byte_method_id, 0);
231
+ return (char)(FIX2INT(byte));
232
+ }
233
+
234
+ static int16_t read_i16_direct(VALUE self) {
235
+ VALUE rbuf = rb_ivar_get(self, rbuf_ivar_id);
236
+ rb_funcall(GET_TRANSPORT(self), read_into_buffer_method_id, 2, rbuf, INT2FIX(2));
237
+ return (int16_t)(((uint8_t)(RSTRING_PTR(rbuf)[1])) | ((uint16_t)((RSTRING_PTR(rbuf)[0]) << 8)));
238
+ }
239
+
240
+ static int32_t read_i32_direct(VALUE self) {
241
+ VALUE rbuf = rb_ivar_get(self, rbuf_ivar_id);
242
+ rb_funcall(GET_TRANSPORT(self), read_into_buffer_method_id, 2, rbuf, INT2FIX(4));
243
+ return ((uint8_t)(RSTRING_PTR(rbuf)[3])) |
244
+ (((uint8_t)(RSTRING_PTR(rbuf)[2])) << 8) |
245
+ (((uint8_t)(RSTRING_PTR(rbuf)[1])) << 16) |
246
+ (((uint8_t)(RSTRING_PTR(rbuf)[0])) << 24);
247
+ }
248
+
249
+ static int64_t read_i64_direct(VALUE self) {
250
+ VALUE rbuf = rb_ivar_get(self, rbuf_ivar_id);
251
+ rb_funcall(GET_TRANSPORT(self), read_into_buffer_method_id, 2, rbuf, INT2FIX(8));
252
+ uint64_t hi = ((uint8_t)(RSTRING_PTR(rbuf)[3])) |
253
+ (((uint8_t)(RSTRING_PTR(rbuf)[2])) << 8) |
254
+ (((uint8_t)(RSTRING_PTR(rbuf)[1])) << 16) |
255
+ (((uint8_t)(RSTRING_PTR(rbuf)[0])) << 24);
256
+ uint32_t lo = ((uint8_t)(RSTRING_PTR(rbuf)[7])) |
257
+ (((uint8_t)(RSTRING_PTR(rbuf)[6])) << 8) |
258
+ (((uint8_t)(RSTRING_PTR(rbuf)[5])) << 16) |
259
+ (((uint8_t)(RSTRING_PTR(rbuf)[4])) << 24);
260
+ return (hi << 32) | lo;
261
+ }
262
+
263
+ static VALUE get_protocol_exception(VALUE code, VALUE message) {
264
+ VALUE args[2];
265
+ args[0] = code;
266
+ args[1] = message;
267
+ return rb_class_new_instance(2, (VALUE*)&args, protocol_exception_class);
268
+ }
269
+
270
+ VALUE rb_thrift_binary_proto_read_message_end(VALUE self) {
271
+ return Qnil;
272
+ }
273
+
274
+ VALUE rb_thift_binary_proto_read_struct_begin(VALUE self) {
275
+ return Qnil;
276
+ }
277
+
278
+ VALUE rb_thift_binary_proto_read_struct_end(VALUE self) {
279
+ return Qnil;
280
+ }
281
+
282
+ VALUE rb_thift_binary_proto_read_field_end(VALUE self) {
283
+ return Qnil;
284
+ }
285
+
286
+ VALUE rb_thift_binary_proto_read_map_end(VALUE self) {
287
+ return Qnil;
288
+ }
289
+
290
+ VALUE rb_thift_binary_proto_read_list_end(VALUE self) {
291
+ return Qnil;
292
+ }
293
+
294
+ VALUE rb_thift_binary_proto_read_set_end(VALUE self) {
295
+ return Qnil;
296
+ }
297
+
298
+ VALUE rb_thrift_binary_proto_read_message_begin(VALUE self) {
299
+ VALUE strict_read = GET_STRICT_READ(self);
300
+ VALUE name, seqid;
301
+ int type;
302
+
303
+ int version = read_i32_direct(self);
304
+
305
+ if (version < 0) {
306
+ if ((version & VERSION_MASK) != VERSION_1) {
307
+ rb_exc_raise(get_protocol_exception(INT2FIX(BAD_VERSION), rb_str_new2("Missing version identifier")));
308
+ }
309
+ type = version & TYPE_MASK;
310
+ name = rb_thrift_binary_proto_read_string(self);
311
+ seqid = rb_thrift_binary_proto_read_i32(self);
312
+ } else {
313
+ if (strict_read == Qtrue) {
314
+ rb_exc_raise(get_protocol_exception(INT2FIX(BAD_VERSION), rb_str_new2("No version identifier, old protocol client?")));
315
+ }
316
+ name = READ(self, version);
317
+ type = read_byte_direct(self);
318
+ seqid = rb_thrift_binary_proto_read_i32(self);
319
+ }
320
+
321
+ return rb_ary_new3(3, name, INT2FIX(type), seqid);
322
+ }
323
+
324
+ VALUE rb_thrift_binary_proto_read_field_begin(VALUE self) {
325
+ int type = read_byte_direct(self);
326
+ if (type == TTYPE_STOP) {
327
+ return rb_ary_new3(3, Qnil, INT2FIX(type), INT2FIX(0));
328
+ } else {
329
+ VALUE id = rb_thrift_binary_proto_read_i16(self);
330
+ return rb_ary_new3(3, Qnil, INT2FIX(type), id);
331
+ }
332
+ }
333
+
334
+ VALUE rb_thrift_binary_proto_read_map_begin(VALUE self) {
335
+ VALUE ktype = rb_thrift_binary_proto_read_byte(self);
336
+ VALUE vtype = rb_thrift_binary_proto_read_byte(self);
337
+ VALUE size = rb_thrift_binary_proto_read_i32(self);
338
+ return rb_ary_new3(3, ktype, vtype, size);
339
+ }
340
+
341
+ VALUE rb_thrift_binary_proto_read_list_begin(VALUE self) {
342
+ VALUE etype = rb_thrift_binary_proto_read_byte(self);
343
+ VALUE size = rb_thrift_binary_proto_read_i32(self);
344
+ return rb_ary_new3(2, etype, size);
345
+ }
346
+
347
+ VALUE rb_thrift_binary_proto_read_set_begin(VALUE self) {
348
+ return rb_thrift_binary_proto_read_list_begin(self);
349
+ }
350
+
351
+ VALUE rb_thrift_binary_proto_read_bool(VALUE self) {
352
+ char byte = read_byte_direct(self);
353
+ return byte != 0 ? Qtrue : Qfalse;
354
+ }
355
+
356
+ VALUE rb_thrift_binary_proto_read_byte(VALUE self) {
357
+ return INT2FIX(read_byte_direct(self));
358
+ }
359
+
360
+ VALUE rb_thrift_binary_proto_read_i16(VALUE self) {
361
+ return INT2FIX(read_i16_direct(self));
362
+ }
363
+
364
+ VALUE rb_thrift_binary_proto_read_i32(VALUE self) {
365
+ return INT2NUM(read_i32_direct(self));
366
+ }
367
+
368
+ VALUE rb_thrift_binary_proto_read_i64(VALUE self) {
369
+ return LL2NUM(read_i64_direct(self));
370
+ }
371
+
372
+ VALUE rb_thrift_binary_proto_read_double(VALUE self) {
373
+ union {
374
+ double f;
375
+ int64_t t;
376
+ } transfer;
377
+ transfer.t = read_i64_direct(self);
378
+ return rb_float_new(transfer.f);
379
+ }
380
+
381
+ VALUE rb_thrift_binary_proto_read_string(VALUE self) {
382
+ int size = read_i32_direct(self);
383
+ return READ(self, size);
384
+ }
385
+
386
+ void Init_binary_protocol_accelerated() {
387
+ VALUE thrift_binary_protocol_class = rb_const_get(thrift_module, rb_intern("BinaryProtocol"));
388
+
389
+ VERSION_1 = rb_num2ll(rb_const_get(thrift_binary_protocol_class, rb_intern("VERSION_1")));
390
+ VERSION_MASK = rb_num2ll(rb_const_get(thrift_binary_protocol_class, rb_intern("VERSION_MASK")));
391
+ TYPE_MASK = rb_num2ll(rb_const_get(thrift_binary_protocol_class, rb_intern("TYPE_MASK")));
392
+
393
+ VALUE bpa_class = rb_define_class_under(thrift_module, "BinaryProtocolAccelerated", thrift_binary_protocol_class);
394
+
395
+ rb_define_method(bpa_class, "native?", rb_thrift_binary_proto_native_qmark, 0);
396
+
397
+ rb_define_method(bpa_class, "write_message_begin", rb_thrift_binary_proto_write_message_begin, 3);
398
+ rb_define_method(bpa_class, "write_field_begin", rb_thrift_binary_proto_write_field_begin, 3);
399
+ rb_define_method(bpa_class, "write_field_stop", rb_thrift_binary_proto_write_field_stop, 0);
400
+ rb_define_method(bpa_class, "write_map_begin", rb_thrift_binary_proto_write_map_begin, 3);
401
+ rb_define_method(bpa_class, "write_list_begin", rb_thrift_binary_proto_write_list_begin, 2);
402
+ rb_define_method(bpa_class, "write_set_begin", rb_thrift_binary_proto_write_set_begin, 2);
403
+ rb_define_method(bpa_class, "write_byte", rb_thrift_binary_proto_write_byte, 1);
404
+ rb_define_method(bpa_class, "write_bool", rb_thrift_binary_proto_write_bool, 1);
405
+ rb_define_method(bpa_class, "write_i16", rb_thrift_binary_proto_write_i16, 1);
406
+ rb_define_method(bpa_class, "write_i32", rb_thrift_binary_proto_write_i32, 1);
407
+ rb_define_method(bpa_class, "write_i64", rb_thrift_binary_proto_write_i64, 1);
408
+ rb_define_method(bpa_class, "write_double", rb_thrift_binary_proto_write_double, 1);
409
+ rb_define_method(bpa_class, "write_string", rb_thrift_binary_proto_write_string, 1);
410
+ // unused methods
411
+ rb_define_method(bpa_class, "write_message_end", rb_thrift_binary_proto_write_message_end, 0);
412
+ rb_define_method(bpa_class, "write_struct_begin", rb_thrift_binary_proto_write_struct_begin, 1);
413
+ rb_define_method(bpa_class, "write_struct_end", rb_thrift_binary_proto_write_struct_end, 0);
414
+ rb_define_method(bpa_class, "write_field_end", rb_thrift_binary_proto_write_field_end, 0);
415
+ rb_define_method(bpa_class, "write_map_end", rb_thrift_binary_proto_write_map_end, 0);
416
+ rb_define_method(bpa_class, "write_list_end", rb_thrift_binary_proto_write_list_end, 0);
417
+ rb_define_method(bpa_class, "write_set_end", rb_thrift_binary_proto_write_set_end, 0);
418
+
419
+ rb_define_method(bpa_class, "read_message_begin", rb_thrift_binary_proto_read_message_begin, 0);
420
+ rb_define_method(bpa_class, "read_field_begin", rb_thrift_binary_proto_read_field_begin, 0);
421
+ rb_define_method(bpa_class, "read_map_begin", rb_thrift_binary_proto_read_map_begin, 0);
422
+ rb_define_method(bpa_class, "read_list_begin", rb_thrift_binary_proto_read_list_begin, 0);
423
+ rb_define_method(bpa_class, "read_set_begin", rb_thrift_binary_proto_read_set_begin, 0);
424
+ rb_define_method(bpa_class, "read_byte", rb_thrift_binary_proto_read_byte, 0);
425
+ rb_define_method(bpa_class, "read_bool", rb_thrift_binary_proto_read_bool, 0);
426
+ rb_define_method(bpa_class, "read_i16", rb_thrift_binary_proto_read_i16, 0);
427
+ rb_define_method(bpa_class, "read_i32", rb_thrift_binary_proto_read_i32, 0);
428
+ rb_define_method(bpa_class, "read_i64", rb_thrift_binary_proto_read_i64, 0);
429
+ rb_define_method(bpa_class, "read_double", rb_thrift_binary_proto_read_double, 0);
430
+ rb_define_method(bpa_class, "read_string", rb_thrift_binary_proto_read_string, 0);
431
+ // unused methods
432
+ rb_define_method(bpa_class, "read_message_end", rb_thrift_binary_proto_read_message_end, 0);
433
+ rb_define_method(bpa_class, "read_struct_begin", rb_thift_binary_proto_read_struct_begin, 0);
434
+ rb_define_method(bpa_class, "read_struct_end", rb_thift_binary_proto_read_struct_end, 0);
435
+ rb_define_method(bpa_class, "read_field_end", rb_thift_binary_proto_read_field_end, 0);
436
+ rb_define_method(bpa_class, "read_map_end", rb_thift_binary_proto_read_map_end, 0);
437
+ rb_define_method(bpa_class, "read_list_end", rb_thift_binary_proto_read_list_end, 0);
438
+ rb_define_method(bpa_class, "read_set_end", rb_thift_binary_proto_read_set_end, 0);
439
+
440
+ rbuf_ivar_id = rb_intern("@rbuf");
441
+ }