nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
Nicolas "Pixel" Noble | d51d121 | 2016-01-31 11:33:19 +0100 | [diff] [blame] | 34 | #include <ruby/ruby.h> |
Nicolas "Pixel" Noble | 9fcdc87 | 2016-05-05 06:15:34 +0200 | [diff] [blame] | 35 | |
Nicolas "Pixel" Noble | d51d121 | 2016-01-31 11:33:19 +0100 | [diff] [blame] | 36 | #include "rb_grpc_imports.generated.h" |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 37 | #include "rb_grpc.h" |
| 38 | |
| 39 | #include <math.h> |
Yuki Yugui Sonoda | 2288791 | 2015-04-16 20:57:17 +0900 | [diff] [blame] | 40 | #include <ruby/vm.h> |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 41 | #include <sys/time.h> |
| 42 | |
| 43 | #include <grpc/grpc.h> |
| 44 | #include <grpc/support/time.h> |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 45 | #include "rb_call.h" |
murgatroid99 | 9946f2b | 2015-12-04 14:36:27 -0800 | [diff] [blame] | 46 | #include "rb_call_credentials.h" |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 47 | #include "rb_channel.h" |
Tim Emiola | 9332ea6 | 2015-10-27 23:48:29 -0700 | [diff] [blame] | 48 | #include "rb_channel_credentials.h" |
Nicolas "Pixel" Noble | d51d121 | 2016-01-31 11:33:19 +0100 | [diff] [blame] | 49 | #include "rb_loader.h" |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 50 | #include "rb_server.h" |
nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 51 | #include "rb_server_credentials.h" |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 52 | |
Yuki Yugui Sonoda | 3c88e5d | 2015-04-16 20:09:00 +0900 | [diff] [blame] | 53 | static VALUE grpc_rb_cTimeVal = Qnil; |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 54 | |
Yuki Yugui Sonoda | d441c2e | 2015-04-11 15:33:58 +0900 | [diff] [blame] | 55 | static rb_data_type_t grpc_rb_timespec_data_type = { |
| 56 | "gpr_timespec", |
murgatroid99 | 87afb5d | 2015-07-16 16:01:02 -0700 | [diff] [blame] | 57 | {GRPC_RB_GC_NOT_MARKED, GRPC_RB_GC_DONT_FREE, GRPC_RB_MEMSIZE_UNAVAILABLE, |
| 58 | {NULL, NULL}}, |
Yuki Yugui Sonoda | d441c2e | 2015-04-11 15:33:58 +0900 | [diff] [blame] | 59 | NULL, |
| 60 | NULL, |
Tim Emiola | 9161a82 | 2015-11-11 15:58:44 -0800 | [diff] [blame] | 61 | #ifdef RUBY_TYPED_FREE_IMMEDIATELY |
| 62 | RUBY_TYPED_FREE_IMMEDIATELY |
| 63 | #endif |
| 64 | }; |
Yuki Yugui Sonoda | d441c2e | 2015-04-11 15:33:58 +0900 | [diff] [blame] | 65 | |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 66 | /* Alloc func that blocks allocation of a given object by raising an |
| 67 | * exception. */ |
| 68 | VALUE grpc_rb_cannot_alloc(VALUE cls) { |
| 69 | rb_raise(rb_eTypeError, |
| 70 | "allocation of %s only allowed from the gRPC native layer", |
| 71 | rb_class2name(cls)); |
| 72 | return Qnil; |
| 73 | } |
| 74 | |
| 75 | /* Init func that fails by raising an exception. */ |
| 76 | VALUE grpc_rb_cannot_init(VALUE self) { |
| 77 | rb_raise(rb_eTypeError, |
| 78 | "initialization of %s only allowed from the gRPC native layer", |
| 79 | rb_obj_classname(self)); |
| 80 | return Qnil; |
| 81 | } |
| 82 | |
| 83 | /* Init/Clone func that fails by raising an exception. */ |
| 84 | VALUE grpc_rb_cannot_init_copy(VALUE copy, VALUE self) { |
murgatroid99 | 87afb5d | 2015-07-16 16:01:02 -0700 | [diff] [blame] | 85 | (void)self; |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 86 | rb_raise(rb_eTypeError, |
murgatroid99 | cc0f4e1 | 2016-06-02 16:00:54 -0700 | [diff] [blame] | 87 | "Copy initialization of %s is not supported", |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 88 | rb_obj_classname(copy)); |
| 89 | return Qnil; |
| 90 | } |
| 91 | |
| 92 | /* id_tv_{,u}sec are accessor methods on Ruby Time instances. */ |
| 93 | static ID id_tv_sec; |
| 94 | static ID id_tv_nsec; |
| 95 | |
| 96 | /** |
Jan Tattermusch | 8808637 | 2015-12-10 10:54:12 -0800 | [diff] [blame] | 97 | * grpc_rb_time_timeval creates a timeval from a ruby time object. |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 98 | * |
| 99 | * This func is copied from ruby source, MRI/source/time.c, which is published |
| 100 | * under the same license as the ruby.h, on which the entire extensions is |
| 101 | * based. |
| 102 | */ |
| 103 | gpr_timespec grpc_rb_time_timeval(VALUE time, int interval) { |
| 104 | gpr_timespec t; |
| 105 | gpr_timespec *time_const; |
| 106 | const char *tstr = interval ? "time interval" : "time"; |
| 107 | const char *want = " want <secs from epoch>|<Time>|<GRPC::TimeConst.*>"; |
| 108 | |
Craig Tiller | 5a1e7fd | 2015-07-14 07:14:47 -0700 | [diff] [blame] | 109 | t.clock_type = GPR_CLOCK_REALTIME; |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 110 | switch (TYPE(time)) { |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 111 | case T_DATA: |
Yuki Yugui Sonoda | a7d369e | 2015-04-11 11:48:36 +0900 | [diff] [blame] | 112 | if (CLASS_OF(time) == grpc_rb_cTimeVal) { |
Yuki Yugui Sonoda | d441c2e | 2015-04-11 15:33:58 +0900 | [diff] [blame] | 113 | TypedData_Get_Struct(time, gpr_timespec, &grpc_rb_timespec_data_type, |
| 114 | time_const); |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 115 | t = *time_const; |
| 116 | } else if (CLASS_OF(time) == rb_cTime) { |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 117 | t.tv_sec = NUM2INT(rb_funcall(time, id_tv_sec, 0)); |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 118 | t.tv_nsec = NUM2INT(rb_funcall(time, id_tv_nsec, 0)); |
| 119 | } else { |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 120 | rb_raise(rb_eTypeError, "bad input: (%s)->c_timeval, got <%s>,%s", tstr, |
| 121 | rb_obj_classname(time), want); |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 122 | } |
| 123 | break; |
| 124 | |
| 125 | case T_FIXNUM: |
| 126 | t.tv_sec = FIX2LONG(time); |
| 127 | if (interval && t.tv_sec < 0) |
| 128 | rb_raise(rb_eArgError, "%s must be positive", tstr); |
| 129 | t.tv_nsec = 0; |
| 130 | break; |
| 131 | |
| 132 | case T_FLOAT: |
mattn | b9e1563 | 2015-02-28 23:45:58 +0900 | [diff] [blame] | 133 | if (interval && RFLOAT_VALUE(time) < 0.0) |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 134 | rb_raise(rb_eArgError, "%s must be positive", tstr); |
| 135 | else { |
| 136 | double f, d; |
| 137 | |
mattn | b9e1563 | 2015-02-28 23:45:58 +0900 | [diff] [blame] | 138 | d = modf(RFLOAT_VALUE(time), &f); |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 139 | if (d < 0) { |
| 140 | d += 1; |
| 141 | f -= 1; |
| 142 | } |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 143 | t.tv_sec = (int64_t)f; |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 144 | if (f != t.tv_sec) { |
| 145 | rb_raise(rb_eRangeError, "%f out of Time range", |
mattn | b9e1563 | 2015-02-28 23:45:58 +0900 | [diff] [blame] | 146 | RFLOAT_VALUE(time)); |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 147 | } |
Marcin Wyszynski | 1a2ac33 | 2015-07-23 20:12:33 +0200 | [diff] [blame] | 148 | t.tv_nsec = (int)(d * 1e9 + 0.5); |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 149 | } |
| 150 | break; |
| 151 | |
| 152 | case T_BIGNUM: |
| 153 | t.tv_sec = NUM2LONG(time); |
| 154 | if (interval && t.tv_sec < 0) |
| 155 | rb_raise(rb_eArgError, "%s must be positive", tstr); |
| 156 | t.tv_nsec = 0; |
| 157 | break; |
| 158 | |
| 159 | default: |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 160 | rb_raise(rb_eTypeError, "bad input: (%s)->c_timeval, got <%s>,%s", tstr, |
| 161 | rb_obj_classname(time), want); |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 162 | break; |
| 163 | } |
| 164 | return t; |
| 165 | } |
| 166 | |
Yuki Yugui Sonoda | f0eee5f | 2015-04-16 20:25:28 +0900 | [diff] [blame] | 167 | static void Init_grpc_status_codes() { |
temiola | 5832791 | 2014-12-15 17:51:16 -0800 | [diff] [blame] | 168 | /* Constants representing the status codes or grpc_status_code in status.h */ |
Yuki Yugui Sonoda | a7d369e | 2015-04-11 11:48:36 +0900 | [diff] [blame] | 169 | VALUE grpc_rb_mStatusCodes = |
| 170 | rb_define_module_under(grpc_rb_mGrpcCore, "StatusCodes"); |
| 171 | rb_define_const(grpc_rb_mStatusCodes, "OK", INT2NUM(GRPC_STATUS_OK)); |
| 172 | rb_define_const(grpc_rb_mStatusCodes, "CANCELLED", |
| 173 | INT2NUM(GRPC_STATUS_CANCELLED)); |
| 174 | rb_define_const(grpc_rb_mStatusCodes, "UNKNOWN", |
| 175 | INT2NUM(GRPC_STATUS_UNKNOWN)); |
| 176 | rb_define_const(grpc_rb_mStatusCodes, "INVALID_ARGUMENT", |
temiola | 5832791 | 2014-12-15 17:51:16 -0800 | [diff] [blame] | 177 | INT2NUM(GRPC_STATUS_INVALID_ARGUMENT)); |
Yuki Yugui Sonoda | a7d369e | 2015-04-11 11:48:36 +0900 | [diff] [blame] | 178 | rb_define_const(grpc_rb_mStatusCodes, "DEADLINE_EXCEEDED", |
temiola | 5832791 | 2014-12-15 17:51:16 -0800 | [diff] [blame] | 179 | INT2NUM(GRPC_STATUS_DEADLINE_EXCEEDED)); |
Yuki Yugui Sonoda | a7d369e | 2015-04-11 11:48:36 +0900 | [diff] [blame] | 180 | rb_define_const(grpc_rb_mStatusCodes, "NOT_FOUND", |
| 181 | INT2NUM(GRPC_STATUS_NOT_FOUND)); |
| 182 | rb_define_const(grpc_rb_mStatusCodes, "ALREADY_EXISTS", |
temiola | 5832791 | 2014-12-15 17:51:16 -0800 | [diff] [blame] | 183 | INT2NUM(GRPC_STATUS_ALREADY_EXISTS)); |
Yuki Yugui Sonoda | a7d369e | 2015-04-11 11:48:36 +0900 | [diff] [blame] | 184 | rb_define_const(grpc_rb_mStatusCodes, "PERMISSION_DENIED", |
temiola | 5832791 | 2014-12-15 17:51:16 -0800 | [diff] [blame] | 185 | INT2NUM(GRPC_STATUS_PERMISSION_DENIED)); |
Yuki Yugui Sonoda | a7d369e | 2015-04-11 11:48:36 +0900 | [diff] [blame] | 186 | rb_define_const(grpc_rb_mStatusCodes, "UNAUTHENTICATED", |
temiola | 5832791 | 2014-12-15 17:51:16 -0800 | [diff] [blame] | 187 | INT2NUM(GRPC_STATUS_UNAUTHENTICATED)); |
Yuki Yugui Sonoda | a7d369e | 2015-04-11 11:48:36 +0900 | [diff] [blame] | 188 | rb_define_const(grpc_rb_mStatusCodes, "RESOURCE_EXHAUSTED", |
temiola | 5832791 | 2014-12-15 17:51:16 -0800 | [diff] [blame] | 189 | INT2NUM(GRPC_STATUS_RESOURCE_EXHAUSTED)); |
Yuki Yugui Sonoda | a7d369e | 2015-04-11 11:48:36 +0900 | [diff] [blame] | 190 | rb_define_const(grpc_rb_mStatusCodes, "FAILED_PRECONDITION", |
temiola | 5832791 | 2014-12-15 17:51:16 -0800 | [diff] [blame] | 191 | INT2NUM(GRPC_STATUS_FAILED_PRECONDITION)); |
Yuki Yugui Sonoda | a7d369e | 2015-04-11 11:48:36 +0900 | [diff] [blame] | 192 | rb_define_const(grpc_rb_mStatusCodes, "ABORTED", |
| 193 | INT2NUM(GRPC_STATUS_ABORTED)); |
| 194 | rb_define_const(grpc_rb_mStatusCodes, "OUT_OF_RANGE", |
temiola | 5832791 | 2014-12-15 17:51:16 -0800 | [diff] [blame] | 195 | INT2NUM(GRPC_STATUS_OUT_OF_RANGE)); |
Yuki Yugui Sonoda | a7d369e | 2015-04-11 11:48:36 +0900 | [diff] [blame] | 196 | rb_define_const(grpc_rb_mStatusCodes, "UNIMPLEMENTED", |
temiola | 5832791 | 2014-12-15 17:51:16 -0800 | [diff] [blame] | 197 | INT2NUM(GRPC_STATUS_UNIMPLEMENTED)); |
Yuki Yugui Sonoda | a7d369e | 2015-04-11 11:48:36 +0900 | [diff] [blame] | 198 | rb_define_const(grpc_rb_mStatusCodes, "INTERNAL", |
| 199 | INT2NUM(GRPC_STATUS_INTERNAL)); |
| 200 | rb_define_const(grpc_rb_mStatusCodes, "UNAVAILABLE", |
temiola | 5832791 | 2014-12-15 17:51:16 -0800 | [diff] [blame] | 201 | INT2NUM(GRPC_STATUS_UNAVAILABLE)); |
Yuki Yugui Sonoda | a7d369e | 2015-04-11 11:48:36 +0900 | [diff] [blame] | 202 | rb_define_const(grpc_rb_mStatusCodes, "DATA_LOSS", |
| 203 | INT2NUM(GRPC_STATUS_DATA_LOSS)); |
temiola | 5832791 | 2014-12-15 17:51:16 -0800 | [diff] [blame] | 204 | } |
| 205 | |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 206 | /* id_at is the constructor method of the ruby standard Time class. */ |
| 207 | static ID id_at; |
| 208 | |
| 209 | /* id_inspect is the inspect method found on various ruby objects. */ |
| 210 | static ID id_inspect; |
| 211 | |
| 212 | /* id_to_s is the to_s method found on various ruby objects. */ |
| 213 | static ID id_to_s; |
| 214 | |
Tim Emiola | 98a32d3 | 2015-03-28 01:48:44 -0700 | [diff] [blame] | 215 | /* Converts a wrapped time constant to a standard time. */ |
Yuki Yugui Sonoda | f0eee5f | 2015-04-16 20:25:28 +0900 | [diff] [blame] | 216 | static VALUE grpc_rb_time_val_to_time(VALUE self) { |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 217 | gpr_timespec *time_const = NULL; |
Craig Tiller | 94329d0 | 2015-07-23 09:52:11 -0700 | [diff] [blame] | 218 | gpr_timespec real_time; |
Yuki Yugui Sonoda | d441c2e | 2015-04-11 15:33:58 +0900 | [diff] [blame] | 219 | TypedData_Get_Struct(self, gpr_timespec, &grpc_rb_timespec_data_type, |
| 220 | time_const); |
Craig Tiller | 94329d0 | 2015-07-23 09:52:11 -0700 | [diff] [blame] | 221 | real_time = gpr_convert_clock_type(*time_const, GPR_CLOCK_REALTIME); |
| 222 | return rb_funcall(rb_cTime, id_at, 2, INT2NUM(real_time.tv_sec), |
| 223 | INT2NUM(real_time.tv_nsec)); |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | /* Invokes inspect on the ctime version of the time val. */ |
Yuki Yugui Sonoda | f0eee5f | 2015-04-16 20:25:28 +0900 | [diff] [blame] | 227 | static VALUE grpc_rb_time_val_inspect(VALUE self) { |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 228 | return rb_funcall(grpc_rb_time_val_to_time(self), id_inspect, 0); |
| 229 | } |
| 230 | |
| 231 | /* Invokes to_s on the ctime version of the time val. */ |
Yuki Yugui Sonoda | f0eee5f | 2015-04-16 20:25:28 +0900 | [diff] [blame] | 232 | static VALUE grpc_rb_time_val_to_s(VALUE self) { |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 233 | return rb_funcall(grpc_rb_time_val_to_time(self), id_to_s, 0); |
| 234 | } |
| 235 | |
Craig Tiller | 354398f | 2015-07-13 09:16:03 -0700 | [diff] [blame] | 236 | static gpr_timespec zero_realtime; |
| 237 | static gpr_timespec inf_future_realtime; |
| 238 | static gpr_timespec inf_past_realtime; |
| 239 | |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 240 | /* Adds a module with constants that map to gpr's static timeval structs. */ |
Yuki Yugui Sonoda | f0eee5f | 2015-04-16 20:25:28 +0900 | [diff] [blame] | 241 | static void Init_grpc_time_consts() { |
Yuki Yugui Sonoda | a7d369e | 2015-04-11 11:48:36 +0900 | [diff] [blame] | 242 | VALUE grpc_rb_mTimeConsts = |
| 243 | rb_define_module_under(grpc_rb_mGrpcCore, "TimeConsts"); |
| 244 | grpc_rb_cTimeVal = |
| 245 | rb_define_class_under(grpc_rb_mGrpcCore, "TimeSpec", rb_cObject); |
Craig Tiller | 354398f | 2015-07-13 09:16:03 -0700 | [diff] [blame] | 246 | zero_realtime = gpr_time_0(GPR_CLOCK_REALTIME); |
| 247 | inf_future_realtime = gpr_inf_future(GPR_CLOCK_REALTIME); |
| 248 | inf_past_realtime = gpr_inf_past(GPR_CLOCK_REALTIME); |
Yuki Yugui Sonoda | d441c2e | 2015-04-11 15:33:58 +0900 | [diff] [blame] | 249 | rb_define_const( |
| 250 | grpc_rb_mTimeConsts, "ZERO", |
| 251 | TypedData_Wrap_Struct(grpc_rb_cTimeVal, &grpc_rb_timespec_data_type, |
Craig Tiller | 354398f | 2015-07-13 09:16:03 -0700 | [diff] [blame] | 252 | (void *)&zero_realtime)); |
Yuki Yugui Sonoda | d441c2e | 2015-04-11 15:33:58 +0900 | [diff] [blame] | 253 | rb_define_const( |
| 254 | grpc_rb_mTimeConsts, "INFINITE_FUTURE", |
| 255 | TypedData_Wrap_Struct(grpc_rb_cTimeVal, &grpc_rb_timespec_data_type, |
Craig Tiller | 354398f | 2015-07-13 09:16:03 -0700 | [diff] [blame] | 256 | (void *)&inf_future_realtime)); |
Yuki Yugui Sonoda | d441c2e | 2015-04-11 15:33:58 +0900 | [diff] [blame] | 257 | rb_define_const( |
| 258 | grpc_rb_mTimeConsts, "INFINITE_PAST", |
| 259 | TypedData_Wrap_Struct(grpc_rb_cTimeVal, &grpc_rb_timespec_data_type, |
Craig Tiller | 354398f | 2015-07-13 09:16:03 -0700 | [diff] [blame] | 260 | (void *)&inf_past_realtime)); |
Yuki Yugui Sonoda | a7d369e | 2015-04-11 11:48:36 +0900 | [diff] [blame] | 261 | rb_define_method(grpc_rb_cTimeVal, "to_time", grpc_rb_time_val_to_time, 0); |
| 262 | rb_define_method(grpc_rb_cTimeVal, "inspect", grpc_rb_time_val_inspect, 0); |
| 263 | rb_define_method(grpc_rb_cTimeVal, "to_s", grpc_rb_time_val_to_s, 0); |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 264 | id_at = rb_intern("at"); |
| 265 | id_inspect = rb_intern("inspect"); |
| 266 | id_to_s = rb_intern("to_s"); |
| 267 | id_tv_sec = rb_intern("tv_sec"); |
| 268 | id_tv_nsec = rb_intern("tv_nsec"); |
| 269 | } |
| 270 | |
Nicolas "Pixel" Noble | 6f4f02c | 2016-02-21 01:30:56 +0100 | [diff] [blame] | 271 | static void grpc_rb_shutdown(void) { |
murgatroid99 | 87afb5d | 2015-07-16 16:01:02 -0700 | [diff] [blame] | 272 | grpc_shutdown(); |
| 273 | } |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 274 | |
Tim Emiola | 409e6c8 | 2015-02-17 17:46:35 -0800 | [diff] [blame] | 275 | /* Initialize the GRPC module structs */ |
temiola | 21bb60c | 2014-12-18 10:58:22 -0800 | [diff] [blame] | 276 | |
Yuki Yugui Sonoda | a7d369e | 2015-04-11 11:48:36 +0900 | [diff] [blame] | 277 | /* grpc_rb_sNewServerRpc is the struct that holds new server rpc details. */ |
| 278 | VALUE grpc_rb_sNewServerRpc = Qnil; |
| 279 | /* grpc_rb_sStatus is the struct that holds status details. */ |
| 280 | VALUE grpc_rb_sStatus = Qnil; |
temiola | 21bb60c | 2014-12-18 10:58:22 -0800 | [diff] [blame] | 281 | |
Tim Emiola | 409e6c8 | 2015-02-17 17:46:35 -0800 | [diff] [blame] | 282 | /* Initialize the GRPC module. */ |
Yuki Yugui Sonoda | a7d369e | 2015-04-11 11:48:36 +0900 | [diff] [blame] | 283 | VALUE grpc_rb_mGRPC = Qnil; |
| 284 | VALUE grpc_rb_mGrpcCore = Qnil; |
temiola | 21bb60c | 2014-12-18 10:58:22 -0800 | [diff] [blame] | 285 | |
Yuki Yugui Sonoda | 99eb9f9 | 2015-04-16 20:09:55 +0900 | [diff] [blame] | 286 | /* cached Symbols for members in Status struct */ |
| 287 | VALUE sym_code = Qundef; |
| 288 | VALUE sym_details = Qundef; |
| 289 | VALUE sym_metadata = Qundef; |
| 290 | |
Nicolas "Pixel" Noble | cb90397 | 2016-02-21 22:58:26 +0100 | [diff] [blame] | 291 | static gpr_once g_once_init = GPR_ONCE_INIT; |
| 292 | |
| 293 | static void grpc_ruby_once_init() { |
| 294 | grpc_init(); |
| 295 | atexit(grpc_rb_shutdown); |
| 296 | } |
| 297 | |
Nicolas "Pixel" Noble | eade6e0 | 2016-01-29 22:53:31 +0100 | [diff] [blame] | 298 | void Init_grpc_c() { |
Nicolas "Pixel" Noble | d51d121 | 2016-01-31 11:33:19 +0100 | [diff] [blame] | 299 | if (!grpc_rb_load_core()) { |
| 300 | rb_raise(rb_eLoadError, "Couldn't find or load gRPC's dynamic C core"); |
| 301 | return; |
| 302 | } |
| 303 | |
Nicolas "Pixel" Noble | 88dc3c5 | 2016-02-22 03:21:08 +0100 | [diff] [blame] | 304 | /* ruby_vm_at_exit doesn't seem to be working. It would crash once every |
| 305 | * blue moon, and some users are getting it repeatedly. See the discussions |
| 306 | * - https://github.com/grpc/grpc/pull/5337 |
| 307 | * - https://bugs.ruby-lang.org/issues/12095 |
| 308 | * |
| 309 | * In order to still be able to handle the (unlikely) situation where the |
| 310 | * extension is loaded by a first Ruby VM that is subsequently destroyed, |
| 311 | * then loaded again by another VM within the same process, we need to |
| 312 | * schedule our initialization and destruction only once. |
| 313 | */ |
Nicolas "Pixel" Noble | cb90397 | 2016-02-21 22:58:26 +0100 | [diff] [blame] | 314 | gpr_once_init(&g_once_init, grpc_ruby_once_init); |
Tim Emiola | 9161a82 | 2015-11-11 15:58:44 -0800 | [diff] [blame] | 315 | |
Yuki Yugui Sonoda | a7d369e | 2015-04-11 11:48:36 +0900 | [diff] [blame] | 316 | grpc_rb_mGRPC = rb_define_module("GRPC"); |
| 317 | grpc_rb_mGrpcCore = rb_define_module_under(grpc_rb_mGRPC, "Core"); |
| 318 | grpc_rb_sNewServerRpc = |
| 319 | rb_struct_define("NewServerRpc", "method", "host", |
murgatroid99 | ec1588b | 2016-06-06 15:37:45 -0700 | [diff] [blame] | 320 | "deadline", "metadata", "call", NULL); |
Yuki Yugui Sonoda | a7d369e | 2015-04-11 11:48:36 +0900 | [diff] [blame] | 321 | grpc_rb_sStatus = |
| 322 | rb_struct_define("Status", "code", "details", "metadata", NULL); |
Tim Emiola | 98a32d3 | 2015-03-28 01:48:44 -0700 | [diff] [blame] | 323 | sym_code = ID2SYM(rb_intern("code")); |
| 324 | sym_details = ID2SYM(rb_intern("details")); |
| 325 | sym_metadata = ID2SYM(rb_intern("metadata")); |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 326 | |
Tim Emiola | 409e6c8 | 2015-02-17 17:46:35 -0800 | [diff] [blame] | 327 | Init_grpc_channel(); |
Tim Emiola | 409e6c8 | 2015-02-17 17:46:35 -0800 | [diff] [blame] | 328 | Init_grpc_call(); |
murgatroid99 | 9946f2b | 2015-12-04 14:36:27 -0800 | [diff] [blame] | 329 | Init_grpc_call_credentials(); |
Tim Emiola | 9332ea6 | 2015-10-27 23:48:29 -0700 | [diff] [blame] | 330 | Init_grpc_channel_credentials(); |
Tim Emiola | 409e6c8 | 2015-02-17 17:46:35 -0800 | [diff] [blame] | 331 | Init_grpc_server(); |
| 332 | Init_grpc_server_credentials(); |
| 333 | Init_grpc_status_codes(); |
| 334 | Init_grpc_time_consts(); |
nnoble | 097ef9b | 2014-12-01 17:06:10 -0800 | [diff] [blame] | 335 | } |