blob: 5d6607451be43bd3e87f252258aaa2d0648ed879 [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) {
79 write_i32_direct(trans, RSTRING(str)->len);
80 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;
203 transfer.f = RFLOAT(rb_Float(dub))->value;
204 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);
212 // write_i32_direct(trans, RSTRING(str)->len);
213 // rb_funcall(trans, write_method_id, 1, str);
214 write_string_direct(trans, str);
215 return Qnil;
216}
217
218//---------------------------------------
219// interface reading methods
220//---------------------------------------
221
Bryan Duxburyc0166282009-02-02 00:48:17 +0000222VALUE rb_thrift_binary_proto_read_string(VALUE self);
223VALUE rb_thrift_binary_proto_read_byte(VALUE self);
224VALUE rb_thrift_binary_proto_read_i32(VALUE self);
225VALUE rb_thrift_binary_proto_read_i16(VALUE self);
226
227static char read_byte_direct(VALUE self) {
228 return (RSTRING(READ(self, 1))->ptr)[0];
229}
230
231static int16_t read_i16_direct(VALUE self) {
232 VALUE buf = READ(self, 2);
233 return (int16_t)(((uint8_t)(RSTRING(buf)->ptr[1])) | ((uint16_t)((RSTRING(buf)->ptr[0]) << 8)));
234}
235
236static int32_t read_i32_direct(VALUE self) {
237 VALUE buf = READ(self, 4);
238 return ((uint8_t)(RSTRING(buf)->ptr[3])) |
239 (((uint8_t)(RSTRING(buf)->ptr[2])) << 8) |
240 (((uint8_t)(RSTRING(buf)->ptr[1])) << 16) |
241 (((uint8_t)(RSTRING(buf)->ptr[0])) << 24);
242}
243
244static int64_t read_i64_direct(VALUE self) {
245 uint64_t hi = read_i32_direct(self);
246 uint32_t lo = read_i32_direct(self);
247 return (hi << 32) | lo;
248}
249
250static VALUE get_protocol_exception(VALUE code, VALUE message) {
251 VALUE args[2];
252 args[0] = code;
253 args[1] = message;
254 return rb_class_new_instance(2, (VALUE*)&args, protocol_exception_class);
255}
256
257VALUE rb_thrift_binary_proto_read_message_end(VALUE self) {
258 return Qnil;
259}
260
261VALUE rb_thift_binary_proto_read_struct_begin(VALUE self) {
262 return Qnil;
263}
264
265VALUE rb_thift_binary_proto_read_struct_end(VALUE self) {
266 return Qnil;
267}
268
269VALUE rb_thift_binary_proto_read_field_end(VALUE self) {
270 return Qnil;
271}
272
273VALUE rb_thift_binary_proto_read_map_end(VALUE self) {
274 return Qnil;
275}
276
277VALUE rb_thift_binary_proto_read_list_end(VALUE self) {
278 return Qnil;
279}
280
281VALUE rb_thift_binary_proto_read_set_end(VALUE self) {
282 return Qnil;
283}
284
285VALUE rb_thrift_binary_proto_read_message_begin(VALUE self) {
Kevin Clarkead33822009-02-04 22:43:59 +0000286 VALUE strict_read = GET_STRICT_READ(self);
287 VALUE name, seqid;
288 int type;
Bryan Duxburyc0166282009-02-02 00:48:17 +0000289
Kevin Clarkead33822009-02-04 22:43:59 +0000290 int version = read_i32_direct(self);
291
292 if (version < 0) {
293 if ((version & VERSION_MASK) != VERSION_1) {
294 rb_exc_raise(get_protocol_exception(INT2FIX(BAD_VERSION), rb_str_new2("Missing version identifier")));
295 }
296 type = version & TYPE_MASK;
297 name = rb_thrift_binary_proto_read_string(self);
298 seqid = rb_thrift_binary_proto_read_i32(self);
299 } else {
300 if (strict_read == Qtrue) {
301 rb_exc_raise(get_protocol_exception(INT2FIX(BAD_VERSION), rb_str_new2("No version identifier, old protocol client?")));
302 }
303 name = READ(self, version);
304 type = rb_thrift_binary_proto_read_byte(self);
305 seqid = rb_thrift_binary_proto_read_i32(self);
306 }
Bryan Duxburyc0166282009-02-02 00:48:17 +0000307
308 return rb_ary_new3(3, name, INT2FIX(type), seqid);
309}
310
311VALUE rb_thrift_binary_proto_read_field_begin(VALUE self) {
312 int type = read_byte_direct(self);
313 if (type == TTYPE_STOP) {
314 return rb_ary_new3(3, Qnil, INT2FIX(type), INT2FIX(0));
315 } else {
316 VALUE id = rb_thrift_binary_proto_read_i16(self);
317 return rb_ary_new3(3, Qnil, INT2FIX(type), id);
318 }
319}
320
321VALUE rb_thrift_binary_proto_read_map_begin(VALUE self) {
322 VALUE ktype = rb_thrift_binary_proto_read_byte(self);
323 VALUE vtype = rb_thrift_binary_proto_read_byte(self);
324 VALUE size = rb_thrift_binary_proto_read_i32(self);
325 return rb_ary_new3(3, ktype, vtype, size);
326}
327
328VALUE rb_thrift_binary_proto_read_list_begin(VALUE self) {
329 VALUE etype = rb_thrift_binary_proto_read_byte(self);
330 VALUE size = rb_thrift_binary_proto_read_i32(self);
331 return rb_ary_new3(2, etype, size);
332}
333
334VALUE rb_thrift_binary_proto_read_set_begin(VALUE self) {
335 return rb_thrift_binary_proto_read_list_begin(self);
336}
337
338VALUE rb_thrift_binary_proto_read_bool(VALUE self) {
339 char byte = read_byte_direct(self);
340 return byte == 1 ? Qtrue : Qfalse;
341}
342
343VALUE rb_thrift_binary_proto_read_byte(VALUE self) {
344 return INT2FIX(read_byte_direct(self));
345}
346
347VALUE rb_thrift_binary_proto_read_i16(VALUE self) {
348 return INT2FIX(read_i16_direct(self));
349}
350
351VALUE rb_thrift_binary_proto_read_i32(VALUE self) {
352 return INT2NUM(read_i32_direct(self));
353}
354
355VALUE rb_thrift_binary_proto_read_i64(VALUE self) {
356 return LL2NUM(read_i64_direct(self));
357}
358
359VALUE rb_thrift_binary_proto_read_double(VALUE self) {
360 union {
361 double f;
362 int64_t t;
363 } transfer;
364 transfer.t = read_i64_direct(self);
365 return rb_float_new(transfer.f);
366}
367
368VALUE rb_thrift_binary_proto_read_string(VALUE self) {
369 int size = read_i32_direct(self);
370 return READ(self, size);
371}
372
373void Init_binary_protocol_accelerated() {
374 VALUE thrift_binary_protocol_class = rb_const_get(thrift_module, rb_intern("BinaryProtocol"));
375
376 VERSION_1 = rb_num2ll(rb_const_get(thrift_binary_protocol_class, rb_intern("VERSION_1")));
377 VERSION_MASK = rb_num2ll(rb_const_get(thrift_binary_protocol_class, rb_intern("VERSION_MASK")));
Kevin Clarkead33822009-02-04 22:43:59 +0000378 TYPE_MASK = rb_num2ll(rb_const_get(thrift_binary_protocol_class, rb_intern("TYPE_MASK")));
Bryan Duxburyc0166282009-02-02 00:48:17 +0000379
380 VALUE bpa_class = rb_define_class_under(thrift_module, "BinaryProtocolAccelerated", thrift_binary_protocol_class);
381
382 rb_define_method(bpa_class, "native?", rb_thrift_binary_proto_native_qmark, 0);
383
384 rb_define_method(bpa_class, "write_message_begin", rb_thrift_binary_proto_write_message_begin, 3);
385 rb_define_method(bpa_class, "write_field_begin", rb_thrift_binary_proto_write_field_begin, 3);
386 rb_define_method(bpa_class, "write_field_stop", rb_thrift_binary_proto_write_field_stop, 0);
387 rb_define_method(bpa_class, "write_map_begin", rb_thrift_binary_proto_write_map_begin, 3);
388 rb_define_method(bpa_class, "write_list_begin", rb_thrift_binary_proto_write_list_begin, 2);
389 rb_define_method(bpa_class, "write_set_begin", rb_thrift_binary_proto_write_set_begin, 2);
390 rb_define_method(bpa_class, "write_byte", rb_thrift_binary_proto_write_byte, 1);
391 rb_define_method(bpa_class, "write_bool", rb_thrift_binary_proto_write_bool, 1);
392 rb_define_method(bpa_class, "write_i16", rb_thrift_binary_proto_write_i16, 1);
393 rb_define_method(bpa_class, "write_i32", rb_thrift_binary_proto_write_i32, 1);
394 rb_define_method(bpa_class, "write_i64", rb_thrift_binary_proto_write_i64, 1);
395 rb_define_method(bpa_class, "write_double", rb_thrift_binary_proto_write_double, 1);
396 rb_define_method(bpa_class, "write_string", rb_thrift_binary_proto_write_string, 1);
397 // unused methods
398 rb_define_method(bpa_class, "write_message_end", rb_thrift_binary_proto_write_message_end, 0);
399 rb_define_method(bpa_class, "write_struct_begin", rb_thrift_binary_proto_write_struct_begin, 1);
400 rb_define_method(bpa_class, "write_struct_end", rb_thrift_binary_proto_write_struct_end, 0);
401 rb_define_method(bpa_class, "write_field_end", rb_thrift_binary_proto_write_field_end, 0);
402 rb_define_method(bpa_class, "write_map_end", rb_thrift_binary_proto_write_map_end, 0);
403 rb_define_method(bpa_class, "write_list_end", rb_thrift_binary_proto_write_list_end, 0);
404 rb_define_method(bpa_class, "write_set_end", rb_thrift_binary_proto_write_set_end, 0);
405
406
407
408 rb_define_method(bpa_class, "read_message_begin", rb_thrift_binary_proto_read_message_begin, 0);
409 rb_define_method(bpa_class, "read_field_begin", rb_thrift_binary_proto_read_field_begin, 0);
410 rb_define_method(bpa_class, "read_map_begin", rb_thrift_binary_proto_read_map_begin, 0);
411 rb_define_method(bpa_class, "read_list_begin", rb_thrift_binary_proto_read_list_begin, 0);
412 rb_define_method(bpa_class, "read_set_begin", rb_thrift_binary_proto_read_set_begin, 0);
413 rb_define_method(bpa_class, "read_byte", rb_thrift_binary_proto_read_byte, 0);
414 rb_define_method(bpa_class, "read_bool", rb_thrift_binary_proto_read_bool, 0);
415 rb_define_method(bpa_class, "read_i16", rb_thrift_binary_proto_read_i16, 0);
416 rb_define_method(bpa_class, "read_i32", rb_thrift_binary_proto_read_i32, 0);
417 rb_define_method(bpa_class, "read_i64", rb_thrift_binary_proto_read_i64, 0);
418 rb_define_method(bpa_class, "read_double", rb_thrift_binary_proto_read_double, 0);
419 rb_define_method(bpa_class, "read_string", rb_thrift_binary_proto_read_string, 0);
420 // unused methods
421 rb_define_method(bpa_class, "read_message_end", rb_thrift_binary_proto_read_message_end, 0);
422 rb_define_method(bpa_class, "read_struct_begin", rb_thift_binary_proto_read_struct_begin, 0);
423 rb_define_method(bpa_class, "read_struct_end", rb_thift_binary_proto_read_struct_end, 0);
424 rb_define_method(bpa_class, "read_field_end", rb_thift_binary_proto_read_field_end, 0);
425 rb_define_method(bpa_class, "read_map_end", rb_thift_binary_proto_read_map_end, 0);
426 rb_define_method(bpa_class, "read_list_end", rb_thift_binary_proto_read_list_end, 0);
427 rb_define_method(bpa_class, "read_set_end", rb_thift_binary_proto_read_set_end, 0);
428
429 // set up native method table
430 native_proto_method_table *npmt;
431 npmt = ALLOC(native_proto_method_table);
432
433 npmt->write_field_begin = rb_thrift_binary_proto_write_field_begin;
434 npmt->write_field_stop = rb_thrift_binary_proto_write_field_stop;
435 npmt->write_map_begin = rb_thrift_binary_proto_write_map_begin;
436 npmt->write_list_begin = rb_thrift_binary_proto_write_list_begin;
437 npmt->write_set_begin = rb_thrift_binary_proto_write_set_begin;
438 npmt->write_byte = rb_thrift_binary_proto_write_byte;
439 npmt->write_bool = rb_thrift_binary_proto_write_bool;
440 npmt->write_i16 = rb_thrift_binary_proto_write_i16;
441 npmt->write_i32 = rb_thrift_binary_proto_write_i32;
442 npmt->write_i64 = rb_thrift_binary_proto_write_i64;
443 npmt->write_double = rb_thrift_binary_proto_write_double;
444 npmt->write_string = rb_thrift_binary_proto_write_string;
445 npmt->write_message_end = rb_thrift_binary_proto_write_message_end;
446 npmt->write_struct_begin = rb_thrift_binary_proto_write_struct_begin;
447 npmt->write_struct_end = rb_thrift_binary_proto_write_struct_end;
448 npmt->write_field_end = rb_thrift_binary_proto_write_field_end;
449 npmt->write_map_end = rb_thrift_binary_proto_write_map_end;
450 npmt->write_list_end = rb_thrift_binary_proto_write_list_end;
451 npmt->write_set_end = rb_thrift_binary_proto_write_set_end;
452
453 npmt->read_message_begin = rb_thrift_binary_proto_read_message_begin;
454 npmt->read_field_begin = rb_thrift_binary_proto_read_field_begin;
455 npmt->read_map_begin = rb_thrift_binary_proto_read_map_begin;
456 npmt->read_list_begin = rb_thrift_binary_proto_read_list_begin;
457 npmt->read_set_begin = rb_thrift_binary_proto_read_set_begin;
458 npmt->read_byte = rb_thrift_binary_proto_read_byte;
459 npmt->read_bool = rb_thrift_binary_proto_read_bool;
460 npmt->read_i16 = rb_thrift_binary_proto_read_i16;
461 npmt->read_i32 = rb_thrift_binary_proto_read_i32;
462 npmt->read_i64 = rb_thrift_binary_proto_read_i64;
463 npmt->read_double = rb_thrift_binary_proto_read_double;
464 npmt->read_string = rb_thrift_binary_proto_read_string;
465 npmt->read_message_end = rb_thrift_binary_proto_read_message_end;
466 npmt->read_struct_begin = rb_thift_binary_proto_read_struct_begin;
467 npmt->read_struct_end = rb_thift_binary_proto_read_struct_end;
468 npmt->read_field_end = rb_thift_binary_proto_read_field_end;
469 npmt->read_map_end = rb_thift_binary_proto_read_map_end;
470 npmt->read_list_end = rb_thift_binary_proto_read_list_end;
471 npmt->read_set_end = rb_thift_binary_proto_read_set_end;
472
473 VALUE method_table_object = Data_Wrap_Struct(rb_cObject, 0, free, npmt);
474 rb_const_set(bpa_class, rb_intern("@native_method_table"), method_table_object);
Bryan Duxbury1e80d442009-02-03 18:16:54 +0000475}