blob: 11b6cfbae791f6491947c9f8b105f36c046d3e22 [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);
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700321 channel->wrapper = le->channel;
322 }
323 }
mlumishb892a272014-12-09 16:28:23 -0800324}
325
326/**
Stanley Cheungdb98e082015-07-27 10:19:45 -0700327 * Get the endpoint this call/stream is connected to
328 * @return string The URI of the endpoint
329 */
330PHP_METHOD(Channel, getTarget) {
thinkeroua3730b72016-07-20 16:59:54 +0800331 wrapped_grpc_channel *channel = Z_WRAPPED_GRPC_CHANNEL_P(getThis());
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700332 gpr_mu_lock(&channel->wrapper->mu);
333 if (channel->wrapper->wrapped == NULL) {
334 zend_throw_exception(spl_ce_RuntimeException,
335 "Channel already closed", 1 TSRMLS_CC);
336 gpr_mu_unlock(&channel->wrapper->mu);
337 return;
338 }
339 char *target = grpc_channel_get_target(channel->wrapper->wrapped);
340 gpr_mu_unlock(&channel->wrapper->mu);
Zhouyihai Dingd3b55242018-01-22 12:52:56 -0800341 PHP_GRPC_RETVAL_STRING(target, 1);
342 gpr_free(target);
Stanley Cheungdb98e082015-07-27 10:19:45 -0700343}
344
345/**
Stanley Cheunge63354a2015-08-10 15:46:42 -0700346 * Get the connectivity state of the channel
thinkerouefbc9e72016-08-16 20:00:36 +0800347 * @param bool $try_to_connect Try to connect on the channel (optional)
Stanley Cheunge63354a2015-08-10 15:46:42 -0700348 * @return long The grpc connectivity state
349 */
350PHP_METHOD(Channel, getConnectivityState) {
thinkeroua3730b72016-07-20 16:59:54 +0800351 wrapped_grpc_channel *channel = Z_WRAPPED_GRPC_CHANNEL_P(getThis());
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700352 gpr_mu_lock(&channel->wrapper->mu);
353 if (channel->wrapper->wrapped == NULL) {
354 zend_throw_exception(spl_ce_RuntimeException,
355 "Channel already closed", 1 TSRMLS_CC);
356 gpr_mu_unlock(&channel->wrapper->mu);
357 return;
358 }
359
thinkeroua3730b72016-07-20 16:59:54 +0800360 bool try_to_connect = false;
361
Stanley Cheunge63354a2015-08-10 15:46:42 -0700362 /* "|b" == 1 optional bool */
thinkeroua3730b72016-07-20 16:59:54 +0800363 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &try_to_connect)
364 == FAILURE) {
Stanley Cheunge63354a2015-08-10 15:46:42 -0700365 zend_throw_exception(spl_ce_InvalidArgumentException,
366 "getConnectivityState expects a bool", 1 TSRMLS_CC);
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700367 gpr_mu_unlock(&channel->wrapper->mu);
Stanley Cheunge63354a2015-08-10 15:46:42 -0700368 return;
369 }
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700370 int state = grpc_channel_check_connectivity_state(channel->wrapper->wrapped,
371 (int)try_to_connect);
372 // this can happen if another shared Channel object close the underlying
373 // channel
374 if (state == GRPC_CHANNEL_SHUTDOWN) {
375 channel->wrapper->wrapped = NULL;
376 }
377 gpr_mu_unlock(&channel->wrapper->mu);
378 RETURN_LONG(state);
Stanley Cheunge63354a2015-08-10 15:46:42 -0700379}
380
381/**
Stanley Cheunga63fdd02015-08-11 15:11:11 -0700382 * Watch the connectivity state of the channel until it changed
thinkerouefbc9e72016-08-16 20:00:36 +0800383 * @param long $last_state The previous connectivity state of the channel
384 * @param Timeval $deadline_obj The deadline this function should wait until
Stanley Cheung4c5c7b82015-08-12 16:28:58 -0700385 * @return bool If the connectivity state changes from last_state
386 * before deadline
Stanley Cheunga63fdd02015-08-11 15:11:11 -0700387 */
388PHP_METHOD(Channel, watchConnectivityState) {
thinkeroua3730b72016-07-20 16:59:54 +0800389 wrapped_grpc_channel *channel = Z_WRAPPED_GRPC_CHANNEL_P(getThis());
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700390 gpr_mu_lock(&channel->wrapper->mu);
391 if (channel->wrapper->wrapped == NULL) {
392 zend_throw_exception(spl_ce_RuntimeException,
393 "Channel already closed", 1 TSRMLS_CC);
394 gpr_mu_unlock(&channel->wrapper->mu);
395 return;
396 }
397
thinkerou19304682016-07-22 02:43:19 +0800398 php_grpc_long last_state;
Stanley Cheunga63fdd02015-08-11 15:11:11 -0700399 zval *deadline_obj;
thinkeroua3730b72016-07-20 16:59:54 +0800400
Stanley Cheunga63fdd02015-08-11 15:11:11 -0700401 /* "lO" == 1 long 1 object */
402 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lO",
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700403 &last_state, &deadline_obj,
404 grpc_ce_timeval) == FAILURE) {
Stanley Cheunga63fdd02015-08-11 15:11:11 -0700405 zend_throw_exception(spl_ce_InvalidArgumentException,
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700406 "watchConnectivityState expects 1 long 1 timeval",
407 1 TSRMLS_CC);
408 gpr_mu_unlock(&channel->wrapper->mu);
Stanley Cheunga63fdd02015-08-11 15:11:11 -0700409 return;
410 }
411
thinkeroua3730b72016-07-20 16:59:54 +0800412 wrapped_grpc_timeval *deadline = Z_WRAPPED_GRPC_TIMEVAL_P(deadline_obj);
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700413 grpc_channel_watch_connectivity_state(channel->wrapper->wrapped,
thinkeroua3730b72016-07-20 16:59:54 +0800414 (grpc_connectivity_state)last_state,
415 deadline->wrapped, completion_queue,
416 NULL);
417 grpc_event event =
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700418 grpc_completion_queue_pluck(completion_queue, NULL,
419 gpr_inf_future(GPR_CLOCK_REALTIME), NULL);
420 gpr_mu_unlock(&channel->wrapper->mu);
Stanley Cheung1567c0c2015-08-13 11:12:54 -0700421 RETURN_BOOL(event.success);
Stanley Cheunga63fdd02015-08-11 15:11:11 -0700422}
423
424/**
mlumishb892a272014-12-09 16:28:23 -0800425 * Close the channel
thinkerou03dc2192016-08-16 19:31:44 +0800426 * @return void
mlumishb892a272014-12-09 16:28:23 -0800427 */
Craig Tillerb5dcec52015-01-13 11:13:42 -0800428PHP_METHOD(Channel, close) {
thinkeroua3730b72016-07-20 16:59:54 +0800429 wrapped_grpc_channel *channel = Z_WRAPPED_GRPC_CHANNEL_P(getThis());
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700430 gpr_mu_lock(&channel->wrapper->mu);
431 if (channel->wrapper->wrapped != NULL) {
432 grpc_channel_destroy(channel->wrapper->wrapped);
Stanley Cheung5d559482017-08-22 13:15:01 -0700433 free(channel->wrapper->target);
434 free(channel->wrapper->args_hashstr);
Zhouyihai Ding4b9f8d82018-01-22 12:58:21 -0800435 if (channel->wrapper->creds_hashstr != NULL) {
436 free(channel->wrapper->creds_hashstr);
437 channel->wrapper->creds_hashstr = NULL;
438 }
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700439 channel->wrapper->wrapped = NULL;
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700440
Stanley Cheung5d559482017-08-22 13:15:01 -0700441 php_grpc_delete_persistent_list_entry(channel->wrapper->key,
442 strlen(channel->wrapper->key)
443 TSRMLS_CC);
444 }
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700445 gpr_mu_unlock(&channel->wrapper->mu);
446}
447
448// Delete an entry from the persistent list
449// Note: this does not destroy or close the underlying grpc_channel
450void php_grpc_delete_persistent_list_entry(char *key, php_grpc_int key_len
451 TSRMLS_DC) {
452 php_grpc_zend_resource *rsrc;
453 gpr_mu_lock(&global_persistent_list_mu);
454 if (PHP_GRPC_PERSISTENT_LIST_FIND(&EG(persistent_list), key,
455 key_len, rsrc)) {
456 channel_persistent_le_t *le;
457 le = (channel_persistent_le_t *)rsrc->ptr;
458 le->channel = NULL;
459 php_grpc_zend_hash_del(&EG(persistent_list), key, key_len+1);
Zhouyihai Ding33af9aa2018-01-22 12:54:42 -0800460 free(le);
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700461 }
462 gpr_mu_unlock(&global_persistent_list_mu);
463}
464
465// A destructor associated with each list entry from the persistent list
466static void php_grpc_channel_plink_dtor(php_grpc_zend_resource *rsrc
467 TSRMLS_DC) {
468 channel_persistent_le_t *le = (channel_persistent_le_t *)rsrc->ptr;
469 if (le->channel != NULL) {
470 gpr_mu_lock(&le->channel->mu);
471 if (le->channel->wrapped != NULL) {
472 grpc_channel_destroy(le->channel->wrapped);
Stanley Cheung5d559482017-08-22 13:15:01 -0700473 free(le->channel->target);
474 free(le->channel->args_hashstr);
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700475 }
476 gpr_mu_unlock(&le->channel->mu);
477 }
mlumishb892a272014-12-09 16:28:23 -0800478}
479
Stanley Cheung6a5c83d2017-02-16 12:25:32 -0800480ZEND_BEGIN_ARG_INFO_EX(arginfo_construct, 0, 0, 2)
481 ZEND_ARG_INFO(0, target)
482 ZEND_ARG_INFO(0, args)
483ZEND_END_ARG_INFO()
484
485ZEND_BEGIN_ARG_INFO_EX(arginfo_getTarget, 0, 0, 0)
486ZEND_END_ARG_INFO()
487
488ZEND_BEGIN_ARG_INFO_EX(arginfo_getConnectivityState, 0, 0, 0)
489 ZEND_ARG_INFO(0, try_to_connect)
490ZEND_END_ARG_INFO()
491
492ZEND_BEGIN_ARG_INFO_EX(arginfo_watchConnectivityState, 0, 0, 2)
493 ZEND_ARG_INFO(0, last_state)
494 ZEND_ARG_INFO(0, deadline)
495ZEND_END_ARG_INFO()
496
497ZEND_BEGIN_ARG_INFO_EX(arginfo_close, 0, 0, 0)
498ZEND_END_ARG_INFO()
499
mlumishb892a272014-12-09 16:28:23 -0800500static zend_function_entry channel_methods[] = {
Stanley Cheung6a5c83d2017-02-16 12:25:32 -0800501 PHP_ME(Channel, __construct, arginfo_construct,
502 ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
503 PHP_ME(Channel, getTarget, arginfo_getTarget,
504 ZEND_ACC_PUBLIC)
505 PHP_ME(Channel, getConnectivityState, arginfo_getConnectivityState,
506 ZEND_ACC_PUBLIC)
507 PHP_ME(Channel, watchConnectivityState, arginfo_watchConnectivityState,
508 ZEND_ACC_PUBLIC)
509 PHP_ME(Channel, close, arginfo_close,
510 ZEND_ACC_PUBLIC)
thinkeroua3730b72016-07-20 16:59:54 +0800511 PHP_FE_END
512};
mlumishb892a272014-12-09 16:28:23 -0800513
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700514GRPC_STARTUP_FUNCTION(channel) {
mlumishb892a272014-12-09 16:28:23 -0800515 zend_class_entry ce;
516 INIT_CLASS_ENTRY(ce, "Grpc\\Channel", channel_methods);
517 ce.create_object = create_wrapped_grpc_channel;
518 grpc_ce_channel = zend_register_internal_class(&ce TSRMLS_CC);
Stanley Cheung5d559482017-08-22 13:15:01 -0700519 gpr_mu_init(&global_persistent_list_mu);
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700520 le_plink = zend_register_list_destructors_ex(
521 NULL, php_grpc_channel_plink_dtor, "Persistent Channel", module_number);
thinkerou5dafd822016-07-28 22:43:38 +0800522 PHP_GRPC_INIT_HANDLER(wrapped_grpc_channel, channel_ce_handlers);
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700523 return SUCCESS;
mlumishb892a272014-12-09 16:28:23 -0800524}