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; |
| 61 | |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 62 | #if PHP_MAJOR_VERSION < 7 |
| 63 | |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 64 | /* Frees and destroys an instance of wrapped_grpc_call */ |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 65 | void free_wrapped_grpc_call(void *object TSRMLS_DC) { |
| 66 | wrapped_grpc_call *call = (wrapped_grpc_call *)object; |
| 67 | if (call->owned && call->wrapped != NULL) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 68 | grpc_call_destroy(call->wrapped); |
| 69 | } |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 70 | zend_object_std_dtor(&call->std TSRMLS_CC); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 71 | efree(call); |
| 72 | } |
| 73 | |
| 74 | /* Initializes an instance of wrapped_grpc_call to be associated with an object |
| 75 | * of a class specified by class_type */ |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 76 | zend_object_value create_wrapped_grpc_call(zend_class_entry *class_type |
| 77 | TSRMLS_DC) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 78 | zend_object_value retval; |
| 79 | wrapped_grpc_call *intern; |
| 80 | |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 81 | intern = (wrapped_grpc_call *)emalloc(sizeof(wrapped_grpc_call)); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 82 | memset(intern, 0, sizeof(wrapped_grpc_call)); |
| 83 | |
| 84 | zend_object_std_init(&intern->std, class_type TSRMLS_CC); |
| 85 | object_properties_init(&intern->std, class_type); |
| 86 | retval.handle = zend_objects_store_put( |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 87 | intern, (zend_objects_store_dtor_t)zend_objects_destroy_object, |
| 88 | free_wrapped_grpc_call, NULL TSRMLS_CC); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 89 | retval.handlers = zend_get_std_object_handlers(); |
| 90 | return retval; |
| 91 | } |
| 92 | |
mlumish | 34cd1f0 | 2015-01-02 13:32:41 -0800 | [diff] [blame] | 93 | /* Wraps a grpc_call struct in a PHP object. Owned indicates whether the struct |
| 94 | should be destroyed at the end of the object's lifecycle */ |
Michael Bausor | 4f8e40b | 2016-05-16 11:41:25 -0700 | [diff] [blame] | 95 | zval *grpc_php_wrap_call(grpc_call *wrapped, bool owned TSRMLS_DC) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 96 | zval *call_object; |
| 97 | MAKE_STD_ZVAL(call_object); |
| 98 | object_init_ex(call_object, grpc_ce_call); |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 99 | wrapped_grpc_call *call = |
| 100 | (wrapped_grpc_call *)zend_object_store_get_object(call_object TSRMLS_CC); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 101 | call->wrapped = wrapped; |
thinkerou | d04376d | 2016-04-27 19:58:49 +0800 | [diff] [blame] | 102 | call->owned = owned; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 103 | return call_object; |
| 104 | } |
| 105 | |
murgatroid99 | 9c4425a | 2015-03-24 09:43:41 -0700 | [diff] [blame] | 106 | /* Creates and returns a PHP array object with the data in a |
| 107 | * grpc_metadata_array. Returns NULL on failure */ |
Michael Bausor | 4f8e40b | 2016-05-16 11:41:25 -0700 | [diff] [blame] | 108 | zval *grpc_parse_metadata_array(grpc_metadata_array *metadata_array TSRMLS_DC) { |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 109 | int count = metadata_array->count; |
| 110 | grpc_metadata *elements = metadata_array->metadata; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 111 | int i; |
| 112 | zval *array; |
| 113 | zval **data = NULL; |
| 114 | HashTable *array_hash; |
| 115 | zval *inner_array; |
| 116 | char *str_key; |
| 117 | char *str_val; |
| 118 | size_t key_len; |
| 119 | MAKE_STD_ZVAL(array); |
| 120 | array_init(array); |
| 121 | array_hash = Z_ARRVAL_P(array); |
| 122 | grpc_metadata *elem; |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 123 | for (i = 0; i < count; i++) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 124 | elem = &elements[i]; |
| 125 | key_len = strlen(elem->key); |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 126 | str_key = ecalloc(key_len + 1, sizeof(char)); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 127 | memcpy(str_key, elem->key, key_len); |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 128 | str_val = ecalloc(elem->value_length + 1, sizeof(char)); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 129 | memcpy(str_val, elem->value, elem->value_length); |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 130 | if (zend_hash_find(array_hash, str_key, key_len, (void **)data) == |
| 131 | SUCCESS) { |
Stanley Cheung | b91f0f2 | 2016-02-16 09:36:36 -0800 | [diff] [blame] | 132 | if (Z_TYPE_P(*data) != IS_ARRAY) { |
Michael Bausor | 4f8e40b | 2016-05-16 11:41:25 -0700 | [diff] [blame] | 133 | zend_throw_exception(zend_exception_get_default(TSRMLS_C), |
Stanley Cheung | b91f0f2 | 2016-02-16 09:36:36 -0800 | [diff] [blame] | 134 | "Metadata hash somehow contains wrong types.", |
| 135 | 1 TSRMLS_CC); |
| 136 | efree(str_key); |
| 137 | efree(str_val); |
| 138 | return NULL; |
| 139 | } |
| 140 | add_next_index_stringl(*data, str_val, elem->value_length, false); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 141 | } else { |
murgatroid99 | 5ca9f92 | 2015-02-03 11:21:11 -0800 | [diff] [blame] | 142 | MAKE_STD_ZVAL(inner_array); |
| 143 | array_init(inner_array); |
| 144 | add_next_index_stringl(inner_array, str_val, elem->value_length, false); |
| 145 | add_assoc_zval(array, str_key, inner_array); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | return array; |
| 149 | } |
| 150 | |
murgatroid99 | 9c4425a | 2015-03-24 09:43:41 -0700 | [diff] [blame] | 151 | /* Populates a grpc_metadata_array with the data in a PHP array object. |
| 152 | Returns true on success and false on failure */ |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 153 | bool create_metadata_array(zval *array, grpc_metadata_array *metadata) { |
| 154 | zval **inner_array; |
| 155 | zval **value; |
| 156 | HashTable *array_hash; |
| 157 | HashPosition array_pointer; |
| 158 | HashTable *inner_array_hash; |
| 159 | HashPosition inner_array_pointer; |
| 160 | char *key; |
| 161 | uint key_len; |
| 162 | ulong index; |
| 163 | if (Z_TYPE_P(array) != IS_ARRAY) { |
| 164 | return false; |
| 165 | } |
| 166 | grpc_metadata_array_init(metadata); |
| 167 | array_hash = Z_ARRVAL_P(array); |
| 168 | for (zend_hash_internal_pointer_reset_ex(array_hash, &array_pointer); |
| 169 | zend_hash_get_current_data_ex(array_hash, (void**)&inner_array, |
| 170 | &array_pointer) == SUCCESS; |
| 171 | zend_hash_move_forward_ex(array_hash, &array_pointer)) { |
| 172 | if (zend_hash_get_current_key_ex(array_hash, &key, &key_len, &index, 0, |
| 173 | &array_pointer) != HASH_KEY_IS_STRING) { |
| 174 | return false; |
| 175 | } |
| 176 | if (Z_TYPE_P(*inner_array) != IS_ARRAY) { |
| 177 | return false; |
| 178 | } |
| 179 | inner_array_hash = Z_ARRVAL_P(*inner_array); |
| 180 | metadata->capacity += zend_hash_num_elements(inner_array_hash); |
| 181 | } |
| 182 | metadata->metadata = gpr_malloc(metadata->capacity * sizeof(grpc_metadata)); |
| 183 | for (zend_hash_internal_pointer_reset_ex(array_hash, &array_pointer); |
| 184 | zend_hash_get_current_data_ex(array_hash, (void**)&inner_array, |
| 185 | &array_pointer) == SUCCESS; |
| 186 | zend_hash_move_forward_ex(array_hash, &array_pointer)) { |
| 187 | if (zend_hash_get_current_key_ex(array_hash, &key, &key_len, &index, 0, |
| 188 | &array_pointer) != HASH_KEY_IS_STRING) { |
| 189 | return false; |
| 190 | } |
| 191 | inner_array_hash = Z_ARRVAL_P(*inner_array); |
| 192 | for (zend_hash_internal_pointer_reset_ex(inner_array_hash, |
| 193 | &inner_array_pointer); |
| 194 | zend_hash_get_current_data_ex(inner_array_hash, (void**)&value, |
| 195 | &inner_array_pointer) == SUCCESS; |
| 196 | zend_hash_move_forward_ex(inner_array_hash, &inner_array_pointer)) { |
| 197 | if (Z_TYPE_P(*value) != IS_STRING) { |
| 198 | return false; |
| 199 | } |
| 200 | metadata->metadata[metadata->count].key = key; |
| 201 | metadata->metadata[metadata->count].value = Z_STRVAL_P(*value); |
| 202 | metadata->metadata[metadata->count].value_length = Z_STRLEN_P(*value); |
| 203 | metadata->count += 1; |
| 204 | } |
| 205 | } |
| 206 | return true; |
| 207 | } |
| 208 | |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 209 | #else |
| 210 | |
| 211 | static zend_object_handlers call_ce_handlers; |
| 212 | |
| 213 | /* Frees and destroys an instance of wrapped_grpc_call */ |
| 214 | static void free_wrapped_grpc_call(zend_object *object) { |
| 215 | wrapped_grpc_call *call = wrapped_grpc_call_from_obj(object); |
| 216 | if (call->owned && call->wrapped != NULL) { |
| 217 | grpc_call_destroy(call->wrapped); |
| 218 | } |
| 219 | zend_object_std_dtor(&call->std); |
| 220 | } |
| 221 | |
| 222 | /* Initializes an instance of wrapped_grpc_call to be associated with an |
| 223 | * object of a class specified by class_type */ |
| 224 | zend_object *create_wrapped_grpc_call(zend_class_entry *class_type) { |
| 225 | wrapped_grpc_call *intern; |
| 226 | intern = ecalloc(1, sizeof(wrapped_grpc_call) + |
| 227 | zend_object_properties_size(class_type)); |
| 228 | zend_object_std_init(&intern->std, class_type); |
| 229 | object_properties_init(&intern->std, class_type); |
| 230 | intern->std.handlers = &call_ce_handlers; |
| 231 | return &intern->std; |
| 232 | } |
| 233 | |
| 234 | /* Wraps a grpc_call struct in a PHP object. Owned indicates whether the |
| 235 | struct should be destroyed at the end of the object's lifecycle */ |
| 236 | void grpc_php_wrap_call(grpc_call *wrapped, bool owned, zval *call_object) { |
| 237 | object_init_ex(call_object, grpc_ce_call); |
| 238 | wrapped_grpc_call *call = Z_WRAPPED_GRPC_CALL_P(call_object); |
| 239 | call->wrapped = wrapped; |
| 240 | call->owned = owned; |
| 241 | } |
| 242 | |
| 243 | /* Creates and returns a PHP array object with the data in a |
| 244 | * grpc_metadata_array. Returns NULL on failure */ |
| 245 | void grpc_parse_metadata_array(grpc_metadata_array *metadata_array, |
| 246 | zval *array) { |
| 247 | int count = metadata_array->count; |
| 248 | grpc_metadata *elements = metadata_array->metadata; |
| 249 | int i; |
| 250 | zval *data; |
| 251 | HashTable *array_hash; |
| 252 | zval inner_array; |
| 253 | char *str_key; |
| 254 | char *str_val; |
| 255 | size_t key_len; |
| 256 | |
| 257 | array_init(array); |
| 258 | array_hash = HASH_OF(array); |
| 259 | grpc_metadata *elem; |
| 260 | for (i = 0; i < count; i++) { |
| 261 | elem = &elements[i]; |
| 262 | key_len = strlen(elem->key); |
| 263 | str_key = ecalloc(key_len + 1, sizeof(char)); |
| 264 | memcpy(str_key, elem->key, key_len); |
| 265 | str_val = ecalloc(elem->value_length + 1, sizeof(char)); |
| 266 | memcpy(str_val, elem->value, elem->value_length); |
| 267 | if ((data = zend_hash_str_find(array_hash, str_key, key_len)) != NULL) { |
| 268 | if (Z_TYPE_P(data) != IS_ARRAY) { |
| 269 | zend_throw_exception(zend_exception_get_default(), |
| 270 | "Metadata hash somehow contains wrong types.", |
| 271 | 1); |
| 272 | efree(str_key); |
| 273 | efree(str_val); |
| 274 | return; |
| 275 | } |
| 276 | add_next_index_stringl(data, str_val, elem->value_length); |
| 277 | } else { |
| 278 | array_init(&inner_array); |
| 279 | add_next_index_stringl(&inner_array, str_val, elem->value_length); |
| 280 | add_assoc_zval(array, str_key, &inner_array); |
| 281 | } |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | /* Populates a grpc_metadata_array with the data in a PHP array object. |
| 286 | Returns true on success and false on failure */ |
| 287 | bool create_metadata_array(zval *array, grpc_metadata_array *metadata) { |
| 288 | zval *inner_array; |
| 289 | zval *value; |
| 290 | HashTable *array_hash; |
| 291 | HashTable *inner_array_hash; |
| 292 | zend_string *key; |
| 293 | if (Z_TYPE_P(array) != IS_ARRAY) { |
| 294 | return false; |
| 295 | } |
| 296 | grpc_metadata_array_init(metadata); |
| 297 | array_hash = HASH_OF(array); |
| 298 | |
| 299 | ZEND_HASH_FOREACH_STR_KEY_VAL(array_hash, key, inner_array) { |
| 300 | if (key == NULL) { |
| 301 | return false; |
| 302 | } |
| 303 | if (Z_TYPE_P(inner_array) != IS_ARRAY) { |
| 304 | return false; |
| 305 | } |
| 306 | inner_array_hash = HASH_OF(inner_array); |
| 307 | metadata->capacity += zend_hash_num_elements(inner_array_hash); |
| 308 | } |
| 309 | ZEND_HASH_FOREACH_END(); |
| 310 | |
| 311 | metadata->metadata = gpr_malloc(metadata->capacity * sizeof(grpc_metadata)); |
| 312 | |
| 313 | ZEND_HASH_FOREACH_STR_KEY_VAL(array_hash, key, inner_array) { |
| 314 | if (key == NULL) { |
| 315 | return false; |
| 316 | } |
| 317 | inner_array_hash = HASH_OF(inner_array); |
| 318 | |
| 319 | ZEND_HASH_FOREACH_VAL(inner_array_hash, value) { |
| 320 | if (Z_TYPE_P(value) != IS_STRING) { |
| 321 | return false; |
| 322 | } |
| 323 | metadata->metadata[metadata->count].key = ZSTR_VAL(key); |
| 324 | metadata->metadata[metadata->count].value = Z_STRVAL_P(value); |
| 325 | metadata->metadata[metadata->count].value_length = Z_STRLEN_P(value); |
| 326 | metadata->count += 1; |
| 327 | } ZEND_HASH_FOREACH_END(); |
| 328 | } ZEND_HASH_FOREACH_END(); |
| 329 | return true; |
| 330 | } |
| 331 | |
| 332 | #endif |
| 333 | |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 334 | /** |
| 335 | * Constructs a new instance of the Call class. |
| 336 | * @param Channel $channel The channel to associate the call with. Must not be |
| 337 | * closed. |
| 338 | * @param string $method The method to call |
| 339 | * @param Timeval $absolute_deadline The deadline for completing the call |
| 340 | */ |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 341 | PHP_METHOD(Call, __construct) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 342 | zval *channel_obj; |
| 343 | char *method; |
thinkerou | 1930468 | 2016-07-22 02:43:19 +0800 | [diff] [blame^] | 344 | php_grpc_int method_len; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 345 | zval *deadline_obj; |
Stanley Cheung | 478fb00 | 2015-08-19 14:25:00 -0700 | [diff] [blame] | 346 | char *host_override = NULL; |
thinkerou | 1930468 | 2016-07-22 02:43:19 +0800 | [diff] [blame^] | 347 | php_grpc_int host_override_len = 0; |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 348 | wrapped_grpc_call *call = Z_WRAPPED_GRPC_CALL_P(getThis()); |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 349 | |
Stanley Cheung | 478fb00 | 2015-08-19 14:25:00 -0700 | [diff] [blame] | 350 | /* "OsO|s" == 1 Object, 1 string, 1 Object, 1 optional string */ |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 351 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "OsO|s", &channel_obj, |
| 352 | grpc_ce_channel, &method, &method_len, |
| 353 | &deadline_obj, grpc_ce_timeval, &host_override, |
| 354 | &host_override_len) == FAILURE) { |
| 355 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 356 | "Call expects a Channel, a String, a Timeval and " |
| 357 | "an optional String", 1 TSRMLS_CC); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 358 | return; |
| 359 | } |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 360 | wrapped_grpc_channel *channel = Z_WRAPPED_GRPC_CHANNEL_P(channel_obj); |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 361 | if (channel->wrapped == NULL) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 362 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 363 | "Call cannot be constructed from a closed Channel", |
| 364 | 1 TSRMLS_CC); |
| 365 | return; |
| 366 | } |
| 367 | add_property_zval(getThis(), "channel", channel_obj); |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 368 | wrapped_grpc_timeval *deadline = Z_WRAPPED_GRPC_TIMEVAL_P(deadline_obj); |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 369 | call->wrapped = |
| 370 | grpc_channel_create_call(channel->wrapped, NULL, GRPC_PROPAGATE_DEFAULTS, |
| 371 | completion_queue, method, host_override, |
| 372 | deadline->wrapped, NULL); |
Stanley Cheung | 51b3691 | 2016-06-29 15:05:59 -0700 | [diff] [blame] | 373 | call->owned = true; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | /** |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 377 | * Start a batch of RPC actions. |
| 378 | * @param array batch Array of actions to take |
| 379 | * @return object Object with results of all actions |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 380 | */ |
murgatroid99 | c1d7e24 | 2015-04-02 10:02:43 -0700 | [diff] [blame] | 381 | PHP_METHOD(Call, startBatch) { |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 382 | #if PHP_MAJOR_VERSION < 7 |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 383 | wrapped_grpc_call *call = |
| 384 | (wrapped_grpc_call *)zend_object_store_get_object(getThis() TSRMLS_CC); |
murgatroid99 | 5ca9f92 | 2015-02-03 11:21:11 -0800 | [diff] [blame] | 385 | zval **value; |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 386 | zval **inner_value; |
murgatroid99 | 5ca9f92 | 2015-02-03 11:21:11 -0800 | [diff] [blame] | 387 | HashPosition array_pointer; |
Stanley Cheung | 3ab8e79 | 2015-08-24 16:58:42 -0700 | [diff] [blame] | 388 | zval **message_value; |
| 389 | zval **message_flags; |
murgatroid99 | 5ca9f92 | 2015-02-03 11:21:11 -0800 | [diff] [blame] | 390 | char *key; |
| 391 | uint key_len; |
| 392 | ulong index; |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 393 | zval *result; |
| 394 | zval *recv_status; |
| 395 | MAKE_STD_ZVAL(result); |
| 396 | object_init(result); |
| 397 | #else |
| 398 | wrapped_grpc_call *call = Z_WRAPPED_GRPC_CALL_P(getThis()); |
| 399 | zval *value; |
| 400 | zval *inner_value; |
| 401 | zval *message_value; |
| 402 | zval *message_flags; |
| 403 | zend_string *key; |
| 404 | zend_ulong index; |
| 405 | zval recv_status; |
| 406 | object_init(return_value); |
| 407 | #endif |
| 408 | |
| 409 | grpc_op ops[8]; |
| 410 | size_t op_num = 0; |
| 411 | zval *array; |
| 412 | HashTable *array_hash; |
| 413 | HashTable *status_hash; |
| 414 | HashTable *message_hash; |
| 415 | |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 416 | grpc_metadata_array metadata; |
| 417 | grpc_metadata_array trailing_metadata; |
| 418 | grpc_metadata_array recv_metadata; |
| 419 | grpc_metadata_array recv_trailing_metadata; |
| 420 | grpc_status_code status; |
| 421 | char *status_details = NULL; |
murgatroid99 | 9fe516a | 2015-03-11 14:47:10 -0700 | [diff] [blame] | 422 | size_t status_details_capacity = 0; |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 423 | grpc_byte_buffer *message; |
| 424 | int cancelled; |
| 425 | grpc_call_error error; |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 426 | char *message_str; |
| 427 | size_t message_len; |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 428 | |
| 429 | |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 430 | grpc_metadata_array_init(&metadata); |
| 431 | grpc_metadata_array_init(&trailing_metadata); |
murgatroid99 | d8bb957 | 2015-03-11 09:18:06 -0700 | [diff] [blame] | 432 | grpc_metadata_array_init(&recv_metadata); |
| 433 | grpc_metadata_array_init(&recv_trailing_metadata); |
David Garcia Quintas | a301eaa | 2016-05-06 16:59:03 -0700 | [diff] [blame] | 434 | memset(ops, 0, sizeof(ops)); |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 435 | |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 436 | /* "a" == 1 array */ |
| 437 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &array) == |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 438 | FAILURE) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 439 | zend_throw_exception(spl_ce_InvalidArgumentException, |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 440 | "start_batch expects an array", 1 TSRMLS_CC); |
| 441 | goto cleanup; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 442 | } |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 443 | |
| 444 | #if PHP_MAJOR_VERSION < 7 |
| 445 | |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 446 | array_hash = Z_ARRVAL_P(array); |
murgatroid99 | 5ca9f92 | 2015-02-03 11:21:11 -0800 | [diff] [blame] | 447 | for (zend_hash_internal_pointer_reset_ex(array_hash, &array_pointer); |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 448 | zend_hash_get_current_data_ex(array_hash, (void**)&value, |
murgatroid99 | 5ca9f92 | 2015-02-03 11:21:11 -0800 | [diff] [blame] | 449 | &array_pointer) == SUCCESS; |
| 450 | zend_hash_move_forward_ex(array_hash, &array_pointer)) { |
| 451 | if (zend_hash_get_current_key_ex(array_hash, &key, &key_len, &index, 0, |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 452 | &array_pointer) != HASH_KEY_IS_LONG) { |
murgatroid99 | 5ca9f92 | 2015-02-03 11:21:11 -0800 | [diff] [blame] | 453 | zend_throw_exception(spl_ce_InvalidArgumentException, |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 454 | "batch keys must be integers", 1 TSRMLS_CC); |
| 455 | goto cleanup; |
murgatroid99 | 5ca9f92 | 2015-02-03 11:21:11 -0800 | [diff] [blame] | 456 | } |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 457 | switch(index) { |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 458 | case GRPC_OP_SEND_INITIAL_METADATA: |
| 459 | if (!create_metadata_array(*value, &metadata)) { |
murgatroid99 | 5ca9f92 | 2015-02-03 11:21:11 -0800 | [diff] [blame] | 460 | zend_throw_exception(spl_ce_InvalidArgumentException, |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 461 | "Bad metadata value given", 1 TSRMLS_CC); |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 462 | goto cleanup; |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 463 | } |
| 464 | ops[op_num].data.send_initial_metadata.count = |
| 465 | metadata.count; |
| 466 | ops[op_num].data.send_initial_metadata.metadata = |
| 467 | metadata.metadata; |
| 468 | break; |
| 469 | case GRPC_OP_SEND_MESSAGE: |
| 470 | if (Z_TYPE_PP(value) != IS_ARRAY) { |
| 471 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 472 | "Expected an array for send message", |
| 473 | 1 TSRMLS_CC); |
| 474 | goto cleanup; |
| 475 | } |
| 476 | message_hash = Z_ARRVAL_PP(value); |
| 477 | if (zend_hash_find(message_hash, "flags", sizeof("flags"), |
| 478 | (void **)&message_flags) == SUCCESS) { |
| 479 | if (Z_TYPE_PP(message_flags) != IS_LONG) { |
| 480 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 481 | "Expected an int for message flags", |
| 482 | 1 TSRMLS_CC); |
| 483 | } |
| 484 | ops[op_num].flags = Z_LVAL_PP(message_flags) & GRPC_WRITE_USED_MASK; |
| 485 | } |
| 486 | if (zend_hash_find(message_hash, "message", sizeof("message"), |
| 487 | (void **)&message_value) != SUCCESS || |
| 488 | Z_TYPE_PP(message_value) != IS_STRING) { |
| 489 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 490 | "Expected a string for send message", |
| 491 | 1 TSRMLS_CC); |
| 492 | goto cleanup; |
| 493 | } |
| 494 | ops[op_num].data.send_message = |
| 495 | string_to_byte_buffer(Z_STRVAL_PP(message_value), |
| 496 | Z_STRLEN_PP(message_value)); |
| 497 | break; |
| 498 | case GRPC_OP_SEND_CLOSE_FROM_CLIENT: |
| 499 | break; |
| 500 | case GRPC_OP_SEND_STATUS_FROM_SERVER: |
| 501 | status_hash = Z_ARRVAL_PP(value); |
| 502 | if (zend_hash_find(status_hash, "metadata", sizeof("metadata"), |
| 503 | (void **)&inner_value) == SUCCESS) { |
| 504 | if (!create_metadata_array(*inner_value, &trailing_metadata)) { |
| 505 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 506 | "Bad trailing metadata value given", |
| 507 | 1 TSRMLS_CC); |
| 508 | goto cleanup; |
| 509 | } |
| 510 | ops[op_num].data.send_status_from_server.trailing_metadata = |
| 511 | trailing_metadata.metadata; |
| 512 | ops[op_num].data.send_status_from_server.trailing_metadata_count = |
| 513 | trailing_metadata.count; |
| 514 | } |
| 515 | if (zend_hash_find(status_hash, "code", sizeof("code"), |
| 516 | (void**)&inner_value) == SUCCESS) { |
| 517 | if (Z_TYPE_PP(inner_value) != IS_LONG) { |
| 518 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 519 | "Status code must be an integer", |
| 520 | 1 TSRMLS_CC); |
| 521 | goto cleanup; |
| 522 | } |
| 523 | ops[op_num].data.send_status_from_server.status = |
| 524 | Z_LVAL_PP(inner_value); |
| 525 | } else { |
| 526 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 527 | "Integer status code is required", |
| 528 | 1 TSRMLS_CC); |
| 529 | goto cleanup; |
| 530 | } |
| 531 | if (zend_hash_find(status_hash, "details", sizeof("details"), |
| 532 | (void**)&inner_value) == SUCCESS) { |
| 533 | if (Z_TYPE_PP(inner_value) != IS_STRING) { |
| 534 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 535 | "Status details must be a string", |
| 536 | 1 TSRMLS_CC); |
| 537 | goto cleanup; |
| 538 | } |
| 539 | ops[op_num].data.send_status_from_server.status_details = |
| 540 | Z_STRVAL_PP(inner_value); |
| 541 | } else { |
| 542 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 543 | "String status details is required", |
| 544 | 1 TSRMLS_CC); |
| 545 | goto cleanup; |
| 546 | } |
| 547 | break; |
| 548 | case GRPC_OP_RECV_INITIAL_METADATA: |
| 549 | ops[op_num].data.recv_initial_metadata = &recv_metadata; |
| 550 | break; |
| 551 | case GRPC_OP_RECV_MESSAGE: |
| 552 | ops[op_num].data.recv_message = &message; |
| 553 | break; |
| 554 | case GRPC_OP_RECV_STATUS_ON_CLIENT: |
| 555 | ops[op_num].data.recv_status_on_client.trailing_metadata = |
| 556 | &recv_trailing_metadata; |
| 557 | ops[op_num].data.recv_status_on_client.status = &status; |
| 558 | ops[op_num].data.recv_status_on_client.status_details = |
| 559 | &status_details; |
| 560 | ops[op_num].data.recv_status_on_client.status_details_capacity = |
| 561 | &status_details_capacity; |
| 562 | break; |
| 563 | case GRPC_OP_RECV_CLOSE_ON_SERVER: |
| 564 | ops[op_num].data.recv_close_on_server.cancelled = &cancelled; |
| 565 | break; |
| 566 | default: |
| 567 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 568 | "Unrecognized key in batch", 1 TSRMLS_CC); |
| 569 | goto cleanup; |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 570 | } |
| 571 | ops[op_num].op = (grpc_op_type)index; |
David Garcia Quintas | ba710e5 | 2015-06-15 13:31:15 -0700 | [diff] [blame] | 572 | ops[op_num].flags = 0; |
Craig Tiller | 4275899 | 2015-08-18 10:34:32 -0700 | [diff] [blame] | 573 | ops[op_num].reserved = NULL; |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 574 | op_num++; |
| 575 | } |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 576 | |
| 577 | #else |
| 578 | |
| 579 | array_hash = HASH_OF(array); |
| 580 | ZEND_HASH_FOREACH_KEY_VAL(array_hash, index, key, value) { |
| 581 | if (key) { |
| 582 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 583 | "batch keys must be integers", 1); |
| 584 | goto cleanup; |
| 585 | } |
| 586 | |
| 587 | switch(index) { |
| 588 | case GRPC_OP_SEND_INITIAL_METADATA: |
| 589 | if (!create_metadata_array(value, &metadata)) { |
| 590 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 591 | "Bad metadata value given", 1); |
| 592 | goto cleanup; |
| 593 | } |
| 594 | ops[op_num].data.send_initial_metadata.count = metadata.count; |
| 595 | ops[op_num].data.send_initial_metadata.metadata = metadata.metadata; |
| 596 | break; |
| 597 | case GRPC_OP_SEND_MESSAGE: |
| 598 | if (Z_TYPE_P(value) != IS_ARRAY) { |
| 599 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 600 | "Expected an array for send message", 1); |
| 601 | goto cleanup; |
| 602 | } |
| 603 | message_hash = HASH_OF(value); |
| 604 | if ((message_flags = |
| 605 | zend_hash_str_find(message_hash, "flags", |
| 606 | sizeof("flags") - 1)) != NULL) { |
| 607 | if (Z_TYPE_P(message_flags) != IS_LONG) { |
| 608 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 609 | "Expected an int for message flags", 1); |
| 610 | } |
| 611 | ops[op_num].flags = Z_LVAL_P(message_flags) & GRPC_WRITE_USED_MASK; |
| 612 | } |
| 613 | if ((message_value = zend_hash_str_find(message_hash, "message", |
| 614 | sizeof("message") - 1)) |
| 615 | == NULL || Z_TYPE_P(message_value) != IS_STRING) { |
| 616 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 617 | "Expected a string for send message", 1); |
| 618 | goto cleanup; |
| 619 | } |
| 620 | ops[op_num].data.send_message = |
| 621 | string_to_byte_buffer(Z_STRVAL_P(message_value), |
| 622 | Z_STRLEN_P(message_value)); |
| 623 | break; |
| 624 | case GRPC_OP_SEND_CLOSE_FROM_CLIENT: |
| 625 | break; |
| 626 | case GRPC_OP_SEND_STATUS_FROM_SERVER: |
| 627 | status_hash = HASH_OF(value); |
| 628 | if ((inner_value = zend_hash_str_find(status_hash, "metadata", |
| 629 | sizeof("metadata") - 1)) |
| 630 | != NULL) { |
| 631 | if (!create_metadata_array(inner_value, &trailing_metadata)) { |
| 632 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 633 | "Bad trailing metadata value given", 1); |
| 634 | goto cleanup; |
| 635 | } |
| 636 | ops[op_num].data.send_status_from_server.trailing_metadata = |
| 637 | trailing_metadata.metadata; |
| 638 | ops[op_num].data.send_status_from_server.trailing_metadata_count = |
| 639 | trailing_metadata.count; |
| 640 | } |
| 641 | if ((inner_value = zend_hash_str_find(status_hash, "code", |
| 642 | sizeof("code") - 1)) != NULL) { |
| 643 | if (Z_TYPE_P(inner_value) != IS_LONG) { |
| 644 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 645 | "Status code must be an integer", 1); |
| 646 | goto cleanup; |
| 647 | } |
| 648 | ops[op_num].data.send_status_from_server.status = |
| 649 | Z_LVAL_P(inner_value); |
| 650 | } else { |
| 651 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 652 | "Integer status code is required", 1); |
| 653 | goto cleanup; |
| 654 | } |
| 655 | if ((inner_value = zend_hash_str_find(status_hash, "details", |
| 656 | sizeof("details") - 1)) != NULL) { |
| 657 | if (Z_TYPE_P(inner_value) != IS_STRING) { |
| 658 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 659 | "Status details must be a string", 1); |
| 660 | goto cleanup; |
| 661 | } |
| 662 | ops[op_num].data.send_status_from_server.status_details = |
| 663 | Z_STRVAL_P(inner_value); |
| 664 | } else { |
| 665 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 666 | "String status details is required", 1); |
| 667 | goto cleanup; |
| 668 | } |
| 669 | break; |
| 670 | case GRPC_OP_RECV_INITIAL_METADATA: |
| 671 | ops[op_num].data.recv_initial_metadata = &recv_metadata; |
| 672 | break; |
| 673 | case GRPC_OP_RECV_MESSAGE: |
| 674 | ops[op_num].data.recv_message = &message; |
| 675 | break; |
| 676 | case GRPC_OP_RECV_STATUS_ON_CLIENT: |
| 677 | ops[op_num].data.recv_status_on_client.trailing_metadata = |
| 678 | &recv_trailing_metadata; |
| 679 | ops[op_num].data.recv_status_on_client.status = &status; |
| 680 | ops[op_num].data.recv_status_on_client.status_details = |
| 681 | &status_details; |
| 682 | ops[op_num].data.recv_status_on_client.status_details_capacity = |
| 683 | &status_details_capacity; |
| 684 | break; |
| 685 | case GRPC_OP_RECV_CLOSE_ON_SERVER: |
| 686 | ops[op_num].data.recv_close_on_server.cancelled = &cancelled; |
| 687 | break; |
| 688 | default: |
| 689 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 690 | "Unrecognized key in batch", 1); |
| 691 | goto cleanup; |
| 692 | } |
| 693 | ops[op_num].op = (grpc_op_type)index; |
| 694 | ops[op_num].flags = 0; |
| 695 | ops[op_num].reserved = NULL; |
| 696 | op_num++; |
| 697 | } |
| 698 | ZEND_HASH_FOREACH_END(); |
| 699 | |
| 700 | #endif |
| 701 | |
Nicolas "Pixel" Noble | 150b7c4 | 2015-08-01 01:15:10 +0200 | [diff] [blame] | 702 | error = grpc_call_start_batch(call->wrapped, ops, op_num, call->wrapped, |
| 703 | NULL); |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 704 | if (error != GRPC_CALL_OK) { |
| 705 | zend_throw_exception(spl_ce_LogicException, |
| 706 | "start_batch was called incorrectly", |
| 707 | (long)error TSRMLS_CC); |
| 708 | goto cleanup; |
| 709 | } |
Stanley Cheung | c0c9ba9 | 2015-08-18 16:19:38 -0700 | [diff] [blame] | 710 | grpc_completion_queue_pluck(completion_queue, call->wrapped, |
| 711 | gpr_inf_future(GPR_CLOCK_REALTIME), NULL); |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 712 | #if PHP_MAJOR_VERSION < 7 |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 713 | for (int i = 0; i < op_num; i++) { |
| 714 | switch(ops[i].op) { |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 715 | case GRPC_OP_SEND_INITIAL_METADATA: |
| 716 | add_property_bool(result, "send_metadata", true); |
| 717 | break; |
| 718 | case GRPC_OP_SEND_MESSAGE: |
| 719 | add_property_bool(result, "send_message", true); |
| 720 | break; |
| 721 | case GRPC_OP_SEND_CLOSE_FROM_CLIENT: |
| 722 | add_property_bool(result, "send_close", true); |
| 723 | break; |
| 724 | case GRPC_OP_SEND_STATUS_FROM_SERVER: |
| 725 | add_property_bool(result, "send_status", true); |
| 726 | break; |
| 727 | case GRPC_OP_RECV_INITIAL_METADATA: |
| 728 | array = grpc_parse_metadata_array(&recv_metadata TSRMLS_CC); |
| 729 | add_property_zval(result, "metadata", array); |
| 730 | Z_DELREF_P(array); |
| 731 | break; |
| 732 | case GRPC_OP_RECV_MESSAGE: |
| 733 | byte_buffer_to_string(message, &message_str, &message_len); |
| 734 | if (message_str == NULL) { |
| 735 | add_property_null(result, "message"); |
| 736 | } else { |
| 737 | add_property_stringl(result, "message", message_str, message_len, |
| 738 | false); |
| 739 | } |
| 740 | break; |
| 741 | case GRPC_OP_RECV_STATUS_ON_CLIENT: |
| 742 | MAKE_STD_ZVAL(recv_status); |
| 743 | object_init(recv_status); |
| 744 | array = grpc_parse_metadata_array(&recv_trailing_metadata TSRMLS_CC); |
| 745 | add_property_zval(recv_status, "metadata", array); |
| 746 | Z_DELREF_P(array); |
| 747 | add_property_long(recv_status, "code", status); |
| 748 | add_property_string(recv_status, "details", status_details, true); |
| 749 | add_property_zval(result, "status", recv_status); |
| 750 | Z_DELREF_P(recv_status); |
| 751 | break; |
| 752 | case GRPC_OP_RECV_CLOSE_ON_SERVER: |
| 753 | add_property_bool(result, "cancelled", cancelled); |
| 754 | break; |
| 755 | default: |
| 756 | break; |
murgatroid99 | 5ca9f92 | 2015-02-03 11:21:11 -0800 | [diff] [blame] | 757 | } |
| 758 | } |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 759 | #else |
| 760 | for (int i = 0; i < op_num; i++) { |
| 761 | switch(ops[i].op) { |
| 762 | case GRPC_OP_SEND_INITIAL_METADATA: |
| 763 | add_property_bool(return_value, "send_metadata", true); |
| 764 | break; |
| 765 | case GRPC_OP_SEND_MESSAGE: |
| 766 | add_property_bool(return_value, "send_message", true); |
| 767 | break; |
| 768 | case GRPC_OP_SEND_CLOSE_FROM_CLIENT: |
| 769 | add_property_bool(return_value, "send_close", true); |
| 770 | break; |
| 771 | case GRPC_OP_SEND_STATUS_FROM_SERVER: |
| 772 | add_property_bool(return_value, "send_status", true); |
| 773 | break; |
| 774 | case GRPC_OP_RECV_INITIAL_METADATA: |
| 775 | grpc_parse_metadata_array(&recv_metadata, array); |
| 776 | add_property_zval(return_value, "metadata", array); |
| 777 | break; |
| 778 | case GRPC_OP_RECV_MESSAGE: |
| 779 | byte_buffer_to_string(message, &message_str, &message_len); |
| 780 | if (message_str == NULL) { |
| 781 | add_property_null(return_value, "message"); |
| 782 | } else { |
| 783 | add_property_stringl(return_value, "message", message_str, |
| 784 | message_len); |
| 785 | } |
| 786 | break; |
| 787 | case GRPC_OP_RECV_STATUS_ON_CLIENT: |
| 788 | object_init(&recv_status); |
| 789 | grpc_parse_metadata_array(&recv_trailing_metadata, array); |
| 790 | add_property_zval(&recv_status, "metadata", array); |
| 791 | add_property_long(&recv_status, "code", status); |
| 792 | add_property_string(&recv_status, "details", status_details); |
| 793 | add_property_zval(return_value, "status", &recv_status); |
| 794 | break; |
| 795 | case GRPC_OP_RECV_CLOSE_ON_SERVER: |
| 796 | add_property_bool(return_value, "cancelled", cancelled); |
| 797 | break; |
| 798 | default: |
| 799 | break; |
| 800 | } |
| 801 | } |
| 802 | #endif |
| 803 | |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 804 | cleanup: |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 805 | grpc_metadata_array_destroy(&metadata); |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 806 | grpc_metadata_array_destroy(&trailing_metadata); |
| 807 | grpc_metadata_array_destroy(&recv_metadata); |
| 808 | grpc_metadata_array_destroy(&recv_trailing_metadata); |
| 809 | if (status_details != NULL) { |
| 810 | gpr_free(status_details); |
mlumish | dba8789 | 2015-01-02 13:27:28 -0800 | [diff] [blame] | 811 | } |
Stanley Cheung | 82e6f32 | 2016-04-06 11:51:57 -0700 | [diff] [blame] | 812 | for (int i = 0; i < op_num; i++) { |
| 813 | if (ops[i].op == GRPC_OP_SEND_MESSAGE) { |
| 814 | grpc_byte_buffer_destroy(ops[i].data.send_message); |
| 815 | } |
| 816 | if (ops[i].op == GRPC_OP_RECV_MESSAGE) { |
| 817 | grpc_byte_buffer_destroy(message); |
| 818 | } |
| 819 | } |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 820 | #if PHP_MAJOR_VERSION < 7 |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 821 | RETURN_DESTROY_ZVAL(result); |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 822 | #else |
| 823 | RETURN_DESTROY_ZVAL(return_value); |
| 824 | #endif |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 825 | } |
| 826 | |
murgatroid99 | c1da8f2 | 2015-03-25 11:33:05 -0700 | [diff] [blame] | 827 | /** |
Stanley Cheung | db98e08 | 2015-07-27 10:19:45 -0700 | [diff] [blame] | 828 | * Get the endpoint this call/stream is connected to |
| 829 | * @return string The URI of the endpoint |
| 830 | */ |
| 831 | PHP_METHOD(Call, getPeer) { |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 832 | #if PHP_MAJOR_VERSION < 7 |
Stanley Cheung | db98e08 | 2015-07-27 10:19:45 -0700 | [diff] [blame] | 833 | wrapped_grpc_call *call = |
| 834 | (wrapped_grpc_call *)zend_object_store_get_object(getThis() TSRMLS_CC); |
| 835 | RETURN_STRING(grpc_call_get_peer(call->wrapped), 1); |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 836 | #else |
| 837 | wrapped_grpc_call *call = Z_WRAPPED_GRPC_CALL_P(getThis()); |
| 838 | RETURN_STRING(grpc_call_get_peer(call->wrapped)); |
| 839 | #endif |
Stanley Cheung | db98e08 | 2015-07-27 10:19:45 -0700 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | /** |
murgatroid99 | c1da8f2 | 2015-03-25 11:33:05 -0700 | [diff] [blame] | 843 | * Cancel the call. This will cause the call to end with STATUS_CANCELLED if it |
| 844 | * has not already ended with another status. |
| 845 | */ |
| 846 | PHP_METHOD(Call, cancel) { |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 847 | #if PHP_MAJOR_VERSION < 7 |
murgatroid99 | c1da8f2 | 2015-03-25 11:33:05 -0700 | [diff] [blame] | 848 | wrapped_grpc_call *call = |
| 849 | (wrapped_grpc_call *)zend_object_store_get_object(getThis() TSRMLS_CC); |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 850 | #else |
| 851 | wrapped_grpc_call *call = Z_WRAPPED_GRPC_CALL_P(getThis()); |
| 852 | #endif |
Nicolas "Pixel" Noble | 150b7c4 | 2015-08-01 01:15:10 +0200 | [diff] [blame] | 853 | grpc_call_cancel(call->wrapped, NULL); |
murgatroid99 | c1da8f2 | 2015-03-25 11:33:05 -0700 | [diff] [blame] | 854 | } |
| 855 | |
Stanley Cheung | 3580580 | 2015-12-10 11:42:55 -0800 | [diff] [blame] | 856 | /** |
| 857 | * Set the CallCredentials for this call. |
| 858 | * @param CallCredentials creds_obj The CallCredentials object |
| 859 | * @param int The error code |
| 860 | */ |
| 861 | PHP_METHOD(Call, setCredentials) { |
| 862 | zval *creds_obj; |
| 863 | |
| 864 | /* "O" == 1 Object */ |
| 865 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &creds_obj, |
| 866 | grpc_ce_call_credentials) == FAILURE) { |
| 867 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 868 | "setCredentials expects 1 CallCredentials", |
| 869 | 1 TSRMLS_CC); |
| 870 | return; |
| 871 | } |
| 872 | |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 873 | wrapped_grpc_call_credentials *creds = |
| 874 | Z_WRAPPED_GRPC_CALL_CREDS_P(creds_obj); |
| 875 | wrapped_grpc_call *call = Z_WRAPPED_GRPC_CALL_P(getThis()); |
Stanley Cheung | 3580580 | 2015-12-10 11:42:55 -0800 | [diff] [blame] | 876 | |
| 877 | grpc_call_error error = GRPC_CALL_ERROR; |
| 878 | error = grpc_call_set_credentials(call->wrapped, creds->wrapped); |
| 879 | RETURN_LONG(error); |
| 880 | } |
| 881 | |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 882 | static zend_function_entry call_methods[] = { |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 883 | PHP_ME(Call, __construct, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR) |
| 884 | PHP_ME(Call, startBatch, NULL, ZEND_ACC_PUBLIC) |
| 885 | PHP_ME(Call, getPeer, NULL, ZEND_ACC_PUBLIC) |
| 886 | PHP_ME(Call, cancel, NULL, ZEND_ACC_PUBLIC) |
| 887 | PHP_ME(Call, setCredentials, NULL, ZEND_ACC_PUBLIC) |
| 888 | PHP_FE_END |
| 889 | }; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 890 | |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 891 | void grpc_init_call(TSRMLS_D) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 892 | zend_class_entry ce; |
| 893 | INIT_CLASS_ENTRY(ce, "Grpc\\Call", call_methods); |
| 894 | ce.create_object = create_wrapped_grpc_call; |
| 895 | grpc_ce_call = zend_register_internal_class(&ce TSRMLS_CC); |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 896 | #if PHP_MAJOR_VERSION >= 7 |
| 897 | memcpy(&call_ce_handlers, zend_get_std_object_handlers(), |
| 898 | sizeof(zend_object_handlers)); |
| 899 | call_ce_handlers.offset = XtOffsetOf(wrapped_grpc_call, std); |
| 900 | call_ce_handlers.free_obj = free_wrapped_grpc_call; |
| 901 | #endif |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 902 | } |