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