Craig Tiller | 1a61b17 | 2015-02-16 11:53:47 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | * Copyright 2015 gRPC authors. |
Craig Tiller | 1a61b17 | 2015-02-16 11:53:47 -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 |
Craig Tiller | 1a61b17 | 2015-02-16 11:53:47 -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 |
Craig Tiller | 1a61b17 | 2015-02-16 11:53:47 -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. |
Craig Tiller | 1a61b17 | 2015-02-16 11:53:47 -0800 | [diff] [blame] | 16 | * |
| 17 | */ |
| 18 | |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 19 | #include "channel.h" |
| 20 | |
| 21 | #ifdef HAVE_CONFIG_H |
| 22 | #include "config.h" |
| 23 | #endif |
| 24 | |
murgatroid99 | 8242ba7 | 2015-04-01 15:29:44 -0700 | [diff] [blame] | 25 | #include <php.h> |
| 26 | #include <php_ini.h> |
| 27 | #include <ext/standard/info.h> |
Stanley Cheung | 5b3dc4a | 2017-08-03 18:00:25 -0700 | [diff] [blame^] | 28 | #include <ext/standard/php_var.h> |
| 29 | #include <ext/standard/sha1.h> |
| 30 | #if PHP_MAJOR_VERSION < 7 |
| 31 | #include <ext/standard/php_smart_str.h> |
| 32 | #else |
| 33 | #include <zend_smart_str.h> |
| 34 | #endif |
murgatroid99 | 8242ba7 | 2015-04-01 15:29:44 -0700 | [diff] [blame] | 35 | #include <ext/spl/spl_exceptions.h> |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 36 | #include "php_grpc.h" |
| 37 | |
murgatroid99 | 8242ba7 | 2015-04-01 15:29:44 -0700 | [diff] [blame] | 38 | #include <zend_exceptions.h> |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 39 | |
| 40 | #include <stdbool.h> |
| 41 | |
murgatroid99 | 8242ba7 | 2015-04-01 15:29:44 -0700 | [diff] [blame] | 42 | #include <grpc/grpc.h> |
murgatroid99 | 8242ba7 | 2015-04-01 15:29:44 -0700 | [diff] [blame] | 43 | #include <grpc/grpc_security.h> |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 44 | |
Stanley Cheung | a63fdd0 | 2015-08-11 15:11:11 -0700 | [diff] [blame] | 45 | #include "completion_queue.h" |
Stanley Cheung | 9c0b35e | 2015-10-21 17:07:56 -0700 | [diff] [blame] | 46 | #include "channel_credentials.h" |
Stanley Cheung | a63fdd0 | 2015-08-11 15:11:11 -0700 | [diff] [blame] | 47 | #include "server.h" |
| 48 | #include "timeval.h" |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 49 | |
Xiaoguang Sun | 8a929a9 | 2015-03-13 14:22:31 +0800 | [diff] [blame] | 50 | zend_class_entry *grpc_ce_channel; |
thinkerou | dba5b0c | 2016-07-27 18:39:16 +0800 | [diff] [blame] | 51 | #if PHP_MAJOR_VERSION >= 7 |
| 52 | static zend_object_handlers channel_ce_handlers; |
| 53 | #endif |
Stanley Cheung | 5b3dc4a | 2017-08-03 18:00:25 -0700 | [diff] [blame^] | 54 | static gpr_mu global_persistent_list_mu; |
| 55 | int le_plink; |
Xiaoguang Sun | 8a929a9 | 2015-03-13 14:22:31 +0800 | [diff] [blame] | 56 | |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 57 | /* Frees and destroys an instance of wrapped_grpc_channel */ |
thinkerou | 011d1ef | 2016-07-27 09:44:49 +0800 | [diff] [blame] | 58 | PHP_GRPC_FREE_WRAPPED_FUNC_START(wrapped_grpc_channel) |
Stanley Cheung | 5b3dc4a | 2017-08-03 18:00:25 -0700 | [diff] [blame^] | 59 | if (p->wrapper != NULL) { |
| 60 | gpr_mu_lock(&p->wrapper->mu); |
| 61 | if (p->wrapper->wrapped != NULL) { |
| 62 | php_grpc_zend_resource *rsrc; |
| 63 | php_grpc_int key_len = strlen(p->wrapper->key); |
| 64 | // only destroy the channel here if not found in the persistent list |
| 65 | gpr_mu_lock(&global_persistent_list_mu); |
| 66 | if (!(PHP_GRPC_PERSISTENT_LIST_FIND(&EG(persistent_list), p->wrapper->key, |
| 67 | key_len, rsrc))) { |
| 68 | grpc_channel_destroy(p->wrapper->wrapped); |
| 69 | } |
| 70 | gpr_mu_unlock(&global_persistent_list_mu); |
| 71 | } |
| 72 | gpr_mu_unlock(&p->wrapper->mu); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 73 | } |
thinkerou | 011d1ef | 2016-07-27 09:44:49 +0800 | [diff] [blame] | 74 | PHP_GRPC_FREE_WRAPPED_FUNC_END() |
| 75 | |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 76 | /* Initializes an instance of wrapped_grpc_channel to be associated with an |
| 77 | * object of a class specified by class_type */ |
thinkerou | dba5b0c | 2016-07-27 18:39:16 +0800 | [diff] [blame] | 78 | php_grpc_zend_object create_wrapped_grpc_channel(zend_class_entry *class_type |
| 79 | TSRMLS_DC) { |
thinkerou | ba75c01 | 2016-07-28 02:30:08 +0800 | [diff] [blame] | 80 | PHP_GRPC_ALLOC_CLASS_OBJECT(wrapped_grpc_channel); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 81 | zend_object_std_init(&intern->std, class_type TSRMLS_CC); |
| 82 | object_properties_init(&intern->std, class_type); |
thinkerou | dc673c5 | 2016-07-28 09:49:38 +0800 | [diff] [blame] | 83 | PHP_GRPC_FREE_CLASS_OBJECT(wrapped_grpc_channel, channel_ce_handlers); |
thinkerou | dba5b0c | 2016-07-27 18:39:16 +0800 | [diff] [blame] | 84 | } |
thinkerou | 6f9d30b | 2016-07-27 03:19:03 +0800 | [diff] [blame] | 85 | |
Stanley Cheung | 5b3dc4a | 2017-08-03 18:00:25 -0700 | [diff] [blame^] | 86 | int php_grpc_read_args_array(zval *args_array, |
| 87 | grpc_channel_args *args TSRMLS_DC) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 88 | HashTable *array_hash; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 89 | int args_index; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 90 | array_hash = Z_ARRVAL_P(args_array); |
thinkerou | 6f9d30b | 2016-07-27 03:19:03 +0800 | [diff] [blame] | 91 | if (!array_hash) { |
| 92 | zend_throw_exception(spl_ce_InvalidArgumentException, |
Stanley Cheung | c1f25fb | 2016-07-29 13:41:22 -0700 | [diff] [blame] | 93 | "array_hash is NULL", 1 TSRMLS_CC); |
Stanley Cheung | 5b3dc4a | 2017-08-03 18:00:25 -0700 | [diff] [blame^] | 94 | return FAILURE; |
thinkerou | 6f9d30b | 2016-07-27 03:19:03 +0800 | [diff] [blame] | 95 | } |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 96 | args->num_args = zend_hash_num_elements(array_hash); |
| 97 | args->args = ecalloc(args->num_args, sizeof(grpc_arg)); |
| 98 | args_index = 0; |
thinkerou | 6f9d30b | 2016-07-27 03:19:03 +0800 | [diff] [blame] | 99 | |
thinkerou | ba75c01 | 2016-07-28 02:30:08 +0800 | [diff] [blame] | 100 | char *key = NULL; |
| 101 | zval *data; |
| 102 | int key_type; |
| 103 | |
| 104 | PHP_GRPC_HASH_FOREACH_STR_KEY_VAL_START(array_hash, key, key_type, data) |
| 105 | if (key_type != HASH_KEY_IS_STRING) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 106 | zend_throw_exception(spl_ce_InvalidArgumentException, |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 107 | "args keys must be strings", 1 TSRMLS_CC); |
Stanley Cheung | 5b3dc4a | 2017-08-03 18:00:25 -0700 | [diff] [blame^] | 108 | return FAILURE; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 109 | } |
| 110 | args->args[args_index].key = key; |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 111 | switch (Z_TYPE_P(data)) { |
| 112 | case IS_LONG: |
| 113 | args->args[args_index].value.integer = (int)Z_LVAL_P(data); |
| 114 | args->args[args_index].type = GRPC_ARG_INTEGER; |
| 115 | break; |
| 116 | case IS_STRING: |
| 117 | args->args[args_index].value.string = Z_STRVAL_P(data); |
| 118 | args->args[args_index].type = GRPC_ARG_STRING; |
| 119 | break; |
| 120 | default: |
| 121 | zend_throw_exception(spl_ce_InvalidArgumentException, |
thinkerou | ba75c01 | 2016-07-28 02:30:08 +0800 | [diff] [blame] | 122 | "args values must be int or string", 1 TSRMLS_CC); |
Stanley Cheung | 5b3dc4a | 2017-08-03 18:00:25 -0700 | [diff] [blame^] | 123 | return FAILURE; |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 124 | } |
| 125 | args_index++; |
thinkerou | ba75c01 | 2016-07-28 02:30:08 +0800 | [diff] [blame] | 126 | PHP_GRPC_HASH_FOREACH_END() |
Stanley Cheung | 5b3dc4a | 2017-08-03 18:00:25 -0700 | [diff] [blame^] | 127 | return SUCCESS; |
| 128 | } |
| 129 | |
| 130 | void generate_sha1_str(char *sha1str, char *str, php_grpc_int len) { |
| 131 | PHP_SHA1_CTX context; |
| 132 | unsigned char digest[20]; |
| 133 | sha1str[0] = '\0'; |
| 134 | PHP_SHA1Init(&context); |
| 135 | PHP_GRPC_SHA1Update(&context, str, len); |
| 136 | PHP_SHA1Final(digest, &context); |
| 137 | make_sha1_digest(sha1str, digest); |
| 138 | } |
| 139 | |
| 140 | void create_channel( |
| 141 | wrapped_grpc_channel *channel, |
| 142 | char *target, |
| 143 | grpc_channel_args args, |
| 144 | wrapped_grpc_channel_credentials *creds) { |
| 145 | if (creds == NULL) { |
| 146 | channel->wrapper->wrapped = grpc_insecure_channel_create(target, &args, |
| 147 | NULL); |
| 148 | } else { |
| 149 | channel->wrapper->wrapped = |
| 150 | grpc_secure_channel_create(creds->wrapped, target, &args, NULL); |
| 151 | } |
| 152 | efree(args.args); |
| 153 | } |
| 154 | |
| 155 | void create_and_add_channel_to_persistent_list( |
| 156 | wrapped_grpc_channel *channel, |
| 157 | char *target, |
| 158 | grpc_channel_args args, |
| 159 | wrapped_grpc_channel_credentials *creds, |
| 160 | char *key, |
| 161 | php_grpc_int key_len) { |
| 162 | php_grpc_zend_resource new_rsrc; |
| 163 | channel_persistent_le_t *le; |
| 164 | // this links each persistent list entry to a destructor |
| 165 | new_rsrc.type = le_plink; |
| 166 | le = malloc(sizeof(channel_persistent_le_t)); |
| 167 | |
| 168 | create_channel(channel, target, args, creds); |
| 169 | |
| 170 | le->channel = channel->wrapper; |
| 171 | new_rsrc.ptr = le; |
| 172 | gpr_mu_lock(&global_persistent_list_mu); |
| 173 | PHP_GRPC_PERSISTENT_LIST_UPDATE(&EG(persistent_list), key, key_len, |
| 174 | (void *)&new_rsrc); |
| 175 | gpr_mu_unlock(&global_persistent_list_mu); |
thinkerou | 6f9d30b | 2016-07-27 03:19:03 +0800 | [diff] [blame] | 176 | } |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 177 | |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 178 | /** |
Stanley Cheung | 5b3dc4a | 2017-08-03 18:00:25 -0700 | [diff] [blame^] | 179 | * Construct an instance of the Channel class. |
| 180 | * |
| 181 | * By default, the underlying grpc_channel is "persistent". That is, given |
| 182 | * the same set of parameters passed to the constructor, the same underlying |
| 183 | * grpc_channel will be returned. |
| 184 | * |
| 185 | * If the $args array contains a "credentials" key mapping to a |
| 186 | * ChannelCredentials object, a secure channel will be created with those |
| 187 | * credentials. |
| 188 | * |
| 189 | * If the $args array contains a "force_new" key mapping to a boolean value |
| 190 | * of "true", a new underlying grpc_channel will be created regardless. If |
| 191 | * there are any opened channels on the same hostname, user must manually |
| 192 | * call close() on those dangling channels before the end of the PHP |
| 193 | * script. |
| 194 | * |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 195 | * @param string $target The hostname to associate with this channel |
thinkerou | efbc9e7 | 2016-08-16 20:00:36 +0800 | [diff] [blame] | 196 | * @param array $args_array The arguments to pass to the Channel |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 197 | */ |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 198 | PHP_METHOD(Channel, __construct) { |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 199 | wrapped_grpc_channel *channel = Z_WRAPPED_GRPC_CHANNEL_P(getThis()); |
| 200 | zval *creds_obj = NULL; |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 201 | char *target; |
thinkerou | 1930468 | 2016-07-22 02:43:19 +0800 | [diff] [blame] | 202 | php_grpc_int target_length; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 203 | zval *args_array = NULL; |
| 204 | grpc_channel_args args; |
| 205 | HashTable *array_hash; |
Stanley Cheung | aeea102 | 2015-10-21 17:00:49 -0700 | [diff] [blame] | 206 | wrapped_grpc_channel_credentials *creds = NULL; |
Stanley Cheung | 5b3dc4a | 2017-08-03 18:00:25 -0700 | [diff] [blame^] | 207 | php_grpc_zend_resource *rsrc; |
| 208 | bool force_new = false; |
| 209 | zval *force_new_obj = NULL; |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 210 | |
Stanley Cheung | f77a4ad | 2016-02-16 09:45:51 -0800 | [diff] [blame] | 211 | /* "sa" == 1 string, 1 array */ |
| 212 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sa", &target, |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 213 | &target_length, &args_array) == FAILURE) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 214 | zend_throw_exception(spl_ce_InvalidArgumentException, |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 215 | "Channel expects a string and an array", 1 TSRMLS_CC); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 216 | return; |
| 217 | } |
Stanley Cheung | cccf929 | 2016-02-12 16:37:19 -0800 | [diff] [blame] | 218 | array_hash = Z_ARRVAL_P(args_array); |
thinkerou | ba75c01 | 2016-07-28 02:30:08 +0800 | [diff] [blame] | 219 | if (php_grpc_zend_hash_find(array_hash, "credentials", sizeof("credentials"), |
Stanley Cheung | 5b3dc4a | 2017-08-03 18:00:25 -0700 | [diff] [blame^] | 220 | (void **)&creds_obj) == SUCCESS) { |
thinkerou | ba75c01 | 2016-07-28 02:30:08 +0800 | [diff] [blame] | 221 | if (Z_TYPE_P(creds_obj) == IS_NULL) { |
Stanley Cheung | cccf929 | 2016-02-12 16:37:19 -0800 | [diff] [blame] | 222 | creds = NULL; |
thinkerou | ba75c01 | 2016-07-28 02:30:08 +0800 | [diff] [blame] | 223 | php_grpc_zend_hash_del(array_hash, "credentials", sizeof("credentials")); |
| 224 | } else if (PHP_GRPC_GET_CLASS_ENTRY(creds_obj) != |
| 225 | grpc_ce_channel_credentials) { |
Stanley Cheung | cccf929 | 2016-02-12 16:37:19 -0800 | [diff] [blame] | 226 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 227 | "credentials must be a ChannelCredentials object", |
| 228 | 1 TSRMLS_CC); |
| 229 | return; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 230 | } else { |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 231 | creds = Z_WRAPPED_GRPC_CHANNEL_CREDS_P(creds_obj); |
thinkerou | ba75c01 | 2016-07-28 02:30:08 +0800 | [diff] [blame] | 232 | php_grpc_zend_hash_del(array_hash, "credentials", sizeof("credentials")); |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 233 | } |
| 234 | } |
Stanley Cheung | 5b3dc4a | 2017-08-03 18:00:25 -0700 | [diff] [blame^] | 235 | if (php_grpc_zend_hash_find(array_hash, "force_new", sizeof("force_new"), |
| 236 | (void **)&force_new_obj) == SUCCESS) { |
| 237 | if (PHP_GRPC_BVAL_IS_TRUE(force_new_obj)) { |
| 238 | force_new = true; |
| 239 | } |
| 240 | php_grpc_zend_hash_del(array_hash, "force_new", sizeof("force_new")); |
Stanley Cheung | cccf929 | 2016-02-12 16:37:19 -0800 | [diff] [blame] | 241 | } |
Stanley Cheung | 5b3dc4a | 2017-08-03 18:00:25 -0700 | [diff] [blame^] | 242 | |
| 243 | // parse the rest of the channel args array |
| 244 | if (php_grpc_read_args_array(args_array, &args TSRMLS_CC) == FAILURE) { |
| 245 | return; |
| 246 | } |
| 247 | |
| 248 | // Construct a hashkey for the persistent channel |
| 249 | // Currently, the hashkey contains 3 parts: |
| 250 | // 1. hostname |
| 251 | // 2. hash value of the channel args array (excluding "credentials" |
| 252 | // and "force_new") |
| 253 | // 3. (optional) hash value of the ChannelCredentials object |
| 254 | php_serialize_data_t var_hash; |
| 255 | smart_str buf = {0}; |
| 256 | PHP_VAR_SERIALIZE_INIT(var_hash); |
| 257 | PHP_GRPC_VAR_SERIALIZE(&buf, args_array, &var_hash); |
| 258 | PHP_VAR_SERIALIZE_DESTROY(var_hash); |
| 259 | |
| 260 | char sha1str[41]; |
| 261 | generate_sha1_str(sha1str, PHP_GRPC_SERIALIZED_BUF_STR(buf), |
| 262 | PHP_GRPC_SERIALIZED_BUF_LEN(buf)); |
| 263 | |
| 264 | php_grpc_int key_len = target_length + strlen(sha1str); |
| 265 | if (creds != NULL && creds->hashstr != NULL) { |
| 266 | key_len += strlen(creds->hashstr); |
| 267 | } |
| 268 | char *key = malloc(key_len + 1); |
| 269 | strcpy(key, target); |
| 270 | strcat(key, sha1str); |
| 271 | if (creds != NULL && creds->hashstr != NULL) { |
| 272 | strcat(key, creds->hashstr); |
| 273 | } |
| 274 | channel->wrapper = malloc(sizeof(grpc_channel_wrapper)); |
| 275 | channel->wrapper->key = key; |
| 276 | channel->wrapper->target = target; |
| 277 | channel->wrapper->args_hashstr = sha1str; |
| 278 | if (creds != NULL && creds->hashstr != NULL) { |
| 279 | channel->wrapper->creds_hashstr = creds->hashstr; |
| 280 | } |
| 281 | gpr_mu_init(&channel->wrapper->mu); |
| 282 | smart_str_free(&buf); |
| 283 | |
| 284 | if (force_new) { |
| 285 | php_grpc_delete_persistent_list_entry(key, key_len TSRMLS_CC); |
| 286 | } |
| 287 | |
| 288 | if (creds != NULL && creds->has_call_creds) { |
| 289 | // If the ChannelCredentials object was composed with a CallCredentials |
| 290 | // object, there is no way we can tell them apart. Do NOT persist |
| 291 | // them. They should be individually destroyed. |
| 292 | create_channel(channel, target, args, creds); |
| 293 | } else if (!(PHP_GRPC_PERSISTENT_LIST_FIND(&EG(persistent_list), key, |
| 294 | key_len, rsrc))) { |
| 295 | create_and_add_channel_to_persistent_list( |
| 296 | channel, target, args, creds, key, key_len); |
| 297 | } else { |
| 298 | // Found a previously stored channel in the persistent list |
| 299 | channel_persistent_le_t *le = (channel_persistent_le_t *)rsrc->ptr; |
| 300 | if (strcmp(target, le->channel->target) != 0 || |
| 301 | strcmp(sha1str, le->channel->args_hashstr) != 0 || |
| 302 | (creds != NULL && creds->hashstr != NULL && |
| 303 | strcmp(creds->hashstr, le->channel->creds_hashstr) != 0)) { |
| 304 | // somehow hash collision |
| 305 | create_and_add_channel_to_persistent_list( |
| 306 | channel, target, args, creds, key, key_len); |
| 307 | } else { |
| 308 | channel->wrapper = le->channel; |
| 309 | } |
| 310 | } |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | /** |
Stanley Cheung | db98e08 | 2015-07-27 10:19:45 -0700 | [diff] [blame] | 314 | * Get the endpoint this call/stream is connected to |
| 315 | * @return string The URI of the endpoint |
| 316 | */ |
| 317 | PHP_METHOD(Channel, getTarget) { |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 318 | wrapped_grpc_channel *channel = Z_WRAPPED_GRPC_CHANNEL_P(getThis()); |
Stanley Cheung | 5b3dc4a | 2017-08-03 18:00:25 -0700 | [diff] [blame^] | 319 | gpr_mu_lock(&channel->wrapper->mu); |
| 320 | if (channel->wrapper->wrapped == NULL) { |
| 321 | zend_throw_exception(spl_ce_RuntimeException, |
| 322 | "Channel already closed", 1 TSRMLS_CC); |
| 323 | gpr_mu_unlock(&channel->wrapper->mu); |
| 324 | return; |
| 325 | } |
| 326 | char *target = grpc_channel_get_target(channel->wrapper->wrapped); |
| 327 | gpr_mu_unlock(&channel->wrapper->mu); |
| 328 | PHP_GRPC_RETURN_STRING(target, 1); |
Stanley Cheung | db98e08 | 2015-07-27 10:19:45 -0700 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | /** |
Stanley Cheung | e63354a | 2015-08-10 15:46:42 -0700 | [diff] [blame] | 332 | * Get the connectivity state of the channel |
thinkerou | efbc9e7 | 2016-08-16 20:00:36 +0800 | [diff] [blame] | 333 | * @param bool $try_to_connect Try to connect on the channel (optional) |
Stanley Cheung | e63354a | 2015-08-10 15:46:42 -0700 | [diff] [blame] | 334 | * @return long The grpc connectivity state |
| 335 | */ |
| 336 | PHP_METHOD(Channel, getConnectivityState) { |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 337 | wrapped_grpc_channel *channel = Z_WRAPPED_GRPC_CHANNEL_P(getThis()); |
Stanley Cheung | 5b3dc4a | 2017-08-03 18:00:25 -0700 | [diff] [blame^] | 338 | gpr_mu_lock(&channel->wrapper->mu); |
| 339 | if (channel->wrapper->wrapped == NULL) { |
| 340 | zend_throw_exception(spl_ce_RuntimeException, |
| 341 | "Channel already closed", 1 TSRMLS_CC); |
| 342 | gpr_mu_unlock(&channel->wrapper->mu); |
| 343 | return; |
| 344 | } |
| 345 | |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 346 | bool try_to_connect = false; |
| 347 | |
Stanley Cheung | e63354a | 2015-08-10 15:46:42 -0700 | [diff] [blame] | 348 | /* "|b" == 1 optional bool */ |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 349 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &try_to_connect) |
| 350 | == FAILURE) { |
Stanley Cheung | e63354a | 2015-08-10 15:46:42 -0700 | [diff] [blame] | 351 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 352 | "getConnectivityState expects a bool", 1 TSRMLS_CC); |
Stanley Cheung | 5b3dc4a | 2017-08-03 18:00:25 -0700 | [diff] [blame^] | 353 | gpr_mu_unlock(&channel->wrapper->mu); |
Stanley Cheung | e63354a | 2015-08-10 15:46:42 -0700 | [diff] [blame] | 354 | return; |
| 355 | } |
Stanley Cheung | 5b3dc4a | 2017-08-03 18:00:25 -0700 | [diff] [blame^] | 356 | int state = grpc_channel_check_connectivity_state(channel->wrapper->wrapped, |
| 357 | (int)try_to_connect); |
| 358 | // this can happen if another shared Channel object close the underlying |
| 359 | // channel |
| 360 | if (state == GRPC_CHANNEL_SHUTDOWN) { |
| 361 | channel->wrapper->wrapped = NULL; |
| 362 | } |
| 363 | gpr_mu_unlock(&channel->wrapper->mu); |
| 364 | RETURN_LONG(state); |
Stanley Cheung | e63354a | 2015-08-10 15:46:42 -0700 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | /** |
Stanley Cheung | a63fdd0 | 2015-08-11 15:11:11 -0700 | [diff] [blame] | 368 | * Watch the connectivity state of the channel until it changed |
thinkerou | efbc9e7 | 2016-08-16 20:00:36 +0800 | [diff] [blame] | 369 | * @param long $last_state The previous connectivity state of the channel |
| 370 | * @param Timeval $deadline_obj The deadline this function should wait until |
Stanley Cheung | 4c5c7b8 | 2015-08-12 16:28:58 -0700 | [diff] [blame] | 371 | * @return bool If the connectivity state changes from last_state |
| 372 | * before deadline |
Stanley Cheung | a63fdd0 | 2015-08-11 15:11:11 -0700 | [diff] [blame] | 373 | */ |
| 374 | PHP_METHOD(Channel, watchConnectivityState) { |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 375 | wrapped_grpc_channel *channel = Z_WRAPPED_GRPC_CHANNEL_P(getThis()); |
Stanley Cheung | 5b3dc4a | 2017-08-03 18:00:25 -0700 | [diff] [blame^] | 376 | gpr_mu_lock(&channel->wrapper->mu); |
| 377 | if (channel->wrapper->wrapped == NULL) { |
| 378 | zend_throw_exception(spl_ce_RuntimeException, |
| 379 | "Channel already closed", 1 TSRMLS_CC); |
| 380 | gpr_mu_unlock(&channel->wrapper->mu); |
| 381 | return; |
| 382 | } |
| 383 | |
thinkerou | 1930468 | 2016-07-22 02:43:19 +0800 | [diff] [blame] | 384 | php_grpc_long last_state; |
Stanley Cheung | a63fdd0 | 2015-08-11 15:11:11 -0700 | [diff] [blame] | 385 | zval *deadline_obj; |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 386 | |
Stanley Cheung | a63fdd0 | 2015-08-11 15:11:11 -0700 | [diff] [blame] | 387 | /* "lO" == 1 long 1 object */ |
| 388 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lO", |
Stanley Cheung | 5b3dc4a | 2017-08-03 18:00:25 -0700 | [diff] [blame^] | 389 | &last_state, &deadline_obj, |
| 390 | grpc_ce_timeval) == FAILURE) { |
Stanley Cheung | a63fdd0 | 2015-08-11 15:11:11 -0700 | [diff] [blame] | 391 | zend_throw_exception(spl_ce_InvalidArgumentException, |
Stanley Cheung | 5b3dc4a | 2017-08-03 18:00:25 -0700 | [diff] [blame^] | 392 | "watchConnectivityState expects 1 long 1 timeval", |
| 393 | 1 TSRMLS_CC); |
| 394 | gpr_mu_unlock(&channel->wrapper->mu); |
Stanley Cheung | a63fdd0 | 2015-08-11 15:11:11 -0700 | [diff] [blame] | 395 | return; |
| 396 | } |
| 397 | |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 398 | wrapped_grpc_timeval *deadline = Z_WRAPPED_GRPC_TIMEVAL_P(deadline_obj); |
Stanley Cheung | 5b3dc4a | 2017-08-03 18:00:25 -0700 | [diff] [blame^] | 399 | grpc_channel_watch_connectivity_state(channel->wrapper->wrapped, |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 400 | (grpc_connectivity_state)last_state, |
| 401 | deadline->wrapped, completion_queue, |
| 402 | NULL); |
| 403 | grpc_event event = |
Stanley Cheung | 5b3dc4a | 2017-08-03 18:00:25 -0700 | [diff] [blame^] | 404 | grpc_completion_queue_pluck(completion_queue, NULL, |
| 405 | gpr_inf_future(GPR_CLOCK_REALTIME), NULL); |
| 406 | gpr_mu_unlock(&channel->wrapper->mu); |
Stanley Cheung | 1567c0c | 2015-08-13 11:12:54 -0700 | [diff] [blame] | 407 | RETURN_BOOL(event.success); |
Stanley Cheung | a63fdd0 | 2015-08-11 15:11:11 -0700 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | /** |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 411 | * Close the channel |
thinkerou | 03dc219 | 2016-08-16 19:31:44 +0800 | [diff] [blame] | 412 | * @return void |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 413 | */ |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 414 | PHP_METHOD(Channel, close) { |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 415 | wrapped_grpc_channel *channel = Z_WRAPPED_GRPC_CHANNEL_P(getThis()); |
Stanley Cheung | 5b3dc4a | 2017-08-03 18:00:25 -0700 | [diff] [blame^] | 416 | gpr_mu_lock(&channel->wrapper->mu); |
| 417 | if (channel->wrapper->wrapped != NULL) { |
| 418 | grpc_channel_destroy(channel->wrapper->wrapped); |
| 419 | channel->wrapper->wrapped = NULL; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 420 | } |
Stanley Cheung | 5b3dc4a | 2017-08-03 18:00:25 -0700 | [diff] [blame^] | 421 | |
| 422 | php_grpc_delete_persistent_list_entry(channel->wrapper->key, |
| 423 | strlen(channel->wrapper->key) |
| 424 | TSRMLS_CC); |
| 425 | gpr_mu_unlock(&channel->wrapper->mu); |
| 426 | } |
| 427 | |
| 428 | // Delete an entry from the persistent list |
| 429 | // Note: this does not destroy or close the underlying grpc_channel |
| 430 | void php_grpc_delete_persistent_list_entry(char *key, php_grpc_int key_len |
| 431 | TSRMLS_DC) { |
| 432 | php_grpc_zend_resource *rsrc; |
| 433 | gpr_mu_lock(&global_persistent_list_mu); |
| 434 | if (PHP_GRPC_PERSISTENT_LIST_FIND(&EG(persistent_list), key, |
| 435 | key_len, rsrc)) { |
| 436 | channel_persistent_le_t *le; |
| 437 | le = (channel_persistent_le_t *)rsrc->ptr; |
| 438 | le->channel = NULL; |
| 439 | php_grpc_zend_hash_del(&EG(persistent_list), key, key_len+1); |
| 440 | } |
| 441 | gpr_mu_unlock(&global_persistent_list_mu); |
| 442 | } |
| 443 | |
| 444 | // A destructor associated with each list entry from the persistent list |
| 445 | static void php_grpc_channel_plink_dtor(php_grpc_zend_resource *rsrc |
| 446 | TSRMLS_DC) { |
| 447 | channel_persistent_le_t *le = (channel_persistent_le_t *)rsrc->ptr; |
| 448 | if (le->channel != NULL) { |
| 449 | gpr_mu_lock(&le->channel->mu); |
| 450 | if (le->channel->wrapped != NULL) { |
| 451 | grpc_channel_destroy(le->channel->wrapped); |
| 452 | free(le->channel->key); |
| 453 | free(le->channel); |
| 454 | } |
| 455 | gpr_mu_unlock(&le->channel->mu); |
| 456 | } |
| 457 | free(le); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 458 | } |
| 459 | |
Stanley Cheung | 6a5c83d | 2017-02-16 12:25:32 -0800 | [diff] [blame] | 460 | ZEND_BEGIN_ARG_INFO_EX(arginfo_construct, 0, 0, 2) |
| 461 | ZEND_ARG_INFO(0, target) |
| 462 | ZEND_ARG_INFO(0, args) |
| 463 | ZEND_END_ARG_INFO() |
| 464 | |
| 465 | ZEND_BEGIN_ARG_INFO_EX(arginfo_getTarget, 0, 0, 0) |
| 466 | ZEND_END_ARG_INFO() |
| 467 | |
| 468 | ZEND_BEGIN_ARG_INFO_EX(arginfo_getConnectivityState, 0, 0, 0) |
| 469 | ZEND_ARG_INFO(0, try_to_connect) |
| 470 | ZEND_END_ARG_INFO() |
| 471 | |
| 472 | ZEND_BEGIN_ARG_INFO_EX(arginfo_watchConnectivityState, 0, 0, 2) |
| 473 | ZEND_ARG_INFO(0, last_state) |
| 474 | ZEND_ARG_INFO(0, deadline) |
| 475 | ZEND_END_ARG_INFO() |
| 476 | |
| 477 | ZEND_BEGIN_ARG_INFO_EX(arginfo_close, 0, 0, 0) |
| 478 | ZEND_END_ARG_INFO() |
| 479 | |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 480 | static zend_function_entry channel_methods[] = { |
Stanley Cheung | 6a5c83d | 2017-02-16 12:25:32 -0800 | [diff] [blame] | 481 | PHP_ME(Channel, __construct, arginfo_construct, |
| 482 | ZEND_ACC_PUBLIC | ZEND_ACC_CTOR) |
| 483 | PHP_ME(Channel, getTarget, arginfo_getTarget, |
| 484 | ZEND_ACC_PUBLIC) |
| 485 | PHP_ME(Channel, getConnectivityState, arginfo_getConnectivityState, |
| 486 | ZEND_ACC_PUBLIC) |
| 487 | PHP_ME(Channel, watchConnectivityState, arginfo_watchConnectivityState, |
| 488 | ZEND_ACC_PUBLIC) |
| 489 | PHP_ME(Channel, close, arginfo_close, |
| 490 | ZEND_ACC_PUBLIC) |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 491 | PHP_FE_END |
| 492 | }; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 493 | |
Stanley Cheung | 5b3dc4a | 2017-08-03 18:00:25 -0700 | [diff] [blame^] | 494 | GRPC_STARTUP_FUNCTION(channel) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 495 | zend_class_entry ce; |
| 496 | INIT_CLASS_ENTRY(ce, "Grpc\\Channel", channel_methods); |
| 497 | ce.create_object = create_wrapped_grpc_channel; |
| 498 | grpc_ce_channel = zend_register_internal_class(&ce TSRMLS_CC); |
Stanley Cheung | 5b3dc4a | 2017-08-03 18:00:25 -0700 | [diff] [blame^] | 499 | le_plink = zend_register_list_destructors_ex( |
| 500 | NULL, php_grpc_channel_plink_dtor, "Persistent Channel", module_number); |
thinkerou | 5dafd82 | 2016-07-28 22:43:38 +0800 | [diff] [blame] | 501 | PHP_GRPC_INIT_HANDLER(wrapped_grpc_channel, channel_ce_handlers); |
Stanley Cheung | 5b3dc4a | 2017-08-03 18:00:25 -0700 | [diff] [blame^] | 502 | return SUCCESS; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 503 | } |