grpc 1.42.0.pre1-arm64-darwin

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of grpc might be problematic. Click here for more details.

Files changed (128) hide show
  1. checksums.yaml +7 -0
  2. data/etc/roots.pem +4337 -0
  3. data/grpc_c.32.ruby +0 -0
  4. data/grpc_c.64.ruby +0 -0
  5. data/src/ruby/bin/math_client.rb +140 -0
  6. data/src/ruby/bin/math_pb.rb +34 -0
  7. data/src/ruby/bin/math_server.rb +191 -0
  8. data/src/ruby/bin/math_services_pb.rb +51 -0
  9. data/src/ruby/bin/noproto_client.rb +93 -0
  10. data/src/ruby/bin/noproto_server.rb +97 -0
  11. data/src/ruby/ext/grpc/ext-export.clang +1 -0
  12. data/src/ruby/ext/grpc/ext-export.gcc +6 -0
  13. data/src/ruby/ext/grpc/extconf.rb +123 -0
  14. data/src/ruby/ext/grpc/rb_byte_buffer.c +65 -0
  15. data/src/ruby/ext/grpc/rb_byte_buffer.h +35 -0
  16. data/src/ruby/ext/grpc/rb_call.c +1051 -0
  17. data/src/ruby/ext/grpc/rb_call.h +57 -0
  18. data/src/ruby/ext/grpc/rb_call_credentials.c +341 -0
  19. data/src/ruby/ext/grpc/rb_call_credentials.h +31 -0
  20. data/src/ruby/ext/grpc/rb_channel.c +846 -0
  21. data/src/ruby/ext/grpc/rb_channel.h +34 -0
  22. data/src/ruby/ext/grpc/rb_channel_args.c +155 -0
  23. data/src/ruby/ext/grpc/rb_channel_args.h +38 -0
  24. data/src/ruby/ext/grpc/rb_channel_credentials.c +286 -0
  25. data/src/ruby/ext/grpc/rb_channel_credentials.h +37 -0
  26. data/src/ruby/ext/grpc/rb_completion_queue.c +101 -0
  27. data/src/ruby/ext/grpc/rb_completion_queue.h +36 -0
  28. data/src/ruby/ext/grpc/rb_compression_options.c +471 -0
  29. data/src/ruby/ext/grpc/rb_compression_options.h +29 -0
  30. data/src/ruby/ext/grpc/rb_enable_cpp.cc +22 -0
  31. data/src/ruby/ext/grpc/rb_event_thread.c +145 -0
  32. data/src/ruby/ext/grpc/rb_event_thread.h +21 -0
  33. data/src/ruby/ext/grpc/rb_grpc.c +333 -0
  34. data/src/ruby/ext/grpc/rb_grpc.h +77 -0
  35. data/src/ruby/ext/grpc/rb_grpc_imports.generated.c +605 -0
  36. data/src/ruby/ext/grpc/rb_grpc_imports.generated.h +913 -0
  37. data/src/ruby/ext/grpc/rb_loader.c +57 -0
  38. data/src/ruby/ext/grpc/rb_loader.h +25 -0
  39. data/src/ruby/ext/grpc/rb_server.c +385 -0
  40. data/src/ruby/ext/grpc/rb_server.h +32 -0
  41. data/src/ruby/ext/grpc/rb_server_credentials.c +259 -0
  42. data/src/ruby/ext/grpc/rb_server_credentials.h +37 -0
  43. data/src/ruby/ext/grpc/rb_xds_channel_credentials.c +218 -0
  44. data/src/ruby/ext/grpc/rb_xds_channel_credentials.h +37 -0
  45. data/src/ruby/ext/grpc/rb_xds_server_credentials.c +170 -0
  46. data/src/ruby/ext/grpc/rb_xds_server_credentials.h +37 -0
  47. data/src/ruby/lib/grpc/2.4/grpc_c.bundle +0 -0
  48. data/src/ruby/lib/grpc/2.5/grpc_c.bundle +0 -0
  49. data/src/ruby/lib/grpc/2.6/grpc_c.bundle +0 -0
  50. data/src/ruby/lib/grpc/2.7/grpc_c.bundle +0 -0
  51. data/src/ruby/lib/grpc/3.0/grpc_c.bundle +0 -0
  52. data/src/ruby/lib/grpc/core/status_codes.rb +135 -0
  53. data/src/ruby/lib/grpc/core/time_consts.rb +56 -0
  54. data/src/ruby/lib/grpc/errors.rb +277 -0
  55. data/src/ruby/lib/grpc/generic/active_call.rb +669 -0
  56. data/src/ruby/lib/grpc/generic/bidi_call.rb +233 -0
  57. data/src/ruby/lib/grpc/generic/client_stub.rb +503 -0
  58. data/src/ruby/lib/grpc/generic/interceptor_registry.rb +53 -0
  59. data/src/ruby/lib/grpc/generic/interceptors.rb +186 -0
  60. data/src/ruby/lib/grpc/generic/rpc_desc.rb +204 -0
  61. data/src/ruby/lib/grpc/generic/rpc_server.rb +551 -0
  62. data/src/ruby/lib/grpc/generic/service.rb +211 -0
  63. data/src/ruby/lib/grpc/google_rpc_status_utils.rb +40 -0
  64. data/src/ruby/lib/grpc/grpc.rb +24 -0
  65. data/src/ruby/lib/grpc/logconfig.rb +44 -0
  66. data/src/ruby/lib/grpc/notifier.rb +45 -0
  67. data/src/ruby/lib/grpc/structs.rb +15 -0
  68. data/src/ruby/lib/grpc/version.rb +18 -0
  69. data/src/ruby/lib/grpc.rb +37 -0
  70. data/src/ruby/pb/README.md +42 -0
  71. data/src/ruby/pb/generate_proto_ruby.sh +51 -0
  72. data/src/ruby/pb/grpc/health/checker.rb +75 -0
  73. data/src/ruby/pb/grpc/health/v1/health_pb.rb +31 -0
  74. data/src/ruby/pb/grpc/health/v1/health_services_pb.rb +62 -0
  75. data/src/ruby/pb/grpc/testing/duplicate/echo_duplicate_services_pb.rb +44 -0
  76. data/src/ruby/pb/grpc/testing/metrics_pb.rb +28 -0
  77. data/src/ruby/pb/grpc/testing/metrics_services_pb.rb +49 -0
  78. data/src/ruby/pb/src/proto/grpc/testing/empty_pb.rb +17 -0
  79. data/src/ruby/pb/src/proto/grpc/testing/messages_pb.rb +145 -0
  80. data/src/ruby/pb/src/proto/grpc/testing/test_pb.rb +16 -0
  81. data/src/ruby/pb/src/proto/grpc/testing/test_services_pb.rb +152 -0
  82. data/src/ruby/pb/test/client.rb +769 -0
  83. data/src/ruby/pb/test/server.rb +252 -0
  84. data/src/ruby/pb/test/xds_client.rb +415 -0
  85. data/src/ruby/spec/call_credentials_spec.rb +42 -0
  86. data/src/ruby/spec/call_spec.rb +180 -0
  87. data/src/ruby/spec/channel_connection_spec.rb +126 -0
  88. data/src/ruby/spec/channel_credentials_spec.rb +124 -0
  89. data/src/ruby/spec/channel_spec.rb +245 -0
  90. data/src/ruby/spec/client_auth_spec.rb +152 -0
  91. data/src/ruby/spec/client_server_spec.rb +664 -0
  92. data/src/ruby/spec/compression_options_spec.rb +149 -0
  93. data/src/ruby/spec/debug_message_spec.rb +134 -0
  94. data/src/ruby/spec/error_sanity_spec.rb +49 -0
  95. data/src/ruby/spec/errors_spec.rb +142 -0
  96. data/src/ruby/spec/generic/active_call_spec.rb +683 -0
  97. data/src/ruby/spec/generic/client_interceptors_spec.rb +153 -0
  98. data/src/ruby/spec/generic/client_stub_spec.rb +1083 -0
  99. data/src/ruby/spec/generic/interceptor_registry_spec.rb +65 -0
  100. data/src/ruby/spec/generic/rpc_desc_spec.rb +374 -0
  101. data/src/ruby/spec/generic/rpc_server_pool_spec.rb +127 -0
  102. data/src/ruby/spec/generic/rpc_server_spec.rb +748 -0
  103. data/src/ruby/spec/generic/server_interceptors_spec.rb +218 -0
  104. data/src/ruby/spec/generic/service_spec.rb +263 -0
  105. data/src/ruby/spec/google_rpc_status_utils_spec.rb +282 -0
  106. data/src/ruby/spec/pb/codegen/grpc/testing/package_options.proto +28 -0
  107. data/src/ruby/spec/pb/codegen/grpc/testing/package_options_import.proto +22 -0
  108. data/src/ruby/spec/pb/codegen/grpc/testing/package_options_import2.proto +23 -0
  109. data/src/ruby/spec/pb/codegen/grpc/testing/package_options_ruby_style.proto +41 -0
  110. data/src/ruby/spec/pb/codegen/grpc/testing/same_package_service_name.proto +27 -0
  111. data/src/ruby/spec/pb/codegen/grpc/testing/same_ruby_package_service_name.proto +29 -0
  112. data/src/ruby/spec/pb/codegen/package_option_spec.rb +98 -0
  113. data/src/ruby/spec/pb/duplicate/codegen_spec.rb +57 -0
  114. data/src/ruby/spec/pb/health/checker_spec.rb +236 -0
  115. data/src/ruby/spec/server_credentials_spec.rb +104 -0
  116. data/src/ruby/spec/server_spec.rb +231 -0
  117. data/src/ruby/spec/spec_helper.rb +61 -0
  118. data/src/ruby/spec/support/helpers.rb +107 -0
  119. data/src/ruby/spec/support/services.rb +160 -0
  120. data/src/ruby/spec/testdata/README +1 -0
  121. data/src/ruby/spec/testdata/ca.pem +20 -0
  122. data/src/ruby/spec/testdata/client.key +28 -0
  123. data/src/ruby/spec/testdata/client.pem +20 -0
  124. data/src/ruby/spec/testdata/server1.key +28 -0
  125. data/src/ruby/spec/testdata/server1.pem +22 -0
  126. data/src/ruby/spec/time_consts_spec.rb +74 -0
  127. data/src/ruby/spec/user_agent_spec.rb +74 -0
  128. metadata +404 -0
