fl-thrift 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (83) hide show
  1. data/CHANGELOG +4 -0
  2. data/Manifest +81 -0
  3. data/README +43 -0
  4. data/Rakefile +102 -0
  5. data/benchmark/Benchmark.thrift +24 -0
  6. data/benchmark/benchmark.rb +271 -0
  7. data/benchmark/client.rb +74 -0
  8. data/benchmark/server.rb +82 -0
  9. data/benchmark/thin_server.rb +44 -0
  10. data/ext/binary_protocol_accelerated.c +474 -0
  11. data/ext/binary_protocol_accelerated.h +20 -0
  12. data/ext/compact_protocol.c +665 -0
  13. data/ext/compact_protocol.h +20 -0
  14. data/ext/constants.h +95 -0
  15. data/ext/extconf.rb +26 -0
  16. data/ext/macros.h +41 -0
  17. data/ext/memory_buffer.c +76 -0
  18. data/ext/memory_buffer.h +20 -0
  19. data/ext/protocol.c +185 -0
  20. data/ext/protocol.h +20 -0
  21. data/ext/struct.c +606 -0
  22. data/ext/struct.h +67 -0
  23. data/ext/thrift_native.c +194 -0
  24. data/lib/thrift.rb +59 -0
  25. data/lib/thrift/client.rb +62 -0
  26. data/lib/thrift/core_ext.rb +23 -0
  27. data/lib/thrift/core_ext/fixnum.rb +29 -0
  28. data/lib/thrift/exceptions.rb +82 -0
  29. data/lib/thrift/processor.rb +57 -0
  30. data/lib/thrift/protocol/base_protocol.rb +290 -0
  31. data/lib/thrift/protocol/binary_protocol.rb +225 -0
  32. data/lib/thrift/protocol/binary_protocol_accelerated.rb +35 -0
  33. data/lib/thrift/protocol/compact_protocol.rb +422 -0
  34. data/lib/thrift/serializer/deserializer.rb +33 -0
  35. data/lib/thrift/serializer/serializer.rb +34 -0
  36. data/lib/thrift/server/base_server.rb +31 -0
  37. data/lib/thrift/server/mongrel_http_server.rb +58 -0
  38. data/lib/thrift/server/nonblocking_server.rb +296 -0
  39. data/lib/thrift/server/simple_server.rb +43 -0
  40. data/lib/thrift/server/thread_pool_server.rb +75 -0
  41. data/lib/thrift/server/threaded_server.rb +47 -0
  42. data/lib/thrift/struct.rb +298 -0
  43. data/lib/thrift/thrift_native.rb +24 -0
  44. data/lib/thrift/transport/base_server_transport.rb +37 -0
  45. data/lib/thrift/transport/base_transport.rb +70 -0
  46. data/lib/thrift/transport/buffered_transport.rb +77 -0
  47. data/lib/thrift/transport/framed_transport.rb +90 -0
  48. data/lib/thrift/transport/http_client_transport.rb +45 -0
  49. data/lib/thrift/transport/io_stream_transport.rb +39 -0
  50. data/lib/thrift/transport/memory_buffer_transport.rb +96 -0
  51. data/lib/thrift/transport/server_socket.rb +63 -0
  52. data/lib/thrift/transport/socket.rb +136 -0
  53. data/lib/thrift/transport/unix_server_socket.rb +60 -0
  54. data/lib/thrift/transport/unix_socket.rb +40 -0
  55. data/lib/thrift/types.rb +101 -0
  56. data/script/proto_benchmark.rb +121 -0
  57. data/script/read_struct.rb +43 -0
  58. data/script/write_struct.rb +30 -0
  59. data/setup.rb +1585 -0
  60. data/spec/ThriftSpec.thrift +84 -0
  61. data/spec/base_protocol_spec.rb +160 -0
  62. data/spec/base_transport_spec.rb +351 -0
  63. data/spec/binary_protocol_accelerated_spec.rb +41 -0
  64. data/spec/binary_protocol_spec.rb +63 -0
  65. data/spec/binary_protocol_spec_shared.rb +375 -0
  66. data/spec/client_spec.rb +100 -0
  67. data/spec/compact_protocol_spec.rb +117 -0
  68. data/spec/exception_spec.rb +142 -0
  69. data/spec/http_client_spec.rb +49 -0
  70. data/spec/mongrel_http_server_spec.rb +117 -0
  71. data/spec/nonblocking_server_spec.rb +265 -0
  72. data/spec/processor_spec.rb +83 -0
  73. data/spec/serializer_spec.rb +69 -0
  74. data/spec/server_socket_spec.rb +80 -0
  75. data/spec/server_spec.rb +160 -0
  76. data/spec/socket_spec.rb +61 -0
  77. data/spec/socket_spec_shared.rb +104 -0
  78. data/spec/spec_helper.rb +60 -0
  79. data/spec/struct_spec.rb +252 -0
  80. data/spec/types_spec.rb +116 -0
  81. data/spec/unix_socket_spec.rb +108 -0
  82. data/thrift.gemspec +32 -0
  83. metadata +202 -0
