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