blob: 3f96db7ccb177ad74914f0ffafce3c5ad4ed1e85 [file] [log] [blame]
Craig Tiller1a61b172015-02-16 11:53:47 -08001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
Craig Tiller1a61b172015-02-16 11:53:47 -08004 * 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
mlumishb892a272014-12-09 16:28:23 -080034#include "call.h"
35
36#ifdef HAVE_CONFIG_H
37#include "config.h"
38#endif
39
murgatroid998242ba72015-04-01 15:29:44 -070040#include <php.h>
41#include <php_ini.h>
42#include <ext/standard/info.h>
43#include <ext/spl/spl_exceptions.h>
mlumishb892a272014-12-09 16:28:23 -080044#include "php_grpc.h"
Stanley Cheung35805802015-12-10 11:42:55 -080045#include "call_credentials.h"
mlumishb892a272014-12-09 16:28:23 -080046
murgatroid998242ba72015-04-01 15:29:44 -070047#include <zend_exceptions.h>
48#include <zend_hash.h>
mlumishb892a272014-12-09 16:28:23 -080049
50#include <stdbool.h>
51
murgatroid998242ba72015-04-01 15:29:44 -070052#include <grpc/support/alloc.h>
53#include <grpc/grpc.h>
mlumishb892a272014-12-09 16:28:23 -080054
murgatroid99268acd52015-05-14 15:05:00 -070055#include "completion_queue.h"
mlumishb892a272014-12-09 16:28:23 -080056#include "timeval.h"
57#include "channel.h"
mlumishb892a272014-12-09 16:28:23 -080058#include "byte_buffer.h"
59
Xiaoguang Sun8a929a92015-03-13 14:22:31 +080060zend_class_entry *grpc_ce_call;
61
thinkeroua3730b72016-07-20 16:59:54 +080062#if PHP_MAJOR_VERSION < 7
63
mlumishb892a272014-12-09 16:28:23 -080064/* Frees and destroys an instance of wrapped_grpc_call */
Craig Tillerb5dcec52015-01-13 11:13:42 -080065void free_wrapped_grpc_call(void *object TSRMLS_DC) {
66 wrapped_grpc_call *call = (wrapped_grpc_call *)object;
67 if (call->owned && call->wrapped != NULL) {
mlumishb892a272014-12-09 16:28:23 -080068 grpc_call_destroy(call->wrapped);
69 }
thinkeroua3730b72016-07-20 16:59:54 +080070 zend_object_std_dtor(&call->std TSRMLS_CC);
mlumishb892a272014-12-09 16:28:23 -080071 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 Tillerb5dcec52015-01-13 11:13:42 -080076zend_object_value create_wrapped_grpc_call(zend_class_entry *class_type
77 TSRMLS_DC) {
mlumishb892a272014-12-09 16:28:23 -080078 zend_object_value retval;
79 wrapped_grpc_call *intern;
80
Craig Tillerb5dcec52015-01-13 11:13:42 -080081 intern = (wrapped_grpc_call *)emalloc(sizeof(wrapped_grpc_call));
mlumishb892a272014-12-09 16:28:23 -080082 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 Tillerb5dcec52015-01-13 11:13:42 -080087 intern, (zend_objects_store_dtor_t)zend_objects_destroy_object,
88 free_wrapped_grpc_call, NULL TSRMLS_CC);
mlumishb892a272014-12-09 16:28:23 -080089 retval.handlers = zend_get_std_object_handlers();
90 return retval;
91}
92
mlumish34cd1f02015-01-02 13:32:41 -080093/* 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 Bausor4f8e40b2016-05-16 11:41:25 -070095zval *grpc_php_wrap_call(grpc_call *wrapped, bool owned TSRMLS_DC) {
mlumishb892a272014-12-09 16:28:23 -080096 zval *call_object;
97 MAKE_STD_ZVAL(call_object);
98 object_init_ex(call_object, grpc_ce_call);
Craig Tillerb5dcec52015-01-13 11:13:42 -080099 wrapped_grpc_call *call =
100 (wrapped_grpc_call *)zend_object_store_get_object(call_object TSRMLS_CC);
mlumishb892a272014-12-09 16:28:23 -0800101 call->wrapped = wrapped;
thinkeroud04376d2016-04-27 19:58:49 +0800102 call->owned = owned;
mlumishb892a272014-12-09 16:28:23 -0800103 return call_object;
104}
105
murgatroid999c4425a2015-03-24 09:43:41 -0700106/* Creates and returns a PHP array object with the data in a
107 * grpc_metadata_array. Returns NULL on failure */
Michael Bausor4f8e40b2016-05-16 11:41:25 -0700108zval *grpc_parse_metadata_array(grpc_metadata_array *metadata_array TSRMLS_DC) {
murgatroid99afd541c2015-03-03 18:16:09 -0800109 int count = metadata_array->count;
110 grpc_metadata *elements = metadata_array->metadata;
mlumishb892a272014-12-09 16:28:23 -0800111 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 Tillerb5dcec52015-01-13 11:13:42 -0800123 for (i = 0; i < count; i++) {
mlumishb892a272014-12-09 16:28:23 -0800124 elem = &elements[i];
125 key_len = strlen(elem->key);
Craig Tillerb5dcec52015-01-13 11:13:42 -0800126 str_key = ecalloc(key_len + 1, sizeof(char));
mlumishb892a272014-12-09 16:28:23 -0800127 memcpy(str_key, elem->key, key_len);
Craig Tillerb5dcec52015-01-13 11:13:42 -0800128 str_val = ecalloc(elem->value_length + 1, sizeof(char));
mlumishb892a272014-12-09 16:28:23 -0800129 memcpy(str_val, elem->value, elem->value_length);
Craig Tillerb5dcec52015-01-13 11:13:42 -0800130 if (zend_hash_find(array_hash, str_key, key_len, (void **)data) ==
131 SUCCESS) {
Stanley Cheungb91f0f22016-02-16 09:36:36 -0800132 if (Z_TYPE_P(*data) != IS_ARRAY) {
Michael Bausor4f8e40b2016-05-16 11:41:25 -0700133 zend_throw_exception(zend_exception_get_default(TSRMLS_C),
Stanley Cheungb91f0f22016-02-16 09:36:36 -0800134 "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);
mlumishb892a272014-12-09 16:28:23 -0800141 } else {
murgatroid995ca9f922015-02-03 11:21:11 -0800142 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);
mlumishb892a272014-12-09 16:28:23 -0800146 }
147 }
148 return array;
149}
150
murgatroid999c4425a2015-03-24 09:43:41 -0700151/* Populates a grpc_metadata_array with the data in a PHP array object.
152 Returns true on success and false on failure */
murgatroid99afd541c2015-03-03 18:16:09 -0800153bool 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
thinkeroua3730b72016-07-20 16:59:54 +0800209#else
210
211static zend_object_handlers call_ce_handlers;
212
213/* Frees and destroys an instance of wrapped_grpc_call */
214static 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 */
224zend_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 */
236void 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 */
245void 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 */
287bool 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
mlumishb892a272014-12-09 16:28:23 -0800334/**
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 Tillerb5dcec52015-01-13 11:13:42 -0800341PHP_METHOD(Call, __construct) {
mlumishb892a272014-12-09 16:28:23 -0800342 zval *channel_obj;
343 char *method;
thinkerou19304682016-07-22 02:43:19 +0800344 php_grpc_int method_len;
mlumishb892a272014-12-09 16:28:23 -0800345 zval *deadline_obj;
Stanley Cheung478fb002015-08-19 14:25:00 -0700346 char *host_override = NULL;
thinkerou19304682016-07-22 02:43:19 +0800347 php_grpc_int host_override_len = 0;
thinkeroua3730b72016-07-20 16:59:54 +0800348 wrapped_grpc_call *call = Z_WRAPPED_GRPC_CALL_P(getThis());
thinkeroua3730b72016-07-20 16:59:54 +0800349
Stanley Cheung478fb002015-08-19 14:25:00 -0700350 /* "OsO|s" == 1 Object, 1 string, 1 Object, 1 optional string */
thinkeroua3730b72016-07-20 16:59:54 +0800351 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);
mlumishb892a272014-12-09 16:28:23 -0800358 return;
359 }
thinkeroua3730b72016-07-20 16:59:54 +0800360 wrapped_grpc_channel *channel = Z_WRAPPED_GRPC_CHANNEL_P(channel_obj);
Craig Tillerb5dcec52015-01-13 11:13:42 -0800361 if (channel->wrapped == NULL) {
mlumishb892a272014-12-09 16:28:23 -0800362 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);
thinkeroua3730b72016-07-20 16:59:54 +0800368 wrapped_grpc_timeval *deadline = Z_WRAPPED_GRPC_TIMEVAL_P(deadline_obj);
thinkeroua3730b72016-07-20 16:59:54 +0800369 call->wrapped =
370 grpc_channel_create_call(channel->wrapped, NULL, GRPC_PROPAGATE_DEFAULTS,
371 completion_queue, method, host_override,
372 deadline->wrapped, NULL);
Stanley Cheung51b36912016-06-29 15:05:59 -0700373 call->owned = true;
mlumishb892a272014-12-09 16:28:23 -0800374}
375
376/**
murgatroid99afd541c2015-03-03 18:16:09 -0800377 * Start a batch of RPC actions.
378 * @param array batch Array of actions to take
379 * @return object Object with results of all actions
mlumishb892a272014-12-09 16:28:23 -0800380 */
murgatroid99c1d7e242015-04-02 10:02:43 -0700381PHP_METHOD(Call, startBatch) {
thinkeroua3730b72016-07-20 16:59:54 +0800382#if PHP_MAJOR_VERSION < 7
Craig Tillerb5dcec52015-01-13 11:13:42 -0800383 wrapped_grpc_call *call =
384 (wrapped_grpc_call *)zend_object_store_get_object(getThis() TSRMLS_CC);
murgatroid995ca9f922015-02-03 11:21:11 -0800385 zval **value;
murgatroid99afd541c2015-03-03 18:16:09 -0800386 zval **inner_value;
murgatroid995ca9f922015-02-03 11:21:11 -0800387 HashPosition array_pointer;
Stanley Cheung3ab8e792015-08-24 16:58:42 -0700388 zval **message_value;
389 zval **message_flags;
murgatroid995ca9f922015-02-03 11:21:11 -0800390 char *key;
391 uint key_len;
392 ulong index;
thinkeroua3730b72016-07-20 16:59:54 +0800393 zval *result;
394 zval *recv_status;
395 MAKE_STD_ZVAL(result);
396 object_init(result);
397#else
398 wrapped_grpc_call *call = Z_WRAPPED_GRPC_CALL_P(getThis());
399 zval *value;
400 zval *inner_value;
401 zval *message_value;
402 zval *message_flags;
403 zend_string *key;
404 zend_ulong index;
405 zval recv_status;
406 object_init(return_value);
407#endif
408
409 grpc_op ops[8];
410 size_t op_num = 0;
411 zval *array;
412 HashTable *array_hash;
413 HashTable *status_hash;
414 HashTable *message_hash;
415
murgatroid99afd541c2015-03-03 18:16:09 -0800416 grpc_metadata_array metadata;
417 grpc_metadata_array trailing_metadata;
418 grpc_metadata_array recv_metadata;
419 grpc_metadata_array recv_trailing_metadata;
420 grpc_status_code status;
421 char *status_details = NULL;
murgatroid999fe516a2015-03-11 14:47:10 -0700422 size_t status_details_capacity = 0;
murgatroid99afd541c2015-03-03 18:16:09 -0800423 grpc_byte_buffer *message;
424 int cancelled;
425 grpc_call_error error;
murgatroid99afd541c2015-03-03 18:16:09 -0800426 char *message_str;
427 size_t message_len;
thinkeroua3730b72016-07-20 16:59:54 +0800428
429
murgatroid99afd541c2015-03-03 18:16:09 -0800430 grpc_metadata_array_init(&metadata);
431 grpc_metadata_array_init(&trailing_metadata);
murgatroid99d8bb9572015-03-11 09:18:06 -0700432 grpc_metadata_array_init(&recv_metadata);
433 grpc_metadata_array_init(&recv_trailing_metadata);
David Garcia Quintasa301eaa2016-05-06 16:59:03 -0700434 memset(ops, 0, sizeof(ops));
thinkeroua3730b72016-07-20 16:59:54 +0800435
murgatroid99afd541c2015-03-03 18:16:09 -0800436 /* "a" == 1 array */
437 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &array) ==
Craig Tillerb5dcec52015-01-13 11:13:42 -0800438 FAILURE) {
mlumishb892a272014-12-09 16:28:23 -0800439 zend_throw_exception(spl_ce_InvalidArgumentException,
murgatroid99afd541c2015-03-03 18:16:09 -0800440 "start_batch expects an array", 1 TSRMLS_CC);
441 goto cleanup;
mlumishb892a272014-12-09 16:28:23 -0800442 }
thinkeroua3730b72016-07-20 16:59:54 +0800443
444#if PHP_MAJOR_VERSION < 7
445
mlumishb892a272014-12-09 16:28:23 -0800446 array_hash = Z_ARRVAL_P(array);
murgatroid995ca9f922015-02-03 11:21:11 -0800447 for (zend_hash_internal_pointer_reset_ex(array_hash, &array_pointer);
murgatroid99afd541c2015-03-03 18:16:09 -0800448 zend_hash_get_current_data_ex(array_hash, (void**)&value,
murgatroid995ca9f922015-02-03 11:21:11 -0800449 &array_pointer) == SUCCESS;
450 zend_hash_move_forward_ex(array_hash, &array_pointer)) {
451 if (zend_hash_get_current_key_ex(array_hash, &key, &key_len, &index, 0,
murgatroid99afd541c2015-03-03 18:16:09 -0800452 &array_pointer) != HASH_KEY_IS_LONG) {
murgatroid995ca9f922015-02-03 11:21:11 -0800453 zend_throw_exception(spl_ce_InvalidArgumentException,
murgatroid99afd541c2015-03-03 18:16:09 -0800454 "batch keys must be integers", 1 TSRMLS_CC);
455 goto cleanup;
murgatroid995ca9f922015-02-03 11:21:11 -0800456 }
murgatroid99afd541c2015-03-03 18:16:09 -0800457 switch(index) {
thinkeroua3730b72016-07-20 16:59:54 +0800458 case GRPC_OP_SEND_INITIAL_METADATA:
459 if (!create_metadata_array(*value, &metadata)) {
murgatroid995ca9f922015-02-03 11:21:11 -0800460 zend_throw_exception(spl_ce_InvalidArgumentException,
thinkeroua3730b72016-07-20 16:59:54 +0800461 "Bad metadata value given", 1 TSRMLS_CC);
murgatroid99afd541c2015-03-03 18:16:09 -0800462 goto cleanup;
thinkeroua3730b72016-07-20 16:59:54 +0800463 }
464 ops[op_num].data.send_initial_metadata.count =
465 metadata.count;
466 ops[op_num].data.send_initial_metadata.metadata =
467 metadata.metadata;
468 break;
469 case GRPC_OP_SEND_MESSAGE:
470 if (Z_TYPE_PP(value) != IS_ARRAY) {
471 zend_throw_exception(spl_ce_InvalidArgumentException,
472 "Expected an array for send message",
473 1 TSRMLS_CC);
474 goto cleanup;
475 }
476 message_hash = Z_ARRVAL_PP(value);
477 if (zend_hash_find(message_hash, "flags", sizeof("flags"),
478 (void **)&message_flags) == SUCCESS) {
479 if (Z_TYPE_PP(message_flags) != IS_LONG) {
480 zend_throw_exception(spl_ce_InvalidArgumentException,
481 "Expected an int for message flags",
482 1 TSRMLS_CC);
483 }
484 ops[op_num].flags = Z_LVAL_PP(message_flags) & GRPC_WRITE_USED_MASK;
485 }
486 if (zend_hash_find(message_hash, "message", sizeof("message"),
487 (void **)&message_value) != SUCCESS ||
488 Z_TYPE_PP(message_value) != IS_STRING) {
489 zend_throw_exception(spl_ce_InvalidArgumentException,
490 "Expected a string for send message",
491 1 TSRMLS_CC);
492 goto cleanup;
493 }
494 ops[op_num].data.send_message =
495 string_to_byte_buffer(Z_STRVAL_PP(message_value),
496 Z_STRLEN_PP(message_value));
497 break;
498 case GRPC_OP_SEND_CLOSE_FROM_CLIENT:
499 break;
500 case GRPC_OP_SEND_STATUS_FROM_SERVER:
501 status_hash = Z_ARRVAL_PP(value);
502 if (zend_hash_find(status_hash, "metadata", sizeof("metadata"),
503 (void **)&inner_value) == SUCCESS) {
504 if (!create_metadata_array(*inner_value, &trailing_metadata)) {
505 zend_throw_exception(spl_ce_InvalidArgumentException,
506 "Bad trailing metadata value given",
507 1 TSRMLS_CC);
508 goto cleanup;
509 }
510 ops[op_num].data.send_status_from_server.trailing_metadata =
511 trailing_metadata.metadata;
512 ops[op_num].data.send_status_from_server.trailing_metadata_count =
513 trailing_metadata.count;
514 }
515 if (zend_hash_find(status_hash, "code", sizeof("code"),
516 (void**)&inner_value) == SUCCESS) {
517 if (Z_TYPE_PP(inner_value) != IS_LONG) {
518 zend_throw_exception(spl_ce_InvalidArgumentException,
519 "Status code must be an integer",
520 1 TSRMLS_CC);
521 goto cleanup;
522 }
523 ops[op_num].data.send_status_from_server.status =
524 Z_LVAL_PP(inner_value);
525 } else {
526 zend_throw_exception(spl_ce_InvalidArgumentException,
527 "Integer status code is required",
528 1 TSRMLS_CC);
529 goto cleanup;
530 }
531 if (zend_hash_find(status_hash, "details", sizeof("details"),
532 (void**)&inner_value) == SUCCESS) {
533 if (Z_TYPE_PP(inner_value) != IS_STRING) {
534 zend_throw_exception(spl_ce_InvalidArgumentException,
535 "Status details must be a string",
536 1 TSRMLS_CC);
537 goto cleanup;
538 }
539 ops[op_num].data.send_status_from_server.status_details =
540 Z_STRVAL_PP(inner_value);
541 } else {
542 zend_throw_exception(spl_ce_InvalidArgumentException,
543 "String status details is required",
544 1 TSRMLS_CC);
545 goto cleanup;
546 }
547 break;
548 case GRPC_OP_RECV_INITIAL_METADATA:
549 ops[op_num].data.recv_initial_metadata = &recv_metadata;
550 break;
551 case GRPC_OP_RECV_MESSAGE:
552 ops[op_num].data.recv_message = &message;
553 break;
554 case GRPC_OP_RECV_STATUS_ON_CLIENT:
555 ops[op_num].data.recv_status_on_client.trailing_metadata =
556 &recv_trailing_metadata;
557 ops[op_num].data.recv_status_on_client.status = &status;
558 ops[op_num].data.recv_status_on_client.status_details =
559 &status_details;
560 ops[op_num].data.recv_status_on_client.status_details_capacity =
561 &status_details_capacity;
562 break;
563 case GRPC_OP_RECV_CLOSE_ON_SERVER:
564 ops[op_num].data.recv_close_on_server.cancelled = &cancelled;
565 break;
566 default:
567 zend_throw_exception(spl_ce_InvalidArgumentException,
568 "Unrecognized key in batch", 1 TSRMLS_CC);
569 goto cleanup;
murgatroid99afd541c2015-03-03 18:16:09 -0800570 }
571 ops[op_num].op = (grpc_op_type)index;
David Garcia Quintasba710e52015-06-15 13:31:15 -0700572 ops[op_num].flags = 0;
Craig Tiller42758992015-08-18 10:34:32 -0700573 ops[op_num].reserved = NULL;
murgatroid99afd541c2015-03-03 18:16:09 -0800574 op_num++;
575 }
thinkeroua3730b72016-07-20 16:59:54 +0800576
577#else
578
579array_hash = HASH_OF(array);
580 ZEND_HASH_FOREACH_KEY_VAL(array_hash, index, key, value) {
581 if (key) {
582 zend_throw_exception(spl_ce_InvalidArgumentException,
583 "batch keys must be integers", 1);
584 goto cleanup;
585 }
586
587 switch(index) {
588 case GRPC_OP_SEND_INITIAL_METADATA:
589 if (!create_metadata_array(value, &metadata)) {
590 zend_throw_exception(spl_ce_InvalidArgumentException,
591 "Bad metadata value given", 1);
592 goto cleanup;
593 }
594 ops[op_num].data.send_initial_metadata.count = metadata.count;
595 ops[op_num].data.send_initial_metadata.metadata = metadata.metadata;
596 break;
597 case GRPC_OP_SEND_MESSAGE:
598 if (Z_TYPE_P(value) != IS_ARRAY) {
599 zend_throw_exception(spl_ce_InvalidArgumentException,
600 "Expected an array for send message", 1);
601 goto cleanup;
602 }
603 message_hash = HASH_OF(value);
604 if ((message_flags =
605 zend_hash_str_find(message_hash, "flags",
606 sizeof("flags") - 1)) != NULL) {
607 if (Z_TYPE_P(message_flags) != IS_LONG) {
608 zend_throw_exception(spl_ce_InvalidArgumentException,
609 "Expected an int for message flags", 1);
610 }
611 ops[op_num].flags = Z_LVAL_P(message_flags) & GRPC_WRITE_USED_MASK;
612 }
613 if ((message_value = zend_hash_str_find(message_hash, "message",
614 sizeof("message") - 1))
615 == NULL || Z_TYPE_P(message_value) != IS_STRING) {
616 zend_throw_exception(spl_ce_InvalidArgumentException,
617 "Expected a string for send message", 1);
618 goto cleanup;
619 }
620 ops[op_num].data.send_message =
621 string_to_byte_buffer(Z_STRVAL_P(message_value),
622 Z_STRLEN_P(message_value));
623 break;
624 case GRPC_OP_SEND_CLOSE_FROM_CLIENT:
625 break;
626 case GRPC_OP_SEND_STATUS_FROM_SERVER:
627 status_hash = HASH_OF(value);
628 if ((inner_value = zend_hash_str_find(status_hash, "metadata",
629 sizeof("metadata") - 1))
630 != NULL) {
631 if (!create_metadata_array(inner_value, &trailing_metadata)) {
632 zend_throw_exception(spl_ce_InvalidArgumentException,
633 "Bad trailing metadata value given", 1);
634 goto cleanup;
635 }
636 ops[op_num].data.send_status_from_server.trailing_metadata =
637 trailing_metadata.metadata;
638 ops[op_num].data.send_status_from_server.trailing_metadata_count =
639 trailing_metadata.count;
640 }
641 if ((inner_value = zend_hash_str_find(status_hash, "code",
642 sizeof("code") - 1)) != NULL) {
643 if (Z_TYPE_P(inner_value) != IS_LONG) {
644 zend_throw_exception(spl_ce_InvalidArgumentException,
645 "Status code must be an integer", 1);
646 goto cleanup;
647 }
648 ops[op_num].data.send_status_from_server.status =
649 Z_LVAL_P(inner_value);
650 } else {
651 zend_throw_exception(spl_ce_InvalidArgumentException,
652 "Integer status code is required", 1);
653 goto cleanup;
654 }
655 if ((inner_value = zend_hash_str_find(status_hash, "details",
656 sizeof("details") - 1)) != NULL) {
657 if (Z_TYPE_P(inner_value) != IS_STRING) {
658 zend_throw_exception(spl_ce_InvalidArgumentException,
659 "Status details must be a string", 1);
660 goto cleanup;
661 }
662 ops[op_num].data.send_status_from_server.status_details =
663 Z_STRVAL_P(inner_value);
664 } else {
665 zend_throw_exception(spl_ce_InvalidArgumentException,
666 "String status details is required", 1);
667 goto cleanup;
668 }
669 break;
670 case GRPC_OP_RECV_INITIAL_METADATA:
671 ops[op_num].data.recv_initial_metadata = &recv_metadata;
672 break;
673 case GRPC_OP_RECV_MESSAGE:
674 ops[op_num].data.recv_message = &message;
675 break;
676 case GRPC_OP_RECV_STATUS_ON_CLIENT:
677 ops[op_num].data.recv_status_on_client.trailing_metadata =
678 &recv_trailing_metadata;
679 ops[op_num].data.recv_status_on_client.status = &status;
680 ops[op_num].data.recv_status_on_client.status_details =
681 &status_details;
682 ops[op_num].data.recv_status_on_client.status_details_capacity =
683 &status_details_capacity;
684 break;
685 case GRPC_OP_RECV_CLOSE_ON_SERVER:
686 ops[op_num].data.recv_close_on_server.cancelled = &cancelled;
687 break;
688 default:
689 zend_throw_exception(spl_ce_InvalidArgumentException,
690 "Unrecognized key in batch", 1);
691 goto cleanup;
692 }
693 ops[op_num].op = (grpc_op_type)index;
694 ops[op_num].flags = 0;
695 ops[op_num].reserved = NULL;
696 op_num++;
697 }
698 ZEND_HASH_FOREACH_END();
699
700#endif
701
Nicolas "Pixel" Noble150b7c42015-08-01 01:15:10 +0200702 error = grpc_call_start_batch(call->wrapped, ops, op_num, call->wrapped,
703 NULL);
murgatroid99afd541c2015-03-03 18:16:09 -0800704 if (error != GRPC_CALL_OK) {
705 zend_throw_exception(spl_ce_LogicException,
706 "start_batch was called incorrectly",
707 (long)error TSRMLS_CC);
708 goto cleanup;
709 }
Stanley Cheungc0c9ba92015-08-18 16:19:38 -0700710 grpc_completion_queue_pluck(completion_queue, call->wrapped,
711 gpr_inf_future(GPR_CLOCK_REALTIME), NULL);
thinkeroua3730b72016-07-20 16:59:54 +0800712#if PHP_MAJOR_VERSION < 7
murgatroid99afd541c2015-03-03 18:16:09 -0800713 for (int i = 0; i < op_num; i++) {
714 switch(ops[i].op) {
thinkeroua3730b72016-07-20 16:59:54 +0800715 case GRPC_OP_SEND_INITIAL_METADATA:
716 add_property_bool(result, "send_metadata", true);
717 break;
718 case GRPC_OP_SEND_MESSAGE:
719 add_property_bool(result, "send_message", true);
720 break;
721 case GRPC_OP_SEND_CLOSE_FROM_CLIENT:
722 add_property_bool(result, "send_close", true);
723 break;
724 case GRPC_OP_SEND_STATUS_FROM_SERVER:
725 add_property_bool(result, "send_status", true);
726 break;
727 case GRPC_OP_RECV_INITIAL_METADATA:
728 array = grpc_parse_metadata_array(&recv_metadata TSRMLS_CC);
729 add_property_zval(result, "metadata", array);
730 Z_DELREF_P(array);
731 break;
732 case GRPC_OP_RECV_MESSAGE:
733 byte_buffer_to_string(message, &message_str, &message_len);
734 if (message_str == NULL) {
735 add_property_null(result, "message");
736 } else {
737 add_property_stringl(result, "message", message_str, message_len,
738 false);
739 }
740 break;
741 case GRPC_OP_RECV_STATUS_ON_CLIENT:
742 MAKE_STD_ZVAL(recv_status);
743 object_init(recv_status);
744 array = grpc_parse_metadata_array(&recv_trailing_metadata TSRMLS_CC);
745 add_property_zval(recv_status, "metadata", array);
746 Z_DELREF_P(array);
747 add_property_long(recv_status, "code", status);
748 add_property_string(recv_status, "details", status_details, true);
749 add_property_zval(result, "status", recv_status);
750 Z_DELREF_P(recv_status);
751 break;
752 case GRPC_OP_RECV_CLOSE_ON_SERVER:
753 add_property_bool(result, "cancelled", cancelled);
754 break;
755 default:
756 break;
murgatroid995ca9f922015-02-03 11:21:11 -0800757 }
758 }
thinkeroua3730b72016-07-20 16:59:54 +0800759#else
760 for (int i = 0; i < op_num; i++) {
761 switch(ops[i].op) {
762 case GRPC_OP_SEND_INITIAL_METADATA:
763 add_property_bool(return_value, "send_metadata", true);
764 break;
765 case GRPC_OP_SEND_MESSAGE:
766 add_property_bool(return_value, "send_message", true);
767 break;
768 case GRPC_OP_SEND_CLOSE_FROM_CLIENT:
769 add_property_bool(return_value, "send_close", true);
770 break;
771 case GRPC_OP_SEND_STATUS_FROM_SERVER:
772 add_property_bool(return_value, "send_status", true);
773 break;
774 case GRPC_OP_RECV_INITIAL_METADATA:
775 grpc_parse_metadata_array(&recv_metadata, array);
776 add_property_zval(return_value, "metadata", array);
777 break;
778 case GRPC_OP_RECV_MESSAGE:
779 byte_buffer_to_string(message, &message_str, &message_len);
780 if (message_str == NULL) {
781 add_property_null(return_value, "message");
782 } else {
783 add_property_stringl(return_value, "message", message_str,
784 message_len);
785 }
786 break;
787 case GRPC_OP_RECV_STATUS_ON_CLIENT:
788 object_init(&recv_status);
789 grpc_parse_metadata_array(&recv_trailing_metadata, array);
790 add_property_zval(&recv_status, "metadata", array);
791 add_property_long(&recv_status, "code", status);
792 add_property_string(&recv_status, "details", status_details);
793 add_property_zval(return_value, "status", &recv_status);
794 break;
795 case GRPC_OP_RECV_CLOSE_ON_SERVER:
796 add_property_bool(return_value, "cancelled", cancelled);
797 break;
798 default:
799 break;
800 }
801 }
802#endif
803
murgatroid99afd541c2015-03-03 18:16:09 -0800804cleanup:
murgatroid99afd541c2015-03-03 18:16:09 -0800805 grpc_metadata_array_destroy(&metadata);
murgatroid99afd541c2015-03-03 18:16:09 -0800806 grpc_metadata_array_destroy(&trailing_metadata);
807 grpc_metadata_array_destroy(&recv_metadata);
808 grpc_metadata_array_destroy(&recv_trailing_metadata);
809 if (status_details != NULL) {
810 gpr_free(status_details);
mlumishdba87892015-01-02 13:27:28 -0800811 }
Stanley Cheung82e6f322016-04-06 11:51:57 -0700812 for (int i = 0; i < op_num; i++) {
813 if (ops[i].op == GRPC_OP_SEND_MESSAGE) {
814 grpc_byte_buffer_destroy(ops[i].data.send_message);
815 }
816 if (ops[i].op == GRPC_OP_RECV_MESSAGE) {
817 grpc_byte_buffer_destroy(message);
818 }
819 }
thinkeroua3730b72016-07-20 16:59:54 +0800820#if PHP_MAJOR_VERSION < 7
murgatroid99afd541c2015-03-03 18:16:09 -0800821 RETURN_DESTROY_ZVAL(result);
thinkeroua3730b72016-07-20 16:59:54 +0800822#else
823 RETURN_DESTROY_ZVAL(return_value);
824#endif
mlumishb892a272014-12-09 16:28:23 -0800825}
826
murgatroid99c1da8f22015-03-25 11:33:05 -0700827/**
Stanley Cheungdb98e082015-07-27 10:19:45 -0700828 * Get the endpoint this call/stream is connected to
829 * @return string The URI of the endpoint
830 */
831PHP_METHOD(Call, getPeer) {
thinkeroua3730b72016-07-20 16:59:54 +0800832#if PHP_MAJOR_VERSION < 7
Stanley Cheungdb98e082015-07-27 10:19:45 -0700833 wrapped_grpc_call *call =
834 (wrapped_grpc_call *)zend_object_store_get_object(getThis() TSRMLS_CC);
835 RETURN_STRING(grpc_call_get_peer(call->wrapped), 1);
thinkeroua3730b72016-07-20 16:59:54 +0800836#else
837 wrapped_grpc_call *call = Z_WRAPPED_GRPC_CALL_P(getThis());
838 RETURN_STRING(grpc_call_get_peer(call->wrapped));
839#endif
Stanley Cheungdb98e082015-07-27 10:19:45 -0700840}
841
842/**
murgatroid99c1da8f22015-03-25 11:33:05 -0700843 * Cancel the call. This will cause the call to end with STATUS_CANCELLED if it
844 * has not already ended with another status.
845 */
846PHP_METHOD(Call, cancel) {
thinkeroua3730b72016-07-20 16:59:54 +0800847#if PHP_MAJOR_VERSION < 7
murgatroid99c1da8f22015-03-25 11:33:05 -0700848 wrapped_grpc_call *call =
849 (wrapped_grpc_call *)zend_object_store_get_object(getThis() TSRMLS_CC);
thinkeroua3730b72016-07-20 16:59:54 +0800850#else
851 wrapped_grpc_call *call = Z_WRAPPED_GRPC_CALL_P(getThis());
852#endif
Nicolas "Pixel" Noble150b7c42015-08-01 01:15:10 +0200853 grpc_call_cancel(call->wrapped, NULL);
murgatroid99c1da8f22015-03-25 11:33:05 -0700854}
855
Stanley Cheung35805802015-12-10 11:42:55 -0800856/**
857 * Set the CallCredentials for this call.
858 * @param CallCredentials creds_obj The CallCredentials object
859 * @param int The error code
860 */
861PHP_METHOD(Call, setCredentials) {
862 zval *creds_obj;
863
864 /* "O" == 1 Object */
865 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &creds_obj,
866 grpc_ce_call_credentials) == FAILURE) {
867 zend_throw_exception(spl_ce_InvalidArgumentException,
868 "setCredentials expects 1 CallCredentials",
869 1 TSRMLS_CC);
870 return;
871 }
872
thinkeroua3730b72016-07-20 16:59:54 +0800873 wrapped_grpc_call_credentials *creds =
874 Z_WRAPPED_GRPC_CALL_CREDS_P(creds_obj);
875 wrapped_grpc_call *call = Z_WRAPPED_GRPC_CALL_P(getThis());
Stanley Cheung35805802015-12-10 11:42:55 -0800876
877 grpc_call_error error = GRPC_CALL_ERROR;
878 error = grpc_call_set_credentials(call->wrapped, creds->wrapped);
879 RETURN_LONG(error);
880}
881
mlumishb892a272014-12-09 16:28:23 -0800882static zend_function_entry call_methods[] = {
thinkeroua3730b72016-07-20 16:59:54 +0800883 PHP_ME(Call, __construct, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
884 PHP_ME(Call, startBatch, NULL, ZEND_ACC_PUBLIC)
885 PHP_ME(Call, getPeer, NULL, ZEND_ACC_PUBLIC)
886 PHP_ME(Call, cancel, NULL, ZEND_ACC_PUBLIC)
887 PHP_ME(Call, setCredentials, NULL, ZEND_ACC_PUBLIC)
888 PHP_FE_END
889};
mlumishb892a272014-12-09 16:28:23 -0800890
Craig Tillerb5dcec52015-01-13 11:13:42 -0800891void grpc_init_call(TSRMLS_D) {
mlumishb892a272014-12-09 16:28:23 -0800892 zend_class_entry ce;
893 INIT_CLASS_ENTRY(ce, "Grpc\\Call", call_methods);
894 ce.create_object = create_wrapped_grpc_call;
895 grpc_ce_call = zend_register_internal_class(&ce TSRMLS_CC);
thinkeroua3730b72016-07-20 16:59:54 +0800896#if PHP_MAJOR_VERSION >= 7
897 memcpy(&call_ce_handlers, zend_get_std_object_handlers(),
898 sizeof(zend_object_handlers));
899 call_ce_handlers.offset = XtOffsetOf(wrapped_grpc_call, std);
900 call_ce_handlers.free_obj = free_wrapped_grpc_call;
901#endif
mlumishb892a272014-12-09 16:28:23 -0800902}