Craig Tiller | 1a61b17 | 2015-02-16 11:53:47 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Craig Tiller | 1a61b17 | 2015-02-16 11:53:47 -0800 | [diff] [blame] | 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 34 | #include "call.h" |
| 35 | |
| 36 | #ifdef HAVE_CONFIG_H |
| 37 | #include "config.h" |
| 38 | #endif |
| 39 | |
murgatroid99 | 8242ba7 | 2015-04-01 15:29:44 -0700 | [diff] [blame] | 40 | #include <php.h> |
| 41 | #include <php_ini.h> |
| 42 | #include <ext/standard/info.h> |
| 43 | #include <ext/spl/spl_exceptions.h> |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 44 | #include "php_grpc.h" |
Stanley Cheung | 3580580 | 2015-12-10 11:42:55 -0800 | [diff] [blame] | 45 | #include "call_credentials.h" |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 46 | |
murgatroid99 | 8242ba7 | 2015-04-01 15:29:44 -0700 | [diff] [blame] | 47 | #include <zend_exceptions.h> |
| 48 | #include <zend_hash.h> |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 49 | |
| 50 | #include <stdbool.h> |
| 51 | |
murgatroid99 | 8242ba7 | 2015-04-01 15:29:44 -0700 | [diff] [blame] | 52 | #include <grpc/support/alloc.h> |
| 53 | #include <grpc/grpc.h> |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 54 | |
murgatroid99 | 268acd5 | 2015-05-14 15:05:00 -0700 | [diff] [blame] | 55 | #include "completion_queue.h" |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 56 | #include "timeval.h" |
| 57 | #include "channel.h" |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 58 | #include "byte_buffer.h" |
| 59 | |
Xiaoguang Sun | 8a929a9 | 2015-03-13 14:22:31 +0800 | [diff] [blame] | 60 | zend_class_entry *grpc_ce_call; |
thinkerou | dba5b0c | 2016-07-27 18:39:16 +0800 | [diff] [blame] | 61 | #if PHP_MAJOR_VERSION >= 7 |
| 62 | static zend_object_handlers call_ce_handlers; |
| 63 | #endif |
Xiaoguang Sun | 8a929a9 | 2015-03-13 14:22:31 +0800 | [diff] [blame] | 64 | |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 65 | /* Frees and destroys an instance of wrapped_grpc_call */ |
thinkerou | 011d1ef | 2016-07-27 09:44:49 +0800 | [diff] [blame] | 66 | PHP_GRPC_FREE_WRAPPED_FUNC_START(wrapped_grpc_call) |
| 67 | if (p->owned && p->wrapped != NULL) { |
| 68 | grpc_call_destroy(p->wrapped); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 69 | } |
thinkerou | 011d1ef | 2016-07-27 09:44:49 +0800 | [diff] [blame] | 70 | PHP_GRPC_FREE_WRAPPED_FUNC_END() |
| 71 | |
thinkerou | 03dc219 | 2016-08-16 19:31:44 +0800 | [diff] [blame] | 72 | /* Initializes an instance of wrapped_grpc_call to be associated with an |
| 73 | * object of a class specified by class_type */ |
thinkerou | dba5b0c | 2016-07-27 18:39:16 +0800 | [diff] [blame] | 74 | php_grpc_zend_object create_wrapped_grpc_call(zend_class_entry *class_type |
| 75 | TSRMLS_DC) { |
thinkerou | ba75c01 | 2016-07-28 02:30:08 +0800 | [diff] [blame] | 76 | PHP_GRPC_ALLOC_CLASS_OBJECT(wrapped_grpc_call); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 77 | zend_object_std_init(&intern->std, class_type TSRMLS_CC); |
| 78 | object_properties_init(&intern->std, class_type); |
thinkerou | dc673c5 | 2016-07-28 09:49:38 +0800 | [diff] [blame] | 79 | PHP_GRPC_FREE_CLASS_OBJECT(wrapped_grpc_call, call_ce_handlers); |
thinkerou | dba5b0c | 2016-07-27 18:39:16 +0800 | [diff] [blame] | 80 | } |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 81 | |
| 82 | /* Creates and returns a PHP array object with the data in a |
| 83 | * grpc_metadata_array. Returns NULL on failure */ |
thinkerou | 6f9d30b | 2016-07-27 03:19:03 +0800 | [diff] [blame] | 84 | zval *grpc_parse_metadata_array(grpc_metadata_array |
| 85 | *metadata_array TSRMLS_DC) { |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 86 | int count = metadata_array->count; |
| 87 | grpc_metadata *elements = metadata_array->metadata; |
thinkerou | 6f9d30b | 2016-07-27 03:19:03 +0800 | [diff] [blame] | 88 | zval *array; |
| 89 | PHP_GRPC_MAKE_STD_ZVAL(array); |
| 90 | array_init(array); |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 91 | int i; |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 92 | HashTable *array_hash; |
thinkerou | 6f9d30b | 2016-07-27 03:19:03 +0800 | [diff] [blame] | 93 | zval *inner_array; |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 94 | char *str_key; |
| 95 | char *str_val; |
| 96 | size_t key_len; |
thinkerou | ba75c01 | 2016-07-28 02:30:08 +0800 | [diff] [blame] | 97 | zval *data = NULL; |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 98 | |
thinkerou | 6f9d30b | 2016-07-27 03:19:03 +0800 | [diff] [blame] | 99 | array_hash = Z_ARRVAL_P(array); |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 100 | grpc_metadata *elem; |
| 101 | for (i = 0; i < count; i++) { |
| 102 | elem = &elements[i]; |
Craig Tiller | 9e78d69 | 2016-12-08 17:05:02 -0800 | [diff] [blame^] | 103 | key_len = GRPC_SLICE_LENGTH(elem->key); |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 104 | str_key = ecalloc(key_len + 1, sizeof(char)); |
Craig Tiller | 9e78d69 | 2016-12-08 17:05:02 -0800 | [diff] [blame^] | 105 | memcpy(str_key, GRPC_SLICE_START_PTR(elem->key), key_len); |
| 106 | str_val = ecalloc(GRPC_SLICE_LENGTH(elem->value) + 1, sizeof(char)); |
| 107 | memcpy(str_val, GRPC_SLICE_START_PTR(elem->value), GRPC_SLICE_LENGTH(elem->value)); |
thinkerou | 5dafd82 | 2016-07-28 22:43:38 +0800 | [diff] [blame] | 108 | if (php_grpc_zend_hash_find(array_hash, str_key, key_len, (void **)&data) |
| 109 | == SUCCESS) { |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 110 | if (Z_TYPE_P(data) != IS_ARRAY) { |
thinkerou | 6f9d30b | 2016-07-27 03:19:03 +0800 | [diff] [blame] | 111 | zend_throw_exception(zend_exception_get_default(TSRMLS_C), |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 112 | "Metadata hash somehow contains wrong types.", |
thinkerou | 6f9d30b | 2016-07-27 03:19:03 +0800 | [diff] [blame] | 113 | 1 TSRMLS_CC); |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 114 | efree(str_key); |
| 115 | efree(str_val); |
thinkerou | 6f9d30b | 2016-07-27 03:19:03 +0800 | [diff] [blame] | 116 | return NULL; |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 117 | } |
Craig Tiller | 9e78d69 | 2016-12-08 17:05:02 -0800 | [diff] [blame^] | 118 | php_grpc_add_next_index_stringl(data, str_val, GRPC_SLICE_LENGTH(elem->value), |
thinkerou | 6f9d30b | 2016-07-27 03:19:03 +0800 | [diff] [blame] | 119 | false); |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 120 | } else { |
thinkerou | 6f9d30b | 2016-07-27 03:19:03 +0800 | [diff] [blame] | 121 | PHP_GRPC_MAKE_STD_ZVAL(inner_array); |
| 122 | array_init(inner_array); |
| 123 | php_grpc_add_next_index_stringl(inner_array, str_val, |
Craig Tiller | 9e78d69 | 2016-12-08 17:05:02 -0800 | [diff] [blame^] | 124 | GRPC_SLICE_LENGTH(elem->value), false); |
thinkerou | 6f9d30b | 2016-07-27 03:19:03 +0800 | [diff] [blame] | 125 | add_assoc_zval(array, str_key, inner_array); |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 126 | } |
| 127 | } |
thinkerou | 6f9d30b | 2016-07-27 03:19:03 +0800 | [diff] [blame] | 128 | return array; |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | /* Populates a grpc_metadata_array with the data in a PHP array object. |
| 132 | Returns true on success and false on failure */ |
| 133 | bool create_metadata_array(zval *array, grpc_metadata_array *metadata) { |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 134 | HashTable *array_hash; |
| 135 | HashTable *inner_array_hash; |
thinkerou | 6f9d30b | 2016-07-27 03:19:03 +0800 | [diff] [blame] | 136 | zval *value; |
thinkerou | ba75c01 | 2016-07-28 02:30:08 +0800 | [diff] [blame] | 137 | zval *inner_array; |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 138 | if (Z_TYPE_P(array) != IS_ARRAY) { |
| 139 | return false; |
| 140 | } |
| 141 | grpc_metadata_array_init(metadata); |
thinkerou | 6f9d30b | 2016-07-27 03:19:03 +0800 | [diff] [blame] | 142 | array_hash = Z_ARRVAL_P(array); |
thinkerou | ba75c01 | 2016-07-28 02:30:08 +0800 | [diff] [blame] | 143 | |
thinkerou | 11cb5c5 | 2016-07-28 07:29:17 +0800 | [diff] [blame] | 144 | char *key; |
thinkerou | ba75c01 | 2016-07-28 02:30:08 +0800 | [diff] [blame] | 145 | int key_type; |
| 146 | PHP_GRPC_HASH_FOREACH_STR_KEY_VAL_START(array_hash, key, key_type, |
| 147 | inner_array) |
thinkerou | 11cb5c5 | 2016-07-28 07:29:17 +0800 | [diff] [blame] | 148 | if (key_type != HASH_KEY_IS_STRING || key == NULL) { |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 149 | return false; |
| 150 | } |
| 151 | if (Z_TYPE_P(inner_array) != IS_ARRAY) { |
| 152 | return false; |
| 153 | } |
thinkerou | ba75c01 | 2016-07-28 02:30:08 +0800 | [diff] [blame] | 154 | inner_array_hash = Z_ARRVAL_P(inner_array); |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 155 | metadata->capacity += zend_hash_num_elements(inner_array_hash); |
thinkerou | ba75c01 | 2016-07-28 02:30:08 +0800 | [diff] [blame] | 156 | PHP_GRPC_HASH_FOREACH_END() |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 157 | |
| 158 | metadata->metadata = gpr_malloc(metadata->capacity * sizeof(grpc_metadata)); |
| 159 | |
thinkerou | ba75c01 | 2016-07-28 02:30:08 +0800 | [diff] [blame] | 160 | char *key1 = NULL; |
| 161 | int key_type1; |
| 162 | PHP_GRPC_HASH_FOREACH_STR_KEY_VAL_START(array_hash, key1, key_type1, |
| 163 | inner_array) |
| 164 | if (key_type1 != HASH_KEY_IS_STRING) { |
thinkerou | 6f9d30b | 2016-07-27 03:19:03 +0800 | [diff] [blame] | 165 | return false; |
| 166 | } |
Stanley Cheung | 129bca6 | 2016-08-26 19:54:57 -0700 | [diff] [blame] | 167 | if (!grpc_header_key_is_legal(key1, strlen(key1))) { |
| 168 | return false; |
| 169 | } |
thinkerou | ba75c01 | 2016-07-28 02:30:08 +0800 | [diff] [blame] | 170 | inner_array_hash = Z_ARRVAL_P(inner_array); |
| 171 | PHP_GRPC_HASH_FOREACH_VAL_START(inner_array_hash, value) |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 172 | if (Z_TYPE_P(value) != IS_STRING) { |
| 173 | return false; |
| 174 | } |
thinkerou | ba75c01 | 2016-07-28 02:30:08 +0800 | [diff] [blame] | 175 | metadata->metadata[metadata->count].key = key1; |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 176 | metadata->metadata[metadata->count].value = Z_STRVAL_P(value); |
| 177 | metadata->metadata[metadata->count].value_length = Z_STRLEN_P(value); |
| 178 | metadata->count += 1; |
thinkerou | ba75c01 | 2016-07-28 02:30:08 +0800 | [diff] [blame] | 179 | PHP_GRPC_HASH_FOREACH_END() |
| 180 | PHP_GRPC_HASH_FOREACH_END() |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 181 | return true; |
| 182 | } |
| 183 | |
thinkerou | 6f9d30b | 2016-07-27 03:19:03 +0800 | [diff] [blame] | 184 | /* Wraps a grpc_call struct in a PHP object. Owned indicates whether the |
| 185 | struct should be destroyed at the end of the object's lifecycle */ |
| 186 | zval *grpc_php_wrap_call(grpc_call *wrapped, bool owned TSRMLS_DC) { |
| 187 | zval *call_object; |
| 188 | PHP_GRPC_MAKE_STD_ZVAL(call_object); |
| 189 | object_init_ex(call_object, grpc_ce_call); |
| 190 | wrapped_grpc_call *call = Z_WRAPPED_GRPC_CALL_P(call_object); |
| 191 | call->wrapped = wrapped; |
| 192 | call->owned = owned; |
| 193 | return call_object; |
| 194 | } |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 195 | |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 196 | /** |
| 197 | * Constructs a new instance of the Call class. |
thinkerou | 03dc219 | 2016-08-16 19:31:44 +0800 | [diff] [blame] | 198 | * @param Channel $channel_obj The channel to associate the call with. |
| 199 | * Must not be closed. |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 200 | * @param string $method The method to call |
thinkerou | 03dc219 | 2016-08-16 19:31:44 +0800 | [diff] [blame] | 201 | * @param Timeval $deadline_obj The deadline for completing the call |
thinkerou | efbc9e7 | 2016-08-16 20:00:36 +0800 | [diff] [blame] | 202 | * @param string $host_override The host is set by user (optional) |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 203 | */ |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 204 | PHP_METHOD(Call, __construct) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 205 | zval *channel_obj; |
| 206 | char *method; |
thinkerou | 1930468 | 2016-07-22 02:43:19 +0800 | [diff] [blame] | 207 | php_grpc_int method_len; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 208 | zval *deadline_obj; |
Stanley Cheung | 478fb00 | 2015-08-19 14:25:00 -0700 | [diff] [blame] | 209 | char *host_override = NULL; |
thinkerou | 1930468 | 2016-07-22 02:43:19 +0800 | [diff] [blame] | 210 | php_grpc_int host_override_len = 0; |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 211 | wrapped_grpc_call *call = Z_WRAPPED_GRPC_CALL_P(getThis()); |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 212 | |
Stanley Cheung | 478fb00 | 2015-08-19 14:25:00 -0700 | [diff] [blame] | 213 | /* "OsO|s" == 1 Object, 1 string, 1 Object, 1 optional string */ |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 214 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "OsO|s", &channel_obj, |
| 215 | grpc_ce_channel, &method, &method_len, |
| 216 | &deadline_obj, grpc_ce_timeval, &host_override, |
| 217 | &host_override_len) == FAILURE) { |
| 218 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 219 | "Call expects a Channel, a String, a Timeval and " |
| 220 | "an optional String", 1 TSRMLS_CC); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 221 | return; |
| 222 | } |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 223 | wrapped_grpc_channel *channel = Z_WRAPPED_GRPC_CHANNEL_P(channel_obj); |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 224 | if (channel->wrapped == NULL) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 225 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 226 | "Call cannot be constructed from a closed Channel", |
| 227 | 1 TSRMLS_CC); |
| 228 | return; |
| 229 | } |
| 230 | add_property_zval(getThis(), "channel", channel_obj); |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 231 | wrapped_grpc_timeval *deadline = Z_WRAPPED_GRPC_TIMEVAL_P(deadline_obj); |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 232 | call->wrapped = |
| 233 | grpc_channel_create_call(channel->wrapped, NULL, GRPC_PROPAGATE_DEFAULTS, |
| 234 | completion_queue, method, host_override, |
| 235 | deadline->wrapped, NULL); |
Stanley Cheung | 51b3691 | 2016-06-29 15:05:59 -0700 | [diff] [blame] | 236 | call->owned = true; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | /** |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 240 | * Start a batch of RPC actions. |
thinkerou | efbc9e7 | 2016-08-16 20:00:36 +0800 | [diff] [blame] | 241 | * @param array $array Array of actions to take |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 242 | * @return object Object with results of all actions |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 243 | */ |
murgatroid99 | c1d7e24 | 2015-04-02 10:02:43 -0700 | [diff] [blame] | 244 | PHP_METHOD(Call, startBatch) { |
thinkerou | 6f9d30b | 2016-07-27 03:19:03 +0800 | [diff] [blame] | 245 | zval *result; |
| 246 | PHP_GRPC_MAKE_STD_ZVAL(result); |
| 247 | object_init(result); |
| 248 | php_grpc_ulong index; |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 249 | zval *recv_status; |
thinkerou | ba75c01 | 2016-07-28 02:30:08 +0800 | [diff] [blame] | 250 | PHP_GRPC_MAKE_STD_ZVAL(recv_status); |
| 251 | object_init(recv_status); |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 252 | zval *value; |
| 253 | zval *inner_value; |
| 254 | zval *message_value; |
| 255 | zval *message_flags; |
thinkerou | b9c7f3a | 2016-07-22 09:22:41 +0800 | [diff] [blame] | 256 | wrapped_grpc_call *call = Z_WRAPPED_GRPC_CALL_P(getThis()); |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 257 | |
| 258 | grpc_op ops[8]; |
| 259 | size_t op_num = 0; |
| 260 | zval *array; |
| 261 | HashTable *array_hash; |
| 262 | HashTable *status_hash; |
| 263 | HashTable *message_hash; |
| 264 | |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 265 | grpc_metadata_array metadata; |
| 266 | grpc_metadata_array trailing_metadata; |
| 267 | grpc_metadata_array recv_metadata; |
| 268 | grpc_metadata_array recv_trailing_metadata; |
| 269 | grpc_status_code status; |
| 270 | char *status_details = NULL; |
murgatroid99 | 9fe516a | 2015-03-11 14:47:10 -0700 | [diff] [blame] | 271 | size_t status_details_capacity = 0; |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 272 | grpc_byte_buffer *message; |
| 273 | int cancelled; |
| 274 | grpc_call_error error; |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 275 | char *message_str; |
| 276 | size_t message_len; |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 277 | |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 278 | grpc_metadata_array_init(&metadata); |
| 279 | grpc_metadata_array_init(&trailing_metadata); |
murgatroid99 | d8bb957 | 2015-03-11 09:18:06 -0700 | [diff] [blame] | 280 | grpc_metadata_array_init(&recv_metadata); |
| 281 | grpc_metadata_array_init(&recv_trailing_metadata); |
David Garcia Quintas | a301eaa | 2016-05-06 16:59:03 -0700 | [diff] [blame] | 282 | memset(ops, 0, sizeof(ops)); |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 283 | |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 284 | /* "a" == 1 array */ |
| 285 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &array) == |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 286 | FAILURE) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 287 | zend_throw_exception(spl_ce_InvalidArgumentException, |
thinkerou | 11cb5c5 | 2016-07-28 07:29:17 +0800 | [diff] [blame] | 288 | "start_batch expects an array", 1 TSRMLS_CC); |
| 289 | goto cleanup; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 290 | } |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 291 | |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 292 | array_hash = Z_ARRVAL_P(array); |
thinkerou | ba75c01 | 2016-07-28 02:30:08 +0800 | [diff] [blame] | 293 | |
| 294 | char *key = NULL; |
| 295 | int key_type; |
| 296 | PHP_GRPC_HASH_FOREACH_LONG_KEY_VAL_START(array_hash, key, key_type, index, |
thinkerou | 5dafd82 | 2016-07-28 22:43:38 +0800 | [diff] [blame] | 297 | value) |
thinkerou | 11cb5c5 | 2016-07-28 07:29:17 +0800 | [diff] [blame] | 298 | if (key_type != HASH_KEY_IS_LONG || key != NULL) { |
murgatroid99 | 5ca9f92 | 2015-02-03 11:21:11 -0800 | [diff] [blame] | 299 | zend_throw_exception(spl_ce_InvalidArgumentException, |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 300 | "batch keys must be integers", 1 TSRMLS_CC); |
| 301 | goto cleanup; |
murgatroid99 | 5ca9f92 | 2015-02-03 11:21:11 -0800 | [diff] [blame] | 302 | } |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 303 | switch(index) { |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 304 | case GRPC_OP_SEND_INITIAL_METADATA: |
thinkerou | ba75c01 | 2016-07-28 02:30:08 +0800 | [diff] [blame] | 305 | if (!create_metadata_array(value, &metadata)) { |
murgatroid99 | 5ca9f92 | 2015-02-03 11:21:11 -0800 | [diff] [blame] | 306 | zend_throw_exception(spl_ce_InvalidArgumentException, |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 307 | "Bad metadata value given", 1 TSRMLS_CC); |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 308 | goto cleanup; |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 309 | } |
thinkerou | 5dafd82 | 2016-07-28 22:43:38 +0800 | [diff] [blame] | 310 | ops[op_num].data.send_initial_metadata.count = metadata.count; |
| 311 | ops[op_num].data.send_initial_metadata.metadata = metadata.metadata; |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 312 | break; |
| 313 | case GRPC_OP_SEND_MESSAGE: |
thinkerou | ba75c01 | 2016-07-28 02:30:08 +0800 | [diff] [blame] | 314 | if (Z_TYPE_P(value) != IS_ARRAY) { |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 315 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 316 | "Expected an array for send message", |
| 317 | 1 TSRMLS_CC); |
| 318 | goto cleanup; |
| 319 | } |
thinkerou | ba75c01 | 2016-07-28 02:30:08 +0800 | [diff] [blame] | 320 | message_hash = Z_ARRVAL_P(value); |
| 321 | if (php_grpc_zend_hash_find(message_hash, "flags", sizeof("flags"), |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 322 | (void **)&message_flags) == SUCCESS) { |
thinkerou | ba75c01 | 2016-07-28 02:30:08 +0800 | [diff] [blame] | 323 | if (Z_TYPE_P(message_flags) != IS_LONG) { |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 324 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 325 | "Expected an int for message flags", |
| 326 | 1 TSRMLS_CC); |
| 327 | } |
thinkerou | ba75c01 | 2016-07-28 02:30:08 +0800 | [diff] [blame] | 328 | ops[op_num].flags = Z_LVAL_P(message_flags) & GRPC_WRITE_USED_MASK; |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 329 | } |
thinkerou | ba75c01 | 2016-07-28 02:30:08 +0800 | [diff] [blame] | 330 | if (php_grpc_zend_hash_find(message_hash, "message", sizeof("message"), |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 331 | (void **)&message_value) != SUCCESS || |
thinkerou | ba75c01 | 2016-07-28 02:30:08 +0800 | [diff] [blame] | 332 | Z_TYPE_P(message_value) != IS_STRING) { |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 333 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 334 | "Expected a string for send message", |
| 335 | 1 TSRMLS_CC); |
| 336 | goto cleanup; |
| 337 | } |
| 338 | ops[op_num].data.send_message = |
thinkerou | ba75c01 | 2016-07-28 02:30:08 +0800 | [diff] [blame] | 339 | string_to_byte_buffer(Z_STRVAL_P(message_value), |
| 340 | Z_STRLEN_P(message_value)); |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 341 | break; |
| 342 | case GRPC_OP_SEND_CLOSE_FROM_CLIENT: |
| 343 | break; |
| 344 | case GRPC_OP_SEND_STATUS_FROM_SERVER: |
thinkerou | ba75c01 | 2016-07-28 02:30:08 +0800 | [diff] [blame] | 345 | status_hash = Z_ARRVAL_P(value); |
| 346 | if (php_grpc_zend_hash_find(status_hash, "metadata", sizeof("metadata"), |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 347 | (void **)&inner_value) == SUCCESS) { |
thinkerou | ba75c01 | 2016-07-28 02:30:08 +0800 | [diff] [blame] | 348 | if (!create_metadata_array(inner_value, &trailing_metadata)) { |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 349 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 350 | "Bad trailing metadata value given", |
| 351 | 1 TSRMLS_CC); |
| 352 | goto cleanup; |
| 353 | } |
| 354 | ops[op_num].data.send_status_from_server.trailing_metadata = |
| 355 | trailing_metadata.metadata; |
| 356 | ops[op_num].data.send_status_from_server.trailing_metadata_count = |
| 357 | trailing_metadata.count; |
| 358 | } |
thinkerou | ba75c01 | 2016-07-28 02:30:08 +0800 | [diff] [blame] | 359 | if (php_grpc_zend_hash_find(status_hash, "code", sizeof("code"), |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 360 | (void**)&inner_value) == SUCCESS) { |
thinkerou | ba75c01 | 2016-07-28 02:30:08 +0800 | [diff] [blame] | 361 | if (Z_TYPE_P(inner_value) != IS_LONG) { |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 362 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 363 | "Status code must be an integer", |
| 364 | 1 TSRMLS_CC); |
| 365 | goto cleanup; |
| 366 | } |
| 367 | ops[op_num].data.send_status_from_server.status = |
thinkerou | ba75c01 | 2016-07-28 02:30:08 +0800 | [diff] [blame] | 368 | Z_LVAL_P(inner_value); |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 369 | } else { |
| 370 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 371 | "Integer status code is required", |
| 372 | 1 TSRMLS_CC); |
| 373 | goto cleanup; |
| 374 | } |
thinkerou | ba75c01 | 2016-07-28 02:30:08 +0800 | [diff] [blame] | 375 | if (php_grpc_zend_hash_find(status_hash, "details", sizeof("details"), |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 376 | (void**)&inner_value) == SUCCESS) { |
thinkerou | ba75c01 | 2016-07-28 02:30:08 +0800 | [diff] [blame] | 377 | if (Z_TYPE_P(inner_value) != IS_STRING) { |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 378 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 379 | "Status details must be a string", |
| 380 | 1 TSRMLS_CC); |
| 381 | goto cleanup; |
| 382 | } |
| 383 | ops[op_num].data.send_status_from_server.status_details = |
thinkerou | ba75c01 | 2016-07-28 02:30:08 +0800 | [diff] [blame] | 384 | Z_STRVAL_P(inner_value); |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 385 | } else { |
| 386 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 387 | "String status details is required", |
| 388 | 1 TSRMLS_CC); |
| 389 | goto cleanup; |
| 390 | } |
| 391 | break; |
| 392 | case GRPC_OP_RECV_INITIAL_METADATA: |
| 393 | ops[op_num].data.recv_initial_metadata = &recv_metadata; |
| 394 | break; |
| 395 | case GRPC_OP_RECV_MESSAGE: |
| 396 | ops[op_num].data.recv_message = &message; |
| 397 | break; |
| 398 | case GRPC_OP_RECV_STATUS_ON_CLIENT: |
| 399 | ops[op_num].data.recv_status_on_client.trailing_metadata = |
| 400 | &recv_trailing_metadata; |
| 401 | ops[op_num].data.recv_status_on_client.status = &status; |
| 402 | ops[op_num].data.recv_status_on_client.status_details = |
| 403 | &status_details; |
| 404 | ops[op_num].data.recv_status_on_client.status_details_capacity = |
| 405 | &status_details_capacity; |
| 406 | break; |
| 407 | case GRPC_OP_RECV_CLOSE_ON_SERVER: |
| 408 | ops[op_num].data.recv_close_on_server.cancelled = &cancelled; |
| 409 | break; |
| 410 | default: |
| 411 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 412 | "Unrecognized key in batch", 1 TSRMLS_CC); |
| 413 | goto cleanup; |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 414 | } |
| 415 | ops[op_num].op = (grpc_op_type)index; |
David Garcia Quintas | ba710e5 | 2015-06-15 13:31:15 -0700 | [diff] [blame] | 416 | ops[op_num].flags = 0; |
Craig Tiller | 4275899 | 2015-08-18 10:34:32 -0700 | [diff] [blame] | 417 | ops[op_num].reserved = NULL; |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 418 | op_num++; |
thinkerou | ba75c01 | 2016-07-28 02:30:08 +0800 | [diff] [blame] | 419 | PHP_GRPC_HASH_FOREACH_END() |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 420 | |
Nicolas "Pixel" Noble | 150b7c4 | 2015-08-01 01:15:10 +0200 | [diff] [blame] | 421 | error = grpc_call_start_batch(call->wrapped, ops, op_num, call->wrapped, |
| 422 | NULL); |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 423 | if (error != GRPC_CALL_OK) { |
| 424 | zend_throw_exception(spl_ce_LogicException, |
| 425 | "start_batch was called incorrectly", |
| 426 | (long)error TSRMLS_CC); |
| 427 | goto cleanup; |
| 428 | } |
Stanley Cheung | c0c9ba9 | 2015-08-18 16:19:38 -0700 | [diff] [blame] | 429 | grpc_completion_queue_pluck(completion_queue, call->wrapped, |
| 430 | gpr_inf_future(GPR_CLOCK_REALTIME), NULL); |
thinkerou | ba75c01 | 2016-07-28 02:30:08 +0800 | [diff] [blame] | 431 | #if PHP_MAJOR_VERSION >= 7 |
| 432 | zval recv_md; |
| 433 | #endif |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 434 | for (int i = 0; i < op_num; i++) { |
| 435 | switch(ops[i].op) { |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 436 | case GRPC_OP_SEND_INITIAL_METADATA: |
| 437 | add_property_bool(result, "send_metadata", true); |
| 438 | break; |
| 439 | case GRPC_OP_SEND_MESSAGE: |
| 440 | add_property_bool(result, "send_message", true); |
| 441 | break; |
| 442 | case GRPC_OP_SEND_CLOSE_FROM_CLIENT: |
| 443 | add_property_bool(result, "send_close", true); |
| 444 | break; |
| 445 | case GRPC_OP_SEND_STATUS_FROM_SERVER: |
| 446 | add_property_bool(result, "send_status", true); |
| 447 | break; |
| 448 | case GRPC_OP_RECV_INITIAL_METADATA: |
thinkerou | ba75c01 | 2016-07-28 02:30:08 +0800 | [diff] [blame] | 449 | #if PHP_MAJOR_VERSION < 7 |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 450 | array = grpc_parse_metadata_array(&recv_metadata TSRMLS_CC); |
| 451 | add_property_zval(result, "metadata", array); |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 452 | #else |
thinkerou | 6f9d30b | 2016-07-27 03:19:03 +0800 | [diff] [blame] | 453 | recv_md = *grpc_parse_metadata_array(&recv_metadata); |
| 454 | add_property_zval(result, "metadata", &recv_md); |
thinkerou | ba75c01 | 2016-07-28 02:30:08 +0800 | [diff] [blame] | 455 | #endif |
| 456 | PHP_GRPC_DELREF(array); |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 457 | break; |
| 458 | case GRPC_OP_RECV_MESSAGE: |
| 459 | byte_buffer_to_string(message, &message_str, &message_len); |
| 460 | if (message_str == NULL) { |
thinkerou | 6f9d30b | 2016-07-27 03:19:03 +0800 | [diff] [blame] | 461 | add_property_null(result, "message"); |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 462 | } else { |
thinkerou | ba75c01 | 2016-07-28 02:30:08 +0800 | [diff] [blame] | 463 | php_grpc_add_property_stringl(result, "message", message_str, |
| 464 | message_len, false); |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 465 | } |
| 466 | break; |
| 467 | case GRPC_OP_RECV_STATUS_ON_CLIENT: |
thinkerou | ba75c01 | 2016-07-28 02:30:08 +0800 | [diff] [blame] | 468 | #if PHP_MAJOR_VERSION < 7 |
| 469 | array = grpc_parse_metadata_array(&recv_trailing_metadata TSRMLS_CC); |
| 470 | add_property_zval(recv_status, "metadata", array); |
| 471 | #else |
thinkerou | 6f9d30b | 2016-07-27 03:19:03 +0800 | [diff] [blame] | 472 | recv_md = *grpc_parse_metadata_array(&recv_trailing_metadata); |
thinkerou | ba75c01 | 2016-07-28 02:30:08 +0800 | [diff] [blame] | 473 | add_property_zval(recv_status, "metadata", &recv_md); |
| 474 | #endif |
| 475 | PHP_GRPC_DELREF(array); |
| 476 | add_property_long(recv_status, "code", status); |
Stanley Cheung | 8e56502 | 2016-07-28 10:32:43 -0700 | [diff] [blame] | 477 | php_grpc_add_property_string(recv_status, "details", status_details, |
| 478 | true); |
thinkerou | ba75c01 | 2016-07-28 02:30:08 +0800 | [diff] [blame] | 479 | add_property_zval(result, "status", recv_status); |
| 480 | PHP_GRPC_DELREF(recv_status); |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 481 | break; |
| 482 | case GRPC_OP_RECV_CLOSE_ON_SERVER: |
thinkerou | 6f9d30b | 2016-07-27 03:19:03 +0800 | [diff] [blame] | 483 | add_property_bool(result, "cancelled", cancelled); |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 484 | break; |
| 485 | default: |
| 486 | break; |
| 487 | } |
| 488 | } |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 489 | |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 490 | cleanup: |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 491 | grpc_metadata_array_destroy(&metadata); |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 492 | grpc_metadata_array_destroy(&trailing_metadata); |
| 493 | grpc_metadata_array_destroy(&recv_metadata); |
| 494 | grpc_metadata_array_destroy(&recv_trailing_metadata); |
| 495 | if (status_details != NULL) { |
| 496 | gpr_free(status_details); |
mlumish | dba8789 | 2015-01-02 13:27:28 -0800 | [diff] [blame] | 497 | } |
Stanley Cheung | 82e6f32 | 2016-04-06 11:51:57 -0700 | [diff] [blame] | 498 | for (int i = 0; i < op_num; i++) { |
| 499 | if (ops[i].op == GRPC_OP_SEND_MESSAGE) { |
| 500 | grpc_byte_buffer_destroy(ops[i].data.send_message); |
| 501 | } |
| 502 | if (ops[i].op == GRPC_OP_RECV_MESSAGE) { |
| 503 | grpc_byte_buffer_destroy(message); |
| 504 | } |
| 505 | } |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 506 | RETURN_DESTROY_ZVAL(result); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 507 | } |
| 508 | |
murgatroid99 | c1da8f2 | 2015-03-25 11:33:05 -0700 | [diff] [blame] | 509 | /** |
Stanley Cheung | db98e08 | 2015-07-27 10:19:45 -0700 | [diff] [blame] | 510 | * Get the endpoint this call/stream is connected to |
| 511 | * @return string The URI of the endpoint |
| 512 | */ |
| 513 | PHP_METHOD(Call, getPeer) { |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 514 | wrapped_grpc_call *call = Z_WRAPPED_GRPC_CALL_P(getThis()); |
thinkerou | b9c7f3a | 2016-07-22 09:22:41 +0800 | [diff] [blame] | 515 | PHP_GRPC_RETURN_STRING(grpc_call_get_peer(call->wrapped), 1); |
Stanley Cheung | db98e08 | 2015-07-27 10:19:45 -0700 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | /** |
thinkerou | efbc9e7 | 2016-08-16 20:00:36 +0800 | [diff] [blame] | 519 | * Cancel the call. This will cause the call to end with STATUS_CANCELLED |
| 520 | * if it has not already ended with another status. |
| 521 | * @return void |
murgatroid99 | c1da8f2 | 2015-03-25 11:33:05 -0700 | [diff] [blame] | 522 | */ |
| 523 | PHP_METHOD(Call, cancel) { |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 524 | wrapped_grpc_call *call = Z_WRAPPED_GRPC_CALL_P(getThis()); |
Nicolas "Pixel" Noble | 150b7c4 | 2015-08-01 01:15:10 +0200 | [diff] [blame] | 525 | grpc_call_cancel(call->wrapped, NULL); |
murgatroid99 | c1da8f2 | 2015-03-25 11:33:05 -0700 | [diff] [blame] | 526 | } |
| 527 | |
Stanley Cheung | 3580580 | 2015-12-10 11:42:55 -0800 | [diff] [blame] | 528 | /** |
| 529 | * Set the CallCredentials for this call. |
thinkerou | efbc9e7 | 2016-08-16 20:00:36 +0800 | [diff] [blame] | 530 | * @param CallCredentials $creds_obj The CallCredentials object |
| 531 | * @return int The error code |
Stanley Cheung | 3580580 | 2015-12-10 11:42:55 -0800 | [diff] [blame] | 532 | */ |
| 533 | PHP_METHOD(Call, setCredentials) { |
| 534 | zval *creds_obj; |
| 535 | |
| 536 | /* "O" == 1 Object */ |
| 537 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &creds_obj, |
| 538 | grpc_ce_call_credentials) == FAILURE) { |
| 539 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 540 | "setCredentials expects 1 CallCredentials", |
| 541 | 1 TSRMLS_CC); |
| 542 | return; |
| 543 | } |
| 544 | |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 545 | wrapped_grpc_call_credentials *creds = |
| 546 | Z_WRAPPED_GRPC_CALL_CREDS_P(creds_obj); |
| 547 | wrapped_grpc_call *call = Z_WRAPPED_GRPC_CALL_P(getThis()); |
Stanley Cheung | 3580580 | 2015-12-10 11:42:55 -0800 | [diff] [blame] | 548 | |
| 549 | grpc_call_error error = GRPC_CALL_ERROR; |
| 550 | error = grpc_call_set_credentials(call->wrapped, creds->wrapped); |
| 551 | RETURN_LONG(error); |
| 552 | } |
| 553 | |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 554 | static zend_function_entry call_methods[] = { |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 555 | PHP_ME(Call, __construct, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR) |
| 556 | PHP_ME(Call, startBatch, NULL, ZEND_ACC_PUBLIC) |
| 557 | PHP_ME(Call, getPeer, NULL, ZEND_ACC_PUBLIC) |
| 558 | PHP_ME(Call, cancel, NULL, ZEND_ACC_PUBLIC) |
| 559 | PHP_ME(Call, setCredentials, NULL, ZEND_ACC_PUBLIC) |
| 560 | PHP_FE_END |
| 561 | }; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 562 | |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 563 | void grpc_init_call(TSRMLS_D) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 564 | zend_class_entry ce; |
| 565 | INIT_CLASS_ENTRY(ce, "Grpc\\Call", call_methods); |
| 566 | ce.create_object = create_wrapped_grpc_call; |
| 567 | grpc_ce_call = zend_register_internal_class(&ce TSRMLS_CC); |
thinkerou | 5dafd82 | 2016-07-28 22:43:38 +0800 | [diff] [blame] | 568 | PHP_GRPC_INIT_HANDLER(wrapped_grpc_call, call_ce_handlers); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 569 | } |