blob: e11c7e82a8a11b0ae44b1142c4ea0e9ea9819bbf [file] [log] [blame]
nnoble097ef9b2014-12-01 17:06:10 -08001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2015 gRPC authors.
nnoble097ef9b2014-12-01 17:06:10 -08004 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005 * 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
nnoble097ef9b2014-12-01 17:06:10 -08008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
nnoble097ef9b2014-12-01 17:06:10 -080010 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011 * 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.
nnoble097ef9b2014-12-01 17:06:10 -080016 *
17 */
18
Nicolas "Pixel" Nobled51d1212016-01-31 11:33:19 +010019#include <ruby/ruby.h>
Nicolas "Pixel" Noble9fcdc872016-05-05 06:15:34 +020020
nnoble097ef9b2014-12-01 17:06:10 -080021#include "rb_byte_buffer.h"
Craig Tiller5b1c5f22017-04-19 09:52:18 -070022#include "rb_grpc_imports.generated.h"
nnoble097ef9b2014-12-01 17:06:10 -080023
David Garcia Quintas30bd4eb2015-06-01 21:08:59 -070024#include <grpc/byte_buffer_reader.h>
Craig Tiller5b1c5f22017-04-19 09:52:18 -070025#include <grpc/grpc.h>
Craig Tillerb37d53e2016-10-26 16:16:35 -070026#include <grpc/slice.h>
nnoble097ef9b2014-12-01 17:06:10 -080027#include "rb_grpc.h"
28
Craig Tillerbaa14a92017-11-03 09:09:36 -070029grpc_byte_buffer* grpc_rb_s_to_byte_buffer(char* string, size_t length) {
Craig Tillerd41a4a72016-10-26 16:16:06 -070030 grpc_slice slice = grpc_slice_from_copied_buffer(string, length);
Craig Tillerbaa14a92017-11-03 09:09:36 -070031 grpc_byte_buffer* buffer = grpc_raw_byte_buffer_create(&slice, 1);
Craig Tillerd41a4a72016-10-26 16:16:06 -070032 grpc_slice_unref(slice);
Tim Emiola6b493a32015-03-28 01:44:28 -070033 return buffer;
34}
35
Craig Tillerbaa14a92017-11-03 09:09:36 -070036VALUE grpc_rb_byte_buffer_to_s(grpc_byte_buffer* buffer) {
Zhuochun97daf352016-03-13 16:19:56 +080037 VALUE rb_string;
David Garcia Quintas30bd4eb2015-06-01 21:08:59 -070038 grpc_byte_buffer_reader reader;
Craig Tillerd41a4a72016-10-26 16:16:06 -070039 grpc_slice next;
Tim Emiola6b493a32015-03-28 01:44:28 -070040 if (buffer == NULL) {
41 return Qnil;
Tim Emiola6b493a32015-03-28 01:44:28 -070042 }
Nicolas "Pixel" Noble375d1f42016-03-30 01:53:01 +020043 rb_string = rb_str_buf_new(grpc_byte_buffer_length(buffer));
David Garcia Quintas6721d4f2016-06-30 17:17:23 -070044 if (!grpc_byte_buffer_reader_init(&reader, buffer)) {
45 rb_raise(rb_eRuntimeError, "Error initializing byte buffer reader.");
46 return Qnil;
47 }
David Garcia Quintas30bd4eb2015-06-01 21:08:59 -070048 while (grpc_byte_buffer_reader_next(&reader, &next) != 0) {
Craig Tillerbaa14a92017-11-03 09:09:36 -070049 rb_str_cat(rb_string, (const char*)GRPC_SLICE_START_PTR(next),
Craig Tiller618e67d2016-10-26 21:08:10 -070050 GRPC_SLICE_LENGTH(next));
Craig Tillerd41a4a72016-10-26 16:16:06 -070051 grpc_slice_unref(next);
Tim Emiola6b493a32015-03-28 01:44:28 -070052 }
Alex Polcyn16db6e12016-11-30 13:59:59 -080053 grpc_byte_buffer_reader_destroy(&reader);
Zhuochun97daf352016-03-13 16:19:56 +080054 return rb_string;
Tim Emiola6b493a32015-03-28 01:44:28 -070055}
Craig Tiller7c70b6c2017-01-23 07:48:42 -080056
57VALUE grpc_rb_slice_to_ruby_string(grpc_slice slice) {
58 if (GRPC_SLICE_START_PTR(slice) == NULL) {
Craig Tiller5b1c5f22017-04-19 09:52:18 -070059 rb_raise(rb_eRuntimeError,
60 "attempt to convert uninitialized grpc_slice to ruby string");
Craig Tiller7c70b6c2017-01-23 07:48:42 -080061 }
Craig Tillerbaa14a92017-11-03 09:09:36 -070062 return rb_str_new((char*)GRPC_SLICE_START_PTR(slice),
Craig Tiller5b1c5f22017-04-19 09:52:18 -070063 GRPC_SLICE_LENGTH(slice));
Craig Tiller7c70b6c2017-01-23 07:48:42 -080064}