diff options
author | Tim Emiola <temiola@google.com> | 2015-03-28 01:44:28 -0700 |
---|---|---|
committer | Tim Emiola <temiola@google.com> | 2015-04-10 11:23:42 -0700 |
commit | 6b493a33534ec5235e5907f75b4f925b97622755 (patch) | |
tree | c9d4494bd7e45e3e4acf94b655b437d08e0d5b1c | |
parent | 37b09f4d839d47f11803af930e4cf4cb7af1e1e4 (diff) |
Adds byte_buffer -> ruby string and char * -> byte_buffer convenience methods
-rw-r--r-- | src/ruby/ext/grpc/rb_byte_buffer.c | 31 | ||||
-rw-r--r-- | src/ruby/ext/grpc/rb_byte_buffer.h | 6 |
2 files changed, 35 insertions, 2 deletions
diff --git a/src/ruby/ext/grpc/rb_byte_buffer.c b/src/ruby/ext/grpc/rb_byte_buffer.c index ff5a114de5..9fa6995ee9 100644 --- a/src/ruby/ext/grpc/rb_byte_buffer.c +++ b/src/ruby/ext/grpc/rb_byte_buffer.c @@ -39,6 +39,33 @@ #include <grpc/support/slice.h> #include "rb_grpc.h" +grpc_byte_buffer* grpc_rb_s_to_byte_buffer(char *string, size_t length) { + gpr_slice slice = gpr_slice_from_copied_buffer(string, length); + grpc_byte_buffer *buffer = grpc_byte_buffer_create(&slice, 1); + gpr_slice_unref(slice); + return buffer; +} + +VALUE grpc_rb_byte_buffer_to_s(grpc_byte_buffer *buffer) { + size_t length = 0; + char *string = NULL; + size_t offset = 0; + grpc_byte_buffer_reader *reader = NULL; + gpr_slice next; + if (buffer == NULL) { + return Qnil; + + } + length = grpc_byte_buffer_length(buffer); + string = xmalloc(length + 1); + reader = grpc_byte_buffer_reader_create(buffer); + while (grpc_byte_buffer_reader_next(reader, &next) != 0) { + memcpy(string + offset, GPR_SLICE_START_PTR(next), GPR_SLICE_LENGTH(next)); + offset += GPR_SLICE_LENGTH(next); + } + return rb_str_new(string, length); +} + /* grpc_rb_byte_buffer wraps a grpc_byte_buffer. It provides a peer ruby * object, 'mark' to minimize copying when a byte_buffer is created from * ruby. */ @@ -125,7 +152,7 @@ static VALUE grpc_rb_byte_buffer_init_copy(VALUE copy, VALUE orig) { /* id_empty is used to return the empty string from to_s when necessary. */ static ID id_empty; -static VALUE grpc_rb_byte_buffer_to_s(VALUE self) { +static VALUE grpc_rb_byte_buffer_to_s_obj(VALUE self) { grpc_rb_byte_buffer *wrapper = NULL; grpc_byte_buffer *bb = NULL; grpc_byte_buffer_reader *reader = NULL; @@ -215,7 +242,7 @@ void Init_grpc_byte_buffer() { grpc_rb_byte_buffer_init_copy, 1); /* Provides a to_s method that returns the buffer value */ - rb_define_method(rb_cByteBuffer, "to_s", grpc_rb_byte_buffer_to_s, 0); + rb_define_method(rb_cByteBuffer, "to_s", grpc_rb_byte_buffer_to_s_obj, 0); id_source = rb_intern("__source"); id_empty = rb_intern(""); diff --git a/src/ruby/ext/grpc/rb_byte_buffer.h b/src/ruby/ext/grpc/rb_byte_buffer.h index 6ef72f3e75..440545138a 100644 --- a/src/ruby/ext/grpc/rb_byte_buffer.h +++ b/src/ruby/ext/grpc/rb_byte_buffer.h @@ -51,4 +51,10 @@ VALUE grpc_rb_byte_buffer_create_with_mark(VALUE mark, grpc_byte_buffer* bb); /* Gets the wrapped byte_buffer from its ruby object. */ grpc_byte_buffer* grpc_rb_get_wrapped_byte_buffer(VALUE v); +/* Converts a char* with a length to a grpc_byte_buffer */ +grpc_byte_buffer *grpc_rb_s_to_byte_buffer(char *string, size_t length); + +/* Converts a grpc_byte_buffer to a ruby string */ +VALUE grpc_rb_byte_buffer_to_s(grpc_byte_buffer *buffer); + #endif /* GRPC_RB_BYTE_BUFFER_H_ */ |