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_channel_args.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 | |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 24 | #include <grpc/grpc.h> |
| 25 | |
| 26 | #include "rb_grpc.h" |
| 27 | |
Yuki Yugui Sonoda | d98db4a | 2015-04-11 14:59:59 +0900 | [diff] [blame] | 28 | static rb_data_type_t grpc_rb_channel_args_data_type = { |
| 29 | "grpc_channel_args", |
Craig Tiller | 5b1c5f2 | 2017-04-19 09:52:18 -0700 | [diff] [blame] | 30 | {GRPC_RB_GC_NOT_MARKED, |
| 31 | GRPC_RB_GC_DONT_FREE, |
| 32 | GRPC_RB_MEMSIZE_UNAVAILABLE, |
murgatroid99 | 87afb5d | 2015-07-16 16:01:02 -0700 | [diff] [blame] | 33 | {NULL, NULL}}, |
Craig Tiller | 5b1c5f2 | 2017-04-19 09:52:18 -0700 | [diff] [blame] | 34 | NULL, |
| 35 | NULL, |
Tim Emiola | 9161a82 | 2015-11-11 15:58:44 -0800 | [diff] [blame] | 36 | #ifdef RUBY_TYPED_FREE_IMMEDIATELY |
Yuki Yugui Sonoda | d98db4a | 2015-04-11 14:59:59 +0900 | [diff] [blame] | 37 | RUBY_TYPED_FREE_IMMEDIATELY |
Tim Emiola | 9161a82 | 2015-11-11 15:58:44 -0800 | [diff] [blame] | 38 | #endif |
Yuki Yugui Sonoda | d98db4a | 2015-04-11 14:59:59 +0900 | [diff] [blame] | 39 | }; |
| 40 | |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 41 | /* A callback the processes the hash key values in channel_args hash */ |
| 42 | static int grpc_rb_channel_create_in_process_add_args_hash_cb(VALUE key, |
| 43 | VALUE val, |
| 44 | VALUE args_obj) { |
| 45 | const char* the_key; |
| 46 | grpc_channel_args* args; |
| 47 | |
| 48 | switch (TYPE(key)) { |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 49 | case T_STRING: |
| 50 | the_key = StringValuePtr(key); |
| 51 | break; |
| 52 | |
| 53 | case T_SYMBOL: |
| 54 | the_key = rb_id2name(SYM2ID(key)); |
| 55 | break; |
| 56 | |
| 57 | default: |
| 58 | rb_raise(rb_eTypeError, "bad chan arg: got <%s>, want <String|Symbol>", |
| 59 | rb_obj_classname(key)); |
| 60 | return ST_STOP; |
| 61 | } |
| 62 | |
Yuki Yugui Sonoda | d98db4a | 2015-04-11 14:59:59 +0900 | [diff] [blame] | 63 | TypedData_Get_Struct(args_obj, grpc_channel_args, |
| 64 | &grpc_rb_channel_args_data_type, args); |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 65 | if (args->num_args <= 0) { |
| 66 | rb_raise(rb_eRuntimeError, "hash_cb bug: num_args is %lu for key:%s", |
| 67 | args->num_args, StringValueCStr(key)); |
| 68 | return ST_STOP; |
| 69 | } |
| 70 | |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 71 | args->args[args->num_args - 1].key = (char*)the_key; |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 72 | switch (TYPE(val)) { |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 73 | case T_SYMBOL: |
| 74 | args->args[args->num_args - 1].type = GRPC_ARG_STRING; |
| 75 | args->args[args->num_args - 1].value.string = |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 76 | (char*)rb_id2name(SYM2ID(val)); |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 77 | --args->num_args; |
| 78 | return ST_CONTINUE; |
| 79 | |
| 80 | case T_STRING: |
| 81 | args->args[args->num_args - 1].type = GRPC_ARG_STRING; |
| 82 | args->args[args->num_args - 1].value.string = StringValueCStr(val); |
| 83 | --args->num_args; |
| 84 | return ST_CONTINUE; |
| 85 | |
| 86 | case T_FIXNUM: |
| 87 | args->args[args->num_args - 1].type = GRPC_ARG_INTEGER; |
| 88 | args->args[args->num_args - 1].value.integer = NUM2INT(val); |
| 89 | --args->num_args; |
| 90 | return ST_CONTINUE; |
| 91 | |
| 92 | default: |
| 93 | rb_raise(rb_eTypeError, "%s: bad value: got <%s>, want <String|Fixnum>", |
| 94 | StringValueCStr(key), rb_obj_classname(val)); |
| 95 | return ST_STOP; |
| 96 | } |
| 97 | rb_raise(rb_eRuntimeError, "impl bug: hash_cb reached to far while on key:%s", |
| 98 | StringValueCStr(key)); |
| 99 | return ST_STOP; |
| 100 | } |
| 101 | |
| 102 | /* channel_convert_params allows the call to |
| 103 | grpc_rb_hash_convert_to_channel_args to be made within an rb_protect |
| 104 | exception-handler. This allows any allocated memory to be freed before |
| 105 | propagating any exception that occurs */ |
| 106 | typedef struct channel_convert_params { |
| 107 | VALUE src_hash; |
| 108 | grpc_channel_args* dst; |
| 109 | } channel_convert_params; |
| 110 | |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 111 | static VALUE grpc_rb_hash_convert_to_channel_args0(VALUE as_value) { |
| 112 | ID id_size = rb_intern("size"); |
Yuki Yugui Sonoda | a7d369e | 2015-04-11 11:48:36 +0900 | [diff] [blame] | 113 | VALUE grpc_rb_cChannelArgs = rb_define_class("TmpChannelArgs", rb_cObject); |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 114 | channel_convert_params* params = (channel_convert_params*)as_value; |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 115 | size_t num_args = 0; |
| 116 | |
| 117 | if (!NIL_P(params->src_hash) && TYPE(params->src_hash) != T_HASH) { |
| 118 | rb_raise(rb_eTypeError, "bad channel args: got:<%s> want: a hash or nil", |
| 119 | rb_obj_classname(params->src_hash)); |
| 120 | return Qnil; |
| 121 | } |
| 122 | |
| 123 | if (TYPE(params->src_hash) == T_HASH) { |
| 124 | num_args = NUM2INT(rb_funcall(params->src_hash, id_size, 0)); |
| 125 | params->dst->num_args = num_args; |
| 126 | params->dst->args = ALLOC_N(grpc_arg, num_args); |
| 127 | MEMZERO(params->dst->args, grpc_arg, num_args); |
Craig Tiller | 5b1c5f2 | 2017-04-19 09:52:18 -0700 | [diff] [blame] | 128 | rb_hash_foreach( |
| 129 | params->src_hash, grpc_rb_channel_create_in_process_add_args_hash_cb, |
| 130 | TypedData_Wrap_Struct(grpc_rb_cChannelArgs, |
| 131 | &grpc_rb_channel_args_data_type, params->dst)); |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 132 | /* reset num_args as grpc_rb_channel_create_in_process_add_args_hash_cb |
| 133 | * decrements it during has processing */ |
| 134 | params->dst->num_args = num_args; |
| 135 | } |
| 136 | return Qnil; |
| 137 | } |
| 138 | |
| 139 | void grpc_rb_hash_convert_to_channel_args(VALUE src_hash, |
| 140 | grpc_channel_args* dst) { |
| 141 | channel_convert_params params; |
| 142 | int status = 0; |
| 143 | |
| 144 | /* Make a protected call to grpc_rb_hash_convert_channel_args */ |
| 145 | params.src_hash = src_hash; |
| 146 | params.dst = dst; |
Craig Tiller | 5b1c5f2 | 2017-04-19 09:52:18 -0700 | [diff] [blame] | 147 | rb_protect(grpc_rb_hash_convert_to_channel_args0, (VALUE)¶ms, &status); |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 148 | if (status != 0) { |
| 149 | if (dst->args != NULL) { |
| 150 | /* Free any allocated memory before propagating the error */ |
| 151 | xfree(dst->args); |
| 152 | } |
| 153 | rb_jump_tag(status); |
| 154 | } |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 155 | } |