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