blob: 325f592c554415afafcecfe1f2ce6abbd1d08255 [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>
mlumishb892a272014-12-09 16:28:23 -080044
Stanley Cheunga63fdd02015-08-11 15:11:11 -070045#include "completion_queue.h"
Stanley Cheung9c0b35e2015-10-21 17:07:56 -070046#include "channel_credentials.h"
Stanley Cheunga63fdd02015-08-11 15:11:11 -070047#include "server.h"
48#include "timeval.h"
mlumishb892a272014-12-09 16:28:23 -080049
Xiaoguang Sun8a929a92015-03-13 14:22:31 +080050zend_class_entry *grpc_ce_channel;
thinkeroudba5b0c2016-07-27 18:39:16 +080051#if PHP_MAJOR_VERSION >= 7
52static zend_object_handlers channel_ce_handlers;
53#endif
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -070054static gpr_mu global_persistent_list_mu;
55int le_plink;
Xiaoguang Sun8a929a92015-03-13 14:22:31 +080056
mlumishb892a272014-12-09 16:28:23 -080057/* Frees and destroys an instance of wrapped_grpc_channel */
thinkerou011d1ef2016-07-27 09:44:49 +080058PHP_GRPC_FREE_WRAPPED_FUNC_START(wrapped_grpc_channel)
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -070059 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);
Stanley Cheung5d559482017-08-22 13:15:01 -070069 free(p->wrapper->target);
70 free(p->wrapper->args_hashstr);
Zhouyihai Ding4b9f8d82018-01-22 12:58:21 -080071 if (p->wrapper->creds_hashstr != NULL) {
72 free(p->wrapper->creds_hashstr);
73 p->wrapper->creds_hashstr = NULL;
74 }
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -070075 }
76 gpr_mu_unlock(&global_persistent_list_mu);
77 }
78 gpr_mu_unlock(&p->wrapper->mu);
mlumishb892a272014-12-09 16:28:23 -080079 }
thinkerou011d1ef2016-07-27 09:44:49 +080080PHP_GRPC_FREE_WRAPPED_FUNC_END()
81
mlumishb892a272014-12-09 16:28:23 -080082/* Initializes an instance of wrapped_grpc_channel to be associated with an
83 * object of a class specified by class_type */
thinkeroudba5b0c2016-07-27 18:39:16 +080084php_grpc_zend_object create_wrapped_grpc_channel(zend_class_entry *class_type
85 TSRMLS_DC) {
thinkerouba75c012016-07-28 02:30:08 +080086 PHP_GRPC_ALLOC_CLASS_OBJECT(wrapped_grpc_channel);
mlumishb892a272014-12-09 16:28:23 -080087 zend_object_std_init(&intern->std, class_type TSRMLS_CC);
88 object_properties_init(&intern->std, class_type);
thinkeroudc673c52016-07-28 09:49:38 +080089 PHP_GRPC_FREE_CLASS_OBJECT(wrapped_grpc_channel, channel_ce_handlers);
thinkeroudba5b0c2016-07-27 18:39:16 +080090}
thinkerou6f9d30b2016-07-27 03:19:03 +080091
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -070092int php_grpc_read_args_array(zval *args_array,
93 grpc_channel_args *args TSRMLS_DC) {
mlumishb892a272014-12-09 16:28:23 -080094 HashTable *array_hash;
mlumishb892a272014-12-09 16:28:23 -080095 int args_index;
mlumishb892a272014-12-09 16:28:23 -080096 array_hash = Z_ARRVAL_P(args_array);
thinkerou6f9d30b2016-07-27 03:19:03 +080097 if (!array_hash) {
98 zend_throw_exception(spl_ce_InvalidArgumentException,
Stanley Cheungc1f25fb2016-07-29 13:41:22 -070099 "array_hash is NULL", 1 TSRMLS_CC);
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700100 return FAILURE;
thinkerou6f9d30b2016-07-27 03:19:03 +0800101 }
mlumishb892a272014-12-09 16:28:23 -0800102 args->num_args = zend_hash_num_elements(array_hash);
103 args->args = ecalloc(args->num_args, sizeof(grpc_arg));
104 args_index = 0;
thinkerou6f9d30b2016-07-27 03:19:03 +0800105
thinkerouba75c012016-07-28 02:30:08 +0800106 char *key = NULL;
107 zval *data;
108 int key_type;
109
110 PHP_GRPC_HASH_FOREACH_STR_KEY_VAL_START(array_hash, key, key_type, data)
111 if (key_type != HASH_KEY_IS_STRING) {
mlumishb892a272014-12-09 16:28:23 -0800112 zend_throw_exception(spl_ce_InvalidArgumentException,
Craig Tillerb5dcec52015-01-13 11:13:42 -0800113 "args keys must be strings", 1 TSRMLS_CC);
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700114 return FAILURE;
mlumishb892a272014-12-09 16:28:23 -0800115 }
116 args->args[args_index].key = key;
thinkeroua3730b72016-07-20 16:59:54 +0800117 switch (Z_TYPE_P(data)) {
118 case IS_LONG:
119 args->args[args_index].value.integer = (int)Z_LVAL_P(data);
120 args->args[args_index].type = GRPC_ARG_INTEGER;
121 break;
122 case IS_STRING:
123 args->args[args_index].value.string = Z_STRVAL_P(data);
124 args->args[args_index].type = GRPC_ARG_STRING;
125 break;
126 default:
127 zend_throw_exception(spl_ce_InvalidArgumentException,
thinkerouba75c012016-07-28 02:30:08 +0800128 "args values must be int or string", 1 TSRMLS_CC);
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700129 return FAILURE;
thinkeroua3730b72016-07-20 16:59:54 +0800130 }
131 args_index++;
thinkerouba75c012016-07-28 02:30:08 +0800132 PHP_GRPC_HASH_FOREACH_END()
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700133 return SUCCESS;
134}
135
136void generate_sha1_str(char *sha1str, char *str, php_grpc_int len) {
137 PHP_SHA1_CTX context;
138 unsigned char digest[20];
139 sha1str[0] = '\0';
140 PHP_SHA1Init(&context);
141 PHP_GRPC_SHA1Update(&context, str, len);
142 PHP_SHA1Final(digest, &context);
143 make_sha1_digest(sha1str, digest);
144}
145
146void create_channel(
147 wrapped_grpc_channel *channel,
148 char *target,
149 grpc_channel_args args,
150 wrapped_grpc_channel_credentials *creds) {
151 if (creds == NULL) {
152 channel->wrapper->wrapped = grpc_insecure_channel_create(target, &args,
153 NULL);
154 } else {
155 channel->wrapper->wrapped =
156 grpc_secure_channel_create(creds->wrapped, target, &args, NULL);
157 }
158 efree(args.args);
159}
160
161void create_and_add_channel_to_persistent_list(
162 wrapped_grpc_channel *channel,
163 char *target,
164 grpc_channel_args args,
165 wrapped_grpc_channel_credentials *creds,
166 char *key,
Stanley Cheung5d559482017-08-22 13:15:01 -0700167 php_grpc_int key_len TSRMLS_DC) {
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700168 php_grpc_zend_resource new_rsrc;
169 channel_persistent_le_t *le;
170 // this links each persistent list entry to a destructor
171 new_rsrc.type = le_plink;
172 le = malloc(sizeof(channel_persistent_le_t));
173
174 create_channel(channel, target, args, creds);
175
176 le->channel = channel->wrapper;
177 new_rsrc.ptr = le;
178 gpr_mu_lock(&global_persistent_list_mu);
179 PHP_GRPC_PERSISTENT_LIST_UPDATE(&EG(persistent_list), key, key_len,
180 (void *)&new_rsrc);
181 gpr_mu_unlock(&global_persistent_list_mu);
thinkerou6f9d30b2016-07-27 03:19:03 +0800182}
thinkeroua3730b72016-07-20 16:59:54 +0800183
mlumishb892a272014-12-09 16:28:23 -0800184/**
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700185 * Construct an instance of the Channel class.
186 *
187 * By default, the underlying grpc_channel is "persistent". That is, given
188 * the same set of parameters passed to the constructor, the same underlying
189 * grpc_channel will be returned.
190 *
191 * If the $args array contains a "credentials" key mapping to a
192 * ChannelCredentials object, a secure channel will be created with those
193 * credentials.
194 *
195 * If the $args array contains a "force_new" key mapping to a boolean value
Stanley Cheung5d559482017-08-22 13:15:01 -0700196 * of "true", a new and separate underlying grpc_channel will be created
197 * and returned. This will not affect existing channels.
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700198 *
mlumishb892a272014-12-09 16:28:23 -0800199 * @param string $target The hostname to associate with this channel
thinkerouefbc9e72016-08-16 20:00:36 +0800200 * @param array $args_array The arguments to pass to the Channel
mlumishb892a272014-12-09 16:28:23 -0800201 */
Craig Tillerb5dcec52015-01-13 11:13:42 -0800202PHP_METHOD(Channel, __construct) {
thinkeroua3730b72016-07-20 16:59:54 +0800203 wrapped_grpc_channel *channel = Z_WRAPPED_GRPC_CHANNEL_P(getThis());
204 zval *creds_obj = NULL;
thinkeroua3730b72016-07-20 16:59:54 +0800205 char *target;
thinkerou19304682016-07-22 02:43:19 +0800206 php_grpc_int target_length;
mlumishb892a272014-12-09 16:28:23 -0800207 zval *args_array = NULL;
208 grpc_channel_args args;
209 HashTable *array_hash;
Stanley Cheungaeea1022015-10-21 17:00:49 -0700210 wrapped_grpc_channel_credentials *creds = NULL;
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700211 php_grpc_zend_resource *rsrc;
212 bool force_new = false;
213 zval *force_new_obj = NULL;
thinkeroua3730b72016-07-20 16:59:54 +0800214
Stanley Cheungf77a4ad2016-02-16 09:45:51 -0800215 /* "sa" == 1 string, 1 array */
216 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sa", &target,
Craig Tillerb5dcec52015-01-13 11:13:42 -0800217 &target_length, &args_array) == FAILURE) {
mlumishb892a272014-12-09 16:28:23 -0800218 zend_throw_exception(spl_ce_InvalidArgumentException,
Craig Tillerb5dcec52015-01-13 11:13:42 -0800219 "Channel expects a string and an array", 1 TSRMLS_CC);
mlumishb892a272014-12-09 16:28:23 -0800220 return;
221 }
Stanley Cheungcccf9292016-02-12 16:37:19 -0800222 array_hash = Z_ARRVAL_P(args_array);
thinkerouba75c012016-07-28 02:30:08 +0800223 if (php_grpc_zend_hash_find(array_hash, "credentials", sizeof("credentials"),
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700224 (void **)&creds_obj) == SUCCESS) {
thinkerouba75c012016-07-28 02:30:08 +0800225 if (Z_TYPE_P(creds_obj) == IS_NULL) {
Stanley Cheungcccf9292016-02-12 16:37:19 -0800226 creds = NULL;
thinkerouba75c012016-07-28 02:30:08 +0800227 php_grpc_zend_hash_del(array_hash, "credentials", sizeof("credentials"));
228 } else if (PHP_GRPC_GET_CLASS_ENTRY(creds_obj) !=
229 grpc_ce_channel_credentials) {
Stanley Cheungcccf9292016-02-12 16:37:19 -0800230 zend_throw_exception(spl_ce_InvalidArgumentException,
231 "credentials must be a ChannelCredentials object",
232 1 TSRMLS_CC);
233 return;
mlumishb892a272014-12-09 16:28:23 -0800234 } else {
thinkeroua3730b72016-07-20 16:59:54 +0800235 creds = Z_WRAPPED_GRPC_CHANNEL_CREDS_P(creds_obj);
thinkerouba75c012016-07-28 02:30:08 +0800236 php_grpc_zend_hash_del(array_hash, "credentials", sizeof("credentials"));
thinkeroua3730b72016-07-20 16:59:54 +0800237 }
238 }
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700239 if (php_grpc_zend_hash_find(array_hash, "force_new", sizeof("force_new"),
240 (void **)&force_new_obj) == SUCCESS) {
241 if (PHP_GRPC_BVAL_IS_TRUE(force_new_obj)) {
242 force_new = true;
243 }
244 php_grpc_zend_hash_del(array_hash, "force_new", sizeof("force_new"));
Stanley Cheungcccf9292016-02-12 16:37:19 -0800245 }
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700246
247 // parse the rest of the channel args array
248 if (php_grpc_read_args_array(args_array, &args TSRMLS_CC) == FAILURE) {
Zhouyihai Dingb6cdfca2018-01-13 16:25:28 -0800249 efree(args.args);
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700250 return;
251 }
252
253 // Construct a hashkey for the persistent channel
254 // Currently, the hashkey contains 3 parts:
255 // 1. hostname
256 // 2. hash value of the channel args array (excluding "credentials"
257 // and "force_new")
258 // 3. (optional) hash value of the ChannelCredentials object
259 php_serialize_data_t var_hash;
260 smart_str buf = {0};
261 PHP_VAR_SERIALIZE_INIT(var_hash);
262 PHP_GRPC_VAR_SERIALIZE(&buf, args_array, &var_hash);
263 PHP_VAR_SERIALIZE_DESTROY(var_hash);
264
265 char sha1str[41];
266 generate_sha1_str(sha1str, PHP_GRPC_SERIALIZED_BUF_STR(buf),
267 PHP_GRPC_SERIALIZED_BUF_LEN(buf));
268
269 php_grpc_int key_len = target_length + strlen(sha1str);
270 if (creds != NULL && creds->hashstr != NULL) {
271 key_len += strlen(creds->hashstr);
272 }
273 char *key = malloc(key_len + 1);
274 strcpy(key, target);
275 strcat(key, sha1str);
276 if (creds != NULL && creds->hashstr != NULL) {
277 strcat(key, creds->hashstr);
278 }
279 channel->wrapper = malloc(sizeof(grpc_channel_wrapper));
280 channel->wrapper->key = key;
Stanley Cheung5d559482017-08-22 13:15:01 -0700281 channel->wrapper->target = strdup(target);
282 channel->wrapper->args_hashstr = strdup(sha1str);
Zhouyihai Ding4b9f8d82018-01-22 12:58:21 -0800283 channel->wrapper->creds_hashstr = NULL;
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700284 if (creds != NULL && creds->hashstr != NULL) {
Zhouyihai Ding4b9f8d82018-01-22 12:58:21 -0800285 php_grpc_int creds_hashstr_len = strlen(creds->hashstr);
286 char *channel_creds_hashstr = malloc(creds_hashstr_len + 1);
287 strcpy(channel_creds_hashstr, creds->hashstr);
288 channel->wrapper->creds_hashstr = channel_creds_hashstr;
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700289 }
Zhouyihai Ding4b9f8d82018-01-22 12:58:21 -0800290
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700291 gpr_mu_init(&channel->wrapper->mu);
292 smart_str_free(&buf);
293
Stanley Cheung5d559482017-08-22 13:15:01 -0700294 if (force_new || (creds != NULL && creds->has_call_creds)) {
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700295 // If the ChannelCredentials object was composed with a CallCredentials
296 // object, there is no way we can tell them apart. Do NOT persist
297 // them. They should be individually destroyed.
298 create_channel(channel, target, args, creds);
299 } else if (!(PHP_GRPC_PERSISTENT_LIST_FIND(&EG(persistent_list), key,
300 key_len, rsrc))) {
301 create_and_add_channel_to_persistent_list(
Stanley Cheung5d559482017-08-22 13:15:01 -0700302 channel, target, args, creds, key, key_len TSRMLS_CC);
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700303 } else {
304 // Found a previously stored channel in the persistent list
305 channel_persistent_le_t *le = (channel_persistent_le_t *)rsrc->ptr;
306 if (strcmp(target, le->channel->target) != 0 ||
307 strcmp(sha1str, le->channel->args_hashstr) != 0 ||
308 (creds != NULL && creds->hashstr != NULL &&
309 strcmp(creds->hashstr, le->channel->creds_hashstr) != 0)) {
310 // somehow hash collision
311 create_and_add_channel_to_persistent_list(
Stanley Cheung5d559482017-08-22 13:15:01 -0700312 channel, target, args, creds, key, key_len TSRMLS_CC);
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700313 } else {
Zhouyihai Dingb6cdfca2018-01-13 16:25:28 -0800314 efree(args.args);
Zhouyihai Ding4b9f8d82018-01-22 12:58:21 -0800315 if (channel->wrapper->creds_hashstr != NULL){
316 free(channel->wrapper->creds_hashstr);
317 channel->wrapper->creds_hashstr = NULL;
318 }
319 free(channel->wrapper->creds_hashstr);
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700320 channel->wrapper = le->channel;
321 }
322 }
mlumishb892a272014-12-09 16:28:23 -0800323}
324
325/**
Stanley Cheungdb98e082015-07-27 10:19:45 -0700326 * Get the endpoint this call/stream is connected to
327 * @return string The URI of the endpoint
328 */
329PHP_METHOD(Channel, getTarget) {
thinkeroua3730b72016-07-20 16:59:54 +0800330 wrapped_grpc_channel *channel = Z_WRAPPED_GRPC_CHANNEL_P(getThis());
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700331 gpr_mu_lock(&channel->wrapper->mu);
332 if (channel->wrapper->wrapped == NULL) {
333 zend_throw_exception(spl_ce_RuntimeException,
334 "Channel already closed", 1 TSRMLS_CC);
335 gpr_mu_unlock(&channel->wrapper->mu);
336 return;
337 }
338 char *target = grpc_channel_get_target(channel->wrapper->wrapped);
339 gpr_mu_unlock(&channel->wrapper->mu);
340 PHP_GRPC_RETURN_STRING(target, 1);
Stanley Cheungdb98e082015-07-27 10:19:45 -0700341}
342
343/**
Stanley Cheunge63354a2015-08-10 15:46:42 -0700344 * Get the connectivity state of the channel
thinkerouefbc9e72016-08-16 20:00:36 +0800345 * @param bool $try_to_connect Try to connect on the channel (optional)
Stanley Cheunge63354a2015-08-10 15:46:42 -0700346 * @return long The grpc connectivity state
347 */
348PHP_METHOD(Channel, getConnectivityState) {
thinkeroua3730b72016-07-20 16:59:54 +0800349 wrapped_grpc_channel *channel = Z_WRAPPED_GRPC_CHANNEL_P(getThis());
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700350 gpr_mu_lock(&channel->wrapper->mu);
351 if (channel->wrapper->wrapped == NULL) {
352 zend_throw_exception(spl_ce_RuntimeException,
353 "Channel already closed", 1 TSRMLS_CC);
354 gpr_mu_unlock(&channel->wrapper->mu);
355 return;
356 }
357
thinkeroua3730b72016-07-20 16:59:54 +0800358 bool try_to_connect = false;
359
Stanley Cheunge63354a2015-08-10 15:46:42 -0700360 /* "|b" == 1 optional bool */
thinkeroua3730b72016-07-20 16:59:54 +0800361 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &try_to_connect)
362 == FAILURE) {
Stanley Cheunge63354a2015-08-10 15:46:42 -0700363 zend_throw_exception(spl_ce_InvalidArgumentException,
364 "getConnectivityState expects a bool", 1 TSRMLS_CC);
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700365 gpr_mu_unlock(&channel->wrapper->mu);
Stanley Cheunge63354a2015-08-10 15:46:42 -0700366 return;
367 }
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700368 int state = grpc_channel_check_connectivity_state(channel->wrapper->wrapped,
369 (int)try_to_connect);
370 // this can happen if another shared Channel object close the underlying
371 // channel
372 if (state == GRPC_CHANNEL_SHUTDOWN) {
373 channel->wrapper->wrapped = NULL;
374 }
375 gpr_mu_unlock(&channel->wrapper->mu);
376 RETURN_LONG(state);
Stanley Cheunge63354a2015-08-10 15:46:42 -0700377}
378
379/**
Stanley Cheunga63fdd02015-08-11 15:11:11 -0700380 * Watch the connectivity state of the channel until it changed
thinkerouefbc9e72016-08-16 20:00:36 +0800381 * @param long $last_state The previous connectivity state of the channel
382 * @param Timeval $deadline_obj The deadline this function should wait until
Stanley Cheung4c5c7b82015-08-12 16:28:58 -0700383 * @return bool If the connectivity state changes from last_state
384 * before deadline
Stanley Cheunga63fdd02015-08-11 15:11:11 -0700385 */
386PHP_METHOD(Channel, watchConnectivityState) {
thinkeroua3730b72016-07-20 16:59:54 +0800387 wrapped_grpc_channel *channel = Z_WRAPPED_GRPC_CHANNEL_P(getThis());
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700388 gpr_mu_lock(&channel->wrapper->mu);
389 if (channel->wrapper->wrapped == NULL) {
390 zend_throw_exception(spl_ce_RuntimeException,
391 "Channel already closed", 1 TSRMLS_CC);
392 gpr_mu_unlock(&channel->wrapper->mu);
393 return;
394 }
395
thinkerou19304682016-07-22 02:43:19 +0800396 php_grpc_long last_state;
Stanley Cheunga63fdd02015-08-11 15:11:11 -0700397 zval *deadline_obj;
thinkeroua3730b72016-07-20 16:59:54 +0800398
Stanley Cheunga63fdd02015-08-11 15:11:11 -0700399 /* "lO" == 1 long 1 object */
400 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lO",
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700401 &last_state, &deadline_obj,
402 grpc_ce_timeval) == FAILURE) {
Stanley Cheunga63fdd02015-08-11 15:11:11 -0700403 zend_throw_exception(spl_ce_InvalidArgumentException,
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700404 "watchConnectivityState expects 1 long 1 timeval",
405 1 TSRMLS_CC);
406 gpr_mu_unlock(&channel->wrapper->mu);
Stanley Cheunga63fdd02015-08-11 15:11:11 -0700407 return;
408 }
409
thinkeroua3730b72016-07-20 16:59:54 +0800410 wrapped_grpc_timeval *deadline = Z_WRAPPED_GRPC_TIMEVAL_P(deadline_obj);
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700411 grpc_channel_watch_connectivity_state(channel->wrapper->wrapped,
thinkeroua3730b72016-07-20 16:59:54 +0800412 (grpc_connectivity_state)last_state,
413 deadline->wrapped, completion_queue,
414 NULL);
415 grpc_event event =
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700416 grpc_completion_queue_pluck(completion_queue, NULL,
417 gpr_inf_future(GPR_CLOCK_REALTIME), NULL);
418 gpr_mu_unlock(&channel->wrapper->mu);
Stanley Cheung1567c0c2015-08-13 11:12:54 -0700419 RETURN_BOOL(event.success);
Stanley Cheunga63fdd02015-08-11 15:11:11 -0700420}
421
422/**
mlumishb892a272014-12-09 16:28:23 -0800423 * Close the channel
thinkerou03dc2192016-08-16 19:31:44 +0800424 * @return void
mlumishb892a272014-12-09 16:28:23 -0800425 */
Craig Tillerb5dcec52015-01-13 11:13:42 -0800426PHP_METHOD(Channel, close) {
thinkeroua3730b72016-07-20 16:59:54 +0800427 wrapped_grpc_channel *channel = Z_WRAPPED_GRPC_CHANNEL_P(getThis());
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700428 gpr_mu_lock(&channel->wrapper->mu);
429 if (channel->wrapper->wrapped != NULL) {
430 grpc_channel_destroy(channel->wrapper->wrapped);
Stanley Cheung5d559482017-08-22 13:15:01 -0700431 free(channel->wrapper->target);
432 free(channel->wrapper->args_hashstr);
Zhouyihai Ding4b9f8d82018-01-22 12:58:21 -0800433 if (channel->wrapper->creds_hashstr != NULL) {
434 free(channel->wrapper->creds_hashstr);
435 channel->wrapper->creds_hashstr = NULL;
436 }
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700437 channel->wrapper->wrapped = NULL;
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700438
Stanley Cheung5d559482017-08-22 13:15:01 -0700439 php_grpc_delete_persistent_list_entry(channel->wrapper->key,
440 strlen(channel->wrapper->key)
441 TSRMLS_CC);
442 }
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700443 gpr_mu_unlock(&channel->wrapper->mu);
444}
445
446// Delete an entry from the persistent list
447// Note: this does not destroy or close the underlying grpc_channel
448void php_grpc_delete_persistent_list_entry(char *key, php_grpc_int key_len
449 TSRMLS_DC) {
450 php_grpc_zend_resource *rsrc;
451 gpr_mu_lock(&global_persistent_list_mu);
452 if (PHP_GRPC_PERSISTENT_LIST_FIND(&EG(persistent_list), key,
453 key_len, rsrc)) {
454 channel_persistent_le_t *le;
455 le = (channel_persistent_le_t *)rsrc->ptr;
456 le->channel = NULL;
457 php_grpc_zend_hash_del(&EG(persistent_list), key, key_len+1);
458 }
459 gpr_mu_unlock(&global_persistent_list_mu);
460}
461
462// A destructor associated with each list entry from the persistent list
463static void php_grpc_channel_plink_dtor(php_grpc_zend_resource *rsrc
464 TSRMLS_DC) {
465 channel_persistent_le_t *le = (channel_persistent_le_t *)rsrc->ptr;
466 if (le->channel != NULL) {
467 gpr_mu_lock(&le->channel->mu);
468 if (le->channel->wrapped != NULL) {
469 grpc_channel_destroy(le->channel->wrapped);
Stanley Cheung5d559482017-08-22 13:15:01 -0700470 free(le->channel->target);
471 free(le->channel->args_hashstr);
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700472 }
473 gpr_mu_unlock(&le->channel->mu);
474 }
mlumishb892a272014-12-09 16:28:23 -0800475}
476
Stanley Cheung6a5c83d2017-02-16 12:25:32 -0800477ZEND_BEGIN_ARG_INFO_EX(arginfo_construct, 0, 0, 2)
478 ZEND_ARG_INFO(0, target)
479 ZEND_ARG_INFO(0, args)
480ZEND_END_ARG_INFO()
481
482ZEND_BEGIN_ARG_INFO_EX(arginfo_getTarget, 0, 0, 0)
483ZEND_END_ARG_INFO()
484
485ZEND_BEGIN_ARG_INFO_EX(arginfo_getConnectivityState, 0, 0, 0)
486 ZEND_ARG_INFO(0, try_to_connect)
487ZEND_END_ARG_INFO()
488
489ZEND_BEGIN_ARG_INFO_EX(arginfo_watchConnectivityState, 0, 0, 2)
490 ZEND_ARG_INFO(0, last_state)
491 ZEND_ARG_INFO(0, deadline)
492ZEND_END_ARG_INFO()
493
494ZEND_BEGIN_ARG_INFO_EX(arginfo_close, 0, 0, 0)
495ZEND_END_ARG_INFO()
496
mlumishb892a272014-12-09 16:28:23 -0800497static zend_function_entry channel_methods[] = {
Stanley Cheung6a5c83d2017-02-16 12:25:32 -0800498 PHP_ME(Channel, __construct, arginfo_construct,
499 ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
500 PHP_ME(Channel, getTarget, arginfo_getTarget,
501 ZEND_ACC_PUBLIC)
502 PHP_ME(Channel, getConnectivityState, arginfo_getConnectivityState,
503 ZEND_ACC_PUBLIC)
504 PHP_ME(Channel, watchConnectivityState, arginfo_watchConnectivityState,
505 ZEND_ACC_PUBLIC)
506 PHP_ME(Channel, close, arginfo_close,
507 ZEND_ACC_PUBLIC)
thinkeroua3730b72016-07-20 16:59:54 +0800508 PHP_FE_END
509};
mlumishb892a272014-12-09 16:28:23 -0800510
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700511GRPC_STARTUP_FUNCTION(channel) {
mlumishb892a272014-12-09 16:28:23 -0800512 zend_class_entry ce;
513 INIT_CLASS_ENTRY(ce, "Grpc\\Channel", channel_methods);
514 ce.create_object = create_wrapped_grpc_channel;
515 grpc_ce_channel = zend_register_internal_class(&ce TSRMLS_CC);
Stanley Cheung5d559482017-08-22 13:15:01 -0700516 gpr_mu_init(&global_persistent_list_mu);
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700517 le_plink = zend_register_list_destructors_ex(
518 NULL, php_grpc_channel_plink_dtor, "Persistent Channel", module_number);
thinkerou5dafd822016-07-28 22:43:38 +0800519 PHP_GRPC_INIT_HANDLER(wrapped_grpc_channel, channel_ce_handlers);
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700520 return SUCCESS;
mlumishb892a272014-12-09 16:28:23 -0800521}