blob: 3854887f97e31007ec73bb36b04bfe0b49be25d2 [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 Duxbury3647fc62009-09-02 20:05:07 +000079 if (TYPE(str) != T_STRING) {
80 rb_raise(rb_eStandardError, "Value should be a string");
81 }
Bryan Duxburye3ab50d2009-03-25 21:06:53 +000082 write_i32_direct(trans, RSTRING_LEN(str));
Bryan Duxburyc0166282009-02-02 00:48:17 +000083 rb_funcall(trans, write_method_id, 1, str);
84}
85
86//--------------------------------
87// interface writing methods
88//--------------------------------
89
90VALUE rb_thrift_binary_proto_write_message_end(VALUE self) {
91 return Qnil;
92}
93
94VALUE rb_thrift_binary_proto_write_struct_begin(VALUE self, VALUE name) {
95 return Qnil;
96}
97
98VALUE rb_thrift_binary_proto_write_struct_end(VALUE self) {
99 return Qnil;
100}
101
102VALUE rb_thrift_binary_proto_write_field_end(VALUE self) {
103 return Qnil;
104}
105
106VALUE rb_thrift_binary_proto_write_map_end(VALUE self) {
107 return Qnil;
108}
109
110VALUE rb_thrift_binary_proto_write_list_end(VALUE self) {
111 return Qnil;
112}
113
114VALUE rb_thrift_binary_proto_write_set_end(VALUE self) {
115 return Qnil;
116}
117
118VALUE rb_thrift_binary_proto_write_message_begin(VALUE self, VALUE name, VALUE type, VALUE seqid) {
119 VALUE trans = GET_TRANSPORT(self);
Kevin Clarkead33822009-02-04 22:43:59 +0000120 VALUE strict_write = GET_STRICT_WRITE(self);
121
122 if (strict_write == Qtrue) {
123 write_i32_direct(trans, VERSION_1 | FIX2INT(type));
124 write_string_direct(trans, name);
125 write_i32_direct(trans, FIX2INT(seqid));
126 } else {
127 write_string_direct(trans, name);
Bryan Duxburyd40731e2009-03-20 02:21:05 +0000128 write_byte_direct(trans, FIX2INT(type));
Kevin Clarkead33822009-02-04 22:43:59 +0000129 write_i32_direct(trans, FIX2INT(seqid));
130 }
Bryan Duxburyc0166282009-02-02 00:48:17 +0000131
132 return Qnil;
133}
134
135VALUE rb_thrift_binary_proto_write_field_begin(VALUE self, VALUE name, VALUE type, VALUE id) {
136 VALUE trans = GET_TRANSPORT(self);
137 write_byte_direct(trans, FIX2INT(type));
138 write_i16_direct(trans, FIX2INT(id));
139
140 return Qnil;
141}
142
143VALUE rb_thrift_binary_proto_write_field_stop(VALUE self) {
144 write_byte_direct(GET_TRANSPORT(self), TTYPE_STOP);
145 return Qnil;
146}
147
148VALUE rb_thrift_binary_proto_write_map_begin(VALUE self, VALUE ktype, VALUE vtype, VALUE size) {
149 VALUE trans = GET_TRANSPORT(self);
150 write_byte_direct(trans, FIX2INT(ktype));
151 write_byte_direct(trans, FIX2INT(vtype));
152 write_i32_direct(trans, FIX2INT(size));
153
154 return Qnil;
155}
156
157VALUE rb_thrift_binary_proto_write_list_begin(VALUE self, VALUE etype, VALUE size) {
158 VALUE trans = GET_TRANSPORT(self);
159 write_byte_direct(trans, FIX2INT(etype));
160 write_i32_direct(trans, FIX2INT(size));
161
162 return Qnil;
163}
164
165VALUE rb_thrift_binary_proto_write_set_begin(VALUE self, VALUE etype, VALUE size) {
166 rb_thrift_binary_proto_write_list_begin(self, etype, size);
167 return Qnil;
168}
169
170VALUE rb_thrift_binary_proto_write_bool(VALUE self, VALUE b) {
171 write_byte_direct(GET_TRANSPORT(self), RTEST(b) ? 1 : 0);
172 return Qnil;
173}
174
175VALUE rb_thrift_binary_proto_write_byte(VALUE self, VALUE byte) {
176 CHECK_NIL(byte);
177 write_byte_direct(GET_TRANSPORT(self), NUM2INT(byte));
178 return Qnil;
179}
180
181VALUE rb_thrift_binary_proto_write_i16(VALUE self, VALUE i16) {
182 CHECK_NIL(i16);
183 write_i16_direct(GET_TRANSPORT(self), FIX2INT(i16));
184 return Qnil;
185}
186
187VALUE rb_thrift_binary_proto_write_i32(VALUE self, VALUE i32) {
188 CHECK_NIL(i32);
189 write_i32_direct(GET_TRANSPORT(self), NUM2INT(i32));
190 return Qnil;
191}
192
193VALUE rb_thrift_binary_proto_write_i64(VALUE self, VALUE i64) {
194 CHECK_NIL(i64);
195 write_i64_direct(GET_TRANSPORT(self), NUM2LL(i64));
196 return Qnil;
197}
198
199VALUE rb_thrift_binary_proto_write_double(VALUE self, VALUE dub) {
200 CHECK_NIL(dub);
201 // Unfortunately, bitwise_cast doesn't work in C. Bad C!
202 union {
203 double f;
204 int64_t t;
205 } transfer;
Bryan Duxburye3ab50d2009-03-25 21:06:53 +0000206 transfer.f = RFLOAT_VALUE(rb_Float(dub));
Bryan Duxburyc0166282009-02-02 00:48:17 +0000207 write_i64_direct(GET_TRANSPORT(self), transfer.t);
208
209 return Qnil;
210}
211
212VALUE rb_thrift_binary_proto_write_string(VALUE self, VALUE str) {
213 CHECK_NIL(str);
214 VALUE trans = GET_TRANSPORT(self);
Bryan Duxburyc0166282009-02-02 00:48:17 +0000215 write_string_direct(trans, str);
216 return Qnil;
217}
218
219//---------------------------------------
220// interface reading methods
221//---------------------------------------
222
Bryan Duxburyc0166282009-02-02 00:48:17 +0000223VALUE rb_thrift_binary_proto_read_string(VALUE self);
224VALUE rb_thrift_binary_proto_read_byte(VALUE self);
225VALUE rb_thrift_binary_proto_read_i32(VALUE self);
226VALUE rb_thrift_binary_proto_read_i16(VALUE self);
227
228static char read_byte_direct(VALUE self) {
Bryan Duxburye3ab50d2009-03-25 21:06:53 +0000229 VALUE buf = READ(self, 1);
230 return RSTRING_PTR(buf)[0];
Bryan Duxburyc0166282009-02-02 00:48:17 +0000231}
232
233static int16_t read_i16_direct(VALUE self) {
234 VALUE buf = READ(self, 2);
Bryan Duxburye3ab50d2009-03-25 21:06:53 +0000235 return (int16_t)(((uint8_t)(RSTRING_PTR(buf)[1])) | ((uint16_t)((RSTRING_PTR(buf)[0]) << 8)));
Bryan Duxburyc0166282009-02-02 00:48:17 +0000236}
237
238static int32_t read_i32_direct(VALUE self) {
239 VALUE buf = READ(self, 4);
Bryan Duxburye3ab50d2009-03-25 21:06:53 +0000240 return ((uint8_t)(RSTRING_PTR(buf)[3])) |
241 (((uint8_t)(RSTRING_PTR(buf)[2])) << 8) |
242 (((uint8_t)(RSTRING_PTR(buf)[1])) << 16) |
243 (((uint8_t)(RSTRING_PTR(buf)[0])) << 24);
Bryan Duxburyc0166282009-02-02 00:48:17 +0000244}
245
246static int64_t read_i64_direct(VALUE self) {
247 uint64_t hi = read_i32_direct(self);
248 uint32_t lo = read_i32_direct(self);
249 return (hi << 32) | lo;
250}
251
252static VALUE get_protocol_exception(VALUE code, VALUE message) {
253 VALUE args[2];
254 args[0] = code;
255 args[1] = message;
256 return rb_class_new_instance(2, (VALUE*)&args, protocol_exception_class);
257}
258
259VALUE rb_thrift_binary_proto_read_message_end(VALUE self) {
260 return Qnil;
261}
262
263VALUE rb_thift_binary_proto_read_struct_begin(VALUE self) {
264 return Qnil;
265}
266
267VALUE rb_thift_binary_proto_read_struct_end(VALUE self) {
268 return Qnil;
269}
270
271VALUE rb_thift_binary_proto_read_field_end(VALUE self) {
272 return Qnil;
273}
274
275VALUE rb_thift_binary_proto_read_map_end(VALUE self) {
276 return Qnil;
277}
278
279VALUE rb_thift_binary_proto_read_list_end(VALUE self) {
280 return Qnil;
281}
282
283VALUE rb_thift_binary_proto_read_set_end(VALUE self) {
284 return Qnil;
285}
286
287VALUE rb_thrift_binary_proto_read_message_begin(VALUE self) {
Kevin Clarkead33822009-02-04 22:43:59 +0000288 VALUE strict_read = GET_STRICT_READ(self);
289 VALUE name, seqid;
290 int type;
Bryan Duxburyc0166282009-02-02 00:48:17 +0000291
Kevin Clarkead33822009-02-04 22:43:59 +0000292 int version = read_i32_direct(self);
293
294 if (version < 0) {
295 if ((version & VERSION_MASK) != VERSION_1) {
296 rb_exc_raise(get_protocol_exception(INT2FIX(BAD_VERSION), rb_str_new2("Missing version identifier")));
297 }
298 type = version & TYPE_MASK;
299 name = rb_thrift_binary_proto_read_string(self);
300 seqid = rb_thrift_binary_proto_read_i32(self);
301 } else {
302 if (strict_read == Qtrue) {
303 rb_exc_raise(get_protocol_exception(INT2FIX(BAD_VERSION), rb_str_new2("No version identifier, old protocol client?")));
304 }
305 name = READ(self, version);
Bryan Duxburyac002d32009-03-31 23:48:36 +0000306 type = read_byte_direct(self);
Kevin Clarkead33822009-02-04 22:43:59 +0000307 seqid = rb_thrift_binary_proto_read_i32(self);
308 }
Bryan Duxburyc0166282009-02-02 00:48:17 +0000309
310 return rb_ary_new3(3, name, INT2FIX(type), seqid);
311}
312
313VALUE rb_thrift_binary_proto_read_field_begin(VALUE self) {
314 int type = read_byte_direct(self);
315 if (type == TTYPE_STOP) {
316 return rb_ary_new3(3, Qnil, INT2FIX(type), INT2FIX(0));
317 } else {
318 VALUE id = rb_thrift_binary_proto_read_i16(self);
319 return rb_ary_new3(3, Qnil, INT2FIX(type), id);
320 }
321}
322
323VALUE rb_thrift_binary_proto_read_map_begin(VALUE self) {
324 VALUE ktype = rb_thrift_binary_proto_read_byte(self);
325 VALUE vtype = rb_thrift_binary_proto_read_byte(self);
326 VALUE size = rb_thrift_binary_proto_read_i32(self);
327 return rb_ary_new3(3, ktype, vtype, size);
328}
329
330VALUE rb_thrift_binary_proto_read_list_begin(VALUE self) {
331 VALUE etype = rb_thrift_binary_proto_read_byte(self);
332 VALUE size = rb_thrift_binary_proto_read_i32(self);
333 return rb_ary_new3(2, etype, size);
334}
335
336VALUE rb_thrift_binary_proto_read_set_begin(VALUE self) {
337 return rb_thrift_binary_proto_read_list_begin(self);
338}
339
340VALUE rb_thrift_binary_proto_read_bool(VALUE self) {
341 char byte = read_byte_direct(self);
Bryan Duxbury5b8b4842009-04-01 20:10:15 +0000342 return byte != 0 ? Qtrue : Qfalse;
Bryan Duxburyc0166282009-02-02 00:48:17 +0000343}
344
345VALUE rb_thrift_binary_proto_read_byte(VALUE self) {
346 return INT2FIX(read_byte_direct(self));
347}
348
349VALUE rb_thrift_binary_proto_read_i16(VALUE self) {
350 return INT2FIX(read_i16_direct(self));
351}
352
353VALUE rb_thrift_binary_proto_read_i32(VALUE self) {
354 return INT2NUM(read_i32_direct(self));
355}
356
357VALUE rb_thrift_binary_proto_read_i64(VALUE self) {
358 return LL2NUM(read_i64_direct(self));
359}
360
361VALUE rb_thrift_binary_proto_read_double(VALUE self) {
362 union {
363 double f;
364 int64_t t;
365 } transfer;
366 transfer.t = read_i64_direct(self);
367 return rb_float_new(transfer.f);
368}
369
370VALUE rb_thrift_binary_proto_read_string(VALUE self) {
371 int size = read_i32_direct(self);
372 return READ(self, size);
373}
374
375void Init_binary_protocol_accelerated() {
376 VALUE thrift_binary_protocol_class = rb_const_get(thrift_module, rb_intern("BinaryProtocol"));
Bryan Duxburyd2cc5bb2010-07-28 21:00:06 +0000377
Bryan Duxburyc0166282009-02-02 00:48:17 +0000378 VERSION_1 = rb_num2ll(rb_const_get(thrift_binary_protocol_class, rb_intern("VERSION_1")));
379 VERSION_MASK = rb_num2ll(rb_const_get(thrift_binary_protocol_class, rb_intern("VERSION_MASK")));
Kevin Clarkead33822009-02-04 22:43:59 +0000380 TYPE_MASK = rb_num2ll(rb_const_get(thrift_binary_protocol_class, rb_intern("TYPE_MASK")));
Bryan Duxburyd2cc5bb2010-07-28 21:00:06 +0000381
Bryan Duxburyc0166282009-02-02 00:48:17 +0000382 VALUE bpa_class = rb_define_class_under(thrift_module, "BinaryProtocolAccelerated", thrift_binary_protocol_class);
Bryan Duxburyd2cc5bb2010-07-28 21:00:06 +0000383
Bryan Duxburyc0166282009-02-02 00:48:17 +0000384 rb_define_method(bpa_class, "native?", rb_thrift_binary_proto_native_qmark, 0);
Bryan Duxburyd2cc5bb2010-07-28 21:00:06 +0000385
Bryan Duxburyc0166282009-02-02 00:48:17 +0000386 rb_define_method(bpa_class, "write_message_begin", rb_thrift_binary_proto_write_message_begin, 3);
387 rb_define_method(bpa_class, "write_field_begin", rb_thrift_binary_proto_write_field_begin, 3);
388 rb_define_method(bpa_class, "write_field_stop", rb_thrift_binary_proto_write_field_stop, 0);
389 rb_define_method(bpa_class, "write_map_begin", rb_thrift_binary_proto_write_map_begin, 3);
390 rb_define_method(bpa_class, "write_list_begin", rb_thrift_binary_proto_write_list_begin, 2);
391 rb_define_method(bpa_class, "write_set_begin", rb_thrift_binary_proto_write_set_begin, 2);
392 rb_define_method(bpa_class, "write_byte", rb_thrift_binary_proto_write_byte, 1);
393 rb_define_method(bpa_class, "write_bool", rb_thrift_binary_proto_write_bool, 1);
394 rb_define_method(bpa_class, "write_i16", rb_thrift_binary_proto_write_i16, 1);
395 rb_define_method(bpa_class, "write_i32", rb_thrift_binary_proto_write_i32, 1);
396 rb_define_method(bpa_class, "write_i64", rb_thrift_binary_proto_write_i64, 1);
397 rb_define_method(bpa_class, "write_double", rb_thrift_binary_proto_write_double, 1);
398 rb_define_method(bpa_class, "write_string", rb_thrift_binary_proto_write_string, 1);
399 // unused methods
400 rb_define_method(bpa_class, "write_message_end", rb_thrift_binary_proto_write_message_end, 0);
401 rb_define_method(bpa_class, "write_struct_begin", rb_thrift_binary_proto_write_struct_begin, 1);
402 rb_define_method(bpa_class, "write_struct_end", rb_thrift_binary_proto_write_struct_end, 0);
403 rb_define_method(bpa_class, "write_field_end", rb_thrift_binary_proto_write_field_end, 0);
404 rb_define_method(bpa_class, "write_map_end", rb_thrift_binary_proto_write_map_end, 0);
405 rb_define_method(bpa_class, "write_list_end", rb_thrift_binary_proto_write_list_end, 0);
406 rb_define_method(bpa_class, "write_set_end", rb_thrift_binary_proto_write_set_end, 0);
Bryan Duxburyc0166282009-02-02 00:48:17 +0000407
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);
Bryan Duxbury1e80d442009-02-03 18:16:54 +0000428}