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_call.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> |
Alexander Polcyn | cdff92f | 2016-07-12 12:30:30 -0700 | [diff] [blame] | 25 | #include <grpc/impl/codegen/compression_types.h> |
Craig Tiller | 5b1c5f2 | 2017-04-19 09:52:18 -0700 | [diff] [blame] | 26 | #include <grpc/support/alloc.h> |
Alexander Polcyn | 85cc143 | 2017-07-14 16:36:51 -0700 | [diff] [blame] | 27 | #include <grpc/support/log.h> |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 28 | |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 29 | #include "rb_byte_buffer.h" |
murgatroid99 | 9946f2b | 2015-12-04 14:36:27 -0800 | [diff] [blame] | 30 | #include "rb_call_credentials.h" |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 31 | #include "rb_completion_queue.h" |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 32 | #include "rb_grpc.h" |
| 33 | |
Yuki Yugui Sonoda | 3c88e5d | 2015-04-16 20:09:00 +0900 | [diff] [blame] | 34 | /* grpc_rb_cCall is the Call class whose instances proxy grpc_call. */ |
| 35 | static VALUE grpc_rb_cCall; |
| 36 | |
| 37 | /* grpc_rb_eCallError is the ruby class of the exception thrown during call |
| 38 | operations; */ |
| 39 | VALUE grpc_rb_eCallError = Qnil; |
| 40 | |
| 41 | /* grpc_rb_eOutOfTime is the ruby class of the exception thrown to indicate |
| 42 | a timeout. */ |
| 43 | static VALUE grpc_rb_eOutOfTime = Qnil; |
| 44 | |
Yuki Yugui Sonoda | a7d369e | 2015-04-11 11:48:36 +0900 | [diff] [blame] | 45 | /* grpc_rb_sBatchResult is struct class used to hold the results of a batch |
| 46 | * call. */ |
| 47 | static VALUE grpc_rb_sBatchResult; |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 48 | |
Yuki Yugui Sonoda | a7d369e | 2015-04-11 11:48:36 +0900 | [diff] [blame] | 49 | /* grpc_rb_cMdAry is the MetadataArray class whose instances proxy |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 50 | * grpc_metadata_array. */ |
Yuki Yugui Sonoda | a7d369e | 2015-04-11 11:48:36 +0900 | [diff] [blame] | 51 | static VALUE grpc_rb_cMdAry; |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 52 | |
murgatroid99 | 8c9edc2 | 2016-03-14 15:51:56 -0700 | [diff] [blame] | 53 | /* id_credentials is the name of the hidden ivar that preserves the value |
| 54 | * of the credentials added to the call */ |
| 55 | static ID id_credentials; |
| 56 | |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 57 | /* id_metadata is name of the attribute used to access the metadata hash |
| 58 | * received by the call and subsequently saved on it. */ |
| 59 | static ID id_metadata; |
| 60 | |
murgatroid99 | e69f088 | 2016-07-07 15:52:27 -0700 | [diff] [blame] | 61 | /* id_trailing_metadata is the name of the attribute used to access the trailing |
| 62 | * metadata hash received by the call and subsequently saved on it. */ |
| 63 | static ID id_trailing_metadata; |
| 64 | |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 65 | /* id_status is name of the attribute used to access the status object |
| 66 | * received by the call and subsequently saved on it. */ |
| 67 | static ID id_status; |
| 68 | |
Tim Emiola | 7840a55 | 2015-08-20 13:12:33 -0700 | [diff] [blame] | 69 | /* id_write_flag is name of the attribute used to access the write_flag |
| 70 | * saved on the call. */ |
| 71 | static ID id_write_flag; |
| 72 | |
Yuki Yugui Sonoda | a7d369e | 2015-04-11 11:48:36 +0900 | [diff] [blame] | 73 | /* sym_* are the symbol for attributes of grpc_rb_sBatchResult. */ |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 74 | static VALUE sym_send_message; |
| 75 | static VALUE sym_send_metadata; |
| 76 | static VALUE sym_send_close; |
| 77 | static VALUE sym_send_status; |
| 78 | static VALUE sym_message; |
| 79 | static VALUE sym_status; |
| 80 | static VALUE sym_cancelled; |
| 81 | |
murgatroid99 | ec1588b | 2016-06-06 15:37:45 -0700 | [diff] [blame] | 82 | typedef struct grpc_rb_call { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 83 | grpc_call* wrapped; |
| 84 | grpc_completion_queue* queue; |
murgatroid99 | ec1588b | 2016-06-06 15:37:45 -0700 | [diff] [blame] | 85 | } grpc_rb_call; |
| 86 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 87 | static void destroy_call(grpc_rb_call* call) { |
murgatroid99 | 0033865 | 2016-06-28 14:14:49 -0700 | [diff] [blame] | 88 | /* Ensure that we only try to destroy the call once */ |
| 89 | if (call->wrapped != NULL) { |
Craig Tiller | dd36b15 | 2017-03-31 08:27:28 -0700 | [diff] [blame] | 90 | grpc_call_unref(call->wrapped); |
murgatroid99 | 0033865 | 2016-06-28 14:14:49 -0700 | [diff] [blame] | 91 | call->wrapped = NULL; |
| 92 | grpc_rb_completion_queue_destroy(call->queue); |
| 93 | call->queue = NULL; |
| 94 | } |
murgatroid99 | ec1588b | 2016-06-06 15:37:45 -0700 | [diff] [blame] | 95 | } |
| 96 | |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 97 | /* Destroys a Call. */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 98 | static void grpc_rb_call_destroy(void* p) { |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 99 | if (p == NULL) { |
| 100 | return; |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 101 | } |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 102 | destroy_call((grpc_rb_call*)p); |
Alexander Polcyn | d24e966 | 2017-09-11 13:40:12 -0700 | [diff] [blame] | 103 | xfree(p); |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 104 | } |
| 105 | |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 106 | static size_t md_ary_datasize(const void* p) { |
| 107 | const grpc_metadata_array* const ary = (grpc_metadata_array*)p; |
Craig Tiller | 1a727fd | 2015-04-24 13:21:22 -0700 | [diff] [blame] | 108 | size_t i, datasize = sizeof(grpc_metadata_array); |
| 109 | for (i = 0; i < ary->count; ++i) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 110 | const grpc_metadata* const md = &ary->metadata[i]; |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 111 | datasize += GRPC_SLICE_LENGTH(md->key); |
| 112 | datasize += GRPC_SLICE_LENGTH(md->value); |
Craig Tiller | 1a727fd | 2015-04-24 13:21:22 -0700 | [diff] [blame] | 113 | } |
| 114 | datasize += ary->capacity * sizeof(grpc_metadata); |
| 115 | return datasize; |
Yuki Yugui Sonoda | 961f0bc | 2015-04-11 14:39:07 +0900 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | static const rb_data_type_t grpc_rb_md_ary_data_type = { |
| 119 | "grpc_metadata_array", |
Craig Tiller | 5b1c5f2 | 2017-04-19 09:52:18 -0700 | [diff] [blame] | 120 | {GRPC_RB_GC_NOT_MARKED, |
| 121 | GRPC_RB_GC_DONT_FREE, |
| 122 | md_ary_datasize, |
murgatroid99 | 87afb5d | 2015-07-16 16:01:02 -0700 | [diff] [blame] | 123 | {NULL, NULL}}, |
Craig Tiller | 1a727fd | 2015-04-24 13:21:22 -0700 | [diff] [blame] | 124 | NULL, |
| 125 | NULL, |
Tim Emiola | 9161a82 | 2015-11-11 15:58:44 -0800 | [diff] [blame] | 126 | #ifdef RUBY_TYPED_FREE_IMMEDIATELY |
| 127 | /* it is unsafe to specify RUBY_TYPED_FREE_IMMEDIATELY because |
| 128 | * grpc_rb_call_destroy |
| 129 | * touches a hash object. |
| 130 | * TODO(yugui) Directly use st_table and call the free function earlier? |
| 131 | */ |
Craig Tiller | 5b1c5f2 | 2017-04-19 09:52:18 -0700 | [diff] [blame] | 132 | 0, |
Tim Emiola | 9161a82 | 2015-11-11 15:58:44 -0800 | [diff] [blame] | 133 | #endif |
| 134 | }; |
Yuki Yugui Sonoda | 961f0bc | 2015-04-11 14:39:07 +0900 | [diff] [blame] | 135 | |
Yuki Yugui Sonoda | 76801d2 | 2015-04-11 14:20:27 +0900 | [diff] [blame] | 136 | /* Describes grpc_call struct for RTypedData */ |
Craig Tiller | 5b1c5f2 | 2017-04-19 09:52:18 -0700 | [diff] [blame] | 137 | static const rb_data_type_t grpc_call_data_type = {"grpc_call", |
| 138 | {GRPC_RB_GC_NOT_MARKED, |
| 139 | grpc_rb_call_destroy, |
| 140 | GRPC_RB_MEMSIZE_UNAVAILABLE, |
| 141 | {NULL, NULL}}, |
| 142 | NULL, |
| 143 | NULL, |
Tim Emiola | 9161a82 | 2015-11-11 15:58:44 -0800 | [diff] [blame] | 144 | #ifdef RUBY_TYPED_FREE_IMMEDIATELY |
Craig Tiller | 5b1c5f2 | 2017-04-19 09:52:18 -0700 | [diff] [blame] | 145 | RUBY_TYPED_FREE_IMMEDIATELY |
Tim Emiola | 9161a82 | 2015-11-11 15:58:44 -0800 | [diff] [blame] | 146 | #endif |
| 147 | }; |
Yuki Yugui Sonoda | 76801d2 | 2015-04-11 14:20:27 +0900 | [diff] [blame] | 148 | |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 149 | /* Error code details is a hash containing text strings describing errors */ |
| 150 | VALUE rb_error_code_details; |
| 151 | |
| 152 | /* Obtains the error detail string for given error code */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 153 | const char* grpc_call_error_detail_of(grpc_call_error err) { |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 154 | VALUE detail_ref = rb_hash_aref(rb_error_code_details, UINT2NUM(err)); |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 155 | const char* detail = "unknown error code!"; |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 156 | if (detail_ref != Qnil) { |
| 157 | detail = StringValueCStr(detail_ref); |
| 158 | } |
| 159 | return detail; |
| 160 | } |
| 161 | |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 162 | /* Called by clients to cancel an RPC on the server. |
| 163 | Can be called multiple times, from any thread. */ |
| 164 | static VALUE grpc_rb_call_cancel(VALUE self) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 165 | grpc_rb_call* call = NULL; |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 166 | grpc_call_error err; |
Ken Payson | dce1ee6 | 2016-05-20 10:29:34 -0700 | [diff] [blame] | 167 | if (RTYPEDDATA_DATA(self) == NULL) { |
Craig Tiller | 5b1c5f2 | 2017-04-19 09:52:18 -0700 | [diff] [blame] | 168 | // This call has been closed |
Ken Payson | dce1ee6 | 2016-05-20 10:29:34 -0700 | [diff] [blame] | 169 | return Qnil; |
| 170 | } |
| 171 | |
murgatroid99 | ec1588b | 2016-06-06 15:37:45 -0700 | [diff] [blame] | 172 | TypedData_Get_Struct(self, grpc_rb_call, &grpc_call_data_type, call); |
| 173 | err = grpc_call_cancel(call->wrapped, NULL); |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 174 | if (err != GRPC_CALL_OK) { |
Yuki Yugui Sonoda | a7d369e | 2015-04-11 11:48:36 +0900 | [diff] [blame] | 175 | rb_raise(grpc_rb_eCallError, "cancel failed: %s (code=%d)", |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 176 | grpc_call_error_detail_of(err), err); |
| 177 | } |
| 178 | |
| 179 | return Qnil; |
| 180 | } |
| 181 | |
Alexander Polcyn | c6627ca | 2017-04-13 10:47:16 -0700 | [diff] [blame] | 182 | /* TODO: expose this as part of the surface API if needed. |
| 183 | * This is meant for internal usage by the "write thread" of grpc-ruby |
| 184 | * client-side bidi calls. It provides a way for the background write-thread |
| 185 | * to propogate failures to the main read-thread and give the user an error |
| 186 | * message. */ |
| 187 | static VALUE grpc_rb_call_cancel_with_status(VALUE self, VALUE status_code, |
| 188 | VALUE details) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 189 | grpc_rb_call* call = NULL; |
Alexander Polcyn | c6627ca | 2017-04-13 10:47:16 -0700 | [diff] [blame] | 190 | grpc_call_error err; |
| 191 | if (RTYPEDDATA_DATA(self) == NULL) { |
| 192 | // This call has been closed |
| 193 | return Qnil; |
| 194 | } |
| 195 | |
| 196 | if (TYPE(details) != T_STRING || TYPE(status_code) != T_FIXNUM) { |
| 197 | rb_raise(rb_eTypeError, |
| 198 | "Bad parameter type error for cancel with status. Want Fixnum, " |
| 199 | "String."); |
| 200 | return Qnil; |
| 201 | } |
| 202 | |
| 203 | TypedData_Get_Struct(self, grpc_rb_call, &grpc_call_data_type, call); |
| 204 | err = grpc_call_cancel_with_status(call->wrapped, NUM2LONG(status_code), |
| 205 | StringValueCStr(details), NULL); |
| 206 | if (err != GRPC_CALL_OK) { |
| 207 | rb_raise(grpc_rb_eCallError, "cancel with status failed: %s (code=%d)", |
| 208 | grpc_call_error_detail_of(err), err); |
| 209 | } |
| 210 | |
| 211 | return Qnil; |
| 212 | } |
| 213 | |
Ken Payson | dce1ee6 | 2016-05-20 10:29:34 -0700 | [diff] [blame] | 214 | /* Releases the c-level resources associated with a call |
| 215 | Once a call has been closed, no further requests can be |
| 216 | processed. |
| 217 | */ |
| 218 | static VALUE grpc_rb_call_close(VALUE self) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 219 | grpc_rb_call* call = NULL; |
murgatroid99 | ec1588b | 2016-06-06 15:37:45 -0700 | [diff] [blame] | 220 | TypedData_Get_Struct(self, grpc_rb_call, &grpc_call_data_type, call); |
Craig Tiller | 5b1c5f2 | 2017-04-19 09:52:18 -0700 | [diff] [blame] | 221 | if (call != NULL) { |
murgatroid99 | ec1588b | 2016-06-06 15:37:45 -0700 | [diff] [blame] | 222 | destroy_call(call); |
Alexander Polcyn | 40d3062 | 2017-10-17 17:39:57 -0700 | [diff] [blame] | 223 | xfree(RTYPEDDATA_DATA(self)); |
Ken Payson | dce1ee6 | 2016-05-20 10:29:34 -0700 | [diff] [blame] | 224 | RTYPEDDATA_DATA(self) = NULL; |
| 225 | } |
| 226 | return Qnil; |
| 227 | } |
| 228 | |
Tim Emiola | 623a74d | 2015-08-11 09:24:20 -0700 | [diff] [blame] | 229 | /* Called to obtain the peer that this call is connected to. */ |
| 230 | static VALUE grpc_rb_call_get_peer(VALUE self) { |
| 231 | VALUE res = Qnil; |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 232 | grpc_rb_call* call = NULL; |
| 233 | char* peer = NULL; |
Ken Payson | dce1ee6 | 2016-05-20 10:29:34 -0700 | [diff] [blame] | 234 | if (RTYPEDDATA_DATA(self) == NULL) { |
| 235 | rb_raise(grpc_rb_eCallError, "Cannot get peer value on closed call"); |
| 236 | return Qnil; |
| 237 | } |
murgatroid99 | ec1588b | 2016-06-06 15:37:45 -0700 | [diff] [blame] | 238 | TypedData_Get_Struct(self, grpc_rb_call, &grpc_call_data_type, call); |
| 239 | peer = grpc_call_get_peer(call->wrapped); |
Tim Emiola | 623a74d | 2015-08-11 09:24:20 -0700 | [diff] [blame] | 240 | res = rb_str_new2(peer); |
| 241 | gpr_free(peer); |
| 242 | |
| 243 | return res; |
| 244 | } |
| 245 | |
Eric Richardson | 69d735a | 2016-04-20 13:23:32 -0400 | [diff] [blame] | 246 | /* Called to obtain the x509 cert of an authenticated peer. */ |
| 247 | static VALUE grpc_rb_call_get_peer_cert(VALUE self) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 248 | grpc_rb_call* call = NULL; |
Eric Richardson | 69d735a | 2016-04-20 13:23:32 -0400 | [diff] [blame] | 249 | VALUE res = Qnil; |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 250 | grpc_auth_context* ctx = NULL; |
Ken Payson | dce1ee6 | 2016-05-20 10:29:34 -0700 | [diff] [blame] | 251 | if (RTYPEDDATA_DATA(self) == NULL) { |
| 252 | rb_raise(grpc_rb_eCallError, "Cannot get peer cert on closed call"); |
| 253 | return Qnil; |
| 254 | } |
murgatroid99 | ec1588b | 2016-06-06 15:37:45 -0700 | [diff] [blame] | 255 | TypedData_Get_Struct(self, grpc_rb_call, &grpc_call_data_type, call); |
Eric Richardson | 69d735a | 2016-04-20 13:23:32 -0400 | [diff] [blame] | 256 | |
murgatroid99 | ec1588b | 2016-06-06 15:37:45 -0700 | [diff] [blame] | 257 | ctx = grpc_call_auth_context(call->wrapped); |
Eric Richardson | 69d735a | 2016-04-20 13:23:32 -0400 | [diff] [blame] | 258 | |
Eric Richardson | bdfaf48 | 2016-04-20 13:56:55 -0400 | [diff] [blame] | 259 | if (!ctx || !grpc_auth_context_peer_is_authenticated(ctx)) { |
| 260 | return Qnil; |
| 261 | } |
| 262 | |
Eric Richardson | 336b744 | 2016-04-21 14:46:59 -0400 | [diff] [blame] | 263 | { |
Craig Tiller | 5b1c5f2 | 2017-04-19 09:52:18 -0700 | [diff] [blame] | 264 | grpc_auth_property_iterator it = grpc_auth_context_find_properties_by_name( |
| 265 | ctx, GRPC_X509_PEM_CERT_PROPERTY_NAME); |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 266 | const grpc_auth_property* prop = grpc_auth_property_iterator_next(&it); |
Eric Richardson | 336b744 | 2016-04-21 14:46:59 -0400 | [diff] [blame] | 267 | if (prop == NULL) { |
| 268 | return Qnil; |
| 269 | } |
Eric Richardson | 69d735a | 2016-04-20 13:23:32 -0400 | [diff] [blame] | 270 | |
Eric Richardson | 336b744 | 2016-04-21 14:46:59 -0400 | [diff] [blame] | 271 | res = rb_str_new2(prop->value); |
| 272 | } |
Eric Richardson | 69d735a | 2016-04-20 13:23:32 -0400 | [diff] [blame] | 273 | |
Eric Richardson | bdfaf48 | 2016-04-20 13:56:55 -0400 | [diff] [blame] | 274 | grpc_auth_context_release(ctx); |
| 275 | |
Eric Richardson | 69d735a | 2016-04-20 13:23:32 -0400 | [diff] [blame] | 276 | return res; |
| 277 | } |
| 278 | |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 279 | /* |
| 280 | call-seq: |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 281 | status = call.status |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 282 | |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 283 | Gets the status object saved the call. */ |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 284 | static VALUE grpc_rb_call_get_status(VALUE self) { |
| 285 | return rb_ivar_get(self, id_status); |
| 286 | } |
| 287 | |
| 288 | /* |
| 289 | call-seq: |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 290 | call.status = status |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 291 | |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 292 | Saves a status object on the call. */ |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 293 | static VALUE grpc_rb_call_set_status(VALUE self, VALUE status) { |
Yuki Yugui Sonoda | a7d369e | 2015-04-11 11:48:36 +0900 | [diff] [blame] | 294 | if (!NIL_P(status) && rb_obj_class(status) != grpc_rb_sStatus) { |
temiola | 5832791 | 2014-12-15 17:51:16 -0800 | [diff] [blame] | 295 | rb_raise(rb_eTypeError, "bad status: got:<%s> want: <Struct::Status>", |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 296 | rb_obj_classname(status)); |
| 297 | return Qnil; |
| 298 | } |
| 299 | |
| 300 | return rb_ivar_set(self, id_status, status); |
| 301 | } |
| 302 | |
| 303 | /* |
| 304 | call-seq: |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 305 | metadata = call.metadata |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 306 | |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 307 | Gets the metadata object saved the call. */ |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 308 | static VALUE grpc_rb_call_get_metadata(VALUE self) { |
| 309 | return rb_ivar_get(self, id_metadata); |
| 310 | } |
| 311 | |
| 312 | /* |
| 313 | call-seq: |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 314 | call.metadata = metadata |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 315 | |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 316 | Saves the metadata hash on the call. */ |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 317 | static VALUE grpc_rb_call_set_metadata(VALUE self, VALUE metadata) { |
| 318 | if (!NIL_P(metadata) && TYPE(metadata) != T_HASH) { |
| 319 | rb_raise(rb_eTypeError, "bad metadata: got:<%s> want: <Hash>", |
| 320 | rb_obj_classname(metadata)); |
| 321 | return Qnil; |
| 322 | } |
| 323 | |
| 324 | return rb_ivar_set(self, id_metadata, metadata); |
| 325 | } |
| 326 | |
Tim Emiola | 7840a55 | 2015-08-20 13:12:33 -0700 | [diff] [blame] | 327 | /* |
| 328 | call-seq: |
murgatroid99 | e69f088 | 2016-07-07 15:52:27 -0700 | [diff] [blame] | 329 | trailing_metadata = call.trailing_metadata |
| 330 | |
| 331 | Gets the trailing metadata object saved on the call */ |
| 332 | static VALUE grpc_rb_call_get_trailing_metadata(VALUE self) { |
| 333 | return rb_ivar_get(self, id_trailing_metadata); |
| 334 | } |
| 335 | |
| 336 | /* |
| 337 | call-seq: |
| 338 | call.trailing_metadata = trailing_metadata |
| 339 | |
| 340 | Saves the trailing metadata hash on the call. */ |
| 341 | static VALUE grpc_rb_call_set_trailing_metadata(VALUE self, VALUE metadata) { |
| 342 | if (!NIL_P(metadata) && TYPE(metadata) != T_HASH) { |
| 343 | rb_raise(rb_eTypeError, "bad metadata: got:<%s> want: <Hash>", |
| 344 | rb_obj_classname(metadata)); |
| 345 | return Qnil; |
| 346 | } |
| 347 | |
| 348 | return rb_ivar_set(self, id_trailing_metadata, metadata); |
| 349 | } |
| 350 | |
| 351 | /* |
| 352 | call-seq: |
Tim Emiola | 7840a55 | 2015-08-20 13:12:33 -0700 | [diff] [blame] | 353 | write_flag = call.write_flag |
| 354 | |
| 355 | Gets the write_flag value saved the call. */ |
| 356 | static VALUE grpc_rb_call_get_write_flag(VALUE self) { |
| 357 | return rb_ivar_get(self, id_write_flag); |
| 358 | } |
| 359 | |
| 360 | /* |
| 361 | call-seq: |
| 362 | call.write_flag = write_flag |
| 363 | |
| 364 | Saves the write_flag on the call. */ |
| 365 | static VALUE grpc_rb_call_set_write_flag(VALUE self, VALUE write_flag) { |
| 366 | if (!NIL_P(write_flag) && TYPE(write_flag) != T_FIXNUM) { |
| 367 | rb_raise(rb_eTypeError, "bad write_flag: got:<%s> want: <Fixnum>", |
| 368 | rb_obj_classname(write_flag)); |
| 369 | return Qnil; |
| 370 | } |
| 371 | |
| 372 | return rb_ivar_set(self, id_write_flag, write_flag); |
| 373 | } |
| 374 | |
murgatroid99 | 9946f2b | 2015-12-04 14:36:27 -0800 | [diff] [blame] | 375 | /* |
| 376 | call-seq: |
| 377 | call.set_credentials call_credentials |
| 378 | |
| 379 | Sets credentials on a call */ |
| 380 | static VALUE grpc_rb_call_set_credentials(VALUE self, VALUE credentials) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 381 | grpc_rb_call* call = NULL; |
| 382 | grpc_call_credentials* creds; |
murgatroid99 | 9946f2b | 2015-12-04 14:36:27 -0800 | [diff] [blame] | 383 | grpc_call_error err; |
Ken Payson | dce1ee6 | 2016-05-20 10:29:34 -0700 | [diff] [blame] | 384 | if (RTYPEDDATA_DATA(self) == NULL) { |
| 385 | rb_raise(grpc_rb_eCallError, "Cannot set credentials of closed call"); |
| 386 | return Qnil; |
| 387 | } |
murgatroid99 | ec1588b | 2016-06-06 15:37:45 -0700 | [diff] [blame] | 388 | TypedData_Get_Struct(self, grpc_rb_call, &grpc_call_data_type, call); |
murgatroid99 | 9946f2b | 2015-12-04 14:36:27 -0800 | [diff] [blame] | 389 | creds = grpc_rb_get_wrapped_call_credentials(credentials); |
murgatroid99 | ec1588b | 2016-06-06 15:37:45 -0700 | [diff] [blame] | 390 | err = grpc_call_set_credentials(call->wrapped, creds); |
murgatroid99 | 9946f2b | 2015-12-04 14:36:27 -0800 | [diff] [blame] | 391 | if (err != GRPC_CALL_OK) { |
| 392 | rb_raise(grpc_rb_eCallError, |
| 393 | "grpc_call_set_credentials failed with %s (code=%d)", |
| 394 | grpc_call_error_detail_of(err), err); |
| 395 | } |
murgatroid99 | ec1588b | 2016-06-06 15:37:45 -0700 | [diff] [blame] | 396 | /* We need the credentials to be alive for as long as the call is alive, |
| 397 | but we don't care about destruction order. */ |
murgatroid99 | 8c9edc2 | 2016-03-14 15:51:56 -0700 | [diff] [blame] | 398 | rb_ivar_set(self, id_credentials, credentials); |
murgatroid99 | 9946f2b | 2015-12-04 14:36:27 -0800 | [diff] [blame] | 399 | return Qnil; |
| 400 | } |
| 401 | |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 402 | /* grpc_rb_md_ary_fill_hash_cb is the hash iteration callback used |
| 403 | to fill grpc_metadata_array. |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 404 | |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 405 | it's capacity should have been computed via a prior call to |
Alexander Polcyn | 85cc143 | 2017-07-14 16:36:51 -0700 | [diff] [blame] | 406 | grpc_rb_md_ary_capacity_hash_cb |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 407 | */ |
Yuki Yugui Sonoda | f0eee5f | 2015-04-16 20:25:28 +0900 | [diff] [blame] | 408 | static int grpc_rb_md_ary_fill_hash_cb(VALUE key, VALUE val, VALUE md_ary_obj) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 409 | grpc_metadata_array* md_ary = NULL; |
Marcin Wyszynski | ac91edd | 2015-07-23 19:59:46 +0200 | [diff] [blame] | 410 | long array_length; |
Marcin Wyszynski | a5fea60 | 2015-07-24 10:24:32 +0200 | [diff] [blame] | 411 | long i; |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 412 | grpc_slice key_slice; |
| 413 | grpc_slice value_slice; |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 414 | char* tmp_str = NULL; |
murgatroid99 | 56fada5 | 2016-01-06 14:40:38 -0800 | [diff] [blame] | 415 | |
murgatroid99 | c2ea3ae | 2016-01-06 11:48:15 -0800 | [diff] [blame] | 416 | if (TYPE(key) == T_SYMBOL) { |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 417 | key_slice = grpc_slice_from_static_string(rb_id2name(SYM2ID(key))); |
| 418 | } else if (TYPE(key) == T_STRING) { |
Craig Tiller | 5b1c5f2 | 2017-04-19 09:52:18 -0700 | [diff] [blame] | 419 | key_slice = |
| 420 | grpc_slice_from_copied_buffer(RSTRING_PTR(key), RSTRING_LEN(key)); |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 421 | } else { |
Craig Tiller | 5b1c5f2 | 2017-04-19 09:52:18 -0700 | [diff] [blame] | 422 | rb_raise(rb_eTypeError, |
| 423 | "grpc_rb_md_ary_fill_hash_cb: bad type for key parameter"); |
Alexander Polcyn | 85cc143 | 2017-07-14 16:36:51 -0700 | [diff] [blame] | 424 | return ST_STOP; |
murgatroid99 | c2ea3ae | 2016-01-06 11:48:15 -0800 | [diff] [blame] | 425 | } |
| 426 | |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 427 | if (!grpc_header_key_is_legal(key_slice)) { |
| 428 | tmp_str = grpc_slice_to_c_string(key_slice); |
murgatroid99 | c2ea3ae | 2016-01-06 11:48:15 -0800 | [diff] [blame] | 429 | rb_raise(rb_eArgError, |
Craig Tiller | 5b1c5f2 | 2017-04-19 09:52:18 -0700 | [diff] [blame] | 430 | "'%s' is an invalid header key, must match [a-z0-9-_.]+", tmp_str); |
murgatroid99 | c2ea3ae | 2016-01-06 11:48:15 -0800 | [diff] [blame] | 431 | return ST_STOP; |
| 432 | } |
| 433 | |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 434 | /* Construct a metadata object from key and value and add it */ |
Yuki Yugui Sonoda | 961f0bc | 2015-04-11 14:39:07 +0900 | [diff] [blame] | 435 | TypedData_Get_Struct(md_ary_obj, grpc_metadata_array, |
| 436 | &grpc_rb_md_ary_data_type, md_ary); |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 437 | |
| 438 | if (TYPE(val) == T_ARRAY) { |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 439 | array_length = RARRAY_LEN(val); |
murgatroid99 | 56fada5 | 2016-01-06 14:40:38 -0800 | [diff] [blame] | 440 | /* If the value is an array, add capacity for each value in the array */ |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 441 | for (i = 0; i < array_length; i++) { |
Craig Tiller | 5b1c5f2 | 2017-04-19 09:52:18 -0700 | [diff] [blame] | 442 | value_slice = grpc_slice_from_copied_buffer( |
| 443 | RSTRING_PTR(rb_ary_entry(val, i)), RSTRING_LEN(rb_ary_entry(val, i))); |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 444 | if (!grpc_is_binary_header(key_slice) && |
| 445 | !grpc_header_nonbin_value_is_legal(value_slice)) { |
murgatroid99 | 56fada5 | 2016-01-06 14:40:38 -0800 | [diff] [blame] | 446 | // The value has invalid characters |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 447 | tmp_str = grpc_slice_to_c_string(value_slice); |
Craig Tiller | 5b1c5f2 | 2017-04-19 09:52:18 -0700 | [diff] [blame] | 448 | rb_raise(rb_eArgError, "Header value '%s' has invalid characters", |
| 449 | tmp_str); |
murgatroid99 | 56fada5 | 2016-01-06 14:40:38 -0800 | [diff] [blame] | 450 | return ST_STOP; |
| 451 | } |
Alexander Polcyn | 85cc143 | 2017-07-14 16:36:51 -0700 | [diff] [blame] | 452 | GPR_ASSERT(md_ary->count < md_ary->capacity); |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 453 | md_ary->metadata[md_ary->count].key = key_slice; |
| 454 | md_ary->metadata[md_ary->count].value = value_slice; |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 455 | md_ary->count += 1; |
| 456 | } |
Rafael Sales | ac491d8 | 2016-03-02 02:30:29 -0300 | [diff] [blame] | 457 | } else if (TYPE(val) == T_STRING) { |
Craig Tiller | 5b1c5f2 | 2017-04-19 09:52:18 -0700 | [diff] [blame] | 458 | value_slice = |
| 459 | grpc_slice_from_copied_buffer(RSTRING_PTR(val), RSTRING_LEN(val)); |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 460 | if (!grpc_is_binary_header(key_slice) && |
| 461 | !grpc_header_nonbin_value_is_legal(value_slice)) { |
murgatroid99 | 56fada5 | 2016-01-06 14:40:38 -0800 | [diff] [blame] | 462 | // The value has invalid characters |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 463 | tmp_str = grpc_slice_to_c_string(value_slice); |
Craig Tiller | 5b1c5f2 | 2017-04-19 09:52:18 -0700 | [diff] [blame] | 464 | rb_raise(rb_eArgError, "Header value '%s' has invalid characters", |
| 465 | tmp_str); |
murgatroid99 | 56fada5 | 2016-01-06 14:40:38 -0800 | [diff] [blame] | 466 | return ST_STOP; |
| 467 | } |
Alexander Polcyn | 85cc143 | 2017-07-14 16:36:51 -0700 | [diff] [blame] | 468 | GPR_ASSERT(md_ary->count < md_ary->capacity); |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 469 | md_ary->metadata[md_ary->count].key = key_slice; |
| 470 | md_ary->metadata[md_ary->count].value = value_slice; |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 471 | md_ary->count += 1; |
Rafael Sales | ac491d8 | 2016-03-02 02:30:29 -0300 | [diff] [blame] | 472 | } else { |
Craig Tiller | 5b1c5f2 | 2017-04-19 09:52:18 -0700 | [diff] [blame] | 473 | rb_raise(rb_eArgError, "Header values must be of type string or array"); |
Rafael Sales | ac491d8 | 2016-03-02 02:30:29 -0300 | [diff] [blame] | 474 | return ST_STOP; |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 475 | } |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 476 | return ST_CONTINUE; |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 477 | } |
| 478 | |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 479 | /* grpc_rb_md_ary_capacity_hash_cb is the hash iteration callback used |
| 480 | to pre-compute the capacity a grpc_metadata_array. |
| 481 | */ |
Yuki Yugui Sonoda | f0eee5f | 2015-04-16 20:25:28 +0900 | [diff] [blame] | 482 | static int grpc_rb_md_ary_capacity_hash_cb(VALUE key, VALUE val, |
| 483 | VALUE md_ary_obj) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 484 | grpc_metadata_array* md_ary = NULL; |
temiola | 5832791 | 2014-12-15 17:51:16 -0800 | [diff] [blame] | 485 | |
murgatroid99 | 87afb5d | 2015-07-16 16:01:02 -0700 | [diff] [blame] | 486 | (void)key; |
| 487 | |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 488 | /* Construct a metadata object from key and value and add it */ |
Yuki Yugui Sonoda | 961f0bc | 2015-04-11 14:39:07 +0900 | [diff] [blame] | 489 | TypedData_Get_Struct(md_ary_obj, grpc_metadata_array, |
| 490 | &grpc_rb_md_ary_data_type, md_ary); |
temiola | 5832791 | 2014-12-15 17:51:16 -0800 | [diff] [blame] | 491 | |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 492 | if (TYPE(val) == T_ARRAY) { |
| 493 | /* If the value is an array, add capacity for each value in the array */ |
| 494 | md_ary->capacity += RARRAY_LEN(val); |
| 495 | } else { |
| 496 | md_ary->capacity += 1; |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 497 | } |
Alexander Polcyn | 85cc143 | 2017-07-14 16:36:51 -0700 | [diff] [blame] | 498 | |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 499 | return ST_CONTINUE; |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 500 | } |
| 501 | |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 502 | /* grpc_rb_md_ary_convert converts a ruby metadata hash into |
| 503 | a grpc_metadata_array. |
| 504 | */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 505 | void grpc_rb_md_ary_convert(VALUE md_ary_hash, grpc_metadata_array* md_ary) { |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 506 | VALUE md_ary_obj = Qnil; |
| 507 | if (md_ary_hash == Qnil) { |
Craig Tiller | 1a727fd | 2015-04-24 13:21:22 -0700 | [diff] [blame] | 508 | return; /* Do nothing if the expected has value is nil */ |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 509 | } |
| 510 | if (TYPE(md_ary_hash) != T_HASH) { |
| 511 | rb_raise(rb_eTypeError, "md_ary_convert: got <%s>, want <Hash>", |
| 512 | rb_obj_classname(md_ary_hash)); |
| 513 | return; |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 514 | } |
| 515 | |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 516 | /* Initialize the array, compute it's capacity, then fill it. */ |
| 517 | grpc_metadata_array_init(md_ary); |
Craig Tiller | 1a727fd | 2015-04-24 13:21:22 -0700 | [diff] [blame] | 518 | md_ary_obj = |
| 519 | TypedData_Wrap_Struct(grpc_rb_cMdAry, &grpc_rb_md_ary_data_type, md_ary); |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 520 | rb_hash_foreach(md_ary_hash, grpc_rb_md_ary_capacity_hash_cb, md_ary_obj); |
Alexander Polcyn | 85cc143 | 2017-07-14 16:36:51 -0700 | [diff] [blame] | 521 | md_ary->metadata = gpr_zalloc(md_ary->capacity * sizeof(grpc_metadata)); |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 522 | rb_hash_foreach(md_ary_hash, grpc_rb_md_ary_fill_hash_cb, md_ary_obj); |
| 523 | } |
| 524 | |
| 525 | /* Converts a metadata array to a hash. */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 526 | VALUE grpc_rb_md_ary_to_h(grpc_metadata_array* md_ary) { |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 527 | VALUE key = Qnil; |
| 528 | VALUE new_ary = Qnil; |
| 529 | VALUE value = Qnil; |
| 530 | VALUE result = rb_hash_new(); |
| 531 | size_t i; |
| 532 | |
| 533 | for (i = 0; i < md_ary->count; i++) { |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 534 | key = grpc_rb_slice_to_ruby_string(md_ary->metadata[i].key); |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 535 | value = rb_hash_aref(result, key); |
| 536 | if (value == Qnil) { |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 537 | value = grpc_rb_slice_to_ruby_string(md_ary->metadata[i].value); |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 538 | rb_hash_aset(result, key, value); |
| 539 | } else if (TYPE(value) == T_ARRAY) { |
| 540 | /* Add the string to the returned array */ |
Craig Tiller | 5b1c5f2 | 2017-04-19 09:52:18 -0700 | [diff] [blame] | 541 | rb_ary_push(value, |
| 542 | grpc_rb_slice_to_ruby_string(md_ary->metadata[i].value)); |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 543 | } else { |
| 544 | /* Add the current value with this key and the new one to an array */ |
| 545 | new_ary = rb_ary_new(); |
| 546 | rb_ary_push(new_ary, value); |
Craig Tiller | 5b1c5f2 | 2017-04-19 09:52:18 -0700 | [diff] [blame] | 547 | rb_ary_push(new_ary, |
| 548 | grpc_rb_slice_to_ruby_string(md_ary->metadata[i].value)); |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 549 | rb_hash_aset(result, key, new_ary); |
| 550 | } |
| 551 | } |
| 552 | return result; |
| 553 | } |
| 554 | |
| 555 | /* grpc_rb_call_check_op_keys_hash_cb is a hash iteration func that checks |
| 556 | each key of an ops hash is valid. |
| 557 | */ |
Yuki Yugui Sonoda | f0eee5f | 2015-04-16 20:25:28 +0900 | [diff] [blame] | 558 | static int grpc_rb_call_check_op_keys_hash_cb(VALUE key, VALUE val, |
| 559 | VALUE ops_ary) { |
murgatroid99 | 87afb5d | 2015-07-16 16:01:02 -0700 | [diff] [blame] | 560 | (void)val; |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 561 | /* Update the capacity; the value is an array, add capacity for each value in |
| 562 | * the array */ |
| 563 | if (TYPE(key) != T_FIXNUM) { |
| 564 | rb_raise(rb_eTypeError, "invalid operation : got <%s>, want <Fixnum>", |
| 565 | rb_obj_classname(key)); |
| 566 | return ST_STOP; |
| 567 | } |
Craig Tiller | 1a727fd | 2015-04-24 13:21:22 -0700 | [diff] [blame] | 568 | switch (NUM2INT(key)) { |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 569 | case GRPC_OP_SEND_INITIAL_METADATA: |
| 570 | case GRPC_OP_SEND_MESSAGE: |
| 571 | case GRPC_OP_SEND_CLOSE_FROM_CLIENT: |
| 572 | case GRPC_OP_SEND_STATUS_FROM_SERVER: |
| 573 | case GRPC_OP_RECV_INITIAL_METADATA: |
| 574 | case GRPC_OP_RECV_MESSAGE: |
| 575 | case GRPC_OP_RECV_STATUS_ON_CLIENT: |
| 576 | case GRPC_OP_RECV_CLOSE_ON_SERVER: |
| 577 | rb_ary_push(ops_ary, key); |
| 578 | return ST_CONTINUE; |
| 579 | default: |
Craig Tiller | 1a727fd | 2015-04-24 13:21:22 -0700 | [diff] [blame] | 580 | rb_raise(rb_eTypeError, "invalid operation : bad value %d", NUM2INT(key)); |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 581 | }; |
| 582 | return ST_STOP; |
| 583 | } |
| 584 | |
| 585 | /* grpc_rb_op_update_status_from_server adds the values in a ruby status |
| 586 | struct to the 'send_status_from_server' portion of an op. |
| 587 | */ |
Craig Tiller | 5b1c5f2 | 2017-04-19 09:52:18 -0700 | [diff] [blame] | 588 | static void grpc_rb_op_update_status_from_server( |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 589 | grpc_op* op, grpc_metadata_array* md_ary, grpc_slice* send_status_details, |
Craig Tiller | 5b1c5f2 | 2017-04-19 09:52:18 -0700 | [diff] [blame] | 590 | VALUE status) { |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 591 | VALUE code = rb_struct_aref(status, sym_code); |
| 592 | VALUE details = rb_struct_aref(status, sym_details); |
| 593 | VALUE metadata_hash = rb_struct_aref(status, sym_metadata); |
| 594 | |
| 595 | /* TODO: add check to ensure status is the correct struct type */ |
| 596 | if (TYPE(code) != T_FIXNUM) { |
| 597 | rb_raise(rb_eTypeError, "invalid code : got <%s>, want <Fixnum>", |
| 598 | rb_obj_classname(code)); |
| 599 | return; |
| 600 | } |
| 601 | if (TYPE(details) != T_STRING) { |
| 602 | rb_raise(rb_eTypeError, "invalid details : got <%s>, want <String>", |
| 603 | rb_obj_classname(code)); |
| 604 | return; |
| 605 | } |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 606 | |
Craig Tiller | 5b1c5f2 | 2017-04-19 09:52:18 -0700 | [diff] [blame] | 607 | *send_status_details = |
| 608 | grpc_slice_from_copied_buffer(RSTRING_PTR(details), RSTRING_LEN(details)); |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 609 | |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 610 | op->data.send_status_from_server.status = NUM2INT(code); |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 611 | op->data.send_status_from_server.status_details = send_status_details; |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 612 | grpc_rb_md_ary_convert(metadata_hash, md_ary); |
| 613 | op->data.send_status_from_server.trailing_metadata_count = md_ary->count; |
| 614 | op->data.send_status_from_server.trailing_metadata = md_ary->metadata; |
| 615 | } |
| 616 | |
| 617 | /* run_batch_stack holds various values used by the |
| 618 | * grpc_rb_call_run_batch function */ |
| 619 | typedef struct run_batch_stack { |
| 620 | /* The batch ops */ |
Craig Tiller | 1a727fd | 2015-04-24 13:21:22 -0700 | [diff] [blame] | 621 | grpc_op ops[8]; /* 8 is the maximum number of operations */ |
| 622 | size_t op_num; /* tracks the last added operation */ |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 623 | |
| 624 | /* Data being sent */ |
| 625 | grpc_metadata_array send_metadata; |
| 626 | grpc_metadata_array send_trailing_metadata; |
| 627 | |
| 628 | /* Data being received */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 629 | grpc_byte_buffer* recv_message; |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 630 | grpc_metadata_array recv_metadata; |
| 631 | grpc_metadata_array recv_trailing_metadata; |
| 632 | int recv_cancelled; |
| 633 | grpc_status_code recv_status; |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 634 | grpc_slice recv_status_details; |
Nicolas "Pixel" Noble | 7b92db6 | 2016-01-26 22:46:34 +0100 | [diff] [blame] | 635 | unsigned write_flag; |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 636 | grpc_slice send_status_details; |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 637 | } run_batch_stack; |
| 638 | |
| 639 | /* grpc_run_batch_stack_init ensures the run_batch_stack is properly |
| 640 | * initialized */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 641 | static void grpc_run_batch_stack_init(run_batch_stack* st, |
David Garcia Quintas | b4e51b5 | 2016-01-27 19:57:58 -0800 | [diff] [blame] | 642 | unsigned write_flag) { |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 643 | MEMZERO(st, run_batch_stack, 1); |
| 644 | grpc_metadata_array_init(&st->send_metadata); |
| 645 | grpc_metadata_array_init(&st->send_trailing_metadata); |
| 646 | grpc_metadata_array_init(&st->recv_metadata); |
| 647 | grpc_metadata_array_init(&st->recv_trailing_metadata); |
| 648 | st->op_num = 0; |
Tim Emiola | 7840a55 | 2015-08-20 13:12:33 -0700 | [diff] [blame] | 649 | st->write_flag = write_flag; |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 650 | } |
| 651 | |
Alexander Polcyn | 85cc143 | 2017-07-14 16:36:51 -0700 | [diff] [blame] | 652 | void grpc_rb_metadata_array_destroy_including_entries( |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 653 | grpc_metadata_array* array) { |
Alexander Polcyn | 85cc143 | 2017-07-14 16:36:51 -0700 | [diff] [blame] | 654 | size_t i; |
| 655 | if (array->metadata) { |
| 656 | for (i = 0; i < array->count; i++) { |
| 657 | grpc_slice_unref(array->metadata[i].key); |
| 658 | grpc_slice_unref(array->metadata[i].value); |
| 659 | } |
| 660 | } |
| 661 | grpc_metadata_array_destroy(array); |
| 662 | } |
| 663 | |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 664 | /* grpc_run_batch_stack_cleanup ensures the run_batch_stack is properly |
| 665 | * cleaned up */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 666 | static void grpc_run_batch_stack_cleanup(run_batch_stack* st) { |
Zhuochun | 97daf35 | 2016-03-13 16:19:56 +0800 | [diff] [blame] | 667 | size_t i = 0; |
| 668 | |
Alexander Polcyn | 85cc143 | 2017-07-14 16:36:51 -0700 | [diff] [blame] | 669 | grpc_rb_metadata_array_destroy_including_entries(&st->send_metadata); |
| 670 | grpc_rb_metadata_array_destroy_including_entries(&st->send_trailing_metadata); |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 671 | grpc_metadata_array_destroy(&st->recv_metadata); |
| 672 | grpc_metadata_array_destroy(&st->recv_trailing_metadata); |
Zhuochun | 97daf35 | 2016-03-13 16:19:56 +0800 | [diff] [blame] | 673 | |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 674 | if (GRPC_SLICE_START_PTR(st->send_status_details) != NULL) { |
| 675 | grpc_slice_unref(st->send_status_details); |
| 676 | } |
| 677 | |
| 678 | if (GRPC_SLICE_START_PTR(st->recv_status_details) != NULL) { |
| 679 | grpc_slice_unref(st->recv_status_details); |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 680 | } |
Zhuochun | 97daf35 | 2016-03-13 16:19:56 +0800 | [diff] [blame] | 681 | |
| 682 | if (st->recv_message != NULL) { |
| 683 | grpc_byte_buffer_destroy(st->recv_message); |
| 684 | } |
| 685 | |
| 686 | for (i = 0; i < st->op_num; i++) { |
| 687 | if (st->ops[i].op == GRPC_OP_SEND_MESSAGE) { |
Mark D. Roth | 448c1f0 | 2017-01-25 10:44:30 -0800 | [diff] [blame] | 688 | grpc_byte_buffer_destroy(st->ops[i].data.send_message.send_message); |
Zhuochun | 97daf35 | 2016-03-13 16:19:56 +0800 | [diff] [blame] | 689 | } |
| 690 | } |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | /* grpc_run_batch_stack_fill_ops fills the run_batch_stack ops array from |
| 694 | * ops_hash */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 695 | static void grpc_run_batch_stack_fill_ops(run_batch_stack* st, VALUE ops_hash) { |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 696 | VALUE this_op = Qnil; |
| 697 | VALUE this_value = Qnil; |
| 698 | VALUE ops_ary = rb_ary_new(); |
| 699 | size_t i = 0; |
| 700 | |
| 701 | /* Create a ruby array with just the operation keys */ |
| 702 | rb_hash_foreach(ops_hash, grpc_rb_call_check_op_keys_hash_cb, ops_ary); |
| 703 | |
| 704 | /* Fill the ops array */ |
| 705 | for (i = 0; i < (size_t)RARRAY_LEN(ops_ary); i++) { |
| 706 | this_op = rb_ary_entry(ops_ary, i); |
| 707 | this_value = rb_hash_aref(ops_hash, this_op); |
Tim Emiola | 7840a55 | 2015-08-20 13:12:33 -0700 | [diff] [blame] | 708 | st->ops[st->op_num].flags = 0; |
Craig Tiller | 1a727fd | 2015-04-24 13:21:22 -0700 | [diff] [blame] | 709 | switch (NUM2INT(this_op)) { |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 710 | case GRPC_OP_SEND_INITIAL_METADATA: |
| 711 | grpc_rb_md_ary_convert(this_value, &st->send_metadata); |
| 712 | st->ops[st->op_num].data.send_initial_metadata.count = |
| 713 | st->send_metadata.count; |
| 714 | st->ops[st->op_num].data.send_initial_metadata.metadata = |
| 715 | st->send_metadata.metadata; |
| 716 | break; |
| 717 | case GRPC_OP_SEND_MESSAGE: |
Mark D. Roth | 448c1f0 | 2017-01-25 10:44:30 -0800 | [diff] [blame] | 718 | st->ops[st->op_num].data.send_message.send_message = |
| 719 | grpc_rb_s_to_byte_buffer(RSTRING_PTR(this_value), |
| 720 | RSTRING_LEN(this_value)); |
Tim Emiola | 7840a55 | 2015-08-20 13:12:33 -0700 | [diff] [blame] | 721 | st->ops[st->op_num].flags = st->write_flag; |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 722 | break; |
| 723 | case GRPC_OP_SEND_CLOSE_FROM_CLIENT: |
| 724 | break; |
| 725 | case GRPC_OP_SEND_STATUS_FROM_SERVER: |
Craig Tiller | 1a727fd | 2015-04-24 13:21:22 -0700 | [diff] [blame] | 726 | grpc_rb_op_update_status_from_server( |
Craig Tiller | 5b1c5f2 | 2017-04-19 09:52:18 -0700 | [diff] [blame] | 727 | &st->ops[st->op_num], &st->send_trailing_metadata, |
| 728 | &st->send_status_details, this_value); |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 729 | break; |
| 730 | case GRPC_OP_RECV_INITIAL_METADATA: |
Mark D. Roth | 448c1f0 | 2017-01-25 10:44:30 -0800 | [diff] [blame] | 731 | st->ops[st->op_num].data.recv_initial_metadata.recv_initial_metadata = |
| 732 | &st->recv_metadata; |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 733 | break; |
| 734 | case GRPC_OP_RECV_MESSAGE: |
Mark D. Roth | 448c1f0 | 2017-01-25 10:44:30 -0800 | [diff] [blame] | 735 | st->ops[st->op_num].data.recv_message.recv_message = &st->recv_message; |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 736 | break; |
| 737 | case GRPC_OP_RECV_STATUS_ON_CLIENT: |
| 738 | st->ops[st->op_num].data.recv_status_on_client.trailing_metadata = |
| 739 | &st->recv_trailing_metadata; |
| 740 | st->ops[st->op_num].data.recv_status_on_client.status = |
| 741 | &st->recv_status; |
| 742 | st->ops[st->op_num].data.recv_status_on_client.status_details = |
| 743 | &st->recv_status_details; |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 744 | break; |
| 745 | case GRPC_OP_RECV_CLOSE_ON_SERVER: |
| 746 | st->ops[st->op_num].data.recv_close_on_server.cancelled = |
| 747 | &st->recv_cancelled; |
| 748 | break; |
| 749 | default: |
| 750 | grpc_run_batch_stack_cleanup(st); |
| 751 | rb_raise(rb_eTypeError, "invalid operation : bad value %d", |
| 752 | NUM2INT(this_op)); |
| 753 | }; |
| 754 | st->ops[st->op_num].op = (grpc_op_type)NUM2INT(this_op); |
Craig Tiller | 4275899 | 2015-08-18 10:34:32 -0700 | [diff] [blame] | 755 | st->ops[st->op_num].reserved = NULL; |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 756 | st->op_num++; |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | /* grpc_run_batch_stack_build_result fills constructs a ruby BatchResult struct |
| 761 | after the results have run */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 762 | static VALUE grpc_run_batch_stack_build_result(run_batch_stack* st) { |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 763 | size_t i = 0; |
Yuki Yugui Sonoda | a7d369e | 2015-04-11 11:48:36 +0900 | [diff] [blame] | 764 | VALUE result = rb_struct_new(grpc_rb_sBatchResult, Qnil, Qnil, Qnil, Qnil, |
| 765 | Qnil, Qnil, Qnil, Qnil, NULL); |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 766 | for (i = 0; i < st->op_num; i++) { |
Craig Tiller | 1a727fd | 2015-04-24 13:21:22 -0700 | [diff] [blame] | 767 | switch (st->ops[i].op) { |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 768 | case GRPC_OP_SEND_INITIAL_METADATA: |
| 769 | rb_struct_aset(result, sym_send_metadata, Qtrue); |
| 770 | break; |
| 771 | case GRPC_OP_SEND_MESSAGE: |
| 772 | rb_struct_aset(result, sym_send_message, Qtrue); |
| 773 | break; |
| 774 | case GRPC_OP_SEND_CLOSE_FROM_CLIENT: |
| 775 | rb_struct_aset(result, sym_send_close, Qtrue); |
| 776 | break; |
| 777 | case GRPC_OP_SEND_STATUS_FROM_SERVER: |
| 778 | rb_struct_aset(result, sym_send_status, Qtrue); |
| 779 | break; |
| 780 | case GRPC_OP_RECV_INITIAL_METADATA: |
| 781 | rb_struct_aset(result, sym_metadata, |
| 782 | grpc_rb_md_ary_to_h(&st->recv_metadata)); |
| 783 | case GRPC_OP_RECV_MESSAGE: |
| 784 | rb_struct_aset(result, sym_message, |
| 785 | grpc_rb_byte_buffer_to_s(st->recv_message)); |
| 786 | break; |
| 787 | case GRPC_OP_RECV_STATUS_ON_CLIENT: |
| 788 | rb_struct_aset( |
Craig Tiller | 1a727fd | 2015-04-24 13:21:22 -0700 | [diff] [blame] | 789 | result, sym_status, |
Craig Tiller | 5b1c5f2 | 2017-04-19 09:52:18 -0700 | [diff] [blame] | 790 | rb_struct_new( |
| 791 | grpc_rb_sStatus, UINT2NUM(st->recv_status), |
| 792 | (GRPC_SLICE_START_PTR(st->recv_status_details) == NULL |
| 793 | ? Qnil |
| 794 | : grpc_rb_slice_to_ruby_string(st->recv_status_details)), |
| 795 | grpc_rb_md_ary_to_h(&st->recv_trailing_metadata), NULL)); |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 796 | break; |
| 797 | case GRPC_OP_RECV_CLOSE_ON_SERVER: |
| 798 | rb_struct_aset(result, sym_send_close, Qtrue); |
| 799 | break; |
| 800 | default: |
| 801 | break; |
| 802 | } |
| 803 | } |
| 804 | return result; |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | /* call-seq: |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 808 | ops = { |
| 809 | GRPC::Core::CallOps::SEND_INITIAL_METADATA => <op_value>, |
| 810 | GRPC::Core::CallOps::SEND_MESSAGE => <op_value>, |
| 811 | ... |
| 812 | } |
| 813 | tag = Object.new |
| 814 | timeout = 10 |
murgatroid99 | ec1588b | 2016-06-06 15:37:45 -0700 | [diff] [blame] | 815 | call.start_batch(tag, timeout, ops) |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 816 | |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 817 | Start a batch of operations defined in the array ops; when complete, post a |
| 818 | completion of type 'tag' to the completion queue bound to the call. |
temiola | 71bb137 | 2014-12-11 11:27:25 -0800 | [diff] [blame] | 819 | |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 820 | Also waits for the batch to complete, until timeout is reached. |
| 821 | The order of ops specified in the batch has no significance. |
| 822 | Only one operation of each type can be active at once in any given |
| 823 | batch */ |
murgatroid99 | ec1588b | 2016-06-06 15:37:45 -0700 | [diff] [blame] | 824 | static VALUE grpc_rb_call_run_batch(VALUE self, VALUE ops_hash) { |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 825 | run_batch_stack* st = NULL; |
| 826 | grpc_rb_call* call = NULL; |
Craig Tiller | c7df2b8 | 2015-05-11 11:12:18 -0700 | [diff] [blame] | 827 | grpc_event ev; |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 828 | grpc_call_error err; |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 829 | VALUE result = Qnil; |
Tim Emiola | 7840a55 | 2015-08-20 13:12:33 -0700 | [diff] [blame] | 830 | VALUE rb_write_flag = rb_ivar_get(self, id_write_flag); |
Nicolas "Pixel" Noble | 7b92db6 | 2016-01-26 22:46:34 +0100 | [diff] [blame] | 831 | unsigned write_flag = 0; |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 832 | void* tag = (void*)&st; |
Alexander Polcyn | 34bb6df | 2017-03-23 22:18:02 -0700 | [diff] [blame] | 833 | |
Ken Payson | dce1ee6 | 2016-05-20 10:29:34 -0700 | [diff] [blame] | 834 | if (RTYPEDDATA_DATA(self) == NULL) { |
| 835 | rb_raise(grpc_rb_eCallError, "Cannot run batch on closed call"); |
| 836 | return Qnil; |
| 837 | } |
murgatroid99 | ec1588b | 2016-06-06 15:37:45 -0700 | [diff] [blame] | 838 | TypedData_Get_Struct(self, grpc_rb_call, &grpc_call_data_type, call); |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 839 | |
| 840 | /* Validate the ops args, adding them to a ruby array */ |
| 841 | if (TYPE(ops_hash) != T_HASH) { |
| 842 | rb_raise(rb_eTypeError, "call#run_batch: ops hash should be a hash"); |
| 843 | return Qnil; |
temiola | 71bb137 | 2014-12-11 11:27:25 -0800 | [diff] [blame] | 844 | } |
Tim Emiola | 7840a55 | 2015-08-20 13:12:33 -0700 | [diff] [blame] | 845 | if (rb_write_flag != Qnil) { |
| 846 | write_flag = NUM2UINT(rb_write_flag); |
| 847 | } |
Alexander Polcyn | 8d8dce8 | 2017-03-24 10:32:15 -0700 | [diff] [blame] | 848 | st = gpr_malloc(sizeof(run_batch_stack)); |
Alexander Polcyn | 34bb6df | 2017-03-23 22:18:02 -0700 | [diff] [blame] | 849 | grpc_run_batch_stack_init(st, write_flag); |
| 850 | grpc_run_batch_stack_fill_ops(st, ops_hash); |
temiola | 71bb137 | 2014-12-11 11:27:25 -0800 | [diff] [blame] | 851 | |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 852 | /* call grpc_call_start_batch, then wait for it to complete using |
| 853 | * pluck_event */ |
Alexander Polcyn | 34bb6df | 2017-03-23 22:18:02 -0700 | [diff] [blame] | 854 | err = grpc_call_start_batch(call->wrapped, st->ops, st->op_num, tag, NULL); |
temiola | 71bb137 | 2014-12-11 11:27:25 -0800 | [diff] [blame] | 855 | if (err != GRPC_CALL_OK) { |
Alexander Polcyn | 34bb6df | 2017-03-23 22:18:02 -0700 | [diff] [blame] | 856 | grpc_run_batch_stack_cleanup(st); |
| 857 | gpr_free(st); |
Yuki Yugui Sonoda | a7d369e | 2015-04-11 11:48:36 +0900 | [diff] [blame] | 858 | rb_raise(grpc_rb_eCallError, |
| 859 | "grpc_call_start_batch failed with %s (code=%d)", |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 860 | grpc_call_error_detail_of(err), err); |
Tim Emiola | 932b13b | 2015-04-24 08:52:46 -0700 | [diff] [blame] | 861 | return Qnil; |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 862 | } |
murgatroid99 | 5ea4a99 | 2016-06-13 10:36:41 -0700 | [diff] [blame] | 863 | ev = rb_completion_queue_pluck(call->queue, tag, |
| 864 | gpr_inf_future(GPR_CLOCK_REALTIME), NULL); |
| 865 | if (!ev.success) { |
| 866 | rb_raise(grpc_rb_eCallError, "call#run_batch failed somehow"); |
| 867 | } |
Tim Emiola | cdb7ccc | 2015-08-13 21:45:04 -0700 | [diff] [blame] | 868 | /* Build and return the BatchResult struct result, |
| 869 | if there is an error, it's reflected in the status */ |
Alexander Polcyn | 34bb6df | 2017-03-23 22:18:02 -0700 | [diff] [blame] | 870 | result = grpc_run_batch_stack_build_result(st); |
| 871 | grpc_run_batch_stack_cleanup(st); |
| 872 | gpr_free(st); |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 873 | return result; |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 874 | } |
| 875 | |
Tim Emiola | 7840a55 | 2015-08-20 13:12:33 -0700 | [diff] [blame] | 876 | static void Init_grpc_write_flags() { |
| 877 | /* Constants representing the write flags in grpc.h */ |
| 878 | VALUE grpc_rb_mWriteFlags = |
| 879 | rb_define_module_under(grpc_rb_mGrpcCore, "WriteFlags"); |
| 880 | rb_define_const(grpc_rb_mWriteFlags, "BUFFER_HINT", |
| 881 | UINT2NUM(GRPC_WRITE_BUFFER_HINT)); |
| 882 | rb_define_const(grpc_rb_mWriteFlags, "NO_COMPRESS", |
| 883 | UINT2NUM(GRPC_WRITE_NO_COMPRESS)); |
| 884 | } |
| 885 | |
Yuki Yugui Sonoda | f0eee5f | 2015-04-16 20:25:28 +0900 | [diff] [blame] | 886 | static void Init_grpc_error_codes() { |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 887 | /* Constants representing the error codes of grpc_call_error in grpc.h */ |
Yuki Yugui Sonoda | a7d369e | 2015-04-11 11:48:36 +0900 | [diff] [blame] | 888 | VALUE grpc_rb_mRpcErrors = |
| 889 | rb_define_module_under(grpc_rb_mGrpcCore, "RpcErrors"); |
| 890 | rb_define_const(grpc_rb_mRpcErrors, "OK", UINT2NUM(GRPC_CALL_OK)); |
| 891 | rb_define_const(grpc_rb_mRpcErrors, "ERROR", UINT2NUM(GRPC_CALL_ERROR)); |
| 892 | rb_define_const(grpc_rb_mRpcErrors, "NOT_ON_SERVER", |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 893 | UINT2NUM(GRPC_CALL_ERROR_NOT_ON_SERVER)); |
Yuki Yugui Sonoda | a7d369e | 2015-04-11 11:48:36 +0900 | [diff] [blame] | 894 | rb_define_const(grpc_rb_mRpcErrors, "NOT_ON_CLIENT", |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 895 | UINT2NUM(GRPC_CALL_ERROR_NOT_ON_CLIENT)); |
Yuki Yugui Sonoda | a7d369e | 2015-04-11 11:48:36 +0900 | [diff] [blame] | 896 | rb_define_const(grpc_rb_mRpcErrors, "ALREADY_ACCEPTED", |
temiola | 71bb137 | 2014-12-11 11:27:25 -0800 | [diff] [blame] | 897 | UINT2NUM(GRPC_CALL_ERROR_ALREADY_ACCEPTED)); |
Yuki Yugui Sonoda | a7d369e | 2015-04-11 11:48:36 +0900 | [diff] [blame] | 898 | rb_define_const(grpc_rb_mRpcErrors, "ALREADY_INVOKED", |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 899 | UINT2NUM(GRPC_CALL_ERROR_ALREADY_INVOKED)); |
Yuki Yugui Sonoda | a7d369e | 2015-04-11 11:48:36 +0900 | [diff] [blame] | 900 | rb_define_const(grpc_rb_mRpcErrors, "NOT_INVOKED", |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 901 | UINT2NUM(GRPC_CALL_ERROR_NOT_INVOKED)); |
Yuki Yugui Sonoda | a7d369e | 2015-04-11 11:48:36 +0900 | [diff] [blame] | 902 | rb_define_const(grpc_rb_mRpcErrors, "ALREADY_FINISHED", |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 903 | UINT2NUM(GRPC_CALL_ERROR_ALREADY_FINISHED)); |
Yuki Yugui Sonoda | a7d369e | 2015-04-11 11:48:36 +0900 | [diff] [blame] | 904 | rb_define_const(grpc_rb_mRpcErrors, "TOO_MANY_OPERATIONS", |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 905 | UINT2NUM(GRPC_CALL_ERROR_TOO_MANY_OPERATIONS)); |
Yuki Yugui Sonoda | a7d369e | 2015-04-11 11:48:36 +0900 | [diff] [blame] | 906 | rb_define_const(grpc_rb_mRpcErrors, "INVALID_FLAGS", |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 907 | UINT2NUM(GRPC_CALL_ERROR_INVALID_FLAGS)); |
| 908 | |
Andre Medeiros | b0eae8a | 2017-08-21 15:50:36 -0400 | [diff] [blame] | 909 | /* Hint the GC that this is a global and shouldn't be sweeped. */ |
| 910 | rb_global_variable(&rb_error_code_details); |
| 911 | |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 912 | /* Add the detail strings to a Hash */ |
| 913 | rb_error_code_details = rb_hash_new(); |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 914 | rb_hash_aset(rb_error_code_details, UINT2NUM(GRPC_CALL_OK), |
| 915 | rb_str_new2("ok")); |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 916 | rb_hash_aset(rb_error_code_details, UINT2NUM(GRPC_CALL_ERROR), |
| 917 | rb_str_new2("unknown error")); |
| 918 | rb_hash_aset(rb_error_code_details, UINT2NUM(GRPC_CALL_ERROR_NOT_ON_SERVER), |
| 919 | rb_str_new2("not available on a server")); |
| 920 | rb_hash_aset(rb_error_code_details, UINT2NUM(GRPC_CALL_ERROR_NOT_ON_CLIENT), |
| 921 | rb_str_new2("not available on a client")); |
temiola | 71bb137 | 2014-12-11 11:27:25 -0800 | [diff] [blame] | 922 | rb_hash_aset(rb_error_code_details, |
| 923 | UINT2NUM(GRPC_CALL_ERROR_ALREADY_ACCEPTED), |
| 924 | rb_str_new2("call is already accepted")); |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 925 | rb_hash_aset(rb_error_code_details, UINT2NUM(GRPC_CALL_ERROR_ALREADY_INVOKED), |
| 926 | rb_str_new2("call is already invoked")); |
| 927 | rb_hash_aset(rb_error_code_details, UINT2NUM(GRPC_CALL_ERROR_NOT_INVOKED), |
| 928 | rb_str_new2("call is not yet invoked")); |
| 929 | rb_hash_aset(rb_error_code_details, |
| 930 | UINT2NUM(GRPC_CALL_ERROR_ALREADY_FINISHED), |
| 931 | rb_str_new2("call is already finished")); |
| 932 | rb_hash_aset(rb_error_code_details, |
| 933 | UINT2NUM(GRPC_CALL_ERROR_TOO_MANY_OPERATIONS), |
| 934 | rb_str_new2("outstanding read or write present")); |
| 935 | rb_hash_aset(rb_error_code_details, UINT2NUM(GRPC_CALL_ERROR_INVALID_FLAGS), |
| 936 | rb_str_new2("a bad flag was given")); |
Yuki Yugui Sonoda | a7d369e | 2015-04-11 11:48:36 +0900 | [diff] [blame] | 937 | rb_define_const(grpc_rb_mRpcErrors, "ErrorMessages", rb_error_code_details); |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 938 | rb_obj_freeze(rb_error_code_details); |
| 939 | } |
| 940 | |
Yuki Yugui Sonoda | f0eee5f | 2015-04-16 20:25:28 +0900 | [diff] [blame] | 941 | static void Init_grpc_op_codes() { |
Tim Emiola | 37b09f4 | 2015-03-27 13:39:16 -0700 | [diff] [blame] | 942 | /* Constants representing operation type codes in grpc.h */ |
Craig Tiller | 1a727fd | 2015-04-24 13:21:22 -0700 | [diff] [blame] | 943 | VALUE grpc_rb_mCallOps = rb_define_module_under(grpc_rb_mGrpcCore, "CallOps"); |
Yuki Yugui Sonoda | 2e50a7a | 2015-04-11 12:01:58 +0900 | [diff] [blame] | 944 | rb_define_const(grpc_rb_mCallOps, "SEND_INITIAL_METADATA", |
Tim Emiola | 37b09f4 | 2015-03-27 13:39:16 -0700 | [diff] [blame] | 945 | UINT2NUM(GRPC_OP_SEND_INITIAL_METADATA)); |
Yuki Yugui Sonoda | 2e50a7a | 2015-04-11 12:01:58 +0900 | [diff] [blame] | 946 | rb_define_const(grpc_rb_mCallOps, "SEND_MESSAGE", |
| 947 | UINT2NUM(GRPC_OP_SEND_MESSAGE)); |
| 948 | rb_define_const(grpc_rb_mCallOps, "SEND_CLOSE_FROM_CLIENT", |
Tim Emiola | 37b09f4 | 2015-03-27 13:39:16 -0700 | [diff] [blame] | 949 | UINT2NUM(GRPC_OP_SEND_CLOSE_FROM_CLIENT)); |
Yuki Yugui Sonoda | 2e50a7a | 2015-04-11 12:01:58 +0900 | [diff] [blame] | 950 | rb_define_const(grpc_rb_mCallOps, "SEND_STATUS_FROM_SERVER", |
Tim Emiola | 37b09f4 | 2015-03-27 13:39:16 -0700 | [diff] [blame] | 951 | UINT2NUM(GRPC_OP_SEND_STATUS_FROM_SERVER)); |
Yuki Yugui Sonoda | 2e50a7a | 2015-04-11 12:01:58 +0900 | [diff] [blame] | 952 | rb_define_const(grpc_rb_mCallOps, "RECV_INITIAL_METADATA", |
Tim Emiola | 37b09f4 | 2015-03-27 13:39:16 -0700 | [diff] [blame] | 953 | UINT2NUM(GRPC_OP_RECV_INITIAL_METADATA)); |
Yuki Yugui Sonoda | 2e50a7a | 2015-04-11 12:01:58 +0900 | [diff] [blame] | 954 | rb_define_const(grpc_rb_mCallOps, "RECV_MESSAGE", |
Tim Emiola | 37b09f4 | 2015-03-27 13:39:16 -0700 | [diff] [blame] | 955 | UINT2NUM(GRPC_OP_RECV_MESSAGE)); |
Yuki Yugui Sonoda | 2e50a7a | 2015-04-11 12:01:58 +0900 | [diff] [blame] | 956 | rb_define_const(grpc_rb_mCallOps, "RECV_STATUS_ON_CLIENT", |
Tim Emiola | 37b09f4 | 2015-03-27 13:39:16 -0700 | [diff] [blame] | 957 | UINT2NUM(GRPC_OP_RECV_STATUS_ON_CLIENT)); |
Yuki Yugui Sonoda | 2e50a7a | 2015-04-11 12:01:58 +0900 | [diff] [blame] | 958 | rb_define_const(grpc_rb_mCallOps, "RECV_CLOSE_ON_SERVER", |
Tim Emiola | 37b09f4 | 2015-03-27 13:39:16 -0700 | [diff] [blame] | 959 | UINT2NUM(GRPC_OP_RECV_CLOSE_ON_SERVER)); |
| 960 | } |
| 961 | |
Alexander Polcyn | cdff92f | 2016-07-12 12:30:30 -0700 | [diff] [blame] | 962 | static void Init_grpc_metadata_keys() { |
Craig Tiller | 5b1c5f2 | 2017-04-19 09:52:18 -0700 | [diff] [blame] | 963 | VALUE grpc_rb_mMetadataKeys = |
| 964 | rb_define_module_under(grpc_rb_mGrpcCore, "MetadataKeys"); |
Alexander Polcyn | cdff92f | 2016-07-12 12:30:30 -0700 | [diff] [blame] | 965 | rb_define_const(grpc_rb_mMetadataKeys, "COMPRESSION_REQUEST_ALGORITHM", |
| 966 | rb_str_new2(GRPC_COMPRESSION_REQUEST_ALGORITHM_MD_KEY)); |
| 967 | } |
| 968 | |
Tim Emiola | 409e6c8 | 2015-02-17 17:46:35 -0800 | [diff] [blame] | 969 | void Init_grpc_call() { |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 970 | /* CallError inherits from Exception to signal that it is non-recoverable */ |
Yuki Yugui Sonoda | a7d369e | 2015-04-11 11:48:36 +0900 | [diff] [blame] | 971 | grpc_rb_eCallError = |
| 972 | rb_define_class_under(grpc_rb_mGrpcCore, "CallError", rb_eException); |
| 973 | grpc_rb_eOutOfTime = |
| 974 | rb_define_class_under(grpc_rb_mGrpcCore, "OutOfTime", rb_eException); |
| 975 | grpc_rb_cCall = rb_define_class_under(grpc_rb_mGrpcCore, "Call", rb_cObject); |
Craig Tiller | 1a727fd | 2015-04-24 13:21:22 -0700 | [diff] [blame] | 976 | grpc_rb_cMdAry = |
| 977 | rb_define_class_under(grpc_rb_mGrpcCore, "MetadataArray", rb_cObject); |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 978 | |
| 979 | /* Prevent allocation or inialization of the Call class */ |
Yuki Yugui Sonoda | a7d369e | 2015-04-11 11:48:36 +0900 | [diff] [blame] | 980 | rb_define_alloc_func(grpc_rb_cCall, grpc_rb_cannot_alloc); |
| 981 | rb_define_method(grpc_rb_cCall, "initialize", grpc_rb_cannot_init, 0); |
Craig Tiller | 1a727fd | 2015-04-24 13:21:22 -0700 | [diff] [blame] | 982 | rb_define_method(grpc_rb_cCall, "initialize_copy", grpc_rb_cannot_init_copy, |
| 983 | 1); |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 984 | |
| 985 | /* Add ruby analogues of the Call methods. */ |
murgatroid99 | ec1588b | 2016-06-06 15:37:45 -0700 | [diff] [blame] | 986 | rb_define_method(grpc_rb_cCall, "run_batch", grpc_rb_call_run_batch, 1); |
Yuki Yugui Sonoda | a7d369e | 2015-04-11 11:48:36 +0900 | [diff] [blame] | 987 | rb_define_method(grpc_rb_cCall, "cancel", grpc_rb_call_cancel, 0); |
Alexander Polcyn | c6627ca | 2017-04-13 10:47:16 -0700 | [diff] [blame] | 988 | rb_define_method(grpc_rb_cCall, "cancel_with_status", |
| 989 | grpc_rb_call_cancel_with_status, 2); |
Ken Payson | dce1ee6 | 2016-05-20 10:29:34 -0700 | [diff] [blame] | 990 | rb_define_method(grpc_rb_cCall, "close", grpc_rb_call_close, 0); |
Tim Emiola | 623a74d | 2015-08-11 09:24:20 -0700 | [diff] [blame] | 991 | rb_define_method(grpc_rb_cCall, "peer", grpc_rb_call_get_peer, 0); |
Eric Richardson | 69d735a | 2016-04-20 13:23:32 -0400 | [diff] [blame] | 992 | rb_define_method(grpc_rb_cCall, "peer_cert", grpc_rb_call_get_peer_cert, 0); |
Yuki Yugui Sonoda | a7d369e | 2015-04-11 11:48:36 +0900 | [diff] [blame] | 993 | rb_define_method(grpc_rb_cCall, "status", grpc_rb_call_get_status, 0); |
| 994 | rb_define_method(grpc_rb_cCall, "status=", grpc_rb_call_set_status, 1); |
| 995 | rb_define_method(grpc_rb_cCall, "metadata", grpc_rb_call_get_metadata, 0); |
| 996 | rb_define_method(grpc_rb_cCall, "metadata=", grpc_rb_call_set_metadata, 1); |
murgatroid99 | e69f088 | 2016-07-07 15:52:27 -0700 | [diff] [blame] | 997 | rb_define_method(grpc_rb_cCall, "trailing_metadata", |
| 998 | grpc_rb_call_get_trailing_metadata, 0); |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 999 | rb_define_method(grpc_rb_cCall, |
| 1000 | "trailing_metadata=", grpc_rb_call_set_trailing_metadata, 1); |
Tim Emiola | 7840a55 | 2015-08-20 13:12:33 -0700 | [diff] [blame] | 1001 | rb_define_method(grpc_rb_cCall, "write_flag", grpc_rb_call_get_write_flag, 0); |
| 1002 | rb_define_method(grpc_rb_cCall, "write_flag=", grpc_rb_call_set_write_flag, |
| 1003 | 1); |
murgatroid99 | 9946f2b | 2015-12-04 14:36:27 -0800 | [diff] [blame] | 1004 | rb_define_method(grpc_rb_cCall, "set_credentials!", |
| 1005 | grpc_rb_call_set_credentials, 1); |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 1006 | |
| 1007 | /* Ids used to support call attributes */ |
| 1008 | id_metadata = rb_intern("metadata"); |
murgatroid99 | e69f088 | 2016-07-07 15:52:27 -0700 | [diff] [blame] | 1009 | id_trailing_metadata = rb_intern("trailing_metadata"); |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 1010 | id_status = rb_intern("status"); |
Tim Emiola | 7840a55 | 2015-08-20 13:12:33 -0700 | [diff] [blame] | 1011 | id_write_flag = rb_intern("write_flag"); |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 1012 | |
| 1013 | /* Ids used by the c wrapping internals. */ |
murgatroid99 | 8c9edc2 | 2016-03-14 15:51:56 -0700 | [diff] [blame] | 1014 | id_credentials = rb_intern("__credentials"); |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 1015 | |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 1016 | /* Ids used in constructing the batch result. */ |
| 1017 | sym_send_message = ID2SYM(rb_intern("send_message")); |
| 1018 | sym_send_metadata = ID2SYM(rb_intern("send_metadata")); |
| 1019 | sym_send_close = ID2SYM(rb_intern("send_close")); |
| 1020 | sym_send_status = ID2SYM(rb_intern("send_status")); |
| 1021 | sym_message = ID2SYM(rb_intern("message")); |
| 1022 | sym_status = ID2SYM(rb_intern("status")); |
| 1023 | sym_cancelled = ID2SYM(rb_intern("cancelled")); |
| 1024 | |
| 1025 | /* The Struct used to return the run_batch result. */ |
Yuki Yugui Sonoda | a7d369e | 2015-04-11 11:48:36 +0900 | [diff] [blame] | 1026 | grpc_rb_sBatchResult = rb_struct_define( |
Craig Tiller | 1a727fd | 2015-04-24 13:21:22 -0700 | [diff] [blame] | 1027 | "BatchResult", "send_message", "send_metadata", "send_close", |
| 1028 | "send_status", "message", "metadata", "status", "cancelled", NULL); |
Tim Emiola | 1dae4ac | 2015-03-27 17:17:37 -0700 | [diff] [blame] | 1029 | |
Tim Emiola | 409e6c8 | 2015-02-17 17:46:35 -0800 | [diff] [blame] | 1030 | Init_grpc_error_codes(); |
Tim Emiola | 37b09f4 | 2015-03-27 13:39:16 -0700 | [diff] [blame] | 1031 | Init_grpc_op_codes(); |
Tim Emiola | 7840a55 | 2015-08-20 13:12:33 -0700 | [diff] [blame] | 1032 | Init_grpc_write_flags(); |
Alexander Polcyn | cdff92f | 2016-07-12 12:30:30 -0700 | [diff] [blame] | 1033 | Init_grpc_metadata_keys(); |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 1034 | } |
| 1035 | |
| 1036 | /* Gets the call from the ruby object */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1037 | grpc_call* grpc_rb_get_wrapped_call(VALUE v) { |
| 1038 | grpc_rb_call* call = NULL; |
murgatroid99 | ec1588b | 2016-06-06 15:37:45 -0700 | [diff] [blame] | 1039 | TypedData_Get_Struct(v, grpc_rb_call, &grpc_call_data_type, call); |
| 1040 | return call->wrapped; |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 1041 | } |
| 1042 | |
| 1043 | /* Obtains the wrapped object for a given call */ |
Craig Tiller | baa14a9 | 2017-11-03 09:09:36 -0700 | [diff] [blame] | 1044 | VALUE grpc_rb_wrap_call(grpc_call* c, grpc_completion_queue* q) { |
| 1045 | grpc_rb_call* wrapper; |
murgatroid99 | ec1588b | 2016-06-06 15:37:45 -0700 | [diff] [blame] | 1046 | if (c == NULL || q == NULL) { |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 1047 | return Qnil; |
| 1048 | } |
murgatroid99 | 5ea4a99 | 2016-06-13 10:36:41 -0700 | [diff] [blame] | 1049 | wrapper = ALLOC(grpc_rb_call); |
murgatroid99 | ec1588b | 2016-06-06 15:37:45 -0700 | [diff] [blame] | 1050 | wrapper->wrapped = c; |
| 1051 | wrapper->queue = q; |
| 1052 | return TypedData_Wrap_Struct(grpc_rb_cCall, &grpc_call_data_type, wrapper); |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 1053 | } |