blob: f1187e8722f227c610844becefe91cf75f9a6161 [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);
69 }
70 gpr_mu_unlock(&global_persistent_list_mu);
71 }
72 gpr_mu_unlock(&p->wrapper->mu);
mlumishb892a272014-12-09 16:28:23 -080073 }
thinkerou011d1ef2016-07-27 09:44:49 +080074PHP_GRPC_FREE_WRAPPED_FUNC_END()
75
mlumishb892a272014-12-09 16:28:23 -080076/* Initializes an instance of wrapped_grpc_channel to be associated with an
77 * object of a class specified by class_type */
thinkeroudba5b0c2016-07-27 18:39:16 +080078php_grpc_zend_object create_wrapped_grpc_channel(zend_class_entry *class_type
79 TSRMLS_DC) {
thinkerouba75c012016-07-28 02:30:08 +080080 PHP_GRPC_ALLOC_CLASS_OBJECT(wrapped_grpc_channel);
mlumishb892a272014-12-09 16:28:23 -080081 zend_object_std_init(&intern->std, class_type TSRMLS_CC);
82 object_properties_init(&intern->std, class_type);
thinkeroudc673c52016-07-28 09:49:38 +080083 PHP_GRPC_FREE_CLASS_OBJECT(wrapped_grpc_channel, channel_ce_handlers);
thinkeroudba5b0c2016-07-27 18:39:16 +080084}
thinkerou6f9d30b2016-07-27 03:19:03 +080085
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -070086int php_grpc_read_args_array(zval *args_array,
87 grpc_channel_args *args TSRMLS_DC) {
mlumishb892a272014-12-09 16:28:23 -080088 HashTable *array_hash;
mlumishb892a272014-12-09 16:28:23 -080089 int args_index;
mlumishb892a272014-12-09 16:28:23 -080090 array_hash = Z_ARRVAL_P(args_array);
thinkerou6f9d30b2016-07-27 03:19:03 +080091 if (!array_hash) {
92 zend_throw_exception(spl_ce_InvalidArgumentException,
Stanley Cheungc1f25fb2016-07-29 13:41:22 -070093 "array_hash is NULL", 1 TSRMLS_CC);
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -070094 return FAILURE;
thinkerou6f9d30b2016-07-27 03:19:03 +080095 }
mlumishb892a272014-12-09 16:28:23 -080096 args->num_args = zend_hash_num_elements(array_hash);
97 args->args = ecalloc(args->num_args, sizeof(grpc_arg));
98 args_index = 0;
thinkerou6f9d30b2016-07-27 03:19:03 +080099
thinkerouba75c012016-07-28 02:30:08 +0800100 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) {
mlumishb892a272014-12-09 16:28:23 -0800106 zend_throw_exception(spl_ce_InvalidArgumentException,
Craig Tillerb5dcec52015-01-13 11:13:42 -0800107 "args keys must be strings", 1 TSRMLS_CC);
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700108 return FAILURE;
mlumishb892a272014-12-09 16:28:23 -0800109 }
110 args->args[args_index].key = key;
thinkeroua3730b72016-07-20 16:59:54 +0800111 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,
thinkerouba75c012016-07-28 02:30:08 +0800122 "args values must be int or string", 1 TSRMLS_CC);
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700123 return FAILURE;
thinkeroua3730b72016-07-20 16:59:54 +0800124 }
125 args_index++;
thinkerouba75c012016-07-28 02:30:08 +0800126 PHP_GRPC_HASH_FOREACH_END()
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700127 return SUCCESS;
128}
129
130void 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
140void 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
155void 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);
thinkerou6f9d30b2016-07-27 03:19:03 +0800176}
thinkeroua3730b72016-07-20 16:59:54 +0800177
mlumishb892a272014-12-09 16:28:23 -0800178/**
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700179 * 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 *
mlumishb892a272014-12-09 16:28:23 -0800195 * @param string $target The hostname to associate with this channel
thinkerouefbc9e72016-08-16 20:00:36 +0800196 * @param array $args_array The arguments to pass to the Channel
mlumishb892a272014-12-09 16:28:23 -0800197 */
Craig Tillerb5dcec52015-01-13 11:13:42 -0800198PHP_METHOD(Channel, __construct) {
thinkeroua3730b72016-07-20 16:59:54 +0800199 wrapped_grpc_channel *channel = Z_WRAPPED_GRPC_CHANNEL_P(getThis());
200 zval *creds_obj = NULL;
thinkeroua3730b72016-07-20 16:59:54 +0800201 char *target;
thinkerou19304682016-07-22 02:43:19 +0800202 php_grpc_int target_length;
mlumishb892a272014-12-09 16:28:23 -0800203 zval *args_array = NULL;
204 grpc_channel_args args;
205 HashTable *array_hash;
Stanley Cheungaeea1022015-10-21 17:00:49 -0700206 wrapped_grpc_channel_credentials *creds = NULL;
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700207 php_grpc_zend_resource *rsrc;
208 bool force_new = false;
209 zval *force_new_obj = NULL;
thinkeroua3730b72016-07-20 16:59:54 +0800210
Stanley Cheungf77a4ad2016-02-16 09:45:51 -0800211 /* "sa" == 1 string, 1 array */
212 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sa", &target,
Craig Tillerb5dcec52015-01-13 11:13:42 -0800213 &target_length, &args_array) == FAILURE) {
mlumishb892a272014-12-09 16:28:23 -0800214 zend_throw_exception(spl_ce_InvalidArgumentException,
Craig Tillerb5dcec52015-01-13 11:13:42 -0800215 "Channel expects a string and an array", 1 TSRMLS_CC);
mlumishb892a272014-12-09 16:28:23 -0800216 return;
217 }
Stanley Cheungcccf9292016-02-12 16:37:19 -0800218 array_hash = Z_ARRVAL_P(args_array);
thinkerouba75c012016-07-28 02:30:08 +0800219 if (php_grpc_zend_hash_find(array_hash, "credentials", sizeof("credentials"),
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700220 (void **)&creds_obj) == SUCCESS) {
thinkerouba75c012016-07-28 02:30:08 +0800221 if (Z_TYPE_P(creds_obj) == IS_NULL) {
Stanley Cheungcccf9292016-02-12 16:37:19 -0800222 creds = NULL;
thinkerouba75c012016-07-28 02:30:08 +0800223 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 Cheungcccf9292016-02-12 16:37:19 -0800226 zend_throw_exception(spl_ce_InvalidArgumentException,
227 "credentials must be a ChannelCredentials object",
228 1 TSRMLS_CC);
229 return;
mlumishb892a272014-12-09 16:28:23 -0800230 } else {
thinkeroua3730b72016-07-20 16:59:54 +0800231 creds = Z_WRAPPED_GRPC_CHANNEL_CREDS_P(creds_obj);
thinkerouba75c012016-07-28 02:30:08 +0800232 php_grpc_zend_hash_del(array_hash, "credentials", sizeof("credentials"));
thinkeroua3730b72016-07-20 16:59:54 +0800233 }
234 }
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700235 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 Cheungcccf9292016-02-12 16:37:19 -0800241 }
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700242
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 }
mlumishb892a272014-12-09 16:28:23 -0800311}
312
313/**
Stanley Cheungdb98e082015-07-27 10:19:45 -0700314 * Get the endpoint this call/stream is connected to
315 * @return string The URI of the endpoint
316 */
317PHP_METHOD(Channel, getTarget) {
thinkeroua3730b72016-07-20 16:59:54 +0800318 wrapped_grpc_channel *channel = Z_WRAPPED_GRPC_CHANNEL_P(getThis());
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700319 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 Cheungdb98e082015-07-27 10:19:45 -0700329}
330
331/**
Stanley Cheunge63354a2015-08-10 15:46:42 -0700332 * Get the connectivity state of the channel
thinkerouefbc9e72016-08-16 20:00:36 +0800333 * @param bool $try_to_connect Try to connect on the channel (optional)
Stanley Cheunge63354a2015-08-10 15:46:42 -0700334 * @return long The grpc connectivity state
335 */
336PHP_METHOD(Channel, getConnectivityState) {
thinkeroua3730b72016-07-20 16:59:54 +0800337 wrapped_grpc_channel *channel = Z_WRAPPED_GRPC_CHANNEL_P(getThis());
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700338 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
thinkeroua3730b72016-07-20 16:59:54 +0800346 bool try_to_connect = false;
347
Stanley Cheunge63354a2015-08-10 15:46:42 -0700348 /* "|b" == 1 optional bool */
thinkeroua3730b72016-07-20 16:59:54 +0800349 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &try_to_connect)
350 == FAILURE) {
Stanley Cheunge63354a2015-08-10 15:46:42 -0700351 zend_throw_exception(spl_ce_InvalidArgumentException,
352 "getConnectivityState expects a bool", 1 TSRMLS_CC);
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700353 gpr_mu_unlock(&channel->wrapper->mu);
Stanley Cheunge63354a2015-08-10 15:46:42 -0700354 return;
355 }
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700356 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 Cheunge63354a2015-08-10 15:46:42 -0700365}
366
367/**
Stanley Cheunga63fdd02015-08-11 15:11:11 -0700368 * Watch the connectivity state of the channel until it changed
thinkerouefbc9e72016-08-16 20:00:36 +0800369 * @param long $last_state The previous connectivity state of the channel
370 * @param Timeval $deadline_obj The deadline this function should wait until
Stanley Cheung4c5c7b82015-08-12 16:28:58 -0700371 * @return bool If the connectivity state changes from last_state
372 * before deadline
Stanley Cheunga63fdd02015-08-11 15:11:11 -0700373 */
374PHP_METHOD(Channel, watchConnectivityState) {
thinkeroua3730b72016-07-20 16:59:54 +0800375 wrapped_grpc_channel *channel = Z_WRAPPED_GRPC_CHANNEL_P(getThis());
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700376 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
thinkerou19304682016-07-22 02:43:19 +0800384 php_grpc_long last_state;
Stanley Cheunga63fdd02015-08-11 15:11:11 -0700385 zval *deadline_obj;
thinkeroua3730b72016-07-20 16:59:54 +0800386
Stanley Cheunga63fdd02015-08-11 15:11:11 -0700387 /* "lO" == 1 long 1 object */
388 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "lO",
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700389 &last_state, &deadline_obj,
390 grpc_ce_timeval) == FAILURE) {
Stanley Cheunga63fdd02015-08-11 15:11:11 -0700391 zend_throw_exception(spl_ce_InvalidArgumentException,
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700392 "watchConnectivityState expects 1 long 1 timeval",
393 1 TSRMLS_CC);
394 gpr_mu_unlock(&channel->wrapper->mu);
Stanley Cheunga63fdd02015-08-11 15:11:11 -0700395 return;
396 }
397
thinkeroua3730b72016-07-20 16:59:54 +0800398 wrapped_grpc_timeval *deadline = Z_WRAPPED_GRPC_TIMEVAL_P(deadline_obj);
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700399 grpc_channel_watch_connectivity_state(channel->wrapper->wrapped,
thinkeroua3730b72016-07-20 16:59:54 +0800400 (grpc_connectivity_state)last_state,
401 deadline->wrapped, completion_queue,
402 NULL);
403 grpc_event event =
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700404 grpc_completion_queue_pluck(completion_queue, NULL,
405 gpr_inf_future(GPR_CLOCK_REALTIME), NULL);
406 gpr_mu_unlock(&channel->wrapper->mu);
Stanley Cheung1567c0c2015-08-13 11:12:54 -0700407 RETURN_BOOL(event.success);
Stanley Cheunga63fdd02015-08-11 15:11:11 -0700408}
409
410/**
mlumishb892a272014-12-09 16:28:23 -0800411 * Close the channel
thinkerou03dc2192016-08-16 19:31:44 +0800412 * @return void
mlumishb892a272014-12-09 16:28:23 -0800413 */
Craig Tillerb5dcec52015-01-13 11:13:42 -0800414PHP_METHOD(Channel, close) {
thinkeroua3730b72016-07-20 16:59:54 +0800415 wrapped_grpc_channel *channel = Z_WRAPPED_GRPC_CHANNEL_P(getThis());
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700416 gpr_mu_lock(&channel->wrapper->mu);
417 if (channel->wrapper->wrapped != NULL) {
418 grpc_channel_destroy(channel->wrapper->wrapped);
419 channel->wrapper->wrapped = NULL;
mlumishb892a272014-12-09 16:28:23 -0800420 }
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700421
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
430void 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
445static 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);
mlumishb892a272014-12-09 16:28:23 -0800458}
459
Stanley Cheung6a5c83d2017-02-16 12:25:32 -0800460ZEND_BEGIN_ARG_INFO_EX(arginfo_construct, 0, 0, 2)
461 ZEND_ARG_INFO(0, target)
462 ZEND_ARG_INFO(0, args)
463ZEND_END_ARG_INFO()
464
465ZEND_BEGIN_ARG_INFO_EX(arginfo_getTarget, 0, 0, 0)
466ZEND_END_ARG_INFO()
467
468ZEND_BEGIN_ARG_INFO_EX(arginfo_getConnectivityState, 0, 0, 0)
469 ZEND_ARG_INFO(0, try_to_connect)
470ZEND_END_ARG_INFO()
471
472ZEND_BEGIN_ARG_INFO_EX(arginfo_watchConnectivityState, 0, 0, 2)
473 ZEND_ARG_INFO(0, last_state)
474 ZEND_ARG_INFO(0, deadline)
475ZEND_END_ARG_INFO()
476
477ZEND_BEGIN_ARG_INFO_EX(arginfo_close, 0, 0, 0)
478ZEND_END_ARG_INFO()
479
mlumishb892a272014-12-09 16:28:23 -0800480static zend_function_entry channel_methods[] = {
Stanley Cheung6a5c83d2017-02-16 12:25:32 -0800481 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)
thinkeroua3730b72016-07-20 16:59:54 +0800491 PHP_FE_END
492};
mlumishb892a272014-12-09 16:28:23 -0800493
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700494GRPC_STARTUP_FUNCTION(channel) {
mlumishb892a272014-12-09 16:28:23 -0800495 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 Cheung5b3dc4a2017-08-03 18:00:25 -0700499 le_plink = zend_register_list_destructors_ex(
500 NULL, php_grpc_channel_plink_dtor, "Persistent Channel", module_number);
thinkerou5dafd822016-07-28 22:43:38 +0800501 PHP_GRPC_INIT_HANDLER(wrapped_grpc_channel, channel_ce_handlers);
Stanley Cheung5b3dc4a2017-08-03 18:00:25 -0700502 return SUCCESS;
mlumishb892a272014-12-09 16:28:23 -0800503}