blob: fc4b675b7c68884e776561f0168cb337682cd218 [file] [log] [blame]
Bryan Duxburyc0166282009-02-02 00:48:17 +00001#include <ruby.h>
2#include <stdbool.h>
Bryan Duxbury1e80d442009-02-03 18:16:54 +00003#include <stdint.h>
Bryan Duxburyc0166282009-02-02 00:48:17 +00004#include <constants.h>
5#include <struct.h>
6
7#define GET_TRANSPORT(obj) rb_ivar_get(obj, transport_ivar_id)
Kevin Clarkead33822009-02-04 22:43:59 +00008#define GET_STRICT_READ(obj) rb_ivar_get(obj, strict_read_ivar_id)
9#define GET_STRICT_WRITE(obj) rb_ivar_get(obj, strict_write_ivar_id)
Bryan Duxburyc0166282009-02-02 00:48:17 +000010#define WRITE(obj, data, length) rb_funcall(obj, write_method_id, 1, rb_str_new(data, length))
11#define CHECK_NIL(obj) if (NIL_P(obj)) { rb_raise(rb_eStandardError, "nil argument not allowed!");}
12
13VALUE rb_thrift_binary_proto_native_qmark(VALUE self) {
14 return Qtrue;
15}
16
17
18
19static int VERSION_1;
20static int VERSION_MASK;
Kevin Clarkead33822009-02-04 22:43:59 +000021static int TYPE_MASK;
Bryan Duxburyc0166282009-02-02 00:48:17 +000022static int BAD_VERSION;
23
24static void write_byte_direct(VALUE trans, int8_t b) {
25 WRITE(trans, (char*)&b, 1);
26}
27
28static void write_i16_direct(VALUE trans, int16_t value) {
29 char data[2];
30
31 data[1] = value;
32 data[0] = (value >> 8);
33
34 WRITE(trans, data, 2);
35}
36
37static void write_i32_direct(VALUE trans, int32_t value) {
38 char data[4];
39
40 data[3] = value;
41 data[2] = (value >> 8);
42 data[1] = (value >> 16);
43 data[0] = (value >> 24);
44
45 WRITE(trans, data, 4);
46}
47
48
49static void write_i64_direct(VALUE trans, int64_t value) {
50 char data[8];
51
52 data[7] = value;
53 data[6] = (value >> 8);
54 data[5] = (value >> 16);
55 data[4] = (value >> 24);
56 data[3] = (value >> 32);
57 data[2] = (value >> 40);
58 data[1] = (value >> 48);
59 data[0] = (value >> 56);
60
61 WRITE(trans, data, 8);
62}
63
64static void write_string_direct(VALUE trans, VALUE str) {
65 write_i32_direct(trans, RSTRING(str)->len);
66 rb_funcall(trans, write_method_id, 1, str);
67}
68
69//--------------------------------
70// interface writing methods
71//--------------------------------
72
73VALUE rb_thrift_binary_proto_write_message_end(VALUE self) {
74 return Qnil;
75}
76
77VALUE rb_thrift_binary_proto_write_struct_begin(VALUE self, VALUE name) {
78 return Qnil;
79}
80
81VALUE rb_thrift_binary_proto_write_struct_end(VALUE self) {
82 return Qnil;
83}
84
85VALUE rb_thrift_binary_proto_write_field_end(VALUE self) {
86 return Qnil;
87}
88
89VALUE rb_thrift_binary_proto_write_map_end(VALUE self) {
90 return Qnil;
91}
92
93VALUE rb_thrift_binary_proto_write_list_end(VALUE self) {
94 return Qnil;
95}
96
97VALUE rb_thrift_binary_proto_write_set_end(VALUE self) {
98 return Qnil;
99}
100
101VALUE rb_thrift_binary_proto_write_message_begin(VALUE self, VALUE name, VALUE type, VALUE seqid) {
102 VALUE trans = GET_TRANSPORT(self);
Kevin Clarkead33822009-02-04 22:43:59 +0000103 VALUE strict_write = GET_STRICT_WRITE(self);
104
105 if (strict_write == Qtrue) {
106 write_i32_direct(trans, VERSION_1 | FIX2INT(type));
107 write_string_direct(trans, name);
108 write_i32_direct(trans, FIX2INT(seqid));
109 } else {
110 write_string_direct(trans, name);
111 write_byte_direct(trans, type);
112 write_i32_direct(trans, FIX2INT(seqid));
113 }
Bryan Duxburyc0166282009-02-02 00:48:17 +0000114
115 return Qnil;
116}
117
118VALUE rb_thrift_binary_proto_write_field_begin(VALUE self, VALUE name, VALUE type, VALUE id) {
119 VALUE trans = GET_TRANSPORT(self);
120 write_byte_direct(trans, FIX2INT(type));
121 write_i16_direct(trans, FIX2INT(id));
122
123 return Qnil;
124}
125
126VALUE rb_thrift_binary_proto_write_field_stop(VALUE self) {
127 write_byte_direct(GET_TRANSPORT(self), TTYPE_STOP);
128 return Qnil;
129}
130
131VALUE rb_thrift_binary_proto_write_map_begin(VALUE self, VALUE ktype, VALUE vtype, VALUE size) {
132 VALUE trans = GET_TRANSPORT(self);
133 write_byte_direct(trans, FIX2INT(ktype));
134 write_byte_direct(trans, FIX2INT(vtype));
135 write_i32_direct(trans, FIX2INT(size));
136
137 return Qnil;
138}
139
140VALUE rb_thrift_binary_proto_write_list_begin(VALUE self, VALUE etype, VALUE size) {
141 VALUE trans = GET_TRANSPORT(self);
142 write_byte_direct(trans, FIX2INT(etype));
143 write_i32_direct(trans, FIX2INT(size));
144
145 return Qnil;
146}
147
148VALUE rb_thrift_binary_proto_write_set_begin(VALUE self, VALUE etype, VALUE size) {
149 rb_thrift_binary_proto_write_list_begin(self, etype, size);
150 return Qnil;
151}
152
153VALUE rb_thrift_binary_proto_write_bool(VALUE self, VALUE b) {
154 write_byte_direct(GET_TRANSPORT(self), RTEST(b) ? 1 : 0);
155 return Qnil;
156}
157
158VALUE rb_thrift_binary_proto_write_byte(VALUE self, VALUE byte) {
159 CHECK_NIL(byte);
160 write_byte_direct(GET_TRANSPORT(self), NUM2INT(byte));
161 return Qnil;
162}
163
164VALUE rb_thrift_binary_proto_write_i16(VALUE self, VALUE i16) {
165 CHECK_NIL(i16);
166 write_i16_direct(GET_TRANSPORT(self), FIX2INT(i16));
167 return Qnil;
168}
169
170VALUE rb_thrift_binary_proto_write_i32(VALUE self, VALUE i32) {
171 CHECK_NIL(i32);
172 write_i32_direct(GET_TRANSPORT(self), NUM2INT(i32));
173 return Qnil;
174}
175
176VALUE rb_thrift_binary_proto_write_i64(VALUE self, VALUE i64) {
177 CHECK_NIL(i64);
178 write_i64_direct(GET_TRANSPORT(self), NUM2LL(i64));
179 return Qnil;
180}
181
182VALUE rb_thrift_binary_proto_write_double(VALUE self, VALUE dub) {
183 CHECK_NIL(dub);
184 // Unfortunately, bitwise_cast doesn't work in C. Bad C!
185 union {
186 double f;
187 int64_t t;
188 } transfer;
189 transfer.f = RFLOAT(rb_Float(dub))->value;
190 write_i64_direct(GET_TRANSPORT(self), transfer.t);
191
192 return Qnil;
193}
194
195VALUE rb_thrift_binary_proto_write_string(VALUE self, VALUE str) {
196 CHECK_NIL(str);
197 VALUE trans = GET_TRANSPORT(self);
198 // write_i32_direct(trans, RSTRING(str)->len);
199 // rb_funcall(trans, write_method_id, 1, str);
200 write_string_direct(trans, str);
201 return Qnil;
202}
203
204//---------------------------------------
205// interface reading methods
206//---------------------------------------
207
208#define READ(obj, length) rb_funcall(GET_TRANSPORT(obj), read_method_id, 1, INT2FIX(length))
209
210VALUE rb_thrift_binary_proto_read_string(VALUE self);
211VALUE rb_thrift_binary_proto_read_byte(VALUE self);
212VALUE rb_thrift_binary_proto_read_i32(VALUE self);
213VALUE rb_thrift_binary_proto_read_i16(VALUE self);
214
215static char read_byte_direct(VALUE self) {
216 return (RSTRING(READ(self, 1))->ptr)[0];
217}
218
219static int16_t read_i16_direct(VALUE self) {
220 VALUE buf = READ(self, 2);
221 return (int16_t)(((uint8_t)(RSTRING(buf)->ptr[1])) | ((uint16_t)((RSTRING(buf)->ptr[0]) << 8)));
222}
223
224static int32_t read_i32_direct(VALUE self) {
225 VALUE buf = READ(self, 4);
226 return ((uint8_t)(RSTRING(buf)->ptr[3])) |
227 (((uint8_t)(RSTRING(buf)->ptr[2])) << 8) |
228 (((uint8_t)(RSTRING(buf)->ptr[1])) << 16) |
229 (((uint8_t)(RSTRING(buf)->ptr[0])) << 24);
230}
231
232static int64_t read_i64_direct(VALUE self) {
233 uint64_t hi = read_i32_direct(self);
234 uint32_t lo = read_i32_direct(self);
235 return (hi << 32) | lo;
236}
237
238static VALUE get_protocol_exception(VALUE code, VALUE message) {
239 VALUE args[2];
240 args[0] = code;
241 args[1] = message;
242 return rb_class_new_instance(2, (VALUE*)&args, protocol_exception_class);
243}
244
245VALUE rb_thrift_binary_proto_read_message_end(VALUE self) {
246 return Qnil;
247}
248
249VALUE rb_thift_binary_proto_read_struct_begin(VALUE self) {
250 return Qnil;
251}
252
253VALUE rb_thift_binary_proto_read_struct_end(VALUE self) {
254 return Qnil;
255}
256
257VALUE rb_thift_binary_proto_read_field_end(VALUE self) {
258 return Qnil;
259}
260
261VALUE rb_thift_binary_proto_read_map_end(VALUE self) {
262 return Qnil;
263}
264
265VALUE rb_thift_binary_proto_read_list_end(VALUE self) {
266 return Qnil;
267}
268
269VALUE rb_thift_binary_proto_read_set_end(VALUE self) {
270 return Qnil;
271}
272
273VALUE rb_thrift_binary_proto_read_message_begin(VALUE self) {
Kevin Clarkead33822009-02-04 22:43:59 +0000274 VALUE strict_read = GET_STRICT_READ(self);
275 VALUE name, seqid;
276 int type;
Bryan Duxburyc0166282009-02-02 00:48:17 +0000277
Kevin Clarkead33822009-02-04 22:43:59 +0000278 int version = read_i32_direct(self);
279
280 if (version < 0) {
281 if ((version & VERSION_MASK) != VERSION_1) {
282 rb_exc_raise(get_protocol_exception(INT2FIX(BAD_VERSION), rb_str_new2("Missing version identifier")));
283 }
284 type = version & TYPE_MASK;
285 name = rb_thrift_binary_proto_read_string(self);
286 seqid = rb_thrift_binary_proto_read_i32(self);
287 } else {
288 if (strict_read == Qtrue) {
289 rb_exc_raise(get_protocol_exception(INT2FIX(BAD_VERSION), rb_str_new2("No version identifier, old protocol client?")));
290 }
291 name = READ(self, version);
292 type = rb_thrift_binary_proto_read_byte(self);
293 seqid = rb_thrift_binary_proto_read_i32(self);
294 }
Bryan Duxburyc0166282009-02-02 00:48:17 +0000295
296 return rb_ary_new3(3, name, INT2FIX(type), seqid);
297}
298
299VALUE rb_thrift_binary_proto_read_field_begin(VALUE self) {
300 int type = read_byte_direct(self);
301 if (type == TTYPE_STOP) {
302 return rb_ary_new3(3, Qnil, INT2FIX(type), INT2FIX(0));
303 } else {
304 VALUE id = rb_thrift_binary_proto_read_i16(self);
305 return rb_ary_new3(3, Qnil, INT2FIX(type), id);
306 }
307}
308
309VALUE rb_thrift_binary_proto_read_map_begin(VALUE self) {
310 VALUE ktype = rb_thrift_binary_proto_read_byte(self);
311 VALUE vtype = rb_thrift_binary_proto_read_byte(self);
312 VALUE size = rb_thrift_binary_proto_read_i32(self);
313 return rb_ary_new3(3, ktype, vtype, size);
314}
315
316VALUE rb_thrift_binary_proto_read_list_begin(VALUE self) {
317 VALUE etype = rb_thrift_binary_proto_read_byte(self);
318 VALUE size = rb_thrift_binary_proto_read_i32(self);
319 return rb_ary_new3(2, etype, size);
320}
321
322VALUE rb_thrift_binary_proto_read_set_begin(VALUE self) {
323 return rb_thrift_binary_proto_read_list_begin(self);
324}
325
326VALUE rb_thrift_binary_proto_read_bool(VALUE self) {
327 char byte = read_byte_direct(self);
328 return byte == 1 ? Qtrue : Qfalse;
329}
330
331VALUE rb_thrift_binary_proto_read_byte(VALUE self) {
332 return INT2FIX(read_byte_direct(self));
333}
334
335VALUE rb_thrift_binary_proto_read_i16(VALUE self) {
336 return INT2FIX(read_i16_direct(self));
337}
338
339VALUE rb_thrift_binary_proto_read_i32(VALUE self) {
340 return INT2NUM(read_i32_direct(self));
341}
342
343VALUE rb_thrift_binary_proto_read_i64(VALUE self) {
344 return LL2NUM(read_i64_direct(self));
345}
346
347VALUE rb_thrift_binary_proto_read_double(VALUE self) {
348 union {
349 double f;
350 int64_t t;
351 } transfer;
352 transfer.t = read_i64_direct(self);
353 return rb_float_new(transfer.f);
354}
355
356VALUE rb_thrift_binary_proto_read_string(VALUE self) {
357 int size = read_i32_direct(self);
358 return READ(self, size);
359}
360
361void Init_binary_protocol_accelerated() {
362 VALUE thrift_binary_protocol_class = rb_const_get(thrift_module, rb_intern("BinaryProtocol"));
363
364 VERSION_1 = rb_num2ll(rb_const_get(thrift_binary_protocol_class, rb_intern("VERSION_1")));
365 VERSION_MASK = rb_num2ll(rb_const_get(thrift_binary_protocol_class, rb_intern("VERSION_MASK")));
Kevin Clarkead33822009-02-04 22:43:59 +0000366 TYPE_MASK = rb_num2ll(rb_const_get(thrift_binary_protocol_class, rb_intern("TYPE_MASK")));
Bryan Duxburyc0166282009-02-02 00:48:17 +0000367
368 VALUE bpa_class = rb_define_class_under(thrift_module, "BinaryProtocolAccelerated", thrift_binary_protocol_class);
369
370 rb_define_method(bpa_class, "native?", rb_thrift_binary_proto_native_qmark, 0);
371
372 rb_define_method(bpa_class, "write_message_begin", rb_thrift_binary_proto_write_message_begin, 3);
373 rb_define_method(bpa_class, "write_field_begin", rb_thrift_binary_proto_write_field_begin, 3);
374 rb_define_method(bpa_class, "write_field_stop", rb_thrift_binary_proto_write_field_stop, 0);
375 rb_define_method(bpa_class, "write_map_begin", rb_thrift_binary_proto_write_map_begin, 3);
376 rb_define_method(bpa_class, "write_list_begin", rb_thrift_binary_proto_write_list_begin, 2);
377 rb_define_method(bpa_class, "write_set_begin", rb_thrift_binary_proto_write_set_begin, 2);
378 rb_define_method(bpa_class, "write_byte", rb_thrift_binary_proto_write_byte, 1);
379 rb_define_method(bpa_class, "write_bool", rb_thrift_binary_proto_write_bool, 1);
380 rb_define_method(bpa_class, "write_i16", rb_thrift_binary_proto_write_i16, 1);
381 rb_define_method(bpa_class, "write_i32", rb_thrift_binary_proto_write_i32, 1);
382 rb_define_method(bpa_class, "write_i64", rb_thrift_binary_proto_write_i64, 1);
383 rb_define_method(bpa_class, "write_double", rb_thrift_binary_proto_write_double, 1);
384 rb_define_method(bpa_class, "write_string", rb_thrift_binary_proto_write_string, 1);
385 // unused methods
386 rb_define_method(bpa_class, "write_message_end", rb_thrift_binary_proto_write_message_end, 0);
387 rb_define_method(bpa_class, "write_struct_begin", rb_thrift_binary_proto_write_struct_begin, 1);
388 rb_define_method(bpa_class, "write_struct_end", rb_thrift_binary_proto_write_struct_end, 0);
389 rb_define_method(bpa_class, "write_field_end", rb_thrift_binary_proto_write_field_end, 0);
390 rb_define_method(bpa_class, "write_map_end", rb_thrift_binary_proto_write_map_end, 0);
391 rb_define_method(bpa_class, "write_list_end", rb_thrift_binary_proto_write_list_end, 0);
392 rb_define_method(bpa_class, "write_set_end", rb_thrift_binary_proto_write_set_end, 0);
393
394
395
396 rb_define_method(bpa_class, "read_message_begin", rb_thrift_binary_proto_read_message_begin, 0);
397 rb_define_method(bpa_class, "read_field_begin", rb_thrift_binary_proto_read_field_begin, 0);
398 rb_define_method(bpa_class, "read_map_begin", rb_thrift_binary_proto_read_map_begin, 0);
399 rb_define_method(bpa_class, "read_list_begin", rb_thrift_binary_proto_read_list_begin, 0);
400 rb_define_method(bpa_class, "read_set_begin", rb_thrift_binary_proto_read_set_begin, 0);
401 rb_define_method(bpa_class, "read_byte", rb_thrift_binary_proto_read_byte, 0);
402 rb_define_method(bpa_class, "read_bool", rb_thrift_binary_proto_read_bool, 0);
403 rb_define_method(bpa_class, "read_i16", rb_thrift_binary_proto_read_i16, 0);
404 rb_define_method(bpa_class, "read_i32", rb_thrift_binary_proto_read_i32, 0);
405 rb_define_method(bpa_class, "read_i64", rb_thrift_binary_proto_read_i64, 0);
406 rb_define_method(bpa_class, "read_double", rb_thrift_binary_proto_read_double, 0);
407 rb_define_method(bpa_class, "read_string", rb_thrift_binary_proto_read_string, 0);
408 // unused methods
409 rb_define_method(bpa_class, "read_message_end", rb_thrift_binary_proto_read_message_end, 0);
410 rb_define_method(bpa_class, "read_struct_begin", rb_thift_binary_proto_read_struct_begin, 0);
411 rb_define_method(bpa_class, "read_struct_end", rb_thift_binary_proto_read_struct_end, 0);
412 rb_define_method(bpa_class, "read_field_end", rb_thift_binary_proto_read_field_end, 0);
413 rb_define_method(bpa_class, "read_map_end", rb_thift_binary_proto_read_map_end, 0);
414 rb_define_method(bpa_class, "read_list_end", rb_thift_binary_proto_read_list_end, 0);
415 rb_define_method(bpa_class, "read_set_end", rb_thift_binary_proto_read_set_end, 0);
416
417 // set up native method table
418 native_proto_method_table *npmt;
419 npmt = ALLOC(native_proto_method_table);
420
421 npmt->write_field_begin = rb_thrift_binary_proto_write_field_begin;
422 npmt->write_field_stop = rb_thrift_binary_proto_write_field_stop;
423 npmt->write_map_begin = rb_thrift_binary_proto_write_map_begin;
424 npmt->write_list_begin = rb_thrift_binary_proto_write_list_begin;
425 npmt->write_set_begin = rb_thrift_binary_proto_write_set_begin;
426 npmt->write_byte = rb_thrift_binary_proto_write_byte;
427 npmt->write_bool = rb_thrift_binary_proto_write_bool;
428 npmt->write_i16 = rb_thrift_binary_proto_write_i16;
429 npmt->write_i32 = rb_thrift_binary_proto_write_i32;
430 npmt->write_i64 = rb_thrift_binary_proto_write_i64;
431 npmt->write_double = rb_thrift_binary_proto_write_double;
432 npmt->write_string = rb_thrift_binary_proto_write_string;
433 npmt->write_message_end = rb_thrift_binary_proto_write_message_end;
434 npmt->write_struct_begin = rb_thrift_binary_proto_write_struct_begin;
435 npmt->write_struct_end = rb_thrift_binary_proto_write_struct_end;
436 npmt->write_field_end = rb_thrift_binary_proto_write_field_end;
437 npmt->write_map_end = rb_thrift_binary_proto_write_map_end;
438 npmt->write_list_end = rb_thrift_binary_proto_write_list_end;
439 npmt->write_set_end = rb_thrift_binary_proto_write_set_end;
440
441 npmt->read_message_begin = rb_thrift_binary_proto_read_message_begin;
442 npmt->read_field_begin = rb_thrift_binary_proto_read_field_begin;
443 npmt->read_map_begin = rb_thrift_binary_proto_read_map_begin;
444 npmt->read_list_begin = rb_thrift_binary_proto_read_list_begin;
445 npmt->read_set_begin = rb_thrift_binary_proto_read_set_begin;
446 npmt->read_byte = rb_thrift_binary_proto_read_byte;
447 npmt->read_bool = rb_thrift_binary_proto_read_bool;
448 npmt->read_i16 = rb_thrift_binary_proto_read_i16;
449 npmt->read_i32 = rb_thrift_binary_proto_read_i32;
450 npmt->read_i64 = rb_thrift_binary_proto_read_i64;
451 npmt->read_double = rb_thrift_binary_proto_read_double;
452 npmt->read_string = rb_thrift_binary_proto_read_string;
453 npmt->read_message_end = rb_thrift_binary_proto_read_message_end;
454 npmt->read_struct_begin = rb_thift_binary_proto_read_struct_begin;
455 npmt->read_struct_end = rb_thift_binary_proto_read_struct_end;
456 npmt->read_field_end = rb_thift_binary_proto_read_field_end;
457 npmt->read_map_end = rb_thift_binary_proto_read_map_end;
458 npmt->read_list_end = rb_thift_binary_proto_read_list_end;
459 npmt->read_set_end = rb_thift_binary_proto_read_set_end;
460
461 VALUE method_table_object = Data_Wrap_Struct(rb_cObject, 0, free, npmt);
462 rb_const_set(bpa_class, rb_intern("@native_method_table"), method_table_object);
Bryan Duxbury1e80d442009-02-03 18:16:54 +0000463}