@@ -0,0 +1,74 @@
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
+ class Client
26
+ def initialize(host, port, clients_per_process, calls_per_client, log_exceptions)
27
+ @host = host
28
+ @port = port
29
+ @clients_per_process = clients_per_process
30
+ @calls_per_client = calls_per_client
31
+ @log_exceptions = log_exceptions
32
+ end
33
+
34
+ def run
35
+ @clients_per_process.times do
36
+ socket = Thrift::Socket.new(@host, @port)
37
+ transport = Thrift::FramedTransport.new(socket)
38
+ protocol = Thrift::BinaryProtocol.new(transport)
39
+ client = ThriftBenchmark::BenchmarkService::Client.new(protocol)
40
+ begin
41
+ start = Time.now
42
+ transport.open
43
+ Marshal.dump [:start, start], STDOUT
44
+ rescue => e
45
+ Marshal.dump [:connection_failure, Time.now], STDOUT
46
+ print_exception e if @log_exceptions
47
+ else
48
+ begin
49
+ @calls_per_client.times do
50
+ Marshal.dump [:call_start, Time.now], STDOUT
51
+ client.fibonacci(15)
52
+ Marshal.dump [:call_end, Time.now], STDOUT
53
+ end
54
+ transport.close
55
+ Marshal.dump [:end, Time.now], STDOUT
56
+ rescue Thrift::TransportException => e
57
+ Marshal.dump [:connection_error, Time.now], STDOUT
58
+ print_exception e if @log_exceptions
59
+ end
60
+ end
61
+ end
62
+ end
63
+
64
+ def print_exception(e)
65
+ STDERR.puts "ERROR: #{e.message}"
66
+ STDERR.puts "\t#{e.backtrace * "\n\t"}"
67
+ end
68
+ end
69
+
70
+ log_exceptions = true if ARGV[0] == '-log-exceptions' and ARGV.shift
71
+
72
+ host, port, clients_per_process, calls_per_client = ARGV
73
+
74
+ Client.new(host, port.to_i, clients_per_process.to_i, calls_per_client.to_i, log_exceptions).run
@@ -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,474 @@
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
+
38
+ static void write_byte_direct(VALUE trans, int8_t b) {
39
+ WRITE(trans, (char*)&b, 1);
40
+ }
41
+
42
+ static void write_i16_direct(VALUE trans, int16_t value) {
43
+ char data[2];
44
+
45
+ data[1] = value;
46
+ data[0] = (value >> 8);
47
+
48
+ WRITE(trans, data, 2);
49
+ }
50
+
51
+ static void write_i32_direct(VALUE trans, int32_t value) {
52
+ char data[4];
53
+
54
+ data[3] = value;
55
+ data[2] = (value >> 8);
56
+ data[1] = (value >> 16);
57
+ data[0] = (value >> 24);
58
+
59
+ WRITE(trans, data, 4);
60
+ }
61
+
62
+
63
+ static void write_i64_direct(VALUE trans, int64_t value) {
64
+ char data[8];
65
+
66
+ data[7] = value;
67
+ data[6] = (value >> 8);
68
+ data[5] = (value >> 16);
69
+ data[4] = (value >> 24);
70
+ data[3] = (value >> 32);
71
+ data[2] = (value >> 40);
72
+ data[1] = (value >> 48);
73
+ data[0] = (value >> 56);
74
+
75
+ WRITE(trans, data, 8);
76
+ }
77
+
78
+ static void write_string_direct(VALUE trans, VALUE str) {
79
+ write_i32_direct(trans, RSTRING_LEN(str));
80
+ rb_funcall(trans, write_method_id, 1, str);
81
+ }
82
+
83
+ //--------------------------------
84
+ // interface writing methods
85
+ //--------------------------------
86
+
87
+ VALUE rb_thrift_binary_proto_write_message_end(VALUE self) {
88
+ return Qnil;
89
+ }
90
+
91
+ VALUE rb_thrift_binary_proto_write_struct_begin(VALUE self, VALUE name) {
92
+ return Qnil;
93
+ }
94
+
95
+ VALUE rb_thrift_binary_proto_write_struct_end(VALUE self) {
96
+ return Qnil;
97
+ }
98
+
99
+ VALUE rb_thrift_binary_proto_write_field_end(VALUE self) {
100
+ return Qnil;
101
+ }
102
+
103
+ VALUE rb_thrift_binary_proto_write_map_end(VALUE self) {
104
+ return Qnil;
105
+ }
106
+
107
+ VALUE rb_thrift_binary_proto_write_list_end(VALUE self) {
108
+ return Qnil;
109
+ }
110
+
111
+ VALUE rb_thrift_binary_proto_write_set_end(VALUE self) {
112
+ return Qnil;
113
+ }
114
+
115
+ VALUE rb_thrift_binary_proto_write_message_begin(VALUE self, VALUE name, VALUE type, VALUE seqid) {
116
+ VALUE trans = GET_TRANSPORT(self);
117
+ VALUE strict_write = GET_STRICT_WRITE(self);
118
+
119
+ if (strict_write == Qtrue) {
120
+ write_i32_direct(trans, VERSION_1 | FIX2INT(type));
121
+ write_string_direct(trans, name);
122
+ write_i32_direct(trans, FIX2INT(seqid));
123
+ } else {
124
+ write_string_direct(trans, name);
125
+ write_byte_direct(trans, FIX2INT(type));
126
+ write_i32_direct(trans, FIX2INT(seqid));
127
+ }
128
+
129
+ return Qnil;
130
+ }
131
+
132
+ VALUE rb_thrift_binary_proto_write_field_begin(VALUE self, VALUE name, VALUE type, VALUE id) {
133
+ VALUE trans = GET_TRANSPORT(self);
134
+ write_byte_direct(trans, FIX2INT(type));
135
+ write_i16_direct(trans, FIX2INT(id));
136
+
137
+ return Qnil;
138
+ }
139
+
140
+ VALUE rb_thrift_binary_proto_write_field_stop(VALUE self) {
141
+ write_byte_direct(GET_TRANSPORT(self), TTYPE_STOP);
142
+ return Qnil;
143
+ }
144
+
145
+ VALUE rb_thrift_binary_proto_write_map_begin(VALUE self, VALUE ktype, VALUE vtype, VALUE size) {
146
+ VALUE trans = GET_TRANSPORT(self);
147
+ write_byte_direct(trans, FIX2INT(ktype));
148
+ write_byte_direct(trans, FIX2INT(vtype));
149
+ write_i32_direct(trans, FIX2INT(size));
150
+
151
+ return Qnil;
152
+ }
153
+
154
+ VALUE rb_thrift_binary_proto_write_list_begin(VALUE self, VALUE etype, VALUE size) {
155
+ VALUE trans = GET_TRANSPORT(self);
156
+ write_byte_direct(trans, FIX2INT(etype));
157
+ write_i32_direct(trans, FIX2INT(size));
158
+
159
+ return Qnil;
160
+ }
161
+
162
+ VALUE rb_thrift_binary_proto_write_set_begin(VALUE self, VALUE etype, VALUE size) {
163
+ rb_thrift_binary_proto_write_list_begin(self, etype, size);
164
+ return Qnil;
165
+ }
166
+
167
+ VALUE rb_thrift_binary_proto_write_bool(VALUE self, VALUE b) {
168
+ write_byte_direct(GET_TRANSPORT(self), RTEST(b) ? 1 : 0);
169
+ return Qnil;
170
+ }
171
+
172
+ VALUE rb_thrift_binary_proto_write_byte(VALUE self, VALUE byte) {
173
+ CHECK_NIL(byte);
174
+ write_byte_direct(GET_TRANSPORT(self), NUM2INT(byte));
175
+ return Qnil;
176
+ }
177
+
178
+ VALUE rb_thrift_binary_proto_write_i16(VALUE self, VALUE i16) {
179
+ CHECK_NIL(i16);
180
+ write_i16_direct(GET_TRANSPORT(self), FIX2INT(i16));
181
+ return Qnil;
182
+ }
183
+
184
+ VALUE rb_thrift_binary_proto_write_i32(VALUE self, VALUE i32) {
185
+ CHECK_NIL(i32);
186
+ write_i32_direct(GET_TRANSPORT(self), NUM2INT(i32));
187
+ return Qnil;
188
+ }
189
+
190
+ VALUE rb_thrift_binary_proto_write_i64(VALUE self, VALUE i64) {
191
+ CHECK_NIL(i64);
192
+ write_i64_direct(GET_TRANSPORT(self), NUM2LL(i64));
193
+ return Qnil;
194
+ }
195
+
196
+ VALUE rb_thrift_binary_proto_write_double(VALUE self, VALUE dub) {
197
+ CHECK_NIL(dub);
198
+ // Unfortunately, bitwise_cast doesn't work in C. Bad C!
199
+ union {
200
+ double f;
201
+ int64_t t;
202
+ } transfer;
203
+ transfer.f = RFLOAT_VALUE(rb_Float(dub));
204
+ write_i64_direct(GET_TRANSPORT(self), transfer.t);
205
+
206
+ return Qnil;
207
+ }
208
+
209
+ VALUE rb_thrift_binary_proto_write_string(VALUE self, VALUE str) {
210
+ CHECK_NIL(str);
211
+ VALUE trans = GET_TRANSPORT(self);
212
+ write_string_direct(trans, str);
213
+ return Qnil;
214
+ }
215
+
216
+ //---------------------------------------
217
+ // interface reading methods
218
+ //---------------------------------------
219
+
220
+ VALUE rb_thrift_binary_proto_read_string(VALUE self);
221
+ VALUE rb_thrift_binary_proto_read_byte(VALUE self);
222
+ VALUE rb_thrift_binary_proto_read_i32(VALUE self);
223
+ VALUE rb_thrift_binary_proto_read_i16(VALUE self);
224
+
225
+ static char read_byte_direct(VALUE self) {
226
+ VALUE buf = READ(self, 1);
227
+ return RSTRING_PTR(buf)[0];
228
+ }
229
+
230
+ static int16_t read_i16_direct(VALUE self) {
231
+ VALUE buf = READ(self, 2);
232
+ return (int16_t)(((uint8_t)(RSTRING_PTR(buf)[1])) | ((uint16_t)((RSTRING_PTR(buf)[0]) << 8)));
233
+ }
234
+
235
+ static int32_t read_i32_direct(VALUE self) {
236
+ VALUE buf = READ(self, 4);
237
+ return ((uint8_t)(RSTRING_PTR(buf)[3])) |
238
+ (((uint8_t)(RSTRING_PTR(buf)[2])) << 8) |
239
+ (((uint8_t)(RSTRING_PTR(buf)[1])) << 16) |
240
+ (((uint8_t)(RSTRING_PTR(buf)[0])) << 24);
241
+ }
242
+
243
+ static int64_t read_i64_direct(VALUE self) {
244
+ uint64_t hi = read_i32_direct(self);
245
+ uint32_t lo = read_i32_direct(self);
246
+ return (hi << 32) | lo;
247
+ }
248
+
249
+ static VALUE get_protocol_exception(VALUE code, VALUE message) {
250
+ VALUE args[2];
251
+ args[0] = code;
252
+ args[1] = message;
253
+ return rb_class_new_instance(2, (VALUE*)&args, protocol_exception_class);
254
+ }
255
+
256
+ VALUE rb_thrift_binary_proto_read_message_end(VALUE self) {
257
+ return Qnil;
258
+ }
259
+
260
+ VALUE rb_thift_binary_proto_read_struct_begin(VALUE self) {
261
+ return Qnil;
262
+ }
263
+
264
+ VALUE rb_thift_binary_proto_read_struct_end(VALUE self) {
265
+ return Qnil;
266
+ }
267
+
268
+ VALUE rb_thift_binary_proto_read_field_end(VALUE self) {
269
+ return Qnil;
270
+ }
271
+
272
+ VALUE rb_thift_binary_proto_read_map_end(VALUE self) {
273
+ return Qnil;
274
+ }
275
+
276
+ VALUE rb_thift_binary_proto_read_list_end(VALUE self) {
277
+ return Qnil;
278
+ }
279
+
280
+ VALUE rb_thift_binary_proto_read_set_end(VALUE self) {
281
+ return Qnil;
282
+ }
283
+
284
+ VALUE rb_thrift_binary_proto_read_message_begin(VALUE self) {
285
+ VALUE strict_read = GET_STRICT_READ(self);
286
+ VALUE name, seqid;
287
+ int type;
288
+
289
+ int version = read_i32_direct(self);
290
+
291
+ if (version < 0) {
292
+ if ((version & VERSION_MASK) != VERSION_1) {
293
+ rb_exc_raise(get_protocol_exception(INT2FIX(BAD_VERSION), rb_str_new2("Missing version identifier")));
294
+ }
295
+ type = version & TYPE_MASK;
296
+ name = rb_thrift_binary_proto_read_string(self);
297
+ seqid = rb_thrift_binary_proto_read_i32(self);
298
+ } else {
299
+ if (strict_read == Qtrue) {
300
+ rb_exc_raise(get_protocol_exception(INT2FIX(BAD_VERSION), rb_str_new2("No version identifier, old protocol client?")));
301
+ }
302
+ name = READ(self, version);
303
+ type = read_byte_direct(self);
304
+ seqid = rb_thrift_binary_proto_read_i32(self);
305
+ }
306
+
307
+ return rb_ary_new3(3, name, INT2FIX(type), seqid);
308
+ }
309
+
310
+ VALUE rb_thrift_binary_proto_read_field_begin(VALUE self) {
311
+ int type = read_byte_direct(self);
312
+ if (type == TTYPE_STOP) {
313
+ return rb_ary_new3(3, Qnil, INT2FIX(type), INT2FIX(0));
314
+ } else {
315
+ VALUE id = rb_thrift_binary_proto_read_i16(self);
316
+ return rb_ary_new3(3, Qnil, INT2FIX(type), id);
317
+ }
318
+ }
319
+
320
+ VALUE rb_thrift_binary_proto_read_map_begin(VALUE self) {
321
+ VALUE ktype = rb_thrift_binary_proto_read_byte(self);
322
+ VALUE vtype = rb_thrift_binary_proto_read_byte(self);
323
+ VALUE size = rb_thrift_binary_proto_read_i32(self);
324
+ return rb_ary_new3(3, ktype, vtype, size);
325
+ }
326
+
327
+ VALUE rb_thrift_binary_proto_read_list_begin(VALUE self) {
328
+ VALUE etype = rb_thrift_binary_proto_read_byte(self);
329
+ VALUE size = rb_thrift_binary_proto_read_i32(self);
330
+ return rb_ary_new3(2, etype, size);
331
+ }
332
+
333
+ VALUE rb_thrift_binary_proto_read_set_begin(VALUE self) {
334
+ return rb_thrift_binary_proto_read_list_begin(self);
335
+ }
336
+
337
+ VALUE rb_thrift_binary_proto_read_bool(VALUE self) {
338
+ char byte = read_byte_direct(self);
339
+ return byte != 0 ? Qtrue : Qfalse;
340
+ }
341
+
342
+ VALUE rb_thrift_binary_proto_read_byte(VALUE self) {
343
+ return INT2FIX(read_byte_direct(self));
344
+ }
345
+
346
+ VALUE rb_thrift_binary_proto_read_i16(VALUE self) {
347
+ return INT2FIX(read_i16_direct(self));
348
+ }
349
+
350
+ VALUE rb_thrift_binary_proto_read_i32(VALUE self) {
351
+ return INT2NUM(read_i32_direct(self));
352
+ }
353
+
354
+ VALUE rb_thrift_binary_proto_read_i64(VALUE self) {
355
+ return LL2NUM(read_i64_direct(self));
356
+ }
357
+
358
+ VALUE rb_thrift_binary_proto_read_double(VALUE self) {
359
+ union {
360
+ double f;
361
+ int64_t t;
362
+ } transfer;
363
+ transfer.t = read_i64_direct(self);
364
+ return rb_float_new(transfer.f);
365
+ }
366
+
367
+ VALUE rb_thrift_binary_proto_read_string(VALUE self) {
368
+ int size = read_i32_direct(self);
369
+ return READ(self, size);
370
+ }
371
+
372
+ void Init_binary_protocol_accelerated() {
373
+ VALUE thrift_binary_protocol_class = rb_const_get(thrift_module, rb_intern("BinaryProtocol"));
374
+
375
+ VERSION_1 = rb_num2ll(rb_const_get(thrift_binary_protocol_class, rb_intern("VERSION_1")));
376
+ VERSION_MASK = rb_num2ll(rb_const_get(thrift_binary_protocol_class, rb_intern("VERSION_MASK")));
377
+ TYPE_MASK = rb_num2ll(rb_const_get(thrift_binary_protocol_class, rb_intern("TYPE_MASK")));
378
+
379
+ VALUE bpa_class = rb_define_class_under(thrift_module, "BinaryProtocolAccelerated", thrift_binary_protocol_class);
380
+
381
+ rb_define_method(bpa_class, "native?", rb_thrift_binary_proto_native_qmark, 0);
382
+
383
+ rb_define_method(bpa_class, "write_message_begin", rb_thrift_binary_proto_write_message_begin, 3);
384
+ rb_define_method(bpa_class, "write_field_begin", rb_thrift_binary_proto_write_field_begin, 3);
385
+ rb_define_method(bpa_class, "write_field_stop", rb_thrift_binary_proto_write_field_stop, 0);
386
+ rb_define_method(bpa_class, "write_map_begin", rb_thrift_binary_proto_write_map_begin, 3);
387
+ rb_define_method(bpa_class, "write_list_begin", rb_thrift_binary_proto_write_list_begin, 2);
388
+ rb_define_method(bpa_class, "write_set_begin", rb_thrift_binary_proto_write_set_begin, 2);
389
+ rb_define_method(bpa_class, "write_byte", rb_thrift_binary_proto_write_byte, 1);
390
+ rb_define_method(bpa_class, "write_bool", rb_thrift_binary_proto_write_bool, 1);
391
+ rb_define_method(bpa_class, "write_i16", rb_thrift_binary_proto_write_i16, 1);
392
+ rb_define_method(bpa_class, "write_i32", rb_thrift_binary_proto_write_i32, 1);
393
+ rb_define_method(bpa_class, "write_i64", rb_thrift_binary_proto_write_i64, 1);
394
+ rb_define_method(bpa_class, "write_double", rb_thrift_binary_proto_write_double, 1);
395
+ rb_define_method(bpa_class, "write_string", rb_thrift_binary_proto_write_string, 1);
396
+ // unused methods
397
+ rb_define_method(bpa_class, "write_message_end", rb_thrift_binary_proto_write_message_end, 0);
398
+ rb_define_method(bpa_class, "write_struct_begin", rb_thrift_binary_proto_write_struct_begin, 1);
399
+ rb_define_method(bpa_class, "write_struct_end", rb_thrift_binary_proto_write_struct_end, 0);
400
+ rb_define_method(bpa_class, "write_field_end", rb_thrift_binary_proto_write_field_end, 0);
401
+ rb_define_method(bpa_class, "write_map_end", rb_thrift_binary_proto_write_map_end, 0);
402
+ rb_define_method(bpa_class, "write_list_end", rb_thrift_binary_proto_write_list_end, 0);
403
+ rb_define_method(bpa_class, "write_set_end", rb_thrift_binary_proto_write_set_end, 0);
404
+
405
+
406
+
407
+ rb_define_method(bpa_class, "read_message_begin", rb_thrift_binary_proto_read_message_begin, 0);
408
+ rb_define_method(bpa_class, "read_field_begin", rb_thrift_binary_proto_read_field_begin, 0);
409
+ rb_define_method(bpa_class, "read_map_begin", rb_thrift_binary_proto_read_map_begin, 0);
410
+ rb_define_method(bpa_class, "read_list_begin", rb_thrift_binary_proto_read_list_begin, 0);
411
+ rb_define_method(bpa_class, "read_set_begin", rb_thrift_binary_proto_read_set_begin, 0);
412
+ rb_define_method(bpa_class, "read_byte", rb_thrift_binary_proto_read_byte, 0);
413
+ rb_define_method(bpa_class, "read_bool", rb_thrift_binary_proto_read_bool, 0);
414
+ rb_define_method(bpa_class, "read_i16", rb_thrift_binary_proto_read_i16, 0);
415
+ rb_define_method(bpa_class, "read_i32", rb_thrift_binary_proto_read_i32, 0);
416
+ rb_define_method(bpa_class, "read_i64", rb_thrift_binary_proto_read_i64, 0);
417
+ rb_define_method(bpa_class, "read_double", rb_thrift_binary_proto_read_double, 0);
418
+ rb_define_method(bpa_class, "read_string", rb_thrift_binary_proto_read_string, 0);
419
+ // unused methods
420
+ rb_define_method(bpa_class, "read_message_end", rb_thrift_binary_proto_read_message_end, 0);
421
+ rb_define_method(bpa_class, "read_struct_begin", rb_thift_binary_proto_read_struct_begin, 0);
422
+ rb_define_method(bpa_class, "read_struct_end", rb_thift_binary_proto_read_struct_end, 0);
423
+ rb_define_method(bpa_class, "read_field_end", rb_thift_binary_proto_read_field_end, 0);
424
+ rb_define_method(bpa_class, "read_map_end", rb_thift_binary_proto_read_map_end, 0);
425
+ rb_define_method(bpa_class, "read_list_end", rb_thift_binary_proto_read_list_end, 0);
426
+ rb_define_method(bpa_class, "read_set_end", rb_thift_binary_proto_read_set_end, 0);
427
+
428
+ // set up native method table
429
+ native_proto_method_table *npmt;
430
+ npmt = ALLOC(native_proto_method_table);
431
+
432
+ npmt->write_field_begin = rb_thrift_binary_proto_write_field_begin;
433
+ npmt->write_field_stop = rb_thrift_binary_proto_write_field_stop;
434
+ npmt->write_map_begin = rb_thrift_binary_proto_write_map_begin;
435
+ npmt->write_list_begin = rb_thrift_binary_proto_write_list_begin;
436
+ npmt->write_set_begin = rb_thrift_binary_proto_write_set_begin;
437
+ npmt->write_byte = rb_thrift_binary_proto_write_byte;
438
+ npmt->write_bool = rb_thrift_binary_proto_write_bool;
439
+ npmt->write_i16 = rb_thrift_binary_proto_write_i16;
440
+ npmt->write_i32 = rb_thrift_binary_proto_write_i32;
441
+ npmt->write_i64 = rb_thrift_binary_proto_write_i64;
442
+ npmt->write_double = rb_thrift_binary_proto_write_double;
443
+ npmt->write_string = rb_thrift_binary_proto_write_string;
444
+ npmt->write_message_end = rb_thrift_binary_proto_write_message_end;
445
+ npmt->write_struct_begin = rb_thrift_binary_proto_write_struct_begin;
446
+ npmt->write_struct_end = rb_thrift_binary_proto_write_struct_end;
447
+ npmt->write_field_end = rb_thrift_binary_proto_write_field_end;
448
+ npmt->write_map_end = rb_thrift_binary_proto_write_map_end;
449
+ npmt->write_list_end = rb_thrift_binary_proto_write_list_end;
450
+ npmt->write_set_end = rb_thrift_binary_proto_write_set_end;
451
+
452
+ npmt->read_message_begin = rb_thrift_binary_proto_read_message_begin;
453
+ npmt->read_field_begin = rb_thrift_binary_proto_read_field_begin;
454
+ npmt->read_map_begin = rb_thrift_binary_proto_read_map_begin;
455
+ npmt->read_list_begin = rb_thrift_binary_proto_read_list_begin;
456
+ npmt->read_set_begin = rb_thrift_binary_proto_read_set_begin;
457
+ npmt->read_byte = rb_thrift_binary_proto_read_byte;
458
+ npmt->read_bool = rb_thrift_binary_proto_read_bool;
459
+ npmt->read_i16 = rb_thrift_binary_proto_read_i16;
460
+ npmt->read_i32 = rb_thrift_binary_proto_read_i32;
461
+ npmt->read_i64 = rb_thrift_binary_proto_read_i64;
462
+ npmt->read_double = rb_thrift_binary_proto_read_double;
463
+ npmt->read_string = rb_thrift_binary_proto_read_string;
464
+ npmt->read_message_end = rb_thrift_binary_proto_read_message_end;
465
+ npmt->read_struct_begin = rb_thift_binary_proto_read_struct_begin;
466
+ npmt->read_struct_end = rb_thift_binary_proto_read_struct_end;
467
+ npmt->read_field_end = rb_thift_binary_proto_read_field_end;
468
+ npmt->read_map_end = rb_thift_binary_proto_read_map_end;
469
+ npmt->read_list_end = rb_thift_binary_proto_read_list_end;
470
+ npmt->read_set_end = rb_thift_binary_proto_read_set_end;
471
+
472
+ VALUE method_table_object = Data_Wrap_Struct(rb_cObject, 0, free, npmt);
473
+ rb_const_set(bpa_class, rb_intern("@native_method_table"), method_table_object);
474
+ }