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 <constants.h> |
Bryan Duxbury | 6b771d2 | 2009-03-26 04:55:34 +0000 | [diff] [blame] | 22 | #include "macros.h" |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 23 | |
| 24 | ID buf_ivar_id; |
| 25 | ID index_ivar_id; |
| 26 | |
| 27 | ID slice_method_id; |
| 28 | |
| 29 | int GARBAGE_BUFFER_SIZE; |
| 30 | |
| 31 | #define GET_BUF(self) rb_ivar_get(self, buf_ivar_id) |
| 32 | |
| 33 | VALUE rb_thrift_memory_buffer_write(VALUE self, VALUE str) { |
| 34 | VALUE buf = GET_BUF(self); |
Bryan Duxbury | e3ab50d | 2009-03-25 21:06:53 +0000 | [diff] [blame] | 35 | rb_str_buf_cat(buf, RSTRING_PTR(str), RSTRING_LEN(str)); |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 36 | return Qnil; |
| 37 | } |
| 38 | |
| 39 | VALUE rb_thrift_memory_buffer_read(VALUE self, VALUE length_value) { |
| 40 | int length = FIX2INT(length_value); |
| 41 | |
| 42 | VALUE index_value = rb_ivar_get(self, index_ivar_id); |
| 43 | int index = FIX2INT(index_value); |
| 44 | |
| 45 | VALUE buf = GET_BUF(self); |
| 46 | VALUE data = rb_funcall(buf, slice_method_id, 2, index_value, length_value); |
| 47 | |
| 48 | index += length; |
Bryan Duxbury | e3ab50d | 2009-03-25 21:06:53 +0000 | [diff] [blame] | 49 | if (index > RSTRING_LEN(buf)) { |
| 50 | index = RSTRING_LEN(buf); |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 51 | } |
| 52 | if (index >= GARBAGE_BUFFER_SIZE) { |
Bryan Duxbury | e3ab50d | 2009-03-25 21:06:53 +0000 | [diff] [blame] | 53 | rb_ivar_set(self, buf_ivar_id, rb_funcall(buf, slice_method_id, 2, INT2FIX(index), INT2FIX(RSTRING_LEN(buf) - 1))); |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 54 | index = 0; |
| 55 | } |
| 56 | |
Bryan Duxbury | 6f6318a | 2009-09-01 23:18:34 +0000 | [diff] [blame] | 57 | if (RSTRING_LEN(data) < length) { |
Bryan Duxbury | 293086c | 2009-07-31 18:53:14 +0000 | [diff] [blame] | 58 | rb_raise(rb_eEOFError, "Not enough bytes remain in memory buffer"); |
| 59 | } |
| 60 | |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 61 | rb_ivar_set(self, index_ivar_id, INT2FIX(index)); |
| 62 | return data; |
| 63 | } |
| 64 | |
| 65 | void Init_memory_buffer() { |
Bryan Duxbury | d1d1542 | 2009-04-04 00:58:03 +0000 | [diff] [blame] | 66 | VALUE thrift_memory_buffer_class = rb_const_get(thrift_module, rb_intern("MemoryBufferTransport")); |
Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 67 | rb_define_method(thrift_memory_buffer_class, "write", rb_thrift_memory_buffer_write, 1); |
| 68 | rb_define_method(thrift_memory_buffer_class, "read", rb_thrift_memory_buffer_read, 1); |
| 69 | |
| 70 | buf_ivar_id = rb_intern("@buf"); |
| 71 | index_ivar_id = rb_intern("@index"); |
| 72 | |
| 73 | slice_method_id = rb_intern("slice"); |
| 74 | |
| 75 | GARBAGE_BUFFER_SIZE = FIX2INT(rb_const_get(thrift_memory_buffer_class, rb_intern("GARBAGE_BUFFER_SIZE"))); |
Bryan Duxbury | 1e80d44 | 2009-02-03 18:16:54 +0000 | [diff] [blame] | 76 | } |