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