Browse Source

Adds byte_buffer -> ruby string and char * -> byte_buffer convenience methods

Tim Emiola 10 years ago
parent
commit
6b493a3353
2 changed files with 35 additions and 2 deletions
  1. 29 2
      src/ruby/ext/grpc/rb_byte_buffer.c
  2. 6 0
      src/ruby/ext/grpc/rb_byte_buffer.h

+ 29 - 2
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("");

+ 6 - 0
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_ */