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 | |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 62 | /* Frees and destroys an instance of wrapped_grpc_call */ |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 63 | void free_wrapped_grpc_call(void *object TSRMLS_DC) { |
| 64 | wrapped_grpc_call *call = (wrapped_grpc_call *)object; |
| 65 | if (call->owned && call->wrapped != NULL) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 66 | grpc_call_destroy(call->wrapped); |
| 67 | } |
| 68 | efree(call); |
| 69 | } |
| 70 | |
| 71 | /* Initializes an instance of wrapped_grpc_call to be associated with an object |
| 72 | * of a class specified by class_type */ |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 73 | zend_object_value create_wrapped_grpc_call(zend_class_entry *class_type |
| 74 | TSRMLS_DC) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 75 | zend_object_value retval; |
| 76 | wrapped_grpc_call *intern; |
| 77 | |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 78 | intern = (wrapped_grpc_call *)emalloc(sizeof(wrapped_grpc_call)); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 79 | memset(intern, 0, sizeof(wrapped_grpc_call)); |
| 80 | |
| 81 | zend_object_std_init(&intern->std, class_type TSRMLS_CC); |
| 82 | object_properties_init(&intern->std, class_type); |
| 83 | retval.handle = zend_objects_store_put( |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 84 | intern, (zend_objects_store_dtor_t)zend_objects_destroy_object, |
| 85 | free_wrapped_grpc_call, NULL TSRMLS_CC); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 86 | retval.handlers = zend_get_std_object_handlers(); |
| 87 | return retval; |
| 88 | } |
| 89 | |
mlumish | 34cd1f0 | 2015-01-02 13:32:41 -0800 | [diff] [blame] | 90 | /* Wraps a grpc_call struct in a PHP object. Owned indicates whether the struct |
| 91 | should be destroyed at the end of the object's lifecycle */ |
murgatroid99 | 268acd5 | 2015-05-14 15:05:00 -0700 | [diff] [blame] | 92 | zval *grpc_php_wrap_call(grpc_call *wrapped, bool owned) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 93 | zval *call_object; |
| 94 | MAKE_STD_ZVAL(call_object); |
| 95 | object_init_ex(call_object, grpc_ce_call); |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 96 | wrapped_grpc_call *call = |
| 97 | (wrapped_grpc_call *)zend_object_store_get_object(call_object TSRMLS_CC); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 98 | call->wrapped = wrapped; |
thinkerou | d04376d | 2016-04-27 19:58:49 +0800 | [diff] [blame^] | 99 | call->owned = owned; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 100 | return call_object; |
| 101 | } |
| 102 | |
murgatroid99 | 9c4425a | 2015-03-24 09:43:41 -0700 | [diff] [blame] | 103 | /* Creates and returns a PHP array object with the data in a |
| 104 | * grpc_metadata_array. Returns NULL on failure */ |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 105 | zval *grpc_parse_metadata_array(grpc_metadata_array *metadata_array) { |
| 106 | int count = metadata_array->count; |
| 107 | grpc_metadata *elements = metadata_array->metadata; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 108 | int i; |
| 109 | zval *array; |
| 110 | zval **data = NULL; |
| 111 | HashTable *array_hash; |
| 112 | zval *inner_array; |
| 113 | char *str_key; |
| 114 | char *str_val; |
| 115 | size_t key_len; |
| 116 | MAKE_STD_ZVAL(array); |
| 117 | array_init(array); |
| 118 | array_hash = Z_ARRVAL_P(array); |
| 119 | grpc_metadata *elem; |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 120 | for (i = 0; i < count; i++) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 121 | elem = &elements[i]; |
| 122 | key_len = strlen(elem->key); |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 123 | str_key = ecalloc(key_len + 1, sizeof(char)); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 124 | memcpy(str_key, elem->key, key_len); |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 125 | str_val = ecalloc(elem->value_length + 1, sizeof(char)); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 126 | memcpy(str_val, elem->value, elem->value_length); |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 127 | if (zend_hash_find(array_hash, str_key, key_len, (void **)data) == |
| 128 | SUCCESS) { |
Stanley Cheung | b91f0f2 | 2016-02-16 09:36:36 -0800 | [diff] [blame] | 129 | if (Z_TYPE_P(*data) != IS_ARRAY) { |
| 130 | zend_throw_exception(zend_exception_get_default(), |
| 131 | "Metadata hash somehow contains wrong types.", |
| 132 | 1 TSRMLS_CC); |
| 133 | efree(str_key); |
| 134 | efree(str_val); |
| 135 | return NULL; |
| 136 | } |
| 137 | add_next_index_stringl(*data, str_val, elem->value_length, false); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 138 | } else { |
murgatroid99 | 5ca9f92 | 2015-02-03 11:21:11 -0800 | [diff] [blame] | 139 | MAKE_STD_ZVAL(inner_array); |
| 140 | array_init(inner_array); |
| 141 | add_next_index_stringl(inner_array, str_val, elem->value_length, false); |
| 142 | add_assoc_zval(array, str_key, inner_array); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | return array; |
| 146 | } |
| 147 | |
murgatroid99 | 9c4425a | 2015-03-24 09:43:41 -0700 | [diff] [blame] | 148 | /* Populates a grpc_metadata_array with the data in a PHP array object. |
| 149 | Returns true on success and false on failure */ |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 150 | bool create_metadata_array(zval *array, grpc_metadata_array *metadata) { |
| 151 | zval **inner_array; |
| 152 | zval **value; |
| 153 | HashTable *array_hash; |
| 154 | HashPosition array_pointer; |
| 155 | HashTable *inner_array_hash; |
| 156 | HashPosition inner_array_pointer; |
| 157 | char *key; |
| 158 | uint key_len; |
| 159 | ulong index; |
| 160 | if (Z_TYPE_P(array) != IS_ARRAY) { |
| 161 | return false; |
| 162 | } |
| 163 | grpc_metadata_array_init(metadata); |
| 164 | array_hash = Z_ARRVAL_P(array); |
| 165 | for (zend_hash_internal_pointer_reset_ex(array_hash, &array_pointer); |
| 166 | zend_hash_get_current_data_ex(array_hash, (void**)&inner_array, |
| 167 | &array_pointer) == SUCCESS; |
| 168 | zend_hash_move_forward_ex(array_hash, &array_pointer)) { |
| 169 | if (zend_hash_get_current_key_ex(array_hash, &key, &key_len, &index, 0, |
| 170 | &array_pointer) != HASH_KEY_IS_STRING) { |
| 171 | return false; |
| 172 | } |
| 173 | if (Z_TYPE_P(*inner_array) != IS_ARRAY) { |
| 174 | return false; |
| 175 | } |
| 176 | inner_array_hash = Z_ARRVAL_P(*inner_array); |
| 177 | metadata->capacity += zend_hash_num_elements(inner_array_hash); |
| 178 | } |
| 179 | metadata->metadata = gpr_malloc(metadata->capacity * sizeof(grpc_metadata)); |
| 180 | for (zend_hash_internal_pointer_reset_ex(array_hash, &array_pointer); |
| 181 | zend_hash_get_current_data_ex(array_hash, (void**)&inner_array, |
| 182 | &array_pointer) == SUCCESS; |
| 183 | zend_hash_move_forward_ex(array_hash, &array_pointer)) { |
| 184 | if (zend_hash_get_current_key_ex(array_hash, &key, &key_len, &index, 0, |
| 185 | &array_pointer) != HASH_KEY_IS_STRING) { |
| 186 | return false; |
| 187 | } |
| 188 | inner_array_hash = Z_ARRVAL_P(*inner_array); |
| 189 | for (zend_hash_internal_pointer_reset_ex(inner_array_hash, |
| 190 | &inner_array_pointer); |
| 191 | zend_hash_get_current_data_ex(inner_array_hash, (void**)&value, |
| 192 | &inner_array_pointer) == SUCCESS; |
| 193 | zend_hash_move_forward_ex(inner_array_hash, &inner_array_pointer)) { |
| 194 | if (Z_TYPE_P(*value) != IS_STRING) { |
| 195 | return false; |
| 196 | } |
| 197 | metadata->metadata[metadata->count].key = key; |
| 198 | metadata->metadata[metadata->count].value = Z_STRVAL_P(*value); |
| 199 | metadata->metadata[metadata->count].value_length = Z_STRLEN_P(*value); |
| 200 | metadata->count += 1; |
| 201 | } |
| 202 | } |
| 203 | return true; |
| 204 | } |
| 205 | |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 206 | /** |
| 207 | * Constructs a new instance of the Call class. |
| 208 | * @param Channel $channel The channel to associate the call with. Must not be |
| 209 | * closed. |
| 210 | * @param string $method The method to call |
| 211 | * @param Timeval $absolute_deadline The deadline for completing the call |
| 212 | */ |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 213 | PHP_METHOD(Call, __construct) { |
| 214 | wrapped_grpc_call *call = |
| 215 | (wrapped_grpc_call *)zend_object_store_get_object(getThis() TSRMLS_CC); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 216 | zval *channel_obj; |
| 217 | char *method; |
| 218 | int method_len; |
| 219 | zval *deadline_obj; |
Stanley Cheung | 478fb00 | 2015-08-19 14:25:00 -0700 | [diff] [blame] | 220 | char *host_override = NULL; |
| 221 | int host_override_len = 0; |
| 222 | /* "OsO|s" == 1 Object, 1 string, 1 Object, 1 optional string */ |
| 223 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "OsO|s", |
| 224 | &channel_obj, grpc_ce_channel, |
| 225 | &method, &method_len, |
| 226 | &deadline_obj, grpc_ce_timeval, |
| 227 | &host_override, &host_override_len) |
| 228 | == FAILURE) { |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 229 | zend_throw_exception( |
| 230 | spl_ce_InvalidArgumentException, |
Stanley Cheung | 478fb00 | 2015-08-19 14:25:00 -0700 | [diff] [blame] | 231 | "Call expects a Channel, a String, a Timeval and an optional String", |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 232 | 1 TSRMLS_CC); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 233 | return; |
| 234 | } |
| 235 | wrapped_grpc_channel *channel = |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 236 | (wrapped_grpc_channel *)zend_object_store_get_object( |
| 237 | channel_obj TSRMLS_CC); |
| 238 | if (channel->wrapped == NULL) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 239 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 240 | "Call cannot be constructed from a closed Channel", |
| 241 | 1 TSRMLS_CC); |
| 242 | return; |
| 243 | } |
| 244 | add_property_zval(getThis(), "channel", channel_obj); |
| 245 | wrapped_grpc_timeval *deadline = |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 246 | (wrapped_grpc_timeval *)zend_object_store_get_object( |
| 247 | deadline_obj TSRMLS_CC); |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 248 | call->wrapped = grpc_channel_create_call( |
Craig Tiller | e1b0e6e | 2015-07-31 17:07:31 -0700 | [diff] [blame] | 249 | channel->wrapped, NULL, GRPC_PROPAGATE_DEFAULTS, completion_queue, method, |
Stanley Cheung | ddb16a8 | 2015-08-19 15:11:51 -0700 | [diff] [blame] | 250 | host_override, deadline->wrapped, NULL); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | /** |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 254 | * Start a batch of RPC actions. |
| 255 | * @param array batch Array of actions to take |
| 256 | * @return object Object with results of all actions |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 257 | */ |
murgatroid99 | c1d7e24 | 2015-04-02 10:02:43 -0700 | [diff] [blame] | 258 | PHP_METHOD(Call, startBatch) { |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 259 | wrapped_grpc_call *call = |
| 260 | (wrapped_grpc_call *)zend_object_store_get_object(getThis() TSRMLS_CC); |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 261 | grpc_op ops[8]; |
| 262 | size_t op_num = 0; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 263 | zval *array; |
murgatroid99 | 5ca9f92 | 2015-02-03 11:21:11 -0800 | [diff] [blame] | 264 | zval **value; |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 265 | zval **inner_value; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 266 | HashTable *array_hash; |
murgatroid99 | 5ca9f92 | 2015-02-03 11:21:11 -0800 | [diff] [blame] | 267 | HashPosition array_pointer; |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 268 | HashTable *status_hash; |
Stanley Cheung | 3ab8e79 | 2015-08-24 16:58:42 -0700 | [diff] [blame] | 269 | HashTable *message_hash; |
| 270 | zval **message_value; |
| 271 | zval **message_flags; |
murgatroid99 | 5ca9f92 | 2015-02-03 11:21:11 -0800 | [diff] [blame] | 272 | char *key; |
| 273 | uint key_len; |
| 274 | ulong index; |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 275 | grpc_metadata_array metadata; |
| 276 | grpc_metadata_array trailing_metadata; |
| 277 | grpc_metadata_array recv_metadata; |
| 278 | grpc_metadata_array recv_trailing_metadata; |
| 279 | grpc_status_code status; |
| 280 | char *status_details = NULL; |
murgatroid99 | 9fe516a | 2015-03-11 14:47:10 -0700 | [diff] [blame] | 281 | size_t status_details_capacity = 0; |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 282 | grpc_byte_buffer *message; |
| 283 | int cancelled; |
| 284 | grpc_call_error error; |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 285 | zval *result; |
| 286 | char *message_str; |
| 287 | size_t message_len; |
| 288 | zval *recv_status; |
| 289 | grpc_metadata_array_init(&metadata); |
| 290 | grpc_metadata_array_init(&trailing_metadata); |
murgatroid99 | d8bb957 | 2015-03-11 09:18:06 -0700 | [diff] [blame] | 291 | grpc_metadata_array_init(&recv_metadata); |
| 292 | grpc_metadata_array_init(&recv_trailing_metadata); |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 293 | MAKE_STD_ZVAL(result); |
| 294 | object_init(result); |
| 295 | /* "a" == 1 array */ |
| 296 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &array) == |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 297 | FAILURE) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 298 | zend_throw_exception(spl_ce_InvalidArgumentException, |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 299 | "start_batch expects an array", 1 TSRMLS_CC); |
| 300 | goto cleanup; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 301 | } |
| 302 | array_hash = Z_ARRVAL_P(array); |
murgatroid99 | 5ca9f92 | 2015-02-03 11:21:11 -0800 | [diff] [blame] | 303 | for (zend_hash_internal_pointer_reset_ex(array_hash, &array_pointer); |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 304 | zend_hash_get_current_data_ex(array_hash, (void**)&value, |
murgatroid99 | 5ca9f92 | 2015-02-03 11:21:11 -0800 | [diff] [blame] | 305 | &array_pointer) == SUCCESS; |
| 306 | zend_hash_move_forward_ex(array_hash, &array_pointer)) { |
| 307 | 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] | 308 | &array_pointer) != HASH_KEY_IS_LONG) { |
murgatroid99 | 5ca9f92 | 2015-02-03 11:21:11 -0800 | [diff] [blame] | 309 | zend_throw_exception(spl_ce_InvalidArgumentException, |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 310 | "batch keys must be integers", 1 TSRMLS_CC); |
| 311 | goto cleanup; |
murgatroid99 | 5ca9f92 | 2015-02-03 11:21:11 -0800 | [diff] [blame] | 312 | } |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 313 | switch(index) { |
| 314 | case GRPC_OP_SEND_INITIAL_METADATA: |
| 315 | if (!create_metadata_array(*value, &metadata)) { |
| 316 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 317 | "Bad metadata value given", 1 TSRMLS_CC); |
| 318 | goto cleanup; |
| 319 | } |
| 320 | ops[op_num].data.send_initial_metadata.count = |
| 321 | metadata.count; |
| 322 | ops[op_num].data.send_initial_metadata.metadata = |
| 323 | metadata.metadata; |
| 324 | break; |
| 325 | case GRPC_OP_SEND_MESSAGE: |
Stanley Cheung | 3ab8e79 | 2015-08-24 16:58:42 -0700 | [diff] [blame] | 326 | if (Z_TYPE_PP(value) != IS_ARRAY) { |
| 327 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 328 | "Expected an array for send message", |
| 329 | 1 TSRMLS_CC); |
| 330 | goto cleanup; |
| 331 | } |
| 332 | message_hash = Z_ARRVAL_PP(value); |
| 333 | if (zend_hash_find(message_hash, "flags", sizeof("flags"), |
| 334 | (void **)&message_flags) == SUCCESS) { |
| 335 | if (Z_TYPE_PP(message_flags) != IS_LONG) { |
| 336 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 337 | "Expected an int for message flags", |
| 338 | 1 TSRMLS_CC); |
| 339 | } |
| 340 | ops[op_num].flags = Z_LVAL_PP(message_flags) & GRPC_WRITE_USED_MASK; |
| 341 | } |
| 342 | if (zend_hash_find(message_hash, "message", sizeof("message"), |
| 343 | (void **)&message_value) != SUCCESS || |
| 344 | Z_TYPE_PP(message_value) != IS_STRING) { |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 345 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 346 | "Expected a string for send message", |
| 347 | 1 TSRMLS_CC); |
Stanley Cheung | 3ab8e79 | 2015-08-24 16:58:42 -0700 | [diff] [blame] | 348 | goto cleanup; |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 349 | } |
| 350 | ops[op_num].data.send_message = |
Stanley Cheung | 3ab8e79 | 2015-08-24 16:58:42 -0700 | [diff] [blame] | 351 | string_to_byte_buffer(Z_STRVAL_PP(message_value), |
| 352 | Z_STRLEN_PP(message_value)); |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 353 | break; |
| 354 | case GRPC_OP_SEND_CLOSE_FROM_CLIENT: |
| 355 | break; |
| 356 | case GRPC_OP_SEND_STATUS_FROM_SERVER: |
| 357 | status_hash = Z_ARRVAL_PP(value); |
| 358 | if (zend_hash_find(status_hash, "metadata", sizeof("metadata"), |
| 359 | (void **)&inner_value) == SUCCESS) { |
| 360 | if (!create_metadata_array(*inner_value, &trailing_metadata)) { |
| 361 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 362 | "Bad trailing metadata value given", |
| 363 | 1 TSRMLS_CC); |
| 364 | goto cleanup; |
| 365 | } |
| 366 | ops[op_num].data.send_status_from_server.trailing_metadata = |
| 367 | trailing_metadata.metadata; |
| 368 | ops[op_num].data.send_status_from_server.trailing_metadata_count = |
| 369 | trailing_metadata.count; |
| 370 | } |
| 371 | if (zend_hash_find(status_hash, "code", sizeof("code"), |
| 372 | (void**)&inner_value) == SUCCESS) { |
| 373 | if (Z_TYPE_PP(inner_value) != IS_LONG) { |
| 374 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 375 | "Status code must be an integer", |
| 376 | 1 TSRMLS_CC); |
| 377 | goto cleanup; |
| 378 | } |
| 379 | ops[op_num].data.send_status_from_server.status = |
| 380 | Z_LVAL_PP(inner_value); |
| 381 | } else { |
| 382 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 383 | "Integer status code is required", |
| 384 | 1 TSRMLS_CC); |
| 385 | goto cleanup; |
| 386 | } |
| 387 | if (zend_hash_find(status_hash, "details", sizeof("details"), |
| 388 | (void**)&inner_value) == SUCCESS) { |
| 389 | if (Z_TYPE_PP(inner_value) != IS_STRING) { |
| 390 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 391 | "Status details must be a string", |
| 392 | 1 TSRMLS_CC); |
| 393 | goto cleanup; |
| 394 | } |
| 395 | ops[op_num].data.send_status_from_server.status_details = |
| 396 | Z_STRVAL_PP(inner_value); |
| 397 | } else { |
| 398 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 399 | "String status details is required", |
| 400 | 1 TSRMLS_CC); |
| 401 | goto cleanup; |
| 402 | } |
| 403 | break; |
| 404 | case GRPC_OP_RECV_INITIAL_METADATA: |
| 405 | ops[op_num].data.recv_initial_metadata = &recv_metadata; |
| 406 | break; |
| 407 | case GRPC_OP_RECV_MESSAGE: |
| 408 | ops[op_num].data.recv_message = &message; |
| 409 | break; |
| 410 | case GRPC_OP_RECV_STATUS_ON_CLIENT: |
| 411 | ops[op_num].data.recv_status_on_client.trailing_metadata = |
| 412 | &recv_trailing_metadata; |
| 413 | ops[op_num].data.recv_status_on_client.status = &status; |
| 414 | ops[op_num].data.recv_status_on_client.status_details = |
| 415 | &status_details; |
| 416 | ops[op_num].data.recv_status_on_client.status_details_capacity = |
| 417 | &status_details_capacity; |
| 418 | break; |
| 419 | case GRPC_OP_RECV_CLOSE_ON_SERVER: |
| 420 | ops[op_num].data.recv_close_on_server.cancelled = &cancelled; |
| 421 | break; |
| 422 | default: |
murgatroid99 | 5ca9f92 | 2015-02-03 11:21:11 -0800 | [diff] [blame] | 423 | zend_throw_exception(spl_ce_InvalidArgumentException, |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 424 | "Unrecognized key in batch", 1 TSRMLS_CC); |
| 425 | goto cleanup; |
| 426 | } |
| 427 | ops[op_num].op = (grpc_op_type)index; |
David Garcia Quintas | ba710e5 | 2015-06-15 13:31:15 -0700 | [diff] [blame] | 428 | ops[op_num].flags = 0; |
Craig Tiller | 4275899 | 2015-08-18 10:34:32 -0700 | [diff] [blame] | 429 | ops[op_num].reserved = NULL; |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 430 | op_num++; |
| 431 | } |
Nicolas "Pixel" Noble | 150b7c4 | 2015-08-01 01:15:10 +0200 | [diff] [blame] | 432 | error = grpc_call_start_batch(call->wrapped, ops, op_num, call->wrapped, |
| 433 | NULL); |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 434 | if (error != GRPC_CALL_OK) { |
| 435 | zend_throw_exception(spl_ce_LogicException, |
| 436 | "start_batch was called incorrectly", |
| 437 | (long)error TSRMLS_CC); |
| 438 | goto cleanup; |
| 439 | } |
Stanley Cheung | c0c9ba9 | 2015-08-18 16:19:38 -0700 | [diff] [blame] | 440 | grpc_completion_queue_pluck(completion_queue, call->wrapped, |
| 441 | gpr_inf_future(GPR_CLOCK_REALTIME), NULL); |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 442 | for (int i = 0; i < op_num; i++) { |
| 443 | switch(ops[i].op) { |
| 444 | case GRPC_OP_SEND_INITIAL_METADATA: |
| 445 | add_property_bool(result, "send_metadata", true); |
| 446 | break; |
| 447 | case GRPC_OP_SEND_MESSAGE: |
| 448 | add_property_bool(result, "send_message", true); |
| 449 | break; |
| 450 | case GRPC_OP_SEND_CLOSE_FROM_CLIENT: |
| 451 | add_property_bool(result, "send_close", true); |
| 452 | break; |
| 453 | case GRPC_OP_SEND_STATUS_FROM_SERVER: |
| 454 | add_property_bool(result, "send_status", true); |
| 455 | break; |
| 456 | case GRPC_OP_RECV_INITIAL_METADATA: |
murgatroid99 | aa11066 | 2015-04-08 16:53:47 -0700 | [diff] [blame] | 457 | array = grpc_parse_metadata_array(&recv_metadata); |
| 458 | add_property_zval(result, "metadata", array); |
| 459 | Z_DELREF_P(array); |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 460 | break; |
| 461 | case GRPC_OP_RECV_MESSAGE: |
| 462 | byte_buffer_to_string(message, &message_str, &message_len); |
murgatroid99 | c1da8f2 | 2015-03-25 11:33:05 -0700 | [diff] [blame] | 463 | if (message_str == NULL) { |
| 464 | add_property_null(result, "message"); |
| 465 | } else { |
| 466 | add_property_stringl(result, "message", message_str, message_len, |
| 467 | false); |
| 468 | } |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 469 | break; |
| 470 | case GRPC_OP_RECV_STATUS_ON_CLIENT: |
| 471 | MAKE_STD_ZVAL(recv_status); |
| 472 | object_init(recv_status); |
murgatroid99 | aa11066 | 2015-04-08 16:53:47 -0700 | [diff] [blame] | 473 | array = grpc_parse_metadata_array(&recv_trailing_metadata); |
| 474 | add_property_zval(recv_status, "metadata", array); |
| 475 | Z_DELREF_P(array); |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 476 | add_property_long(recv_status, "code", status); |
murgatroid99 | 9fe516a | 2015-03-11 14:47:10 -0700 | [diff] [blame] | 477 | add_property_string(recv_status, "details", status_details, true); |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 478 | add_property_zval(result, "status", recv_status); |
murgatroid99 | aa11066 | 2015-04-08 16:53:47 -0700 | [diff] [blame] | 479 | Z_DELREF_P(recv_status); |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 480 | break; |
| 481 | case GRPC_OP_RECV_CLOSE_ON_SERVER: |
| 482 | add_property_bool(result, "cancelled", cancelled); |
| 483 | break; |
| 484 | default: |
| 485 | break; |
murgatroid99 | 5ca9f92 | 2015-02-03 11:21:11 -0800 | [diff] [blame] | 486 | } |
| 487 | } |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 488 | cleanup: |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 489 | grpc_metadata_array_destroy(&metadata); |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 490 | grpc_metadata_array_destroy(&trailing_metadata); |
| 491 | grpc_metadata_array_destroy(&recv_metadata); |
| 492 | grpc_metadata_array_destroy(&recv_trailing_metadata); |
| 493 | if (status_details != NULL) { |
| 494 | gpr_free(status_details); |
mlumish | dba8789 | 2015-01-02 13:27:28 -0800 | [diff] [blame] | 495 | } |
Stanley Cheung | 82e6f32 | 2016-04-06 11:51:57 -0700 | [diff] [blame] | 496 | for (int i = 0; i < op_num; i++) { |
| 497 | if (ops[i].op == GRPC_OP_SEND_MESSAGE) { |
| 498 | grpc_byte_buffer_destroy(ops[i].data.send_message); |
| 499 | } |
| 500 | if (ops[i].op == GRPC_OP_RECV_MESSAGE) { |
| 501 | grpc_byte_buffer_destroy(message); |
| 502 | } |
| 503 | } |
murgatroid99 | afd541c | 2015-03-03 18:16:09 -0800 | [diff] [blame] | 504 | RETURN_DESTROY_ZVAL(result); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 505 | } |
| 506 | |
murgatroid99 | c1da8f2 | 2015-03-25 11:33:05 -0700 | [diff] [blame] | 507 | /** |
Stanley Cheung | db98e08 | 2015-07-27 10:19:45 -0700 | [diff] [blame] | 508 | * Get the endpoint this call/stream is connected to |
| 509 | * @return string The URI of the endpoint |
| 510 | */ |
| 511 | PHP_METHOD(Call, getPeer) { |
| 512 | wrapped_grpc_call *call = |
| 513 | (wrapped_grpc_call *)zend_object_store_get_object(getThis() TSRMLS_CC); |
| 514 | RETURN_STRING(grpc_call_get_peer(call->wrapped), 1); |
| 515 | } |
| 516 | |
| 517 | /** |
murgatroid99 | c1da8f2 | 2015-03-25 11:33:05 -0700 | [diff] [blame] | 518 | * Cancel the call. This will cause the call to end with STATUS_CANCELLED if it |
| 519 | * has not already ended with another status. |
| 520 | */ |
| 521 | PHP_METHOD(Call, cancel) { |
| 522 | wrapped_grpc_call *call = |
| 523 | (wrapped_grpc_call *)zend_object_store_get_object(getThis() TSRMLS_CC); |
Nicolas "Pixel" Noble | 150b7c4 | 2015-08-01 01:15:10 +0200 | [diff] [blame] | 524 | grpc_call_cancel(call->wrapped, NULL); |
murgatroid99 | c1da8f2 | 2015-03-25 11:33:05 -0700 | [diff] [blame] | 525 | } |
| 526 | |
Stanley Cheung | 3580580 | 2015-12-10 11:42:55 -0800 | [diff] [blame] | 527 | /** |
| 528 | * Set the CallCredentials for this call. |
| 529 | * @param CallCredentials creds_obj The CallCredentials object |
| 530 | * @param int The error code |
| 531 | */ |
| 532 | PHP_METHOD(Call, setCredentials) { |
| 533 | zval *creds_obj; |
| 534 | |
| 535 | /* "O" == 1 Object */ |
| 536 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &creds_obj, |
| 537 | grpc_ce_call_credentials) == FAILURE) { |
| 538 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 539 | "setCredentials expects 1 CallCredentials", |
| 540 | 1 TSRMLS_CC); |
| 541 | return; |
| 542 | } |
| 543 | |
| 544 | wrapped_grpc_call_credentials *creds = |
| 545 | (wrapped_grpc_call_credentials *)zend_object_store_get_object( |
| 546 | creds_obj TSRMLS_CC); |
| 547 | |
| 548 | wrapped_grpc_call *call = |
| 549 | (wrapped_grpc_call *)zend_object_store_get_object(getThis() TSRMLS_CC); |
| 550 | |
| 551 | grpc_call_error error = GRPC_CALL_ERROR; |
| 552 | error = grpc_call_set_credentials(call->wrapped, creds->wrapped); |
| 553 | RETURN_LONG(error); |
| 554 | } |
| 555 | |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 556 | static zend_function_entry call_methods[] = { |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 557 | PHP_ME(Call, __construct, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR) |
murgatroid99 | c1d7e24 | 2015-04-02 10:02:43 -0700 | [diff] [blame] | 558 | PHP_ME(Call, startBatch, NULL, ZEND_ACC_PUBLIC) |
Stanley Cheung | db98e08 | 2015-07-27 10:19:45 -0700 | [diff] [blame] | 559 | PHP_ME(Call, getPeer, NULL, ZEND_ACC_PUBLIC) |
| 560 | PHP_ME(Call, cancel, NULL, ZEND_ACC_PUBLIC) |
Stanley Cheung | 3580580 | 2015-12-10 11:42:55 -0800 | [diff] [blame] | 561 | PHP_ME(Call, setCredentials, NULL, ZEND_ACC_PUBLIC) |
Stanley Cheung | db98e08 | 2015-07-27 10:19:45 -0700 | [diff] [blame] | 562 | PHP_FE_END}; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 563 | |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 564 | void grpc_init_call(TSRMLS_D) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 565 | zend_class_entry ce; |
| 566 | INIT_CLASS_ENTRY(ce, "Grpc\\Call", call_methods); |
| 567 | ce.create_object = create_wrapped_grpc_call; |
| 568 | grpc_ce_call = zend_register_internal_class(&ce TSRMLS_CC); |
| 569 | } |