blob: a8ebe7faf33bf69141cdb534aeef34aedb6f5ceb [file] [log] [blame]
Kevin Clark916f3532009-03-20 04:21:39 +00001/**
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
Bryan Duxburyc0166282009-02-02 00:48:17 +000020#include <ruby.h>
21#include <stdbool.h>
Bryan Duxbury1e80d442009-02-03 18:16:54 +000022#include <stdint.h>
Bryan Duxburyc0166282009-02-02 00:48:17 +000023#include <constants.h>
24#include <struct.h>
Jake Farrellb5a18a12012-10-09 01:10:43 +000025#include <macros.h>
26#include <bytes.h>
Bryan Duxburyc0166282009-02-02 00:48:17 +000027
28VALUE rb_thrift_binary_proto_native_qmark(VALUE self) {
29 return Qtrue;
30}
31
32
33
34static int VERSION_1;
35static int VERSION_MASK;
Kevin Clarkead33822009-02-04 22:43:59 +000036static int TYPE_MASK;
Bryan Duxburyc0166282009-02-02 00:48:17 +000037static int BAD_VERSION;
Bryan Duxburyad0ad822011-06-28 18:46:03 +000038static ID rbuf_ivar_id;
Bryan Duxburyc0166282009-02-02 00:48:17 +000039
40static void write_byte_direct(VALUE trans, int8_t b) {
41 WRITE(trans, (char*)&b, 1);
42}
43
44static void write_i16_direct(VALUE trans, int16_t value) {
45 char data[2];
46
47 data[1] = value;
48 data[0] = (value >> 8);
49
50 WRITE(trans, data, 2);
51}
52
53static void write_i32_direct(VALUE trans, int32_t value) {
54 char data[4];
55
56 data[3] = value;
57 data[2] = (value >> 8);
58 data[1] = (value >> 16);
59 data[0] = (value >> 24);
60
61 WRITE(trans, data, 4);
62}
63
64
65static void write_i64_direct(VALUE trans, int64_t value) {
66 char data[8];
67
68 data[7] = value;
69 data[6] = (value >> 8);
70 data[5] = (value >> 16);
71 data[4] = (value >> 24);
72 data[3] = (value >> 32);
73 data[2] = (value >> 40);
74 data[1] = (value >> 48);
75 data[0] = (value >> 56);
76
77 WRITE(trans, data, 8);
78}
79
80static void write_string_direct(VALUE trans, VALUE str) {
Bryan Duxbury3647fc62009-09-02 20:05:07 +000081 if (TYPE(str) != T_STRING) {
82 rb_raise(rb_eStandardError, "Value should be a string");
83 }
Jake Farrellb5a18a12012-10-09 01:10:43 +000084 str = convert_to_utf8_byte_buffer(str);
Bryan Duxburye3ab50d2009-03-25 21:06:53 +000085 write_i32_direct(trans, RSTRING_LEN(str));
Bryan Duxburyc0166282009-02-02 00:48:17 +000086 rb_funcall(trans, write_method_id, 1, str);
87}
88
89//--------------------------------
90// interface writing methods
91//--------------------------------
92
93VALUE rb_thrift_binary_proto_write_message_end(VALUE self) {
94 return Qnil;
95}
96
97VALUE rb_thrift_binary_proto_write_struct_begin(VALUE self, VALUE name) {
98 return Qnil;
99}
100
101VALUE rb_thrift_binary_proto_write_struct_end(VALUE self) {
102 return Qnil;
103}
104
105VALUE rb_thrift_binary_proto_write_field_end(VALUE self) {
106 return Qnil;
107}
108
109VALUE rb_thrift_binary_proto_write_map_end(VALUE self) {
110 return Qnil;
111}
112
113VALUE rb_thrift_binary_proto_write_list_end(VALUE self) {
114 return Qnil;
115}
116
117VALUE rb_thrift_binary_proto_write_set_end(VALUE self) {
118 return Qnil;
119}
120
121VALUE rb_thrift_binary_proto_write_message_begin(VALUE self, VALUE name, VALUE type, VALUE seqid) {
122 VALUE trans = GET_TRANSPORT(self);
Kevin Clarkead33822009-02-04 22:43:59 +0000123 VALUE strict_write = GET_STRICT_WRITE(self);
124
125 if (strict_write == Qtrue) {
126 write_i32_direct(trans, VERSION_1 | FIX2INT(type));
127 write_string_direct(trans, name);
128 write_i32_direct(trans, FIX2INT(seqid));
129 } else {
130 write_string_direct(trans, name);
Bryan Duxburyd40731e2009-03-20 02:21:05 +0000131 write_byte_direct(trans, FIX2INT(type));
Kevin Clarkead33822009-02-04 22:43:59 +0000132 write_i32_direct(trans, FIX2INT(seqid));
133 }
Bryan Duxburyc0166282009-02-02 00:48:17 +0000134
135 return Qnil;
136}
137
138VALUE rb_thrift_binary_proto_write_field_begin(VALUE self, VALUE name, VALUE type, VALUE id) {
139 VALUE trans = GET_TRANSPORT(self);
140 write_byte_direct(trans, FIX2INT(type));
141 write_i16_direct(trans, FIX2INT(id));
142
143 return Qnil;
144}
145
146VALUE rb_thrift_binary_proto_write_field_stop(VALUE self) {
147 write_byte_direct(GET_TRANSPORT(self), TTYPE_STOP);
148 return Qnil;
149}
150
151VALUE rb_thrift_binary_proto_write_map_begin(VALUE self, VALUE ktype, VALUE vtype, VALUE size) {
152 VALUE trans = GET_TRANSPORT(self);
153 write_byte_direct(trans, FIX2INT(ktype));
154 write_byte_direct(trans, FIX2INT(vtype));
155 write_i32_direct(trans, FIX2INT(size));
156
157 return Qnil;
158}
159
160VALUE rb_thrift_binary_proto_write_list_begin(VALUE self, VALUE etype, VALUE size) {
161 VALUE trans = GET_TRANSPORT(self);
162 write_byte_direct(trans, FIX2INT(etype));
163 write_i32_direct(trans, FIX2INT(size));
164
165 return Qnil;
166}
167
168VALUE rb_thrift_binary_proto_write_set_begin(VALUE self, VALUE etype, VALUE size) {
169 rb_thrift_binary_proto_write_list_begin(self, etype, size);
170 return Qnil;
171}
172
173VALUE rb_thrift_binary_proto_write_bool(VALUE self, VALUE b) {
174 write_byte_direct(GET_TRANSPORT(self), RTEST(b) ? 1 : 0);
175 return Qnil;
176}
177
178VALUE rb_thrift_binary_proto_write_byte(VALUE self, VALUE byte) {
179 CHECK_NIL(byte);
180 write_byte_direct(GET_TRANSPORT(self), NUM2INT(byte));
181 return Qnil;
182}
183
184VALUE rb_thrift_binary_proto_write_i16(VALUE self, VALUE i16) {
185 CHECK_NIL(i16);
186 write_i16_direct(GET_TRANSPORT(self), FIX2INT(i16));
187 return Qnil;
188}
189
190VALUE rb_thrift_binary_proto_write_i32(VALUE self, VALUE i32) {
191 CHECK_NIL(i32);
192 write_i32_direct(GET_TRANSPORT(self), NUM2INT(i32));
193 return Qnil;
194}
195
196VALUE rb_thrift_binary_proto_write_i64(VALUE self, VALUE i64) {
197 CHECK_NIL(i64);
198 write_i64_direct(GET_TRANSPORT(self), NUM2LL(i64));
199 return Qnil;
200}
201
202VALUE rb_thrift_binary_proto_write_double(VALUE self, VALUE dub) {
203 CHECK_NIL(dub);
204 // Unfortunately, bitwise_cast doesn't work in C. Bad C!
205 union {
206 double f;
207 int64_t t;
208 } transfer;
Bryan Duxburye3ab50d2009-03-25 21:06:53 +0000209 transfer.f = RFLOAT_VALUE(rb_Float(dub));
Bryan Duxburyc0166282009-02-02 00:48:17 +0000210 write_i64_direct(GET_TRANSPORT(self), transfer.t);
211
212 return Qnil;
213}
214
215VALUE rb_thrift_binary_proto_write_string(VALUE self, VALUE str) {
216 CHECK_NIL(str);
217 VALUE trans = GET_TRANSPORT(self);
Bryan Duxburyc0166282009-02-02 00:48:17 +0000218 write_string_direct(trans, str);
219 return Qnil;
220}
221
222//---------------------------------------
223// interface reading methods
224//---------------------------------------
225
Bryan Duxburyc0166282009-02-02 00:48:17 +0000226VALUE rb_thrift_binary_proto_read_string(VALUE self);
227VALUE rb_thrift_binary_proto_read_byte(VALUE self);
228VALUE rb_thrift_binary_proto_read_i32(VALUE self);
229VALUE rb_thrift_binary_proto_read_i16(VALUE self);
230
231static char read_byte_direct(VALUE self) {
Bryan Duxburyad0ad822011-06-28 18:46:03 +0000232 VALUE byte = rb_funcall(GET_TRANSPORT(self), read_byte_method_id, 0);
233 return (char)(FIX2INT(byte));
Bryan Duxburyc0166282009-02-02 00:48:17 +0000234}
235
236static int16_t read_i16_direct(VALUE self) {
Bryan Duxburyad0ad822011-06-28 18:46:03 +0000237 VALUE rbuf = rb_ivar_get(self, rbuf_ivar_id);
238 rb_funcall(GET_TRANSPORT(self), read_into_buffer_method_id, 2, rbuf, INT2FIX(2));
239 return (int16_t)(((uint8_t)(RSTRING_PTR(rbuf)[1])) | ((uint16_t)((RSTRING_PTR(rbuf)[0]) << 8)));
Bryan Duxburyc0166282009-02-02 00:48:17 +0000240}
241
242static int32_t read_i32_direct(VALUE self) {
Bryan Duxburyad0ad822011-06-28 18:46:03 +0000243 VALUE rbuf = rb_ivar_get(self, rbuf_ivar_id);
244 rb_funcall(GET_TRANSPORT(self), read_into_buffer_method_id, 2, rbuf, INT2FIX(4));
245 return ((uint8_t)(RSTRING_PTR(rbuf)[3])) |
246 (((uint8_t)(RSTRING_PTR(rbuf)[2])) << 8) |
247 (((uint8_t)(RSTRING_PTR(rbuf)[1])) << 16) |
248 (((uint8_t)(RSTRING_PTR(rbuf)[0])) << 24);
Bryan Duxburyc0166282009-02-02 00:48:17 +0000249}
250
251static int64_t read_i64_direct(VALUE self) {
Bryan Duxburyad0ad822011-06-28 18:46:03 +0000252 VALUE rbuf = rb_ivar_get(self, rbuf_ivar_id);
253 rb_funcall(GET_TRANSPORT(self), read_into_buffer_method_id, 2, rbuf, INT2FIX(8));
254 uint64_t hi = ((uint8_t)(RSTRING_PTR(rbuf)[3])) |
255 (((uint8_t)(RSTRING_PTR(rbuf)[2])) << 8) |
256 (((uint8_t)(RSTRING_PTR(rbuf)[1])) << 16) |
257 (((uint8_t)(RSTRING_PTR(rbuf)[0])) << 24);
258 uint32_t lo = ((uint8_t)(RSTRING_PTR(rbuf)[7])) |
259 (((uint8_t)(RSTRING_PTR(rbuf)[6])) << 8) |
260 (((uint8_t)(RSTRING_PTR(rbuf)[5])) << 16) |
261 (((uint8_t)(RSTRING_PTR(rbuf)[4])) << 24);
Bryan Duxburyc0166282009-02-02 00:48:17 +0000262 return (hi << 32) | lo;
263}
264
265static VALUE get_protocol_exception(VALUE code, VALUE message) {
266 VALUE args[2];
267 args[0] = code;
268 args[1] = message;
269 return rb_class_new_instance(2, (VALUE*)&args, protocol_exception_class);
270}
271
272VALUE rb_thrift_binary_proto_read_message_end(VALUE self) {
273 return Qnil;
274}
275
276VALUE rb_thift_binary_proto_read_struct_begin(VALUE self) {
277 return Qnil;
278}
279
280VALUE rb_thift_binary_proto_read_struct_end(VALUE self) {
281 return Qnil;
282}
283
284VALUE rb_thift_binary_proto_read_field_end(VALUE self) {
285 return Qnil;
286}
287
288VALUE rb_thift_binary_proto_read_map_end(VALUE self) {
289 return Qnil;
290}
291
292VALUE rb_thift_binary_proto_read_list_end(VALUE self) {
293 return Qnil;
294}
295
296VALUE rb_thift_binary_proto_read_set_end(VALUE self) {
297 return Qnil;
298}
299
300VALUE rb_thrift_binary_proto_read_message_begin(VALUE self) {
Kevin Clarkead33822009-02-04 22:43:59 +0000301 VALUE strict_read = GET_STRICT_READ(self);
302 VALUE name, seqid;
303 int type;
Bryan Duxburyc0166282009-02-02 00:48:17 +0000304
Kevin Clarkead33822009-02-04 22:43:59 +0000305 int version = read_i32_direct(self);
306
307 if (version < 0) {
308 if ((version & VERSION_MASK) != VERSION_1) {
309 rb_exc_raise(get_protocol_exception(INT2FIX(BAD_VERSION), rb_str_new2("Missing version identifier")));
310 }
311 type = version & TYPE_MASK;
312 name = rb_thrift_binary_proto_read_string(self);
313 seqid = rb_thrift_binary_proto_read_i32(self);
314 } else {
315 if (strict_read == Qtrue) {
316 rb_exc_raise(get_protocol_exception(INT2FIX(BAD_VERSION), rb_str_new2("No version identifier, old protocol client?")));
317 }
318 name = READ(self, version);
Bryan Duxburyac002d32009-03-31 23:48:36 +0000319 type = read_byte_direct(self);
Kevin Clarkead33822009-02-04 22:43:59 +0000320 seqid = rb_thrift_binary_proto_read_i32(self);
321 }
Bryan Duxburyc0166282009-02-02 00:48:17 +0000322
323 return rb_ary_new3(3, name, INT2FIX(type), seqid);
324}
325
326VALUE rb_thrift_binary_proto_read_field_begin(VALUE self) {
327 int type = read_byte_direct(self);
328 if (type == TTYPE_STOP) {
329 return rb_ary_new3(3, Qnil, INT2FIX(type), INT2FIX(0));
330 } else {
331 VALUE id = rb_thrift_binary_proto_read_i16(self);
332 return rb_ary_new3(3, Qnil, INT2FIX(type), id);
333 }
334}
335
336VALUE rb_thrift_binary_proto_read_map_begin(VALUE self) {
337 VALUE ktype = rb_thrift_binary_proto_read_byte(self);
338 VALUE vtype = rb_thrift_binary_proto_read_byte(self);
339 VALUE size = rb_thrift_binary_proto_read_i32(self);
340 return rb_ary_new3(3, ktype, vtype, size);
341}
342
343VALUE rb_thrift_binary_proto_read_list_begin(VALUE self) {
344 VALUE etype = rb_thrift_binary_proto_read_byte(self);
345 VALUE size = rb_thrift_binary_proto_read_i32(self);
346 return rb_ary_new3(2, etype, size);
347}
348
349VALUE rb_thrift_binary_proto_read_set_begin(VALUE self) {
350 return rb_thrift_binary_proto_read_list_begin(self);
351}
352
353VALUE rb_thrift_binary_proto_read_bool(VALUE self) {
354 char byte = read_byte_direct(self);
Bryan Duxbury5b8b4842009-04-01 20:10:15 +0000355 return byte != 0 ? Qtrue : Qfalse;
Bryan Duxburyc0166282009-02-02 00:48:17 +0000356}
357
358VALUE rb_thrift_binary_proto_read_byte(VALUE self) {
359 return INT2FIX(read_byte_direct(self));
360}
361
362VALUE rb_thrift_binary_proto_read_i16(VALUE self) {
363 return INT2FIX(read_i16_direct(self));
364}
365
366VALUE rb_thrift_binary_proto_read_i32(VALUE self) {
367 return INT2NUM(read_i32_direct(self));
368}
369
370VALUE rb_thrift_binary_proto_read_i64(VALUE self) {
371 return LL2NUM(read_i64_direct(self));
372}
373
374VALUE rb_thrift_binary_proto_read_double(VALUE self) {
375 union {
376 double f;
377 int64_t t;
378 } transfer;
379 transfer.t = read_i64_direct(self);
380 return rb_float_new(transfer.f);
381}
382
383VALUE rb_thrift_binary_proto_read_string(VALUE self) {
384 int size = read_i32_direct(self);
Jake Farrellb5a18a12012-10-09 01:10:43 +0000385 VALUE buffer = READ(self, size);
386 return convert_to_string(buffer);
Bryan Duxburyc0166282009-02-02 00:48:17 +0000387}
388
389void Init_binary_protocol_accelerated() {
390 VALUE thrift_binary_protocol_class = rb_const_get(thrift_module, rb_intern("BinaryProtocol"));
Bryan Duxburyd2cc5bb2010-07-28 21:00:06 +0000391
Bryan Duxburyc0166282009-02-02 00:48:17 +0000392 VERSION_1 = rb_num2ll(rb_const_get(thrift_binary_protocol_class, rb_intern("VERSION_1")));
393 VERSION_MASK = rb_num2ll(rb_const_get(thrift_binary_protocol_class, rb_intern("VERSION_MASK")));
Kevin Clarkead33822009-02-04 22:43:59 +0000394 TYPE_MASK = rb_num2ll(rb_const_get(thrift_binary_protocol_class, rb_intern("TYPE_MASK")));
Bryan Duxburyd2cc5bb2010-07-28 21:00:06 +0000395
Bryan Duxburyc0166282009-02-02 00:48:17 +0000396 VALUE bpa_class = rb_define_class_under(thrift_module, "BinaryProtocolAccelerated", thrift_binary_protocol_class);
Bryan Duxburyd2cc5bb2010-07-28 21:00:06 +0000397
Bryan Duxburyc0166282009-02-02 00:48:17 +0000398 rb_define_method(bpa_class, "native?", rb_thrift_binary_proto_native_qmark, 0);
Bryan Duxburyd2cc5bb2010-07-28 21:00:06 +0000399
Bryan Duxburyc0166282009-02-02 00:48:17 +0000400 rb_define_method(bpa_class, "write_message_begin", rb_thrift_binary_proto_write_message_begin, 3);
401 rb_define_method(bpa_class, "write_field_begin", rb_thrift_binary_proto_write_field_begin, 3);
402 rb_define_method(bpa_class, "write_field_stop", rb_thrift_binary_proto_write_field_stop, 0);
403 rb_define_method(bpa_class, "write_map_begin", rb_thrift_binary_proto_write_map_begin, 3);
404 rb_define_method(bpa_class, "write_list_begin", rb_thrift_binary_proto_write_list_begin, 2);
405 rb_define_method(bpa_class, "write_set_begin", rb_thrift_binary_proto_write_set_begin, 2);
406 rb_define_method(bpa_class, "write_byte", rb_thrift_binary_proto_write_byte, 1);
407 rb_define_method(bpa_class, "write_bool", rb_thrift_binary_proto_write_bool, 1);
408 rb_define_method(bpa_class, "write_i16", rb_thrift_binary_proto_write_i16, 1);
409 rb_define_method(bpa_class, "write_i32", rb_thrift_binary_proto_write_i32, 1);
410 rb_define_method(bpa_class, "write_i64", rb_thrift_binary_proto_write_i64, 1);
411 rb_define_method(bpa_class, "write_double", rb_thrift_binary_proto_write_double, 1);
412 rb_define_method(bpa_class, "write_string", rb_thrift_binary_proto_write_string, 1);
413 // unused methods
414 rb_define_method(bpa_class, "write_message_end", rb_thrift_binary_proto_write_message_end, 0);
415 rb_define_method(bpa_class, "write_struct_begin", rb_thrift_binary_proto_write_struct_begin, 1);
416 rb_define_method(bpa_class, "write_struct_end", rb_thrift_binary_proto_write_struct_end, 0);
417 rb_define_method(bpa_class, "write_field_end", rb_thrift_binary_proto_write_field_end, 0);
418 rb_define_method(bpa_class, "write_map_end", rb_thrift_binary_proto_write_map_end, 0);
419 rb_define_method(bpa_class, "write_list_end", rb_thrift_binary_proto_write_list_end, 0);
420 rb_define_method(bpa_class, "write_set_end", rb_thrift_binary_proto_write_set_end, 0);
Bryan Duxburyc0166282009-02-02 00:48:17 +0000421
422 rb_define_method(bpa_class, "read_message_begin", rb_thrift_binary_proto_read_message_begin, 0);
423 rb_define_method(bpa_class, "read_field_begin", rb_thrift_binary_proto_read_field_begin, 0);
424 rb_define_method(bpa_class, "read_map_begin", rb_thrift_binary_proto_read_map_begin, 0);
425 rb_define_method(bpa_class, "read_list_begin", rb_thrift_binary_proto_read_list_begin, 0);
426 rb_define_method(bpa_class, "read_set_begin", rb_thrift_binary_proto_read_set_begin, 0);
427 rb_define_method(bpa_class, "read_byte", rb_thrift_binary_proto_read_byte, 0);
428 rb_define_method(bpa_class, "read_bool", rb_thrift_binary_proto_read_bool, 0);
429 rb_define_method(bpa_class, "read_i16", rb_thrift_binary_proto_read_i16, 0);
430 rb_define_method(bpa_class, "read_i32", rb_thrift_binary_proto_read_i32, 0);
431 rb_define_method(bpa_class, "read_i64", rb_thrift_binary_proto_read_i64, 0);
432 rb_define_method(bpa_class, "read_double", rb_thrift_binary_proto_read_double, 0);
433 rb_define_method(bpa_class, "read_string", rb_thrift_binary_proto_read_string, 0);
434 // unused methods
435 rb_define_method(bpa_class, "read_message_end", rb_thrift_binary_proto_read_message_end, 0);
436 rb_define_method(bpa_class, "read_struct_begin", rb_thift_binary_proto_read_struct_begin, 0);
437 rb_define_method(bpa_class, "read_struct_end", rb_thift_binary_proto_read_struct_end, 0);
438 rb_define_method(bpa_class, "read_field_end", rb_thift_binary_proto_read_field_end, 0);
439 rb_define_method(bpa_class, "read_map_end", rb_thift_binary_proto_read_map_end, 0);
440 rb_define_method(bpa_class, "read_list_end", rb_thift_binary_proto_read_list_end, 0);
441 rb_define_method(bpa_class, "read_set_end", rb_thift_binary_proto_read_set_end, 0);
Bryan Duxburyad0ad822011-06-28 18:46:03 +0000442
443 rbuf_ivar_id = rb_intern("@rbuf");
Bryan Duxbury1e80d442009-02-03 18:16:54 +0000444}