blob: a9d7374f2a9b09528f839d77db3a900141a31db5 [file] [log] [blame]
Craig Tiller1a61b172015-02-16 11:53:47 -08001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2015 gRPC authors.
Craig Tiller1a61b172015-02-16 11:53:47 -08004 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005 * 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 Tiller1a61b172015-02-16 11:53:47 -08008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
Craig Tiller1a61b172015-02-16 11:53:47 -080010 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011 * 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 Tiller1a61b172015-02-16 11:53:47 -080016 *
17 */
18
mlumishb892a272014-12-09 16:28:23 -080019#include "channel.h"
20
21#ifdef HAVE_CONFIG_H
22#include "config.h"
23#endif
24
murgatroid998242ba72015-04-01 15:29:44 -070025#include <php.h>
26#include <php_ini.h>
27#include <ext/standard/info.h>
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -070028#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
murgatroid998242ba72015-04-01 15:29:44 -070035#include <ext/spl/spl_exceptions.h>
mlumishb892a272014-12-09 16:28:23 -080036#include "php_grpc.h"
37
murgatroid998242ba72015-04-01 15:29:44 -070038#include <zend_exceptions.h>
mlumishb892a272014-12-09 16:28:23 -080039
40#include <stdbool.h>
41
murgatroid998242ba72015-04-01 15:29:44 -070042#include <grpc/grpc.h>
murgatroid998242ba72015-04-01 15:29:44 -070043#include <grpc/grpc_security.h>
Zhouyihai Dingd3b55242018-01-22 12:52:56 -080044#include <grpc/support/alloc.h>
mlumishb892a272014-12-09 16:28:23 -080045
Stanley Cheunga63fdd02015-08-11 15:11:11 -070046#include "completion_queue.h"
Stanley Cheung9c0b35e2015-10-21 17:07:56 -070047#include "channel_credentials.h"
Stanley Cheunga63fdd02015-08-11 15:11:11 -070048#include "server.h"
49#include "timeval.h"
mlumishb892a272014-12-09 16:28:23 -080050
Xiaoguang Sun8a929a92015-03-13 14:22:31 +080051zend_class_entry *grpc_ce_channel;
thinkeroudba5b0c2016-07-27 18:39:16 +080052#if PHP_MAJOR_VERSION >= 7
53static zend_object_handlers channel_ce_handlers;
54#endif
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -070055static gpr_mu global_persistent_list_mu;
56int le_plink;
Xiaoguang Sun8a929a92015-03-13 14:22:31 +080057
mlumishb892a272014-12-09 16:28:23 -080058/* Frees and destroys an instance of wrapped_grpc_channel */
thinkerou011d1ef2016-07-27 09:44:49 +080059PHP_GRPC_FREE_WRAPPED_FUNC_START(wrapped_grpc_channel)
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -070060 if (p->wrapper != NULL) {
61 gpr_mu_lock(&p->wrapper->mu);
62 if (p->wrapper->wrapped != NULL) {
63 php_grpc_zend_resource *rsrc;
64 php_grpc_int key_len = strlen(p->wrapper->key);
65 // only destroy the channel here if not found in the persistent list
66 gpr_mu_lock(&global_persistent_list_mu);
67 if (!(PHP_GRPC_PERSISTENT_LIST_FIND(&EG(persistent_list), p->wrapper->key,
68 key_len, rsrc))) {
69 grpc_channel_destroy(p->wrapper->wrapped);
Stanley Cheung5d559482017-08-22 13:15:01 -070070 free(p->wrapper->target);
71 free(p->wrapper->args_hashstr);
Zhouyihai Ding4b9f8d82018-01-22 12:58:21 -080072 if (p->wrapper->creds_hashstr != NULL) {
73 free(p->wrapper->creds_hashstr);
74 p->wrapper->creds_hashstr = NULL;
75 }
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -070076 }
77 gpr_mu_unlock(&global_persistent_list_mu);
78 }
79 gpr_mu_unlock(&p->wrapper->mu);
mlumishb892a272014-12-09 16:28:23 -080080 }
thinkerou011d1ef2016-07-27 09:44:49 +080081PHP_GRPC_FREE_WRAPPED_FUNC_END()
82
mlumishb892a272014-12-09 16:28:23 -080083/* Initializes an instance of wrapped_grpc_channel to be associated with an
84 * object of a class specified by class_type */
thinkeroudba5b0c2016-07-27 18:39:16 +080085php_grpc_zend_object create_wrapped_grpc_channel(zend_class_entry *class_type
86 TSRMLS_DC) {
thinkerouba75c012016-07-28 02:30:08 +080087 PHP_GRPC_ALLOC_CLASS_OBJECT(wrapped_grpc_channel);
mlumishb892a272014-12-09 16:28:23 -080088 zend_object_std_init(&intern->std, class_type TSRMLS_CC);
89 object_properties_init(&intern->std, class_type);
thinkeroudc673c52016-07-28 09:49:38 +080090 PHP_GRPC_FREE_CLASS_OBJECT(wrapped_grpc_channel, channel_ce_handlers);
thinkeroudba5b0c2016-07-27 18:39:16 +080091}
thinkerou6f9d30b2016-07-27 03:19:03 +080092
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -070093int php_grpc_read_args_array(zval *args_array,
94 grpc_channel_args *args TSRMLS_DC) {
mlumishb892a272014-12-09 16:28:23 -080095 HashTable *array_hash;
mlumishb892a272014-12-09 16:28:23 -080096 int args_index;
mlumishb892a272014-12-09 16:28:23 -080097 array_hash = Z_ARRVAL_P(args_array);
thinkerou6f9d30b2016-07-27 03:19:03 +080098 if (!array_hash) {
99 zend_throw_exception(spl_ce_InvalidArgumentException,
Stanley Cheungc1f25fb2016-07-29 13:41:22 -0700100 "array_hash is NULL", 1 TSRMLS_CC);
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700101 return FAILURE;
thinkerou6f9d30b2016-07-27 03:19:03 +0800102 }
mlumishb892a272014-12-09 16:28:23 -0800103 args->num_args = zend_hash_num_elements(array_hash);
104 args->args = ecalloc(args->num_args, sizeof(grpc_arg));
105 args_index = 0;
thinkerou6f9d30b2016-07-27 03:19:03 +0800106
thinkerouba75c012016-07-28 02:30:08 +0800107 char *key = NULL;
108 zval *data;
109 int key_type;
110
111 PHP_GRPC_HASH_FOREACH_STR_KEY_VAL_START(array_hash, key, key_type, data)
112 if (key_type != HASH_KEY_IS_STRING) {
mlumishb892a272014-12-09 16:28:23 -0800113 zend_throw_exception(spl_ce_InvalidArgumentException,
Craig Tillerb5dcec52015-01-13 11:13:42 -0800114 "args keys must be strings", 1 TSRMLS_CC);
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700115 return FAILURE;
mlumishb892a272014-12-09 16:28:23 -0800116 }
117 args->args[args_index].key = key;
thinkeroua3730b72016-07-20 16:59:54 +0800118 switch (Z_TYPE_P(data)) {
119 case IS_LONG:
120 args->args[args_index].value.integer = (int)Z_LVAL_P(data);
121 args->args[args_index].type = GRPC_ARG_INTEGER;
122 break;
123 case IS_STRING:
124 args->args[args_index].value.string = Z_STRVAL_P(data);
125 args->args[args_index].type = GRPC_ARG_STRING;
126 break;
127 default:
128 zend_throw_exception(spl_ce_InvalidArgumentException,
thinkerouba75c012016-07-28 02:30:08 +0800129 "args values must be int or string", 1 TSRMLS_CC);
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700130 return FAILURE;
thinkeroua3730b72016-07-20 16:59:54 +0800131 }
132 args_index++;
thinkerouba75c012016-07-28 02:30:08 +0800133 PHP_GRPC_HASH_FOREACH_END()
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700134 return SUCCESS;
135}
136
137void generate_sha1_str(char *sha1str, char *str, php_grpc_int len) {
138 PHP_SHA1_CTX context;
139 unsigned char digest[20];
140 sha1str[0] = '\0';
141 PHP_SHA1Init(&context);
142 PHP_GRPC_SHA1Update(&context, str, len);
143 PHP_SHA1Final(digest, &context);
144 make_sha1_digest(sha1str, digest);
145}
146
147void create_channel(
148 wrapped_grpc_channel *channel,
149 char *target,
150 grpc_channel_args args,
151 wrapped_grpc_channel_credentials *creds) {
152 if (creds == NULL) {
153 channel->wrapper->wrapped = grpc_insecure_channel_create(target, &args,
154 NULL);
155 } else {
156 channel->wrapper->wrapped =
157 grpc_secure_channel_create(creds->wrapped, target, &args, NULL);
158 }
159 efree(args.args);
160}
161
162void create_and_add_channel_to_persistent_list(
163 wrapped_grpc_channel *channel,
164 char *target,
165 grpc_channel_args args,
166 wrapped_grpc_channel_credentials *creds,
167 char *key,
Stanley Cheung5d559482017-08-22 13:15:01 -0700168 php_grpc_int key_len TSRMLS_DC) {
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700169 php_grpc_zend_resource new_rsrc;
170 channel_persistent_le_t *le;
171 // this links each persistent list entry to a destructor
172 new_rsrc.type = le_plink;
173 le = malloc(sizeof(channel_persistent_le_t));
174
175 create_channel(channel, target, args, creds);
176
177 le->channel = channel->wrapper;
178 new_rsrc.ptr = le;
179 gpr_mu_lock(&global_persistent_list_mu);
180 PHP_GRPC_PERSISTENT_LIST_UPDATE(&EG(persistent_list), key, key_len,
181 (void *)&new_rsrc);
182 gpr_mu_unlock(&global_persistent_list_mu);
thinkerou6f9d30b2016-07-27 03:19:03 +0800183}
thinkeroua3730b72016-07-20 16:59:54 +0800184
mlumishb892a272014-12-09 16:28:23 -0800185/**
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700186 * Construct an instance of the Channel class.
187 *
188 * By default, the underlying grpc_channel is "persistent". That is, given
189 * the same set of parameters passed to the constructor, the same underlying
190 * grpc_channel will be returned.
191 *
192 * If the $args array contains a "credentials" key mapping to a
193 * ChannelCredentials object, a secure channel will be created with those
194 * credentials.
195 *
196 * If the $args array contains a "force_new" key mapping to a boolean value
Stanley Cheung5d559482017-08-22 13:15:01 -0700197 * of "true", a new and separate underlying grpc_channel will be created
198 * and returned. This will not affect existing channels.
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700199 *
mlumishb892a272014-12-09 16:28:23 -0800200 * @param string $target The hostname to associate with this channel
thinkerouefbc9e72016-08-16 20:00:36 +0800201 * @param array $args_array The arguments to pass to the Channel
mlumishb892a272014-12-09 16:28:23 -0800202 */
Craig Tillerb5dcec52015-01-13 11:13:42 -0800203PHP_METHOD(Channel, __construct) {
thinkeroua3730b72016-07-20 16:59:54 +0800204 wrapped_grpc_channel *channel = Z_WRAPPED_GRPC_CHANNEL_P(getThis());
205 zval *creds_obj = NULL;
thinkeroua3730b72016-07-20 16:59:54 +0800206 char *target;
thinkerou19304682016-07-22 02:43:19 +0800207 php_grpc_int target_length;
mlumishb892a272014-12-09 16:28:23 -0800208 zval *args_array = NULL;
209 grpc_channel_args args;
210 HashTable *array_hash;
Stanley Cheungaeea1022015-10-21 17:00:49 -0700211 wrapped_grpc_channel_credentials *creds = NULL;
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700212 php_grpc_zend_resource *rsrc;
213 bool force_new = false;
214 zval *force_new_obj = NULL;
thinkeroua3730b72016-07-20 16:59:54 +0800215
Stanley Cheungf77a4ad2016-02-16 09:45:51 -0800216 /* "sa" == 1 string, 1 array */
217 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sa", &target,
Craig Tillerb5dcec52015-01-13 11:13:42 -0800218 &target_length, &args_array) == FAILURE) {
mlumishb892a272014-12-09 16:28:23 -0800219 zend_throw_exception(spl_ce_InvalidArgumentException,
Craig Tillerb5dcec52015-01-13 11:13:42 -0800220 "Channel expects a string and an array", 1 TSRMLS_CC);
mlumishb892a272014-12-09 16:28:23 -0800221 return;
222 }
Stanley Cheungcccf9292016-02-12 16:37:19 -0800223 array_hash = Z_ARRVAL_P(args_array);
thinkerouba75c012016-07-28 02:30:08 +0800224 if (php_grpc_zend_hash_find(array_hash, "credentials", sizeof("credentials"),
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700225 (void **)&creds_obj) == SUCCESS) {
thinkerouba75c012016-07-28 02:30:08 +0800226 if (Z_TYPE_P(creds_obj) == IS_NULL) {
Stanley Cheungcccf9292016-02-12 16:37:19 -0800227 creds = NULL;
thinkerouba75c012016-07-28 02:30:08 +0800228 php_grpc_zend_hash_del(array_hash, "credentials", sizeof("credentials"));
229 } else if (PHP_GRPC_GET_CLASS_ENTRY(creds_obj) !=
230 grpc_ce_channel_credentials) {
Stanley Cheungcccf9292016-02-12 16:37:19 -0800231 zend_throw_exception(spl_ce_InvalidArgumentException,
232 "credentials must be a ChannelCredentials object",
233 1 TSRMLS_CC);
234 return;
mlumishb892a272014-12-09 16:28:23 -0800235 } else {
thinkeroua3730b72016-07-20 16:59:54 +0800236 creds = Z_WRAPPED_GRPC_CHANNEL_CREDS_P(creds_obj);
thinkerouba75c012016-07-28 02:30:08 +0800237 php_grpc_zend_hash_del(array_hash, "credentials", sizeof("credentials"));
thinkeroua3730b72016-07-20 16:59:54 +0800238 }
239 }
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700240 if (php_grpc_zend_hash_find(array_hash, "force_new", sizeof("force_new"),
241 (void **)&force_new_obj) == SUCCESS) {
242 if (PHP_GRPC_BVAL_IS_TRUE(force_new_obj)) {
243 force_new = true;
244 }
245 php_grpc_zend_hash_del(array_hash, "force_new", sizeof("force_new"));
Stanley Cheungcccf9292016-02-12 16:37:19 -0800246 }
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700247
248 // parse the rest of the channel args array
249 if (php_grpc_read_args_array(args_array, &args TSRMLS_CC) == FAILURE) {
Zhouyihai Dingb6cdfca2018-01-13 16:25:28 -0800250 efree(args.args);
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700251 return;
252 }
253
254 // Construct a hashkey for the persistent channel
255 // Currently, the hashkey contains 3 parts:
256 // 1. hostname
257 // 2. hash value of the channel args array (excluding "credentials"
258 // and "force_new")
259 // 3. (optional) hash value of the ChannelCredentials object
260 php_serialize_data_t var_hash;
261 smart_str buf = {0};
262 PHP_VAR_SERIALIZE_INIT(var_hash);
263 PHP_GRPC_VAR_SERIALIZE(&buf, args_array, &var_hash);
264 PHP_VAR_SERIALIZE_DESTROY(var_hash);
265
266 char sha1str[41];
267 generate_sha1_str(sha1str, PHP_GRPC_SERIALIZED_BUF_STR(buf),
268 PHP_GRPC_SERIALIZED_BUF_LEN(buf));
269
270 php_grpc_int key_len = target_length + strlen(sha1str);
271 if (creds != NULL && creds->hashstr != NULL) {
272 key_len += strlen(creds->hashstr);
273 }
274 char *key = malloc(key_len + 1);
275 strcpy(key, target);
276 strcat(key, sha1str);
277 if (creds != NULL && creds->hashstr != NULL) {
278 strcat(key, creds->hashstr);
279 }
280 channel->wrapper = malloc(sizeof(grpc_channel_wrapper));
281 channel->wrapper->key = key;
Stanley Cheung5d559482017-08-22 13:15:01 -0700282 channel->wrapper->target = strdup(target);
283 channel->wrapper->args_hashstr = strdup(sha1str);
Zhouyihai Ding4b9f8d82018-01-22 12:58:21 -0800284 channel->wrapper->creds_hashstr = NULL;
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700285 if (creds != NULL && creds->hashstr != NULL) {
Zhouyihai Ding4b9f8d82018-01-22 12:58:21 -0800286 php_grpc_int creds_hashstr_len = strlen(creds->hashstr);
287 char *channel_creds_hashstr = malloc(creds_hashstr_len + 1);
288 strcpy(channel_creds_hashstr, creds->hashstr);
289 channel->wrapper->creds_hashstr = channel_creds_hashstr;
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700290 }
Zhouyihai Ding4b9f8d82018-01-22 12:58:21 -0800291
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700292 gpr_mu_init(&channel->wrapper->mu);
293 smart_str_free(&buf);
294
Stanley Cheung5d559482017-08-22 13:15:01 -0700295 if (force_new || (creds != NULL && creds->has_call_creds)) {
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700296 // If the ChannelCredentials object was composed with a CallCredentials
297 // object, there is no way we can tell them apart. Do NOT persist
298 // them. They should be individually destroyed.
299 create_channel(channel, target, args, creds);
300 } else if (!(PHP_GRPC_PERSISTENT_LIST_FIND(&EG(persistent_list), key,
301 key_len, rsrc))) {
302 create_and_add_channel_to_persistent_list(
Stanley Cheung5d559482017-08-22 13:15:01 -0700303 channel, target, args, creds, key, key_len TSRMLS_CC);
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700304 } else {
305 // Found a previously stored channel in the persistent list
306 channel_persistent_le_t *le = (channel_persistent_le_t *)rsrc->ptr;
307 if (strcmp(target, le->channel->target) != 0 ||
308 strcmp(sha1str, le->channel->args_hashstr) != 0 ||
309 (creds != NULL && creds->hashstr != NULL &&
310 strcmp(creds->hashstr, le->channel->creds_hashstr) != 0)) {
311 // somehow hash collision
312 create_and_add_channel_to_persistent_list(
Stanley Cheung5d559482017-08-22 13:15:01 -0700313 channel, target, args, creds, key, key_len TSRMLS_CC);
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700314 } else {
Zhouyihai Dingb6cdfca2018-01-13 16:25:28 -0800315 efree(args.args);
Zhouyihai Ding4b9f8d82018-01-22 12:58:21 -0800316 if (channel->wrapper->creds_hashstr != NULL){
317 free(channel->wrapper->creds_hashstr);
318 channel->wrapper->creds_hashstr = NULL;
319 }
320 free(channel->wrapper->creds_hashstr);
Zhouyihai Ding8a845932018-01-22 13:39:37 -0800321 free(channel->wrapper->key);
322 free(channel->wrapper->target);
323 free(channel->wrapper->args_hashstr);
324 free(channel->wrapper);
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700325 channel->wrapper = le->channel;
326 }
327 }
mlumishb892a272014-12-09 16:28:23 -0800328}
329
330/**
Stanley Cheungdb98e082015-07-27 10:19:45 -0700331 * Get the endpoint this call/stream is connected to
332 * @return string The URI of the endpoint
333 */
334PHP_METHOD(Channel, getTarget) {
thinkeroua3730b72016-07-20 16:59:54 +0800335 wrapped_grpc_channel *channel = Z_WRAPPED_GRPC_CHANNEL_P(getThis());
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700336 gpr_mu_lock(&channel->wrapper->mu);
337 if (channel->wrapper->wrapped == NULL) {
338 zend_throw_exception(spl_ce_RuntimeException,
339 "Channel already closed", 1 TSRMLS_CC);
340 gpr_mu_unlock(&channel->wrapper->mu);
341 return;
342 }
343 char *target = grpc_channel_get_target(channel->wrapper->wrapped);
344 gpr_mu_unlock(&channel->wrapper->mu);
Zhouyihai Dingd3b55242018-01-22 12:52:56 -0800345 PHP_GRPC_RETVAL_STRING(target, 1);
346 gpr_free(target);
Stanley Cheungdb98e082015-07-27 10:19:45 -0700347}
348
349/**
Stanley Cheunge63354a2015-08-10 15:46:42 -0700350 * Get the connectivity state of the channel
thinkerouefbc9e72016-08-16 20:00:36 +0800351 * @param bool $try_to_connect Try to connect on the channel (optional)
Stanley Cheunge63354a2015-08-10 15:46:42 -0700352 * @return long The grpc connectivity state
353 */
354PHP_METHOD(Channel, getConnectivityState) {
thinkeroua3730b72016-07-20 16:59:54 +0800355 wrapped_grpc_channel *channel = Z_WRAPPED_GRPC_CHANNEL_P(getThis());
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700356 gpr_mu_lock(&channel->wrapper->mu);
357 if (channel->wrapper->wrapped == NULL) {
358 zend_throw_exception(spl_ce_RuntimeException,
359 "Channel already closed", 1 TSRMLS_CC);
360 gpr_mu_unlock(&channel->wrapper->mu);
361 return;
362 }
363
thinkeroua3730b72016-07-20 16:59:54 +0800364 bool try_to_connect = false;
365
Stanley Cheunge63354a2015-08-10 15:46:42 -0700366 /* "|b" == 1 optional bool */
thinkeroua3730b72016-07-20 16:59:54 +0800367 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &try_to_connect)
368 == FAILURE) {
Stanley Cheunge63354a2015-08-10 15:46:42 -0700369 zend_throw_exception(spl_ce_InvalidArgumentException,
370 "getConnectivityState expects a bool", 1 TSRMLS_CC);
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700371 gpr_mu_unlock(&channel->wrapper->mu);
Stanley Cheunge63354a2015-08-10 15:46:42 -0700372 return;
373 }
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700374 int state = grpc_channel_check_connectivity_state(channel->wrapper->wrapped,
375 (int)try_to_connect);
376 // this can happen if another shared Channel object close the underlying
377 // channel
378 if (state == GRPC_CHANNEL_SHUTDOWN) {
379 channel->wrapper->wrapped = NULL;
380 }
381 gpr_mu_unlock(&channel->wrapper->mu);
382 RETURN_LONG(state);
Stanley Cheunge63354a2015-08-10 15:46:42 -0700383}
384
385/**
Stanley Cheunga63fdd02015-08-11 15:11:11 -0700386 * Watch the connectivity state of the channel until it changed
thinkerouefbc9e72016-08-16 20:00:36 +0800387 * @param long $last_state The previous connectivity state of the channel
388 * @param Timeval $deadline_obj The deadline this function should wait until
Stanley Cheung4c5c7b82015-08-12 16:28:58 -0700389 * @return bool If the connectivity state changes from last_state
390 * before deadline
Stanley Cheunga63fdd02015-08-11 15:11:11 -0700391 */
392PHP_METHOD(Channel, watchConnectivityState) {
thinkeroua3730b72016-07-20 16:59:54 +0800393 wrapped_grpc_channel *channel = Z_WRAPPED_GRPC_CHANNEL_P(getThis());
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700394 gpr_mu_lock(&channel->wrapper->mu);
395 if (channel->wrapper->wrapped == NULL) {
396 zend_throw_exception(spl_ce_RuntimeException,
397 "Channel already closed", 1 TSRMLS_CC);
398 gpr_mu_unlock(&channel->wrapper->mu);
399 return;
400 }
401
thinkerou19304682016-07-22 02:43:19 +0800402 php_grpc_long last_state;
Stanley Cheunga63fdd02015-08-11 15:11:11 -0700403 zval *deadline_obj;
thinkeroua3730b72016-07-20 16:59:54 +0800404
Stanley Cheunga63fdd02015-08-11 15:11:11 -0700405 /* "lO" == 1 long 1 object */
406 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lO",
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700407 &last_state, &deadline_obj,
408 grpc_ce_timeval) == FAILURE) {
Stanley Cheunga63fdd02015-08-11 15:11:11 -0700409 zend_throw_exception(spl_ce_InvalidArgumentException,
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700410 "watchConnectivityState expects 1 long 1 timeval",
411 1 TSRMLS_CC);
412 gpr_mu_unlock(&channel->wrapper->mu);
Stanley Cheunga63fdd02015-08-11 15:11:11 -0700413 return;
414 }
415
thinkeroua3730b72016-07-20 16:59:54 +0800416 wrapped_grpc_timeval *deadline = Z_WRAPPED_GRPC_TIMEVAL_P(deadline_obj);
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700417 grpc_channel_watch_connectivity_state(channel->wrapper->wrapped,
thinkeroua3730b72016-07-20 16:59:54 +0800418 (grpc_connectivity_state)last_state,
419 deadline->wrapped, completion_queue,
420 NULL);
421 grpc_event event =
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700422 grpc_completion_queue_pluck(completion_queue, NULL,
423 gpr_inf_future(GPR_CLOCK_REALTIME), NULL);
424 gpr_mu_unlock(&channel->wrapper->mu);
Stanley Cheung1567c0c2015-08-13 11:12:54 -0700425 RETURN_BOOL(event.success);
Stanley Cheunga63fdd02015-08-11 15:11:11 -0700426}
427
428/**
mlumishb892a272014-12-09 16:28:23 -0800429 * Close the channel
thinkerou03dc2192016-08-16 19:31:44 +0800430 * @return void
mlumishb892a272014-12-09 16:28:23 -0800431 */
Craig Tillerb5dcec52015-01-13 11:13:42 -0800432PHP_METHOD(Channel, close) {
thinkeroua3730b72016-07-20 16:59:54 +0800433 wrapped_grpc_channel *channel = Z_WRAPPED_GRPC_CHANNEL_P(getThis());
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700434 gpr_mu_lock(&channel->wrapper->mu);
435 if (channel->wrapper->wrapped != NULL) {
436 grpc_channel_destroy(channel->wrapper->wrapped);
Stanley Cheung5d559482017-08-22 13:15:01 -0700437 free(channel->wrapper->target);
438 free(channel->wrapper->args_hashstr);
Zhouyihai Ding4b9f8d82018-01-22 12:58:21 -0800439 if (channel->wrapper->creds_hashstr != NULL) {
440 free(channel->wrapper->creds_hashstr);
441 channel->wrapper->creds_hashstr = NULL;
442 }
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700443 channel->wrapper->wrapped = NULL;
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700444
Stanley Cheung5d559482017-08-22 13:15:01 -0700445 php_grpc_delete_persistent_list_entry(channel->wrapper->key,
446 strlen(channel->wrapper->key)
447 TSRMLS_CC);
448 }
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700449 gpr_mu_unlock(&channel->wrapper->mu);
450}
451
452// Delete an entry from the persistent list
453// Note: this does not destroy or close the underlying grpc_channel
454void php_grpc_delete_persistent_list_entry(char *key, php_grpc_int key_len
455 TSRMLS_DC) {
456 php_grpc_zend_resource *rsrc;
457 gpr_mu_lock(&global_persistent_list_mu);
458 if (PHP_GRPC_PERSISTENT_LIST_FIND(&EG(persistent_list), key,
459 key_len, rsrc)) {
460 channel_persistent_le_t *le;
461 le = (channel_persistent_le_t *)rsrc->ptr;
462 le->channel = NULL;
463 php_grpc_zend_hash_del(&EG(persistent_list), key, key_len+1);
Zhouyihai Ding33af9aa2018-01-22 12:54:42 -0800464 free(le);
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700465 }
466 gpr_mu_unlock(&global_persistent_list_mu);
467}
468
469// A destructor associated with each list entry from the persistent list
470static void php_grpc_channel_plink_dtor(php_grpc_zend_resource *rsrc
471 TSRMLS_DC) {
472 channel_persistent_le_t *le = (channel_persistent_le_t *)rsrc->ptr;
473 if (le->channel != NULL) {
474 gpr_mu_lock(&le->channel->mu);
475 if (le->channel->wrapped != NULL) {
476 grpc_channel_destroy(le->channel->wrapped);
Stanley Cheung5d559482017-08-22 13:15:01 -0700477 free(le->channel->target);
478 free(le->channel->args_hashstr);
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700479 }
480 gpr_mu_unlock(&le->channel->mu);
481 }
mlumishb892a272014-12-09 16:28:23 -0800482}
483
Stanley Cheung6a5c83d2017-02-16 12:25:32 -0800484ZEND_BEGIN_ARG_INFO_EX(arginfo_construct, 0, 0, 2)
485 ZEND_ARG_INFO(0, target)
486 ZEND_ARG_INFO(0, args)
487ZEND_END_ARG_INFO()
488
489ZEND_BEGIN_ARG_INFO_EX(arginfo_getTarget, 0, 0, 0)
490ZEND_END_ARG_INFO()
491
492ZEND_BEGIN_ARG_INFO_EX(arginfo_getConnectivityState, 0, 0, 0)
493 ZEND_ARG_INFO(0, try_to_connect)
494ZEND_END_ARG_INFO()
495
496ZEND_BEGIN_ARG_INFO_EX(arginfo_watchConnectivityState, 0, 0, 2)
497 ZEND_ARG_INFO(0, last_state)
498 ZEND_ARG_INFO(0, deadline)
499ZEND_END_ARG_INFO()
500
501ZEND_BEGIN_ARG_INFO_EX(arginfo_close, 0, 0, 0)
502ZEND_END_ARG_INFO()
503
mlumishb892a272014-12-09 16:28:23 -0800504static zend_function_entry channel_methods[] = {
Stanley Cheung6a5c83d2017-02-16 12:25:32 -0800505 PHP_ME(Channel, __construct, arginfo_construct,
506 ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
507 PHP_ME(Channel, getTarget, arginfo_getTarget,
508 ZEND_ACC_PUBLIC)
509 PHP_ME(Channel, getConnectivityState, arginfo_getConnectivityState,
510 ZEND_ACC_PUBLIC)
511 PHP_ME(Channel, watchConnectivityState, arginfo_watchConnectivityState,
512 ZEND_ACC_PUBLIC)
513 PHP_ME(Channel, close, arginfo_close,
514 ZEND_ACC_PUBLIC)
thinkeroua3730b72016-07-20 16:59:54 +0800515 PHP_FE_END
516};
mlumishb892a272014-12-09 16:28:23 -0800517
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700518GRPC_STARTUP_FUNCTION(channel) {
mlumishb892a272014-12-09 16:28:23 -0800519 zend_class_entry ce;
520 INIT_CLASS_ENTRY(ce, "Grpc\\Channel", channel_methods);
521 ce.create_object = create_wrapped_grpc_channel;
522 grpc_ce_channel = zend_register_internal_class(&ce TSRMLS_CC);
Stanley Cheung5d559482017-08-22 13:15:01 -0700523 gpr_mu_init(&global_persistent_list_mu);
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700524 le_plink = zend_register_list_destructors_ex(
525 NULL, php_grpc_channel_plink_dtor, "Persistent Channel", module_number);
thinkerou5dafd822016-07-28 22:43:38 +0800526 PHP_GRPC_INIT_HANDLER(wrapped_grpc_channel, channel_ce_handlers);
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700527 return SUCCESS;
mlumishb892a272014-12-09 16:28:23 -0800528}