nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | * Copyright 2015 gRPC authors. |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 4 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 8 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 10 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 16 | * |
| 17 | */ |
| 18 | |
Nicolas "Pixel" Noble | d51d121 | 2016-01-31 11:33:19 +0100 | [diff] [blame] | 19 | #include <ruby/ruby.h> |
Nicolas "Pixel" Noble | 9fcdc87 | 2016-05-05 06:15:34 +0200 | [diff] [blame] | 20 | |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 21 | #include "rb_byte_buffer.h" |
Craig Tiller | 5b1c5f2 | 2017-04-19 09:52:18 -0700 | [diff] [blame] | 22 | #include "rb_grpc_imports.generated.h" |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 23 | |
David Garcia Quintas | 30bd4eb | 2015-06-01 21:08:59 -0700 | [diff] [blame] | 24 | #include <grpc/byte_buffer_reader.h> |
Craig Tiller | 5b1c5f2 | 2017-04-19 09:52:18 -0700 | [diff] [blame] | 25 | #include <grpc/grpc.h> |
Craig Tiller | b37d53e | 2016-10-26 16:16:35 -0700 | [diff] [blame] | 26 | #include <grpc/slice.h> |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 27 | #include "rb_grpc.h" |
| 28 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 29 | grpc_byte_buffer* grpc_rb_s_to_byte_buffer(char* string, size_t length) { |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 30 | grpc_slice slice = grpc_slice_from_copied_buffer(string, length); |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 31 | grpc_byte_buffer* buffer = grpc_raw_byte_buffer_create(&slice, 1); |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 32 | grpc_slice_unref(slice); |
Tim Emiola | 6b493a3 | 2015-03-28 01:44:28 -0700 | [diff] [blame] | 33 | return buffer; |
| 34 | } |
| 35 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 36 | VALUE grpc_rb_byte_buffer_to_s(grpc_byte_buffer* buffer) { |
Zhuochun | 97daf35 | 2016-03-13 16:19:56 +0800 | [diff] [blame] | 37 | VALUE rb_string; |
David Garcia Quintas | 30bd4eb | 2015-06-01 21:08:59 -0700 | [diff] [blame] | 38 | grpc_byte_buffer_reader reader; |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 39 | grpc_slice next; |
Tim Emiola | 6b493a3 | 2015-03-28 01:44:28 -0700 | [diff] [blame] | 40 | if (buffer == NULL) { |
| 41 | return Qnil; |
Tim Emiola | 6b493a3 | 2015-03-28 01:44:28 -0700 | [diff] [blame] | 42 | } |
Nicolas "Pixel" Noble | 375d1f4 | 2016-03-30 01:53:01 +0200 | [diff] [blame] | 43 | rb_string = rb_str_buf_new(grpc_byte_buffer_length(buffer)); |
David Garcia Quintas | 6721d4f | 2016-06-30 17:17:23 -0700 | [diff] [blame] | 44 | if (!grpc_byte_buffer_reader_init(&reader, buffer)) { |
| 45 | rb_raise(rb_eRuntimeError, "Error initializing byte buffer reader."); |
| 46 | return Qnil; |
| 47 | } |
David Garcia Quintas | 30bd4eb | 2015-06-01 21:08:59 -0700 | [diff] [blame] | 48 | while (grpc_byte_buffer_reader_next(&reader, &next) != 0) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 49 | rb_str_cat(rb_string, (const char*)GRPC_SLICE_START_PTR(next), |
Craig Tiller | 618e67d | 2016-10-26 21:08:10 -0700 | [diff] [blame] | 50 | GRPC_SLICE_LENGTH(next)); |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 51 | grpc_slice_unref(next); |
Tim Emiola | 6b493a3 | 2015-03-28 01:44:28 -0700 | [diff] [blame] | 52 | } |
Alex Polcyn | 16db6e1 | 2016-11-30 13:59:59 -0800 | [diff] [blame] | 53 | grpc_byte_buffer_reader_destroy(&reader); |
Zhuochun | 97daf35 | 2016-03-13 16:19:56 +0800 | [diff] [blame] | 54 | return rb_string; |
Tim Emiola | 6b493a3 | 2015-03-28 01:44:28 -0700 | [diff] [blame] | 55 | } |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 56 | |
| 57 | VALUE grpc_rb_slice_to_ruby_string(grpc_slice slice) { |
| 58 | if (GRPC_SLICE_START_PTR(slice) == NULL) { |
Craig Tiller | 5b1c5f2 | 2017-04-19 09:52:18 -0700 | [diff] [blame] | 59 | rb_raise(rb_eRuntimeError, |
| 60 | "attempt to convert uninitialized grpc_slice to ruby string"); |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 61 | } |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 62 | return rb_str_new((char*)GRPC_SLICE_START_PTR(slice), |
Craig Tiller | 5b1c5f2 | 2017-04-19 09:52:18 -0700 | [diff] [blame] | 63 | GRPC_SLICE_LENGTH(slice)); |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 64 | } |