blob: 319b0734a69a8e25e5853d06d6babb75f480625a [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 <constants.h>
Bryan Duxbury6b771d22009-03-26 04:55:34 +000022#include "macros.h"
Bryan Duxburyc0166282009-02-02 00:48:17 +000023
24ID buf_ivar_id;
25ID index_ivar_id;
26
27ID slice_method_id;
28
29int GARBAGE_BUFFER_SIZE;
30
31#define GET_BUF(self) rb_ivar_get(self, buf_ivar_id)
32
Bryan Duxburyad0ad822011-06-28 18:46:03 +000033VALUE rb_thrift_memory_buffer_write(VALUE self, VALUE str);
34VALUE rb_thrift_memory_buffer_read(VALUE self, VALUE length_value);
35VALUE rb_thrift_memory_buffer_read_byte(VALUE self);
36VALUE rb_thrift_memory_buffer_read_into_buffer(VALUE self, VALUE buffer_value, VALUE size_value);
37
Bryan Duxburyc0166282009-02-02 00:48:17 +000038VALUE rb_thrift_memory_buffer_write(VALUE self, VALUE str) {
39 VALUE buf = GET_BUF(self);
Bryan Duxburye3ab50d2009-03-25 21:06:53 +000040 rb_str_buf_cat(buf, RSTRING_PTR(str), RSTRING_LEN(str));
Bryan Duxburyc0166282009-02-02 00:48:17 +000041 return Qnil;
42}
43
44VALUE rb_thrift_memory_buffer_read(VALUE self, VALUE length_value) {
45 int length = FIX2INT(length_value);
46
47 VALUE index_value = rb_ivar_get(self, index_ivar_id);
48 int index = FIX2INT(index_value);
49
50 VALUE buf = GET_BUF(self);
51 VALUE data = rb_funcall(buf, slice_method_id, 2, index_value, length_value);
52
53 index += length;
Bryan Duxburye3ab50d2009-03-25 21:06:53 +000054 if (index > RSTRING_LEN(buf)) {
55 index = RSTRING_LEN(buf);
Bryan Duxburyc0166282009-02-02 00:48:17 +000056 }
57 if (index >= GARBAGE_BUFFER_SIZE) {
Bryan Duxburye3ab50d2009-03-25 21:06:53 +000058 rb_ivar_set(self, buf_ivar_id, rb_funcall(buf, slice_method_id, 2, INT2FIX(index), INT2FIX(RSTRING_LEN(buf) - 1)));
Bryan Duxburyc0166282009-02-02 00:48:17 +000059 index = 0;
60 }
Roger Meier472f9e12011-08-02 10:55:47 +000061 rb_ivar_set(self, index_ivar_id, INT2FIX(index));
Bryan Duxburyc0166282009-02-02 00:48:17 +000062
Bryan Duxbury6f6318a2009-09-01 23:18:34 +000063 if (RSTRING_LEN(data) < length) {
Bryan Duxbury293086c2009-07-31 18:53:14 +000064 rb_raise(rb_eEOFError, "Not enough bytes remain in memory buffer");
65 }
66
Bryan Duxburyc0166282009-02-02 00:48:17 +000067 return data;
68}
69
Bryan Duxburyad0ad822011-06-28 18:46:03 +000070VALUE rb_thrift_memory_buffer_read_byte(VALUE self) {
71 VALUE index_value = rb_ivar_get(self, index_ivar_id);
72 int index = FIX2INT(index_value);
73
74 VALUE buf = GET_BUF(self);
75 if (index >= RSTRING_LEN(buf)) {
76 rb_raise(rb_eEOFError, "Not enough bytes remain in memory buffer");
77 }
78 char byte = RSTRING_PTR(buf)[index++];
Bryan Duxburyad0ad822011-06-28 18:46:03 +000079
80 if (index >= GARBAGE_BUFFER_SIZE) {
81 rb_ivar_set(self, buf_ivar_id, rb_funcall(buf, slice_method_id, 2, INT2FIX(index), INT2FIX(RSTRING_LEN(buf) - 1)));
82 index = 0;
83 }
Roger Meier472f9e12011-08-02 10:55:47 +000084 rb_ivar_set(self, index_ivar_id, INT2FIX(index));
85
Bryan Duxburyad0ad822011-06-28 18:46:03 +000086 int result = (int) byte;
87 return INT2FIX(result);
88}
89
90VALUE rb_thrift_memory_buffer_read_into_buffer(VALUE self, VALUE buffer_value, VALUE size_value) {
91 int i = 0;
92 int size = FIX2INT(size_value);
93 int index;
94 VALUE buf = GET_BUF(self);
95
Bryan Duxbury6530f1d2012-06-29 00:21:19 +000096 index = FIX2INT(rb_ivar_get(self, index_ivar_id));
Bryan Duxburyad0ad822011-06-28 18:46:03 +000097 while (i < size) {
Bryan Duxburyad0ad822011-06-28 18:46:03 +000098 if (index >= RSTRING_LEN(buf)) {
99 rb_raise(rb_eEOFError, "Not enough bytes remain in memory buffer");
100 }
101 char byte = RSTRING_PTR(buf)[index++];
Bryan Duxburyad0ad822011-06-28 18:46:03 +0000102
Bryan Duxburyad0ad822011-06-28 18:46:03 +0000103 if (i >= RSTRING_LEN(buffer_value)) {
104 rb_raise(rb_eIndexError, "index %d out of string", i);
105 }
106 ((char*)RSTRING_PTR(buffer_value))[i] = byte;
107 i++;
108 }
Bryan Duxbury6530f1d2012-06-29 00:21:19 +0000109
110 if (index >= GARBAGE_BUFFER_SIZE) {
111 rb_ivar_set(self, buf_ivar_id, rb_funcall(buf, slice_method_id, 2, INT2FIX(index), INT2FIX(RSTRING_LEN(buf) - 1)));
112 index = 0;
113 }
114 rb_ivar_set(self, index_ivar_id, INT2FIX(index));
115
Bryan Duxburyad0ad822011-06-28 18:46:03 +0000116 return INT2FIX(i);
117}
118
Bryan Duxburyc0166282009-02-02 00:48:17 +0000119void Init_memory_buffer() {
Bryan Duxburyd1d15422009-04-04 00:58:03 +0000120 VALUE thrift_memory_buffer_class = rb_const_get(thrift_module, rb_intern("MemoryBufferTransport"));
Bryan Duxburyc0166282009-02-02 00:48:17 +0000121 rb_define_method(thrift_memory_buffer_class, "write", rb_thrift_memory_buffer_write, 1);
122 rb_define_method(thrift_memory_buffer_class, "read", rb_thrift_memory_buffer_read, 1);
Bryan Duxburyad0ad822011-06-28 18:46:03 +0000123 rb_define_method(thrift_memory_buffer_class, "read_byte", rb_thrift_memory_buffer_read_byte, 0);
124 rb_define_method(thrift_memory_buffer_class, "read_into_buffer", rb_thrift_memory_buffer_read_into_buffer, 2);
Bryan Duxburyc0166282009-02-02 00:48:17 +0000125
126 buf_ivar_id = rb_intern("@buf");
127 index_ivar_id = rb_intern("@index");
128
129 slice_method_id = rb_intern("slice");
130
131 GARBAGE_BUFFER_SIZE = FIX2INT(rb_const_get(thrift_memory_buffer_class, rb_intern("GARBAGE_BUFFER_SIZE")));
Bryan Duxbury1e80d442009-02-03 18:16:54 +0000132}