blob: 728a05725604c44b297e9d3da834fb337b74d5a2 [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>
Bryan Duxburyd815c212009-03-19 18:57:43 +000025#include "macros.h"
Bryan Duxburyc0166282009-02-02 00:48:17 +000026
27VALUE rb_thrift_binary_proto_native_qmark(VALUE self) {
28 return Qtrue;
29}
30
31
32
33static int VERSION_1;
34static int VERSION_MASK;
Kevin Clarkead33822009-02-04 22:43:59 +000035static int TYPE_MASK;
Bryan Duxburyc0166282009-02-02 00:48:17 +000036static int BAD_VERSION;
37
38static void write_byte_direct(VALUE trans, int8_t b) {
39 WRITE(trans, (char*)&b, 1);
40}
41
42static 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
51static 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
63static 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
78static void write_string_direct(VALUE trans, VALUE str) {
Bryan Duxburye3ab50d2009-03-25 21:06:53 +000079 write_i32_direct(trans, RSTRING_LEN(str));
Bryan Duxburyc0166282009-02-02 00:48:17 +000080 rb_funcall(trans, write_method_id, 1, str);
81}
82
83//--------------------------------
84// interface writing methods
85//--------------------------------
86
87VALUE rb_thrift_binary_proto_write_message_end(VALUE self) {
88 return Qnil;
89}
90
91VALUE rb_thrift_binary_proto_write_struct_begin(VALUE self, VALUE name) {
92 return Qnil;
93}
94
95VALUE rb_thrift_binary_proto_write_struct_end(VALUE self) {
96 return Qnil;
97}
98
99VALUE rb_thrift_binary_proto_write_field_end(VALUE self) {
100 return Qnil;
101}
102
103VALUE rb_thrift_binary_proto_write_map_end(VALUE self) {
104 return Qnil;
105}
106
107VALUE rb_thrift_binary_proto_write_list_end(VALUE self) {
108 return Qnil;
109}
110
111VALUE rb_thrift_binary_proto_write_set_end(VALUE self) {
112 return Qnil;
113}
114
115VALUE rb_thrift_binary_proto_write_message_begin(VALUE self, VALUE name, VALUE type, VALUE seqid) {
116 VALUE trans = GET_TRANSPORT(self);
Kevin Clarkead33822009-02-04 22:43:59 +0000117 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);
Bryan Duxburyd40731e2009-03-20 02:21:05 +0000125 write_byte_direct(trans, FIX2INT(type));
Kevin Clarkead33822009-02-04 22:43:59 +0000126 write_i32_direct(trans, FIX2INT(seqid));
127 }
Bryan Duxburyc0166282009-02-02 00:48:17 +0000128
129 return Qnil;
130}
131
132VALUE 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
140VALUE rb_thrift_binary_proto_write_field_stop(VALUE self) {
141 write_byte_direct(GET_TRANSPORT(self), TTYPE_STOP);
142 return Qnil;
143}
144
145VALUE 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
154VALUE 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
162VALUE 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
167VALUE 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
172VALUE 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
178VALUE 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
184VALUE 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
190VALUE 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
196VALUE 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;
Bryan Duxburye3ab50d2009-03-25 21:06:53 +0000203 transfer.f = RFLOAT_VALUE(rb_Float(dub));
Bryan Duxburyc0166282009-02-02 00:48:17 +0000204 write_i64_direct(GET_TRANSPORT(self), transfer.t);
205
206 return Qnil;
207}
208
209VALUE rb_thrift_binary_proto_write_string(VALUE self, VALUE str) {
210 CHECK_NIL(str);
211 VALUE trans = GET_TRANSPORT(self);
Bryan Duxburyc0166282009-02-02 00:48:17 +0000212 write_string_direct(trans, str);
213 return Qnil;
214}
215
216//---------------------------------------
217// interface reading methods
218//---------------------------------------
219
Bryan Duxburyc0166282009-02-02 00:48:17 +0000220VALUE rb_thrift_binary_proto_read_string(VALUE self);
221VALUE rb_thrift_binary_proto_read_byte(VALUE self);
222VALUE rb_thrift_binary_proto_read_i32(VALUE self);
223VALUE rb_thrift_binary_proto_read_i16(VALUE self);
224
225static char read_byte_direct(VALUE self) {
Bryan Duxburye3ab50d2009-03-25 21:06:53 +0000226 VALUE buf = READ(self, 1);
227 return RSTRING_PTR(buf)[0];
Bryan Duxburyc0166282009-02-02 00:48:17 +0000228}
229
230static int16_t read_i16_direct(VALUE self) {
231 VALUE buf = READ(self, 2);
Bryan Duxburye3ab50d2009-03-25 21:06:53 +0000232 return (int16_t)(((uint8_t)(RSTRING_PTR(buf)[1])) | ((uint16_t)((RSTRING_PTR(buf)[0]) << 8)));
Bryan Duxburyc0166282009-02-02 00:48:17 +0000233}
234
235static int32_t read_i32_direct(VALUE self) {
236 VALUE buf = READ(self, 4);
Bryan Duxburye3ab50d2009-03-25 21:06:53 +0000237 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);
Bryan Duxburyc0166282009-02-02 00:48:17 +0000241}
242
243static 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
249static 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
256VALUE rb_thrift_binary_proto_read_message_end(VALUE self) {
257 return Qnil;
258}
259
260VALUE rb_thift_binary_proto_read_struct_begin(VALUE self) {
261 return Qnil;
262}
263
264VALUE rb_thift_binary_proto_read_struct_end(VALUE self) {
265 return Qnil;
266}
267
268VALUE rb_thift_binary_proto_read_field_end(VALUE self) {
269 return Qnil;
270}
271
272VALUE rb_thift_binary_proto_read_map_end(VALUE self) {
273 return Qnil;
274}
275
276VALUE rb_thift_binary_proto_read_list_end(VALUE self) {
277 return Qnil;
278}
279
280VALUE rb_thift_binary_proto_read_set_end(VALUE self) {
281 return Qnil;
282}
283
284VALUE rb_thrift_binary_proto_read_message_begin(VALUE self) {
Kevin Clarkead33822009-02-04 22:43:59 +0000285 VALUE strict_read = GET_STRICT_READ(self);
286 VALUE name, seqid;
287 int type;
Bryan Duxburyc0166282009-02-02 00:48:17 +0000288
Kevin Clarkead33822009-02-04 22:43:59 +0000289 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);
Bryan Duxburyac002d32009-03-31 23:48:36 +0000303 type = read_byte_direct(self);
Kevin Clarkead33822009-02-04 22:43:59 +0000304 seqid = rb_thrift_binary_proto_read_i32(self);
305 }
Bryan Duxburyc0166282009-02-02 00:48:17 +0000306
307 return rb_ary_new3(3, name, INT2FIX(type), seqid);
308}
309
310VALUE 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
320VALUE 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
327VALUE 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
333VALUE rb_thrift_binary_proto_read_set_begin(VALUE self) {
334 return rb_thrift_binary_proto_read_list_begin(self);
335}
336
337VALUE rb_thrift_binary_proto_read_bool(VALUE self) {
338 char byte = read_byte_direct(self);
Bryan Duxbury5b8b4842009-04-01 20:10:15 +0000339 return byte != 0 ? Qtrue : Qfalse;
Bryan Duxburyc0166282009-02-02 00:48:17 +0000340}
341
342VALUE rb_thrift_binary_proto_read_byte(VALUE self) {
343 return INT2FIX(read_byte_direct(self));
344}
345
346VALUE rb_thrift_binary_proto_read_i16(VALUE self) {
347 return INT2FIX(read_i16_direct(self));
348}
349
350VALUE rb_thrift_binary_proto_read_i32(VALUE self) {
351 return INT2NUM(read_i32_direct(self));
352}
353
354VALUE rb_thrift_binary_proto_read_i64(VALUE self) {
355 return LL2NUM(read_i64_direct(self));
356}
357
358VALUE 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
367VALUE rb_thrift_binary_proto_read_string(VALUE self) {
368 int size = read_i32_direct(self);
369 return READ(self, size);
370}
371
372void 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")));
Kevin Clarkead33822009-02-04 22:43:59 +0000377 TYPE_MASK = rb_num2ll(rb_const_get(thrift_binary_protocol_class, rb_intern("TYPE_MASK")));
Bryan Duxburyc0166282009-02-02 00:48:17 +0000378
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);
Bryan Duxbury1e80d442009-02-03 18:16:54 +0000474}