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 |
murgatroid99 | 5ca9f92 | 2015-02-03 11:21:11 -0800 | [diff] [blame] | 383 | zval **value; |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 384 | zval **inner_value; |
murgatroid99 | 5ca9f92 | 2015-02-03 11:21:11 -0800 | [diff] [blame] | 385 | HashPosition array_pointer; |
Stanley Cheung | 3ab8e79 | 2015-08-24 16:58:42 -0700 | [diff] [blame] | 386 | zval **message_value; |
| 387 | zval **message_flags; |
murgatroid99 | 5ca9f92 | 2015-02-03 11:21:11 -0800 | [diff] [blame] | 388 | char *key; |
| 389 | uint key_len; |
| 390 | ulong index; |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 391 | zval *result; |
| 392 | zval *recv_status; |
| 393 | MAKE_STD_ZVAL(result); |
| 394 | object_init(result); |
| 395 | #else |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 396 | zval *value; |
| 397 | zval *inner_value; |
| 398 | zval *message_value; |
| 399 | zval *message_flags; |
| 400 | zend_string *key; |
| 401 | zend_ulong index; |
| 402 | zval recv_status; |
| 403 | object_init(return_value); |
| 404 | #endif |
thinkerou | b9c7f3a | 2016-07-22 09:22:41 +0800 | [diff] [blame^] | 405 | wrapped_grpc_call *call = Z_WRAPPED_GRPC_CALL_P(getThis()); |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 406 | |
| 407 | grpc_op ops[8]; |
| 408 | size_t op_num = 0; |
| 409 | zval *array; |
| 410 | HashTable *array_hash; |
| 411 | HashTable *status_hash; |
| 412 | HashTable *message_hash; |
| 413 | |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 414 | grpc_metadata_array metadata; |
| 415 | grpc_metadata_array trailing_metadata; |
| 416 | grpc_metadata_array recv_metadata; |
| 417 | grpc_metadata_array recv_trailing_metadata; |
| 418 | grpc_status_code status; |
| 419 | char *status_details = NULL; |
murgatroid99 | 9fe516a | 2015-03-11 14:47:10 -0700 | [diff] [blame] | 420 | size_t status_details_capacity = 0; |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 421 | grpc_byte_buffer *message; |
| 422 | int cancelled; |
| 423 | grpc_call_error error; |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 424 | char *message_str; |
| 425 | size_t message_len; |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 426 | |
| 427 | |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 428 | grpc_metadata_array_init(&metadata); |
| 429 | grpc_metadata_array_init(&trailing_metadata); |
murgatroid99 | d8bb957 | 2015-03-11 09:18:06 -0700 | [diff] [blame] | 430 | grpc_metadata_array_init(&recv_metadata); |
| 431 | grpc_metadata_array_init(&recv_trailing_metadata); |
David Garcia Quintas | a301eaa | 2016-05-06 16:59:03 -0700 | [diff] [blame] | 432 | memset(ops, 0, sizeof(ops)); |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 433 | |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 434 | /* "a" == 1 array */ |
| 435 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &array) == |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 436 | FAILURE) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 437 | zend_throw_exception(spl_ce_InvalidArgumentException, |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 438 | "start_batch expects an array", 1 TSRMLS_CC); |
| 439 | goto cleanup; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 440 | } |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 441 | |
| 442 | #if PHP_MAJOR_VERSION < 7 |
| 443 | |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 444 | array_hash = Z_ARRVAL_P(array); |
murgatroid99 | 5ca9f92 | 2015-02-03 11:21:11 -0800 | [diff] [blame] | 445 | for (zend_hash_internal_pointer_reset_ex(array_hash, &array_pointer); |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 446 | zend_hash_get_current_data_ex(array_hash, (void**)&value, |
murgatroid99 | 5ca9f92 | 2015-02-03 11:21:11 -0800 | [diff] [blame] | 447 | &array_pointer) == SUCCESS; |
| 448 | zend_hash_move_forward_ex(array_hash, &array_pointer)) { |
| 449 | 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] | 450 | &array_pointer) != HASH_KEY_IS_LONG) { |
murgatroid99 | 5ca9f92 | 2015-02-03 11:21:11 -0800 | [diff] [blame] | 451 | zend_throw_exception(spl_ce_InvalidArgumentException, |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 452 | "batch keys must be integers", 1 TSRMLS_CC); |
| 453 | goto cleanup; |
murgatroid99 | 5ca9f92 | 2015-02-03 11:21:11 -0800 | [diff] [blame] | 454 | } |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 455 | switch(index) { |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 456 | case GRPC_OP_SEND_INITIAL_METADATA: |
| 457 | if (!create_metadata_array(*value, &metadata)) { |
murgatroid99 | 5ca9f92 | 2015-02-03 11:21:11 -0800 | [diff] [blame] | 458 | zend_throw_exception(spl_ce_InvalidArgumentException, |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 459 | "Bad metadata value given", 1 TSRMLS_CC); |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 460 | goto cleanup; |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 461 | } |
| 462 | ops[op_num].data.send_initial_metadata.count = |
| 463 | metadata.count; |
| 464 | ops[op_num].data.send_initial_metadata.metadata = |
| 465 | metadata.metadata; |
| 466 | break; |
| 467 | case GRPC_OP_SEND_MESSAGE: |
| 468 | if (Z_TYPE_PP(value) != IS_ARRAY) { |
| 469 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 470 | "Expected an array for send message", |
| 471 | 1 TSRMLS_CC); |
| 472 | goto cleanup; |
| 473 | } |
| 474 | message_hash = Z_ARRVAL_PP(value); |
| 475 | if (zend_hash_find(message_hash, "flags", sizeof("flags"), |
| 476 | (void **)&message_flags) == SUCCESS) { |
| 477 | if (Z_TYPE_PP(message_flags) != IS_LONG) { |
| 478 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 479 | "Expected an int for message flags", |
| 480 | 1 TSRMLS_CC); |
| 481 | } |
| 482 | ops[op_num].flags = Z_LVAL_PP(message_flags) & GRPC_WRITE_USED_MASK; |
| 483 | } |
| 484 | if (zend_hash_find(message_hash, "message", sizeof("message"), |
| 485 | (void **)&message_value) != SUCCESS || |
| 486 | Z_TYPE_PP(message_value) != IS_STRING) { |
| 487 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 488 | "Expected a string for send message", |
| 489 | 1 TSRMLS_CC); |
| 490 | goto cleanup; |
| 491 | } |
| 492 | ops[op_num].data.send_message = |
| 493 | string_to_byte_buffer(Z_STRVAL_PP(message_value), |
| 494 | Z_STRLEN_PP(message_value)); |
| 495 | break; |
| 496 | case GRPC_OP_SEND_CLOSE_FROM_CLIENT: |
| 497 | break; |
| 498 | case GRPC_OP_SEND_STATUS_FROM_SERVER: |
| 499 | status_hash = Z_ARRVAL_PP(value); |
| 500 | if (zend_hash_find(status_hash, "metadata", sizeof("metadata"), |
| 501 | (void **)&inner_value) == SUCCESS) { |
| 502 | if (!create_metadata_array(*inner_value, &trailing_metadata)) { |
| 503 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 504 | "Bad trailing metadata value given", |
| 505 | 1 TSRMLS_CC); |
| 506 | goto cleanup; |
| 507 | } |
| 508 | ops[op_num].data.send_status_from_server.trailing_metadata = |
| 509 | trailing_metadata.metadata; |
| 510 | ops[op_num].data.send_status_from_server.trailing_metadata_count = |
| 511 | trailing_metadata.count; |
| 512 | } |
| 513 | if (zend_hash_find(status_hash, "code", sizeof("code"), |
| 514 | (void**)&inner_value) == SUCCESS) { |
| 515 | if (Z_TYPE_PP(inner_value) != IS_LONG) { |
| 516 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 517 | "Status code must be an integer", |
| 518 | 1 TSRMLS_CC); |
| 519 | goto cleanup; |
| 520 | } |
| 521 | ops[op_num].data.send_status_from_server.status = |
| 522 | Z_LVAL_PP(inner_value); |
| 523 | } else { |
| 524 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 525 | "Integer status code is required", |
| 526 | 1 TSRMLS_CC); |
| 527 | goto cleanup; |
| 528 | } |
| 529 | if (zend_hash_find(status_hash, "details", sizeof("details"), |
| 530 | (void**)&inner_value) == SUCCESS) { |
| 531 | if (Z_TYPE_PP(inner_value) != IS_STRING) { |
| 532 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 533 | "Status details must be a string", |
| 534 | 1 TSRMLS_CC); |
| 535 | goto cleanup; |
| 536 | } |
| 537 | ops[op_num].data.send_status_from_server.status_details = |
| 538 | Z_STRVAL_PP(inner_value); |
| 539 | } else { |
| 540 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 541 | "String status details is required", |
| 542 | 1 TSRMLS_CC); |
| 543 | goto cleanup; |
| 544 | } |
| 545 | break; |
| 546 | case GRPC_OP_RECV_INITIAL_METADATA: |
| 547 | ops[op_num].data.recv_initial_metadata = &recv_metadata; |
| 548 | break; |
| 549 | case GRPC_OP_RECV_MESSAGE: |
| 550 | ops[op_num].data.recv_message = &message; |
| 551 | break; |
| 552 | case GRPC_OP_RECV_STATUS_ON_CLIENT: |
| 553 | ops[op_num].data.recv_status_on_client.trailing_metadata = |
| 554 | &recv_trailing_metadata; |
| 555 | ops[op_num].data.recv_status_on_client.status = &status; |
| 556 | ops[op_num].data.recv_status_on_client.status_details = |
| 557 | &status_details; |
| 558 | ops[op_num].data.recv_status_on_client.status_details_capacity = |
| 559 | &status_details_capacity; |
| 560 | break; |
| 561 | case GRPC_OP_RECV_CLOSE_ON_SERVER: |
| 562 | ops[op_num].data.recv_close_on_server.cancelled = &cancelled; |
| 563 | break; |
| 564 | default: |
| 565 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 566 | "Unrecognized key in batch", 1 TSRMLS_CC); |
| 567 | goto cleanup; |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 568 | } |
| 569 | ops[op_num].op = (grpc_op_type)index; |
David Garcia Quintas | ba710e5 | 2015-06-15 13:31:15 -0700 | [diff] [blame] | 570 | ops[op_num].flags = 0; |
Craig Tiller | 4275899 | 2015-08-18 10:34:32 -0700 | [diff] [blame] | 571 | ops[op_num].reserved = NULL; |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 572 | op_num++; |
| 573 | } |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 574 | |
| 575 | #else |
| 576 | |
| 577 | array_hash = HASH_OF(array); |
| 578 | ZEND_HASH_FOREACH_KEY_VAL(array_hash, index, key, value) { |
| 579 | if (key) { |
| 580 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 581 | "batch keys must be integers", 1); |
| 582 | goto cleanup; |
| 583 | } |
| 584 | |
| 585 | switch(index) { |
| 586 | case GRPC_OP_SEND_INITIAL_METADATA: |
| 587 | if (!create_metadata_array(value, &metadata)) { |
| 588 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 589 | "Bad metadata value given", 1); |
| 590 | goto cleanup; |
| 591 | } |
| 592 | ops[op_num].data.send_initial_metadata.count = metadata.count; |
| 593 | ops[op_num].data.send_initial_metadata.metadata = metadata.metadata; |
| 594 | break; |
| 595 | case GRPC_OP_SEND_MESSAGE: |
| 596 | if (Z_TYPE_P(value) != IS_ARRAY) { |
| 597 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 598 | "Expected an array for send message", 1); |
| 599 | goto cleanup; |
| 600 | } |
| 601 | message_hash = HASH_OF(value); |
| 602 | if ((message_flags = |
| 603 | zend_hash_str_find(message_hash, "flags", |
| 604 | sizeof("flags") - 1)) != NULL) { |
| 605 | if (Z_TYPE_P(message_flags) != IS_LONG) { |
| 606 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 607 | "Expected an int for message flags", 1); |
| 608 | } |
| 609 | ops[op_num].flags = Z_LVAL_P(message_flags) & GRPC_WRITE_USED_MASK; |
| 610 | } |
| 611 | if ((message_value = zend_hash_str_find(message_hash, "message", |
| 612 | sizeof("message") - 1)) |
| 613 | == NULL || Z_TYPE_P(message_value) != IS_STRING) { |
| 614 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 615 | "Expected a string for send message", 1); |
| 616 | goto cleanup; |
| 617 | } |
| 618 | ops[op_num].data.send_message = |
| 619 | string_to_byte_buffer(Z_STRVAL_P(message_value), |
| 620 | Z_STRLEN_P(message_value)); |
| 621 | break; |
| 622 | case GRPC_OP_SEND_CLOSE_FROM_CLIENT: |
| 623 | break; |
| 624 | case GRPC_OP_SEND_STATUS_FROM_SERVER: |
| 625 | status_hash = HASH_OF(value); |
| 626 | if ((inner_value = zend_hash_str_find(status_hash, "metadata", |
| 627 | sizeof("metadata") - 1)) |
| 628 | != NULL) { |
| 629 | if (!create_metadata_array(inner_value, &trailing_metadata)) { |
| 630 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 631 | "Bad trailing metadata value given", 1); |
| 632 | goto cleanup; |
| 633 | } |
| 634 | ops[op_num].data.send_status_from_server.trailing_metadata = |
| 635 | trailing_metadata.metadata; |
| 636 | ops[op_num].data.send_status_from_server.trailing_metadata_count = |
| 637 | trailing_metadata.count; |
| 638 | } |
| 639 | if ((inner_value = zend_hash_str_find(status_hash, "code", |
| 640 | sizeof("code") - 1)) != NULL) { |
| 641 | if (Z_TYPE_P(inner_value) != IS_LONG) { |
| 642 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 643 | "Status code must be an integer", 1); |
| 644 | goto cleanup; |
| 645 | } |
| 646 | ops[op_num].data.send_status_from_server.status = |
| 647 | Z_LVAL_P(inner_value); |
| 648 | } else { |
| 649 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 650 | "Integer status code is required", 1); |
| 651 | goto cleanup; |
| 652 | } |
| 653 | if ((inner_value = zend_hash_str_find(status_hash, "details", |
| 654 | sizeof("details") - 1)) != NULL) { |
| 655 | if (Z_TYPE_P(inner_value) != IS_STRING) { |
| 656 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 657 | "Status details must be a string", 1); |
| 658 | goto cleanup; |
| 659 | } |
| 660 | ops[op_num].data.send_status_from_server.status_details = |
| 661 | Z_STRVAL_P(inner_value); |
| 662 | } else { |
| 663 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 664 | "String status details is required", 1); |
| 665 | goto cleanup; |
| 666 | } |
| 667 | break; |
| 668 | case GRPC_OP_RECV_INITIAL_METADATA: |
| 669 | ops[op_num].data.recv_initial_metadata = &recv_metadata; |
| 670 | break; |
| 671 | case GRPC_OP_RECV_MESSAGE: |
| 672 | ops[op_num].data.recv_message = &message; |
| 673 | break; |
| 674 | case GRPC_OP_RECV_STATUS_ON_CLIENT: |
| 675 | ops[op_num].data.recv_status_on_client.trailing_metadata = |
| 676 | &recv_trailing_metadata; |
| 677 | ops[op_num].data.recv_status_on_client.status = &status; |
| 678 | ops[op_num].data.recv_status_on_client.status_details = |
| 679 | &status_details; |
| 680 | ops[op_num].data.recv_status_on_client.status_details_capacity = |
| 681 | &status_details_capacity; |
| 682 | break; |
| 683 | case GRPC_OP_RECV_CLOSE_ON_SERVER: |
| 684 | ops[op_num].data.recv_close_on_server.cancelled = &cancelled; |
| 685 | break; |
| 686 | default: |
| 687 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 688 | "Unrecognized key in batch", 1); |
| 689 | goto cleanup; |
| 690 | } |
| 691 | ops[op_num].op = (grpc_op_type)index; |
| 692 | ops[op_num].flags = 0; |
| 693 | ops[op_num].reserved = NULL; |
| 694 | op_num++; |
| 695 | } |
| 696 | ZEND_HASH_FOREACH_END(); |
| 697 | |
| 698 | #endif |
| 699 | |
Nicolas "Pixel" Noble | 150b7c4 | 2015-08-01 01:15:10 +0200 | [diff] [blame] | 700 | error = grpc_call_start_batch(call->wrapped, ops, op_num, call->wrapped, |
| 701 | NULL); |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 702 | if (error != GRPC_CALL_OK) { |
| 703 | zend_throw_exception(spl_ce_LogicException, |
| 704 | "start_batch was called incorrectly", |
| 705 | (long)error TSRMLS_CC); |
| 706 | goto cleanup; |
| 707 | } |
Stanley Cheung | c0c9ba9 | 2015-08-18 16:19:38 -0700 | [diff] [blame] | 708 | grpc_completion_queue_pluck(completion_queue, call->wrapped, |
| 709 | gpr_inf_future(GPR_CLOCK_REALTIME), NULL); |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 710 | #if PHP_MAJOR_VERSION < 7 |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 711 | for (int i = 0; i < op_num; i++) { |
| 712 | switch(ops[i].op) { |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 713 | case GRPC_OP_SEND_INITIAL_METADATA: |
| 714 | add_property_bool(result, "send_metadata", true); |
| 715 | break; |
| 716 | case GRPC_OP_SEND_MESSAGE: |
| 717 | add_property_bool(result, "send_message", true); |
| 718 | break; |
| 719 | case GRPC_OP_SEND_CLOSE_FROM_CLIENT: |
| 720 | add_property_bool(result, "send_close", true); |
| 721 | break; |
| 722 | case GRPC_OP_SEND_STATUS_FROM_SERVER: |
| 723 | add_property_bool(result, "send_status", true); |
| 724 | break; |
| 725 | case GRPC_OP_RECV_INITIAL_METADATA: |
| 726 | array = grpc_parse_metadata_array(&recv_metadata TSRMLS_CC); |
| 727 | add_property_zval(result, "metadata", array); |
| 728 | Z_DELREF_P(array); |
| 729 | break; |
| 730 | case GRPC_OP_RECV_MESSAGE: |
| 731 | byte_buffer_to_string(message, &message_str, &message_len); |
| 732 | if (message_str == NULL) { |
| 733 | add_property_null(result, "message"); |
| 734 | } else { |
| 735 | add_property_stringl(result, "message", message_str, message_len, |
| 736 | false); |
| 737 | } |
| 738 | break; |
| 739 | case GRPC_OP_RECV_STATUS_ON_CLIENT: |
| 740 | MAKE_STD_ZVAL(recv_status); |
| 741 | object_init(recv_status); |
| 742 | array = grpc_parse_metadata_array(&recv_trailing_metadata TSRMLS_CC); |
| 743 | add_property_zval(recv_status, "metadata", array); |
| 744 | Z_DELREF_P(array); |
| 745 | add_property_long(recv_status, "code", status); |
| 746 | add_property_string(recv_status, "details", status_details, true); |
| 747 | add_property_zval(result, "status", recv_status); |
| 748 | Z_DELREF_P(recv_status); |
| 749 | break; |
| 750 | case GRPC_OP_RECV_CLOSE_ON_SERVER: |
| 751 | add_property_bool(result, "cancelled", cancelled); |
| 752 | break; |
| 753 | default: |
| 754 | break; |
murgatroid99 | 5ca9f92 | 2015-02-03 11:21:11 -0800 | [diff] [blame] | 755 | } |
| 756 | } |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 757 | #else |
| 758 | for (int i = 0; i < op_num; i++) { |
| 759 | switch(ops[i].op) { |
| 760 | case GRPC_OP_SEND_INITIAL_METADATA: |
| 761 | add_property_bool(return_value, "send_metadata", true); |
| 762 | break; |
| 763 | case GRPC_OP_SEND_MESSAGE: |
| 764 | add_property_bool(return_value, "send_message", true); |
| 765 | break; |
| 766 | case GRPC_OP_SEND_CLOSE_FROM_CLIENT: |
| 767 | add_property_bool(return_value, "send_close", true); |
| 768 | break; |
| 769 | case GRPC_OP_SEND_STATUS_FROM_SERVER: |
| 770 | add_property_bool(return_value, "send_status", true); |
| 771 | break; |
| 772 | case GRPC_OP_RECV_INITIAL_METADATA: |
| 773 | grpc_parse_metadata_array(&recv_metadata, array); |
| 774 | add_property_zval(return_value, "metadata", array); |
| 775 | break; |
| 776 | case GRPC_OP_RECV_MESSAGE: |
| 777 | byte_buffer_to_string(message, &message_str, &message_len); |
| 778 | if (message_str == NULL) { |
| 779 | add_property_null(return_value, "message"); |
| 780 | } else { |
| 781 | add_property_stringl(return_value, "message", message_str, |
| 782 | message_len); |
| 783 | } |
| 784 | break; |
| 785 | case GRPC_OP_RECV_STATUS_ON_CLIENT: |
| 786 | object_init(&recv_status); |
| 787 | grpc_parse_metadata_array(&recv_trailing_metadata, array); |
| 788 | add_property_zval(&recv_status, "metadata", array); |
| 789 | add_property_long(&recv_status, "code", status); |
| 790 | add_property_string(&recv_status, "details", status_details); |
| 791 | add_property_zval(return_value, "status", &recv_status); |
| 792 | break; |
| 793 | case GRPC_OP_RECV_CLOSE_ON_SERVER: |
| 794 | add_property_bool(return_value, "cancelled", cancelled); |
| 795 | break; |
| 796 | default: |
| 797 | break; |
| 798 | } |
| 799 | } |
| 800 | #endif |
| 801 | |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 802 | cleanup: |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 803 | grpc_metadata_array_destroy(&metadata); |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 804 | grpc_metadata_array_destroy(&trailing_metadata); |
| 805 | grpc_metadata_array_destroy(&recv_metadata); |
| 806 | grpc_metadata_array_destroy(&recv_trailing_metadata); |
| 807 | if (status_details != NULL) { |
| 808 | gpr_free(status_details); |
mlumish | dba8789 | 2015-01-02 13:27:28 -0800 | [diff] [blame] | 809 | } |
Stanley Cheung | 82e6f32 | 2016-04-06 11:51:57 -0700 | [diff] [blame] | 810 | for (int i = 0; i < op_num; i++) { |
| 811 | if (ops[i].op == GRPC_OP_SEND_MESSAGE) { |
| 812 | grpc_byte_buffer_destroy(ops[i].data.send_message); |
| 813 | } |
| 814 | if (ops[i].op == GRPC_OP_RECV_MESSAGE) { |
| 815 | grpc_byte_buffer_destroy(message); |
| 816 | } |
| 817 | } |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 818 | #if PHP_MAJOR_VERSION < 7 |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 819 | RETURN_DESTROY_ZVAL(result); |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 820 | #else |
| 821 | RETURN_DESTROY_ZVAL(return_value); |
| 822 | #endif |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 823 | } |
| 824 | |
murgatroid99 | c1da8f2 | 2015-03-25 11:33:05 -0700 | [diff] [blame] | 825 | /** |
Stanley Cheung | db98e08 | 2015-07-27 10:19:45 -0700 | [diff] [blame] | 826 | * Get the endpoint this call/stream is connected to |
| 827 | * @return string The URI of the endpoint |
| 828 | */ |
| 829 | PHP_METHOD(Call, getPeer) { |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 830 | wrapped_grpc_call *call = Z_WRAPPED_GRPC_CALL_P(getThis()); |
thinkerou | b9c7f3a | 2016-07-22 09:22:41 +0800 | [diff] [blame^] | 831 | PHP_GRPC_RETURN_STRING(grpc_call_get_peer(call->wrapped), 1); |
Stanley Cheung | db98e08 | 2015-07-27 10:19:45 -0700 | [diff] [blame] | 832 | } |
| 833 | |
| 834 | /** |
murgatroid99 | c1da8f2 | 2015-03-25 11:33:05 -0700 | [diff] [blame] | 835 | * Cancel the call. This will cause the call to end with STATUS_CANCELLED if it |
| 836 | * has not already ended with another status. |
| 837 | */ |
| 838 | PHP_METHOD(Call, cancel) { |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 839 | wrapped_grpc_call *call = Z_WRAPPED_GRPC_CALL_P(getThis()); |
Nicolas "Pixel" Noble | 150b7c4 | 2015-08-01 01:15:10 +0200 | [diff] [blame] | 840 | grpc_call_cancel(call->wrapped, NULL); |
murgatroid99 | c1da8f2 | 2015-03-25 11:33:05 -0700 | [diff] [blame] | 841 | } |
| 842 | |
Stanley Cheung | 3580580 | 2015-12-10 11:42:55 -0800 | [diff] [blame] | 843 | /** |
| 844 | * Set the CallCredentials for this call. |
| 845 | * @param CallCredentials creds_obj The CallCredentials object |
| 846 | * @param int The error code |
| 847 | */ |
| 848 | PHP_METHOD(Call, setCredentials) { |
| 849 | zval *creds_obj; |
| 850 | |
| 851 | /* "O" == 1 Object */ |
| 852 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &creds_obj, |
| 853 | grpc_ce_call_credentials) == FAILURE) { |
| 854 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 855 | "setCredentials expects 1 CallCredentials", |
| 856 | 1 TSRMLS_CC); |
| 857 | return; |
| 858 | } |
| 859 | |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 860 | wrapped_grpc_call_credentials *creds = |
| 861 | Z_WRAPPED_GRPC_CALL_CREDS_P(creds_obj); |
| 862 | wrapped_grpc_call *call = Z_WRAPPED_GRPC_CALL_P(getThis()); |
Stanley Cheung | 3580580 | 2015-12-10 11:42:55 -0800 | [diff] [blame] | 863 | |
| 864 | grpc_call_error error = GRPC_CALL_ERROR; |
| 865 | error = grpc_call_set_credentials(call->wrapped, creds->wrapped); |
| 866 | RETURN_LONG(error); |
| 867 | } |
| 868 | |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 869 | static zend_function_entry call_methods[] = { |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 870 | PHP_ME(Call, __construct, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR) |
| 871 | PHP_ME(Call, startBatch, NULL, ZEND_ACC_PUBLIC) |
| 872 | PHP_ME(Call, getPeer, NULL, ZEND_ACC_PUBLIC) |
| 873 | PHP_ME(Call, cancel, NULL, ZEND_ACC_PUBLIC) |
| 874 | PHP_ME(Call, setCredentials, NULL, ZEND_ACC_PUBLIC) |
| 875 | PHP_FE_END |
| 876 | }; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 877 | |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 878 | void grpc_init_call(TSRMLS_D) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 879 | zend_class_entry ce; |
| 880 | INIT_CLASS_ENTRY(ce, "Grpc\\Call", call_methods); |
| 881 | ce.create_object = create_wrapped_grpc_call; |
| 882 | grpc_ce_call = zend_register_internal_class(&ce TSRMLS_CC); |
thinkerou | a3730b7 | 2016-07-20 16:59:54 +0800 | [diff] [blame] | 883 | #if PHP_MAJOR_VERSION >= 7 |
| 884 | memcpy(&call_ce_handlers, zend_get_std_object_handlers(), |
| 885 | sizeof(zend_object_handlers)); |
| 886 | call_ce_handlers.offset = XtOffsetOf(wrapped_grpc_call, std); |
| 887 | call_ce_handlers.free_obj = free_wrapped_grpc_call; |
| 888 | #endif |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 889 | } |