@@ -0,0 +1,34 @@
1
+ /*
2
+ *
3
+ * Copyright 2015 gRPC authors.
4
+ *
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
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
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.
16
+ *
17
+ */
18
+
19
+ #ifndef GRPC_RB_CHANNEL_H_
20
+ #define GRPC_RB_CHANNEL_H_
21
+
22
+ #include <ruby/ruby.h>
23
+
24
+ #include <grpc/grpc.h>
25
+
26
+ /* Initializes the Channel class. */
27
+ void Init_grpc_channel();
28
+
29
+ void grpc_rb_channel_polling_thread_start();
30
+
31
+ /* Gets the wrapped channel from the ruby wrapper */
32
+ grpc_channel* grpc_rb_get_wrapped_channel(VALUE v);
33
+
34
+ #endif /* GRPC_RB_CHANNEL_H_ */
@@ -0,0 +1,155 @@
1
+ /*
2
+ *
3
+ * Copyright 2015 gRPC authors.
4
+ *
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
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
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.
16
+ *
17
+ */
18
+
19
+ #include <ruby/ruby.h>
20
+
21
+ #include "rb_channel_args.h"
22
+
23
+ #include "rb_grpc.h"
24
+ #include "rb_grpc_imports.generated.h"
25
+
26
+ #include <grpc/grpc.h>
27
+
28
+ static rb_data_type_t grpc_rb_channel_args_data_type = {
29
+ "grpc_channel_args",
30
+ {GRPC_RB_GC_NOT_MARKED,
31
+ GRPC_RB_GC_DONT_FREE,
32
+ GRPC_RB_MEMSIZE_UNAVAILABLE,
33
+ {NULL, NULL}},
34
+ NULL,
35
+ NULL,
36
+ #ifdef RUBY_TYPED_FREE_IMMEDIATELY
37
+ RUBY_TYPED_FREE_IMMEDIATELY
38
+ #endif
39
+ };
40
+
41
+ /* A callback the processes the hash key values in channel_args hash */
42
+ static int grpc_rb_channel_create_in_process_add_args_hash_cb(VALUE key,
43
+ VALUE val,
44
+ VALUE args_obj) {
45
+ const char* the_key;
46
+ grpc_channel_args* args;
47
+
48
+ switch (TYPE(key)) {
49
+ case T_STRING:
50
+ the_key = StringValuePtr(key);
51
+ break;
52
+
53
+ case T_SYMBOL:
54
+ the_key = rb_id2name(SYM2ID(key));
55
+ break;
56
+
57
+ default:
58
+ rb_raise(rb_eTypeError, "bad chan arg: got <%s>, want <String|Symbol>",
59
+ rb_obj_classname(key));
60
+ return ST_STOP;
61
+ }
62
+
63
+ TypedData_Get_Struct(args_obj, grpc_channel_args,
64
+ &grpc_rb_channel_args_data_type, args);
65
+ if (args->num_args <= 0) {
66
+ rb_raise(rb_eRuntimeError, "hash_cb bug: num_args is %lu for key:%s",
67
+ args->num_args, StringValueCStr(key));
68
+ return ST_STOP;
69
+ }
70
+
71
+ args->args[args->num_args - 1].key = (char*)the_key;
72
+ switch (TYPE(val)) {
73
+ case T_SYMBOL:
74
+ args->args[args->num_args - 1].type = GRPC_ARG_STRING;
75
+ args->args[args->num_args - 1].value.string =
76
+ (char*)rb_id2name(SYM2ID(val));
77
+ --args->num_args;
78
+ return ST_CONTINUE;
79
+
80
+ case T_STRING:
81
+ args->args[args->num_args - 1].type = GRPC_ARG_STRING;
82
+ args->args[args->num_args - 1].value.string = StringValueCStr(val);
83
+ --args->num_args;
84
+ return ST_CONTINUE;
85
+
86
+ case T_FIXNUM:
87
+ args->args[args->num_args - 1].type = GRPC_ARG_INTEGER;
88
+ args->args[args->num_args - 1].value.integer = NUM2INT(val);
89
+ --args->num_args;
90
+ return ST_CONTINUE;
91
+
92
+ default:
93
+ rb_raise(rb_eTypeError, "%s: bad value: got <%s>, want <String|Fixnum>",
94
+ StringValueCStr(key), rb_obj_classname(val));
95
+ return ST_STOP;
96
+ }
97
+ rb_raise(rb_eRuntimeError, "impl bug: hash_cb reached to far while on key:%s",
98
+ StringValueCStr(key));
99
+ return ST_STOP;
100
+ }
101
+
102
+ /* channel_convert_params allows the call to
103
+ grpc_rb_hash_convert_to_channel_args to be made within an rb_protect
104
+ exception-handler. This allows any allocated memory to be freed before
105
+ propagating any exception that occurs */
106
+ typedef struct channel_convert_params {
107
+ VALUE src_hash;
108
+ grpc_channel_args* dst;
109
+ } channel_convert_params;
110
+
111
+ static VALUE grpc_rb_hash_convert_to_channel_args0(VALUE as_value) {
112
+ ID id_size = rb_intern("size");
113
+ VALUE grpc_rb_cChannelArgs = rb_define_class("TmpChannelArgs", rb_cObject);
114
+ channel_convert_params* params = (channel_convert_params*)as_value;
115
+ size_t num_args = 0;
116
+
117
+ if (!NIL_P(params->src_hash) && TYPE(params->src_hash) != T_HASH) {
118
+ rb_raise(rb_eTypeError, "bad channel args: got:<%s> want: a hash or nil",
119
+ rb_obj_classname(params->src_hash));
120
+ return Qnil;
121
+ }
122
+
123
+ if (TYPE(params->src_hash) == T_HASH) {
124
+ num_args = NUM2INT(rb_funcall(params->src_hash, id_size, 0));
125
+ params->dst->num_args = num_args;
126
+ params->dst->args = ALLOC_N(grpc_arg, num_args);
127
+ MEMZERO(params->dst->args, grpc_arg, num_args);
128
+ rb_hash_foreach(
129
+ params->src_hash, grpc_rb_channel_create_in_process_add_args_hash_cb,
130
+ TypedData_Wrap_Struct(grpc_rb_cChannelArgs,
131
+ &grpc_rb_channel_args_data_type, params->dst));
132
+ /* reset num_args as grpc_rb_channel_create_in_process_add_args_hash_cb
133
+ * decrements it during has processing */
134
+ params->dst->num_args = num_args;
135
+ }
136
+ return Qnil;
137
+ }
138
+
139
+ void grpc_rb_hash_convert_to_channel_args(VALUE src_hash,
140
+ grpc_channel_args* dst) {
141
+ channel_convert_params params;
142
+ int status = 0;
143
+
144
+ /* Make a protected call to grpc_rb_hash_convert_channel_args */
145
+ params.src_hash = src_hash;
146
+ params.dst = dst;
147
+ rb_protect(grpc_rb_hash_convert_to_channel_args0, (VALUE)&params, &status);
148
+ if (status != 0) {
149
+ if (dst->args != NULL) {
150
+ /* Free any allocated memory before propagating the error */
151
+ xfree(dst->args);
152
+ }
153
+ rb_jump_tag(status);
154
+ }
155
+ }
@@ -0,0 +1,38 @@
1
+ /*
2
+ *
3
+ * Copyright 2015 gRPC authors.
4
+ *
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
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
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.
16
+ *
17
+ */
18
+
19
+ #ifndef GRPC_RB_CHANNEL_ARGS_H_
20
+ #define GRPC_RB_CHANNEL_ARGS_H_
21
+
22
+ #include <ruby/ruby.h>
23
+
24
+ #include <grpc/grpc.h>
25
+
26
+ /* Converts a hash object containing channel args to a channel args instance.
27
+ *
28
+ * This func ALLOCs args->args. The caller is responsible for freeing it. If
29
+ * a ruby error is raised during processing of the hash values, the func takes
30
+ * care to deallocate any memory allocated so far, and propagate the error.
31
+ *
32
+ * @param src_hash A ruby hash
33
+ * @param dst the grpc_channel_args that the hash entries will be added to.
34
+ */
35
+ void grpc_rb_hash_convert_to_channel_args(VALUE src_hash,
36
+ grpc_channel_args* dst);
37
+
38
+ #endif /* GRPC_RB_CHANNEL_ARGS_H_ */
@@ -0,0 +1,286 @@
1
+ /*
2
+ *
3
+ * Copyright 2015 gRPC authors.
4
+ *
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
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
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.
16
+ *
17
+ */
18
+
19
+ #include <ruby/ruby.h>
20
+
21
+ #include "rb_channel_credentials.h"
22
+
23
+ #include <string.h>
24
+
25
+ #include "rb_call_credentials.h"
26
+ #include "rb_grpc.h"
27
+ #include "rb_grpc_imports.generated.h"
28
+
29
+ #include <grpc/grpc.h>
30
+ #include <grpc/grpc_security.h>
31
+ #include <grpc/support/alloc.h>
32
+ #include <grpc/support/log.h>
33
+
34
+ /* grpc_rb_cChannelCredentials is the ruby class that proxies
35
+ grpc_channel_credentials. */
36
+ static VALUE grpc_rb_cChannelCredentials = Qnil;
37
+
38
+ static char* pem_root_certs = NULL;
39
+
40
+ /* grpc_rb_channel_credentials wraps a grpc_channel_credentials. It provides a
41
+ * mark object that is used to hold references to any objects used to create
42
+ * the credentials. */
43
+ typedef struct grpc_rb_channel_credentials {
44
+ /* Holder of ruby objects involved in constructing the credentials */
45
+ VALUE mark;
46
+
47
+ /* The actual credentials */
48
+ grpc_channel_credentials* wrapped;
49
+ } grpc_rb_channel_credentials;
50
+
51
+ static void grpc_rb_channel_credentials_free_internal(void* p) {
52
+ grpc_rb_channel_credentials* wrapper = NULL;
53
+ if (p == NULL) {
54
+ return;
55
+ };
56
+ wrapper = (grpc_rb_channel_credentials*)p;
57
+ grpc_channel_credentials_release(wrapper->wrapped);
58
+ wrapper->wrapped = NULL;
59
+
60
+ xfree(p);
61
+ }
62
+
63
+ /* Destroys the credentials instances. */
64
+ static void grpc_rb_channel_credentials_free(void* p) {
65
+ grpc_rb_channel_credentials_free_internal(p);
66
+ grpc_ruby_shutdown();
67
+ }
68
+
69
+ /* Protects the mark object from GC */
70
+ static void grpc_rb_channel_credentials_mark(void* p) {
71
+ grpc_rb_channel_credentials* wrapper = NULL;
72
+ if (p == NULL) {
73
+ return;
74
+ }
75
+ wrapper = (grpc_rb_channel_credentials*)p;
76
+
77
+ if (wrapper->mark != Qnil) {
78
+ rb_gc_mark(wrapper->mark);
79
+ }
80
+ }
81
+
82
+ static rb_data_type_t grpc_rb_channel_credentials_data_type = {
83
+ "grpc_channel_credentials",
84
+ {grpc_rb_channel_credentials_mark,
85
+ grpc_rb_channel_credentials_free,
86
+ GRPC_RB_MEMSIZE_UNAVAILABLE,
87
+ {NULL, NULL}},
88
+ NULL,
89
+ NULL,
90
+ #ifdef RUBY_TYPED_FREE_IMMEDIATELY
91
+ RUBY_TYPED_FREE_IMMEDIATELY
92
+ #endif
93
+ };
94
+
95
+ /* Allocates ChannelCredential instances.
96
+ Provides safe initial defaults for the instance fields. */
97
+ static VALUE grpc_rb_channel_credentials_alloc(VALUE cls) {
98
+ grpc_ruby_init();
99
+ grpc_rb_channel_credentials* wrapper = ALLOC(grpc_rb_channel_credentials);
100
+ wrapper->wrapped = NULL;
101
+ wrapper->mark = Qnil;
102
+ return TypedData_Wrap_Struct(cls, &grpc_rb_channel_credentials_data_type,
103
+ wrapper);
104
+ }
105
+
106
+ /* Creates a wrapping object for a given channel credentials. This should only
107
+ * be called with grpc_channel_credentials objects that are not already
108
+ * associated with any Ruby object. */
109
+ VALUE grpc_rb_wrap_channel_credentials(grpc_channel_credentials* c,
110
+ VALUE mark) {
111
+ VALUE rb_wrapper;
112
+ grpc_rb_channel_credentials* wrapper;
113
+ if (c == NULL) {
114
+ return Qnil;
115
+ }
116
+ rb_wrapper = grpc_rb_channel_credentials_alloc(grpc_rb_cChannelCredentials);
117
+ TypedData_Get_Struct(rb_wrapper, grpc_rb_channel_credentials,
118
+ &grpc_rb_channel_credentials_data_type, wrapper);
119
+ wrapper->wrapped = c;
120
+ wrapper->mark = mark;
121
+ return rb_wrapper;
122
+ }
123
+
124
+ /* The attribute used on the mark object to hold the pem_root_certs. */
125
+ static ID id_pem_root_certs;
126
+
127
+ /* The attribute used on the mark object to hold the pem_private_key. */
128
+ static ID id_pem_private_key;
129
+
130
+ /* The attribute used on the mark object to hold the pem_private_key. */
131
+ static ID id_pem_cert_chain;
132
+
133
+ /*
134
+ call-seq:
135
+ creds1 = Credentials.new()
136
+ ...
137
+ creds2 = Credentials.new(pem_root_certs)
138
+ ...
139
+ creds3 = Credentials.new(pem_root_certs, pem_private_key,
140
+ pem_cert_chain)
141
+ pem_root_certs: (optional) PEM encoding of the server root certificate
142
+ pem_private_key: (optional) PEM encoding of the client's private key
143
+ pem_cert_chain: (optional) PEM encoding of the client's cert chain
144
+ Initializes Credential instances. */
145
+ static VALUE grpc_rb_channel_credentials_init(int argc, VALUE* argv,
146
+ VALUE self) {
147
+ VALUE pem_root_certs = Qnil;
148
+ VALUE pem_private_key = Qnil;
149
+ VALUE pem_cert_chain = Qnil;
150
+ grpc_rb_channel_credentials* wrapper = NULL;
151
+ grpc_channel_credentials* creds = NULL;
152
+ grpc_ssl_pem_key_cert_pair key_cert_pair;
153
+ const char* pem_root_certs_cstr = NULL;
154
+ MEMZERO(&key_cert_pair, grpc_ssl_pem_key_cert_pair, 1);
155
+
156
+ /* "03" == no mandatory arg, 3 optional */
157
+ rb_scan_args(argc, argv, "03", &pem_root_certs, &pem_private_key,
158
+ &pem_cert_chain);
159
+
160
+ TypedData_Get_Struct(self, grpc_rb_channel_credentials,
161
+ &grpc_rb_channel_credentials_data_type, wrapper);
162
+ if (pem_root_certs != Qnil) {
163
+ pem_root_certs_cstr = RSTRING_PTR(pem_root_certs);
164
+ }
165
+ if (pem_private_key == Qnil && pem_cert_chain == Qnil) {
166
+ creds = grpc_ssl_credentials_create(pem_root_certs_cstr, NULL, NULL, NULL);
167
+ } else {
168
+ if (pem_private_key == Qnil) {
169
+ rb_raise(
170
+ rb_eRuntimeError,
171
+ "could not create a credentials because pem_private_key is NULL");
172
+ }
173
+ if (pem_cert_chain == Qnil) {
174
+ rb_raise(rb_eRuntimeError,
175
+ "could not create a credentials because pem_cert_chain is NULL");
176
+ }
177
+ key_cert_pair.private_key = RSTRING_PTR(pem_private_key);
178
+ key_cert_pair.cert_chain = RSTRING_PTR(pem_cert_chain);
179
+ creds = grpc_ssl_credentials_create(pem_root_certs_cstr, &key_cert_pair,
180
+ NULL, NULL);
181
+ }
182
+ if (creds == NULL) {
183
+ rb_raise(rb_eRuntimeError,
184
+ "the call to grpc_ssl_credentials_create() failed, could not "
185
+ "create a credentials, see "
186
+ "https://github.com/grpc/grpc/blob/master/TROUBLESHOOTING.md for "
187
+ "debugging tips");
188
+ return Qnil;
189
+ }
190
+ wrapper->wrapped = creds;
191
+
192
+ /* Add the input objects as hidden fields to preserve them. */
193
+ rb_ivar_set(self, id_pem_cert_chain, pem_cert_chain);
194
+ rb_ivar_set(self, id_pem_private_key, pem_private_key);
195
+ rb_ivar_set(self, id_pem_root_certs, pem_root_certs);
196
+
197
+ return self;
198
+ }
199
+
200
+ static VALUE grpc_rb_channel_credentials_compose(int argc, VALUE* argv,
201
+ VALUE self) {
202
+ grpc_channel_credentials* creds;
203
+ grpc_call_credentials* other;
204
+ grpc_channel_credentials* prev = NULL;
205
+ VALUE mark;
206
+ if (argc == 0) {
207
+ return self;
208
+ }
209
+ mark = rb_ary_new();
210
+ rb_ary_push(mark, self);
211
+ creds = grpc_rb_get_wrapped_channel_credentials(self);
212
+ for (int i = 0; i < argc; i++) {
213
+ rb_ary_push(mark, argv[i]);
214
+ other = grpc_rb_get_wrapped_call_credentials(argv[i]);
215
+ creds = grpc_composite_channel_credentials_create(creds, other, NULL);
216
+ if (prev != NULL) {
217
+ grpc_channel_credentials_release(prev);
218
+ }
219
+ prev = creds;
220
+
221
+ if (creds == NULL) {
222
+ rb_raise(rb_eRuntimeError,
223
+ "Failed to compose channel and call credentials");
224
+ }
225
+ }
226
+ return grpc_rb_wrap_channel_credentials(creds, mark);
227
+ }
228
+
229
+ static grpc_ssl_roots_override_result get_ssl_roots_override(
230
+ char** pem_root_certs_ptr) {
231
+ *pem_root_certs_ptr = pem_root_certs;
232
+ if (pem_root_certs == NULL) {
233
+ return GRPC_SSL_ROOTS_OVERRIDE_FAIL;
234
+ } else {
235
+ return GRPC_SSL_ROOTS_OVERRIDE_OK;
236
+ }
237
+ }
238
+
239
+ static VALUE grpc_rb_set_default_roots_pem(VALUE self, VALUE roots) {
240
+ char* roots_ptr = StringValueCStr(roots);
241
+ size_t length = strlen(roots_ptr);
242
+ (void)self;
243
+ pem_root_certs = gpr_malloc((length + 1) * sizeof(char));
244
+ memcpy(pem_root_certs, roots_ptr, length + 1);
245
+ return Qnil;
246
+ }
247
+
248
+ void Init_grpc_channel_credentials() {
249
+ grpc_rb_cChannelCredentials = rb_define_class_under(
250
+ grpc_rb_mGrpcCore, "ChannelCredentials", rb_cObject);
251
+
252
+ /* Allocates an object managed by the ruby runtime */
253
+ rb_define_alloc_func(grpc_rb_cChannelCredentials,
254
+ grpc_rb_channel_credentials_alloc);
255
+
256
+ /* Provides a ruby constructor and support for dup/clone. */
257
+ rb_define_method(grpc_rb_cChannelCredentials, "initialize",
258
+ grpc_rb_channel_credentials_init, -1);
259
+ rb_define_method(grpc_rb_cChannelCredentials, "initialize_copy",
260
+ grpc_rb_cannot_init_copy, 1);
261
+ rb_define_method(grpc_rb_cChannelCredentials, "compose",
262
+ grpc_rb_channel_credentials_compose, -1);
263
+ rb_define_module_function(grpc_rb_cChannelCredentials,
264
+ "set_default_roots_pem",
265
+ grpc_rb_set_default_roots_pem, 1);
266
+
267
+ grpc_set_ssl_roots_override_callback(get_ssl_roots_override);
268
+
269
+ id_pem_cert_chain = rb_intern("__pem_cert_chain");
270
+ id_pem_private_key = rb_intern("__pem_private_key");
271
+ id_pem_root_certs = rb_intern("__pem_root_certs");
272
+ }
273
+
274
+ /* Gets the wrapped grpc_channel_credentials from the ruby wrapper */
275
+ grpc_channel_credentials* grpc_rb_get_wrapped_channel_credentials(VALUE v) {
276
+ grpc_rb_channel_credentials* wrapper = NULL;
277
+ Check_TypedStruct(v, &grpc_rb_channel_credentials_data_type);
278
+ TypedData_Get_Struct(v, grpc_rb_channel_credentials,
279
+ &grpc_rb_channel_credentials_data_type, wrapper);
280
+ return wrapper->wrapped;
281
+ }
282
+
283
+ /* Check if v is kind of ChannelCredentials */
284
+ bool grpc_rb_is_channel_credentials(VALUE v) {
285
+ return rb_typeddata_is_kind_of(v, &grpc_rb_channel_credentials_data_type);
286
+ }
@@ -0,0 +1,37 @@
1
+ /*
2
+ *
3
+ * Copyright 2015 gRPC authors.
4
+ *
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
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
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.
16
+ *
17
+ */
18
+
19
+ #ifndef GRPC_RB_CREDENTIALS_H_
20
+ #define GRPC_RB_CREDENTIALS_H_
21
+
22
+ #include <ruby/ruby.h>
23
+
24
+ #include <stdbool.h>
25
+
26
+ #include <grpc/grpc_security.h>
27
+
28
+ /* Initializes the ruby ChannelCredentials class. */
29
+ void Init_grpc_channel_credentials();
30
+
31
+ /* Gets the wrapped credentials from the ruby wrapper */
32
+ grpc_channel_credentials* grpc_rb_get_wrapped_channel_credentials(VALUE v);
33
+
34
+ /* Check if v is kind of ChannelCredentials */
35
+ bool grpc_rb_is_channel_credentials(VALUE v);
36
+
37
+ #endif /* GRPC_RB_CREDENTIALS_H_ */
@@ -0,0 +1,101 @@
1
+ /*
2
+ *
3
+ * Copyright 2015 gRPC authors.
4
+ *
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
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
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.
16
+ *
17
+ */
18
+
19
+ #include <ruby/ruby.h>
20
+
21
+ #include "rb_completion_queue.h"
22
+
23
+ #include <ruby/thread.h>
24
+
25
+ #include "rb_grpc.h"
26
+ #include "rb_grpc_imports.generated.h"
27
+
28
+ #include <grpc/grpc.h>
29
+ #include <grpc/support/log.h>
30
+ #include <grpc/support/time.h>
31
+
32
+ /* Used to allow grpc_completion_queue_next call to release the GIL */
33
+ typedef struct next_call_stack {
34
+ grpc_completion_queue* cq;
35
+ grpc_event event;
36
+ gpr_timespec timeout;
37
+ void* tag;
38
+ volatile int interrupted;
39
+ } next_call_stack;
40
+
41
+ /* Calls grpc_completion_queue_pluck without holding the ruby GIL */
42
+ static void* grpc_rb_completion_queue_pluck_no_gil(void* param) {
43
+ next_call_stack* const next_call = (next_call_stack*)param;
44
+ gpr_timespec increment = gpr_time_from_millis(20, GPR_TIMESPAN);
45
+ gpr_timespec deadline;
46
+ do {
47
+ deadline = gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), increment);
48
+ next_call->event = grpc_completion_queue_pluck(
49
+ next_call->cq, next_call->tag, deadline, NULL);
50
+ if (next_call->event.type != GRPC_QUEUE_TIMEOUT ||
51
+ gpr_time_cmp(deadline, next_call->timeout) > 0) {
52
+ break;
53
+ }
54
+ } while (!next_call->interrupted);
55
+ return NULL;
56
+ }
57
+
58
+ /* Helper function to free a completion queue. */
59
+ void grpc_rb_completion_queue_destroy(grpc_completion_queue* cq) {
60
+ /* Every function that adds an event to a queue also synchronously plucks
61
+ that event from the queue, and holds a reference to the Ruby object that
62
+ holds the queue, so we only get to this point if all of those functions
63
+ have completed, and the queue is empty */
64
+ grpc_completion_queue_shutdown(cq);
65
+ grpc_completion_queue_destroy(cq);
66
+ }
67
+
68
+ static void unblock_func(void* param) {
69
+ next_call_stack* const next_call = (next_call_stack*)param;
70
+ next_call->interrupted = 1;
71
+ }
72
+
73
+ /* Does the same thing as grpc_completion_queue_pluck, while properly releasing
74
+ the GVL and handling interrupts */
75
+ grpc_event rb_completion_queue_pluck(grpc_completion_queue* queue, void* tag,
76
+ gpr_timespec deadline, void* reserved) {
77
+ next_call_stack next_call;
78
+ MEMZERO(&next_call, next_call_stack, 1);
79
+ next_call.cq = queue;
80
+ next_call.timeout = deadline;
81
+ next_call.tag = tag;
82
+ next_call.event.type = GRPC_QUEUE_TIMEOUT;
83
+ (void)reserved;
84
+ /* Loop until we finish a pluck without an interruption. The internal
85
+ pluck function runs either until it is interrupted or it gets an
86
+ event, or time runs out.
87
+
88
+ The basic reason we need this relatively complicated construction is that
89
+ we need to re-acquire the GVL when an interrupt comes in, so that the ruby
90
+ interpreter can do what it needs to do with the interrupt. But we also need
91
+ to get back to plucking when the interrupt has been handled. */
92
+ do {
93
+ next_call.interrupted = 0;
94
+ rb_thread_call_without_gvl(grpc_rb_completion_queue_pluck_no_gil,
95
+ (void*)&next_call, unblock_func,
96
+ (void*)&next_call);
97
+ /* If an interrupt prevented pluck from returning useful information, then
98
+ any plucks that did complete must have timed out */
99
+ } while (next_call.interrupted && next_call.event.type == GRPC_QUEUE_TIMEOUT);
100
+ return next_call.event;
101
+ }
@@ -0,0 +1,36 @@
1
+ /*
2
+ *
3
+ * Copyright 2015 gRPC authors.
4
+ *
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
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
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.
16
+ *
17
+ */
18
+
19
+ #ifndef GRPC_RB_COMPLETION_QUEUE_H_
20
+ #define GRPC_RB_COMPLETION_QUEUE_H_
21
+
22
+ #include <ruby/ruby.h>
23
+
24
+ #include <grpc/grpc.h>
25
+
26
+ void grpc_rb_completion_queue_destroy(grpc_completion_queue* cq);
27
+
28
+ /**
29
+ * Makes the implementation of CompletionQueue#pluck available in other files
30
+ *
31
+ * This avoids having code that holds the GIL repeated at multiple sites.
32
+ */
33
+ grpc_event rb_completion_queue_pluck(grpc_completion_queue* queue, void* tag,
34
+ gpr_timespec deadline, void* reserved);
35
+
36
+ #endif /* GRPC_RB_COMPLETION_QUEUE_H_ */