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