blob: d455002de6d287c9bb7666f01e379b03e5930cac [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
murgatroid995ca9f922015-02-03 11:21:11 -0800383 zval **value;
murgatroid99afd541c2015-03-03 18:16:09 -0800384 zval **inner_value;
murgatroid995ca9f922015-02-03 11:21:11 -0800385 HashPosition array_pointer;
Stanley Cheung3ab8e792015-08-24 16:58:42 -0700386 zval **message_value;
387 zval **message_flags;
murgatroid995ca9f922015-02-03 11:21:11 -0800388 char *key;
389 uint key_len;
390 ulong index;
thinkeroua3730b72016-07-20 16:59:54 +0800391 zval *result;
392 zval *recv_status;
393 MAKE_STD_ZVAL(result);
394 object_init(result);
395#else
thinkeroua3730b72016-07-20 16:59:54 +0800396 zval *value;
397 zval *inner_value;
398 zval *message_value;
399 zval *message_flags;
400 zend_string *key;
401 zend_ulong index;
402 zval recv_status;
403 object_init(return_value);
404#endif
thinkeroub9c7f3a2016-07-22 09:22:41 +0800405 wrapped_grpc_call *call = Z_WRAPPED_GRPC_CALL_P(getThis());
thinkeroua3730b72016-07-20 16:59:54 +0800406
407 grpc_op ops[8];
408 size_t op_num = 0;
409 zval *array;
410 HashTable *array_hash;
411 HashTable *status_hash;
412 HashTable *message_hash;
413
murgatroid99afd541c2015-03-03 18:16:09 -0800414 grpc_metadata_array metadata;
415 grpc_metadata_array trailing_metadata;
416 grpc_metadata_array recv_metadata;
417 grpc_metadata_array recv_trailing_metadata;
418 grpc_status_code status;
419 char *status_details = NULL;
murgatroid999fe516a2015-03-11 14:47:10 -0700420 size_t status_details_capacity = 0;
murgatroid99afd541c2015-03-03 18:16:09 -0800421 grpc_byte_buffer *message;
422 int cancelled;
423 grpc_call_error error;
murgatroid99afd541c2015-03-03 18:16:09 -0800424 char *message_str;
425 size_t message_len;
thinkeroua3730b72016-07-20 16:59:54 +0800426
427
murgatroid99afd541c2015-03-03 18:16:09 -0800428 grpc_metadata_array_init(&metadata);
429 grpc_metadata_array_init(&trailing_metadata);
murgatroid99d8bb9572015-03-11 09:18:06 -0700430 grpc_metadata_array_init(&recv_metadata);
431 grpc_metadata_array_init(&recv_trailing_metadata);
David Garcia Quintasa301eaa2016-05-06 16:59:03 -0700432 memset(ops, 0, sizeof(ops));
thinkeroua3730b72016-07-20 16:59:54 +0800433
murgatroid99afd541c2015-03-03 18:16:09 -0800434 /* "a" == 1 array */
435 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &array) ==
Craig Tillerb5dcec52015-01-13 11:13:42 -0800436 FAILURE) {
mlumishb892a272014-12-09 16:28:23 -0800437 zend_throw_exception(spl_ce_InvalidArgumentException,
murgatroid99afd541c2015-03-03 18:16:09 -0800438 "start_batch expects an array", 1 TSRMLS_CC);
439 goto cleanup;
mlumishb892a272014-12-09 16:28:23 -0800440 }
thinkeroua3730b72016-07-20 16:59:54 +0800441
442#if PHP_MAJOR_VERSION < 7
443
mlumishb892a272014-12-09 16:28:23 -0800444 array_hash = Z_ARRVAL_P(array);
murgatroid995ca9f922015-02-03 11:21:11 -0800445 for (zend_hash_internal_pointer_reset_ex(array_hash, &array_pointer);
murgatroid99afd541c2015-03-03 18:16:09 -0800446 zend_hash_get_current_data_ex(array_hash, (void**)&value,
murgatroid995ca9f922015-02-03 11:21:11 -0800447 &array_pointer) == SUCCESS;
448 zend_hash_move_forward_ex(array_hash, &array_pointer)) {
449 if (zend_hash_get_current_key_ex(array_hash, &key, &key_len, &index, 0,
murgatroid99afd541c2015-03-03 18:16:09 -0800450 &array_pointer) != HASH_KEY_IS_LONG) {
murgatroid995ca9f922015-02-03 11:21:11 -0800451 zend_throw_exception(spl_ce_InvalidArgumentException,
murgatroid99afd541c2015-03-03 18:16:09 -0800452 "batch keys must be integers", 1 TSRMLS_CC);
453 goto cleanup;
murgatroid995ca9f922015-02-03 11:21:11 -0800454 }
murgatroid99afd541c2015-03-03 18:16:09 -0800455 switch(index) {
thinkeroua3730b72016-07-20 16:59:54 +0800456 case GRPC_OP_SEND_INITIAL_METADATA:
457 if (!create_metadata_array(*value, &metadata)) {
murgatroid995ca9f922015-02-03 11:21:11 -0800458 zend_throw_exception(spl_ce_InvalidArgumentException,
thinkeroua3730b72016-07-20 16:59:54 +0800459 "Bad metadata value given", 1 TSRMLS_CC);
murgatroid99afd541c2015-03-03 18:16:09 -0800460 goto cleanup;
thinkeroua3730b72016-07-20 16:59:54 +0800461 }
462 ops[op_num].data.send_initial_metadata.count =
463 metadata.count;
464 ops[op_num].data.send_initial_metadata.metadata =
465 metadata.metadata;
466 break;
467 case GRPC_OP_SEND_MESSAGE:
468 if (Z_TYPE_PP(value) != IS_ARRAY) {
469 zend_throw_exception(spl_ce_InvalidArgumentException,
470 "Expected an array for send message",
471 1 TSRMLS_CC);
472 goto cleanup;
473 }
474 message_hash = Z_ARRVAL_PP(value);
475 if (zend_hash_find(message_hash, "flags", sizeof("flags"),
476 (void **)&message_flags) == SUCCESS) {
477 if (Z_TYPE_PP(message_flags) != IS_LONG) {
478 zend_throw_exception(spl_ce_InvalidArgumentException,
479 "Expected an int for message flags",
480 1 TSRMLS_CC);
481 }
482 ops[op_num].flags = Z_LVAL_PP(message_flags) & GRPC_WRITE_USED_MASK;
483 }
484 if (zend_hash_find(message_hash, "message", sizeof("message"),
485 (void **)&message_value) != SUCCESS ||
486 Z_TYPE_PP(message_value) != IS_STRING) {
487 zend_throw_exception(spl_ce_InvalidArgumentException,
488 "Expected a string for send message",
489 1 TSRMLS_CC);
490 goto cleanup;
491 }
492 ops[op_num].data.send_message =
493 string_to_byte_buffer(Z_STRVAL_PP(message_value),
494 Z_STRLEN_PP(message_value));
495 break;
496 case GRPC_OP_SEND_CLOSE_FROM_CLIENT:
497 break;
498 case GRPC_OP_SEND_STATUS_FROM_SERVER:
499 status_hash = Z_ARRVAL_PP(value);
500 if (zend_hash_find(status_hash, "metadata", sizeof("metadata"),
501 (void **)&inner_value) == SUCCESS) {
502 if (!create_metadata_array(*inner_value, &trailing_metadata)) {
503 zend_throw_exception(spl_ce_InvalidArgumentException,
504 "Bad trailing metadata value given",
505 1 TSRMLS_CC);
506 goto cleanup;
507 }
508 ops[op_num].data.send_status_from_server.trailing_metadata =
509 trailing_metadata.metadata;
510 ops[op_num].data.send_status_from_server.trailing_metadata_count =
511 trailing_metadata.count;
512 }
513 if (zend_hash_find(status_hash, "code", sizeof("code"),
514 (void**)&inner_value) == SUCCESS) {
515 if (Z_TYPE_PP(inner_value) != IS_LONG) {
516 zend_throw_exception(spl_ce_InvalidArgumentException,
517 "Status code must be an integer",
518 1 TSRMLS_CC);
519 goto cleanup;
520 }
521 ops[op_num].data.send_status_from_server.status =
522 Z_LVAL_PP(inner_value);
523 } else {
524 zend_throw_exception(spl_ce_InvalidArgumentException,
525 "Integer status code is required",
526 1 TSRMLS_CC);
527 goto cleanup;
528 }
529 if (zend_hash_find(status_hash, "details", sizeof("details"),
530 (void**)&inner_value) == SUCCESS) {
531 if (Z_TYPE_PP(inner_value) != IS_STRING) {
532 zend_throw_exception(spl_ce_InvalidArgumentException,
533 "Status details must be a string",
534 1 TSRMLS_CC);
535 goto cleanup;
536 }
537 ops[op_num].data.send_status_from_server.status_details =
538 Z_STRVAL_PP(inner_value);
539 } else {
540 zend_throw_exception(spl_ce_InvalidArgumentException,
541 "String status details is required",
542 1 TSRMLS_CC);
543 goto cleanup;
544 }
545 break;
546 case GRPC_OP_RECV_INITIAL_METADATA:
547 ops[op_num].data.recv_initial_metadata = &recv_metadata;
548 break;
549 case GRPC_OP_RECV_MESSAGE:
550 ops[op_num].data.recv_message = &message;
551 break;
552 case GRPC_OP_RECV_STATUS_ON_CLIENT:
553 ops[op_num].data.recv_status_on_client.trailing_metadata =
554 &recv_trailing_metadata;
555 ops[op_num].data.recv_status_on_client.status = &status;
556 ops[op_num].data.recv_status_on_client.status_details =
557 &status_details;
558 ops[op_num].data.recv_status_on_client.status_details_capacity =
559 &status_details_capacity;
560 break;
561 case GRPC_OP_RECV_CLOSE_ON_SERVER:
562 ops[op_num].data.recv_close_on_server.cancelled = &cancelled;
563 break;
564 default:
565 zend_throw_exception(spl_ce_InvalidArgumentException,
566 "Unrecognized key in batch", 1 TSRMLS_CC);
567 goto cleanup;
murgatroid99afd541c2015-03-03 18:16:09 -0800568 }
569 ops[op_num].op = (grpc_op_type)index;
David Garcia Quintasba710e52015-06-15 13:31:15 -0700570 ops[op_num].flags = 0;
Craig Tiller42758992015-08-18 10:34:32 -0700571 ops[op_num].reserved = NULL;
murgatroid99afd541c2015-03-03 18:16:09 -0800572 op_num++;
573 }
thinkeroua3730b72016-07-20 16:59:54 +0800574
575#else
576
577array_hash = HASH_OF(array);
578 ZEND_HASH_FOREACH_KEY_VAL(array_hash, index, key, value) {
579 if (key) {
580 zend_throw_exception(spl_ce_InvalidArgumentException,
581 "batch keys must be integers", 1);
582 goto cleanup;
583 }
584
585 switch(index) {
586 case GRPC_OP_SEND_INITIAL_METADATA:
587 if (!create_metadata_array(value, &metadata)) {
588 zend_throw_exception(spl_ce_InvalidArgumentException,
589 "Bad metadata value given", 1);
590 goto cleanup;
591 }
592 ops[op_num].data.send_initial_metadata.count = metadata.count;
593 ops[op_num].data.send_initial_metadata.metadata = metadata.metadata;
594 break;
595 case GRPC_OP_SEND_MESSAGE:
596 if (Z_TYPE_P(value) != IS_ARRAY) {
597 zend_throw_exception(spl_ce_InvalidArgumentException,
598 "Expected an array for send message", 1);
599 goto cleanup;
600 }
601 message_hash = HASH_OF(value);
602 if ((message_flags =
603 zend_hash_str_find(message_hash, "flags",
604 sizeof("flags") - 1)) != NULL) {
605 if (Z_TYPE_P(message_flags) != IS_LONG) {
606 zend_throw_exception(spl_ce_InvalidArgumentException,
607 "Expected an int for message flags", 1);
608 }
609 ops[op_num].flags = Z_LVAL_P(message_flags) & GRPC_WRITE_USED_MASK;
610 }
611 if ((message_value = zend_hash_str_find(message_hash, "message",
612 sizeof("message") - 1))
613 == NULL || Z_TYPE_P(message_value) != IS_STRING) {
614 zend_throw_exception(spl_ce_InvalidArgumentException,
615 "Expected a string for send message", 1);
616 goto cleanup;
617 }
618 ops[op_num].data.send_message =
619 string_to_byte_buffer(Z_STRVAL_P(message_value),
620 Z_STRLEN_P(message_value));
621 break;
622 case GRPC_OP_SEND_CLOSE_FROM_CLIENT:
623 break;
624 case GRPC_OP_SEND_STATUS_FROM_SERVER:
625 status_hash = HASH_OF(value);
626 if ((inner_value = zend_hash_str_find(status_hash, "metadata",
627 sizeof("metadata") - 1))
628 != NULL) {
629 if (!create_metadata_array(inner_value, &trailing_metadata)) {
630 zend_throw_exception(spl_ce_InvalidArgumentException,
631 "Bad trailing metadata value given", 1);
632 goto cleanup;
633 }
634 ops[op_num].data.send_status_from_server.trailing_metadata =
635 trailing_metadata.metadata;
636 ops[op_num].data.send_status_from_server.trailing_metadata_count =
637 trailing_metadata.count;
638 }
639 if ((inner_value = zend_hash_str_find(status_hash, "code",
640 sizeof("code") - 1)) != NULL) {
641 if (Z_TYPE_P(inner_value) != IS_LONG) {
642 zend_throw_exception(spl_ce_InvalidArgumentException,
643 "Status code must be an integer", 1);
644 goto cleanup;
645 }
646 ops[op_num].data.send_status_from_server.status =
647 Z_LVAL_P(inner_value);
648 } else {
649 zend_throw_exception(spl_ce_InvalidArgumentException,
650 "Integer status code is required", 1);
651 goto cleanup;
652 }
653 if ((inner_value = zend_hash_str_find(status_hash, "details",
654 sizeof("details") - 1)) != NULL) {
655 if (Z_TYPE_P(inner_value) != IS_STRING) {
656 zend_throw_exception(spl_ce_InvalidArgumentException,
657 "Status details must be a string", 1);
658 goto cleanup;
659 }
660 ops[op_num].data.send_status_from_server.status_details =
661 Z_STRVAL_P(inner_value);
662 } else {
663 zend_throw_exception(spl_ce_InvalidArgumentException,
664 "String status details is required", 1);
665 goto cleanup;
666 }
667 break;
668 case GRPC_OP_RECV_INITIAL_METADATA:
669 ops[op_num].data.recv_initial_metadata = &recv_metadata;
670 break;
671 case GRPC_OP_RECV_MESSAGE:
672 ops[op_num].data.recv_message = &message;
673 break;
674 case GRPC_OP_RECV_STATUS_ON_CLIENT:
675 ops[op_num].data.recv_status_on_client.trailing_metadata =
676 &recv_trailing_metadata;
677 ops[op_num].data.recv_status_on_client.status = &status;
678 ops[op_num].data.recv_status_on_client.status_details =
679 &status_details;
680 ops[op_num].data.recv_status_on_client.status_details_capacity =
681 &status_details_capacity;
682 break;
683 case GRPC_OP_RECV_CLOSE_ON_SERVER:
684 ops[op_num].data.recv_close_on_server.cancelled = &cancelled;
685 break;
686 default:
687 zend_throw_exception(spl_ce_InvalidArgumentException,
688 "Unrecognized key in batch", 1);
689 goto cleanup;
690 }
691 ops[op_num].op = (grpc_op_type)index;
692 ops[op_num].flags = 0;
693 ops[op_num].reserved = NULL;
694 op_num++;
695 }
696 ZEND_HASH_FOREACH_END();
697
698#endif
699
Nicolas "Pixel" Noble150b7c42015-08-01 01:15:10 +0200700 error = grpc_call_start_batch(call->wrapped, ops, op_num, call->wrapped,
701 NULL);
murgatroid99afd541c2015-03-03 18:16:09 -0800702 if (error != GRPC_CALL_OK) {
703 zend_throw_exception(spl_ce_LogicException,
704 "start_batch was called incorrectly",
705 (long)error TSRMLS_CC);
706 goto cleanup;
707 }
Stanley Cheungc0c9ba92015-08-18 16:19:38 -0700708 grpc_completion_queue_pluck(completion_queue, call->wrapped,
709 gpr_inf_future(GPR_CLOCK_REALTIME), NULL);
thinkeroua3730b72016-07-20 16:59:54 +0800710#if PHP_MAJOR_VERSION < 7
murgatroid99afd541c2015-03-03 18:16:09 -0800711 for (int i = 0; i < op_num; i++) {
712 switch(ops[i].op) {
thinkeroua3730b72016-07-20 16:59:54 +0800713 case GRPC_OP_SEND_INITIAL_METADATA:
714 add_property_bool(result, "send_metadata", true);
715 break;
716 case GRPC_OP_SEND_MESSAGE:
717 add_property_bool(result, "send_message", true);
718 break;
719 case GRPC_OP_SEND_CLOSE_FROM_CLIENT:
720 add_property_bool(result, "send_close", true);
721 break;
722 case GRPC_OP_SEND_STATUS_FROM_SERVER:
723 add_property_bool(result, "send_status", true);
724 break;
725 case GRPC_OP_RECV_INITIAL_METADATA:
726 array = grpc_parse_metadata_array(&recv_metadata TSRMLS_CC);
727 add_property_zval(result, "metadata", array);
728 Z_DELREF_P(array);
729 break;
730 case GRPC_OP_RECV_MESSAGE:
731 byte_buffer_to_string(message, &message_str, &message_len);
732 if (message_str == NULL) {
733 add_property_null(result, "message");
734 } else {
735 add_property_stringl(result, "message", message_str, message_len,
736 false);
737 }
738 break;
739 case GRPC_OP_RECV_STATUS_ON_CLIENT:
740 MAKE_STD_ZVAL(recv_status);
741 object_init(recv_status);
742 array = grpc_parse_metadata_array(&recv_trailing_metadata TSRMLS_CC);
743 add_property_zval(recv_status, "metadata", array);
744 Z_DELREF_P(array);
745 add_property_long(recv_status, "code", status);
746 add_property_string(recv_status, "details", status_details, true);
747 add_property_zval(result, "status", recv_status);
748 Z_DELREF_P(recv_status);
749 break;
750 case GRPC_OP_RECV_CLOSE_ON_SERVER:
751 add_property_bool(result, "cancelled", cancelled);
752 break;
753 default:
754 break;
murgatroid995ca9f922015-02-03 11:21:11 -0800755 }
756 }
thinkeroua3730b72016-07-20 16:59:54 +0800757#else
758 for (int i = 0; i < op_num; i++) {
759 switch(ops[i].op) {
760 case GRPC_OP_SEND_INITIAL_METADATA:
761 add_property_bool(return_value, "send_metadata", true);
762 break;
763 case GRPC_OP_SEND_MESSAGE:
764 add_property_bool(return_value, "send_message", true);
765 break;
766 case GRPC_OP_SEND_CLOSE_FROM_CLIENT:
767 add_property_bool(return_value, "send_close", true);
768 break;
769 case GRPC_OP_SEND_STATUS_FROM_SERVER:
770 add_property_bool(return_value, "send_status", true);
771 break;
772 case GRPC_OP_RECV_INITIAL_METADATA:
773 grpc_parse_metadata_array(&recv_metadata, array);
774 add_property_zval(return_value, "metadata", array);
775 break;
776 case GRPC_OP_RECV_MESSAGE:
777 byte_buffer_to_string(message, &message_str, &message_len);
778 if (message_str == NULL) {
779 add_property_null(return_value, "message");
780 } else {
781 add_property_stringl(return_value, "message", message_str,
782 message_len);
783 }
784 break;
785 case GRPC_OP_RECV_STATUS_ON_CLIENT:
786 object_init(&recv_status);
787 grpc_parse_metadata_array(&recv_trailing_metadata, array);
788 add_property_zval(&recv_status, "metadata", array);
789 add_property_long(&recv_status, "code", status);
790 add_property_string(&recv_status, "details", status_details);
791 add_property_zval(return_value, "status", &recv_status);
792 break;
793 case GRPC_OP_RECV_CLOSE_ON_SERVER:
794 add_property_bool(return_value, "cancelled", cancelled);
795 break;
796 default:
797 break;
798 }
799 }
800#endif
801
murgatroid99afd541c2015-03-03 18:16:09 -0800802cleanup:
murgatroid99afd541c2015-03-03 18:16:09 -0800803 grpc_metadata_array_destroy(&metadata);
murgatroid99afd541c2015-03-03 18:16:09 -0800804 grpc_metadata_array_destroy(&trailing_metadata);
805 grpc_metadata_array_destroy(&recv_metadata);
806 grpc_metadata_array_destroy(&recv_trailing_metadata);
807 if (status_details != NULL) {
808 gpr_free(status_details);
mlumishdba87892015-01-02 13:27:28 -0800809 }
Stanley Cheung82e6f322016-04-06 11:51:57 -0700810 for (int i = 0; i < op_num; i++) {
811 if (ops[i].op == GRPC_OP_SEND_MESSAGE) {
812 grpc_byte_buffer_destroy(ops[i].data.send_message);
813 }
814 if (ops[i].op == GRPC_OP_RECV_MESSAGE) {
815 grpc_byte_buffer_destroy(message);
816 }
817 }
thinkeroua3730b72016-07-20 16:59:54 +0800818#if PHP_MAJOR_VERSION < 7
murgatroid99afd541c2015-03-03 18:16:09 -0800819 RETURN_DESTROY_ZVAL(result);
thinkeroua3730b72016-07-20 16:59:54 +0800820#else
821 RETURN_DESTROY_ZVAL(return_value);
822#endif
mlumishb892a272014-12-09 16:28:23 -0800823}
824
murgatroid99c1da8f22015-03-25 11:33:05 -0700825/**
Stanley Cheungdb98e082015-07-27 10:19:45 -0700826 * Get the endpoint this call/stream is connected to
827 * @return string The URI of the endpoint
828 */
829PHP_METHOD(Call, getPeer) {
thinkeroua3730b72016-07-20 16:59:54 +0800830 wrapped_grpc_call *call = Z_WRAPPED_GRPC_CALL_P(getThis());
thinkeroub9c7f3a2016-07-22 09:22:41 +0800831 PHP_GRPC_RETURN_STRING(grpc_call_get_peer(call->wrapped), 1);
Stanley Cheungdb98e082015-07-27 10:19:45 -0700832}
833
834/**
murgatroid99c1da8f22015-03-25 11:33:05 -0700835 * Cancel the call. This will cause the call to end with STATUS_CANCELLED if it
836 * has not already ended with another status.
837 */
838PHP_METHOD(Call, cancel) {
thinkeroua3730b72016-07-20 16:59:54 +0800839 wrapped_grpc_call *call = Z_WRAPPED_GRPC_CALL_P(getThis());
Nicolas "Pixel" Noble150b7c42015-08-01 01:15:10 +0200840 grpc_call_cancel(call->wrapped, NULL);
murgatroid99c1da8f22015-03-25 11:33:05 -0700841}
842
Stanley Cheung35805802015-12-10 11:42:55 -0800843/**
844 * Set the CallCredentials for this call.
845 * @param CallCredentials creds_obj The CallCredentials object
846 * @param int The error code
847 */
848PHP_METHOD(Call, setCredentials) {
849 zval *creds_obj;
850
851 /* "O" == 1 Object */
852 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &creds_obj,
853 grpc_ce_call_credentials) == FAILURE) {
854 zend_throw_exception(spl_ce_InvalidArgumentException,
855 "setCredentials expects 1 CallCredentials",
856 1 TSRMLS_CC);
857 return;
858 }
859
thinkeroua3730b72016-07-20 16:59:54 +0800860 wrapped_grpc_call_credentials *creds =
861 Z_WRAPPED_GRPC_CALL_CREDS_P(creds_obj);
862 wrapped_grpc_call *call = Z_WRAPPED_GRPC_CALL_P(getThis());
Stanley Cheung35805802015-12-10 11:42:55 -0800863
864 grpc_call_error error = GRPC_CALL_ERROR;
865 error = grpc_call_set_credentials(call->wrapped, creds->wrapped);
866 RETURN_LONG(error);
867}
868
mlumishb892a272014-12-09 16:28:23 -0800869static zend_function_entry call_methods[] = {
thinkeroua3730b72016-07-20 16:59:54 +0800870 PHP_ME(Call, __construct, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
871 PHP_ME(Call, startBatch, NULL, ZEND_ACC_PUBLIC)
872 PHP_ME(Call, getPeer, NULL, ZEND_ACC_PUBLIC)
873 PHP_ME(Call, cancel, NULL, ZEND_ACC_PUBLIC)
874 PHP_ME(Call, setCredentials, NULL, ZEND_ACC_PUBLIC)
875 PHP_FE_END
876};
mlumishb892a272014-12-09 16:28:23 -0800877
Craig Tillerb5dcec52015-01-13 11:13:42 -0800878void grpc_init_call(TSRMLS_D) {
mlumishb892a272014-12-09 16:28:23 -0800879 zend_class_entry ce;
880 INIT_CLASS_ENTRY(ce, "Grpc\\Call", call_methods);
881 ce.create_object = create_wrapped_grpc_call;
882 grpc_ce_call = zend_register_internal_class(&ce TSRMLS_CC);
thinkeroua3730b72016-07-20 16:59:54 +0800883#if PHP_MAJOR_VERSION >= 7
884 memcpy(&call_ce_handlers, zend_get_std_object_handlers(),
885 sizeof(zend_object_handlers));
886 call_ce_handlers.offset = XtOffsetOf(wrapped_grpc_call, std);
887 call_ce_handlers.free_obj = free_wrapped_grpc_call;
888#endif
mlumishb892a272014-12-09 16:28:23 -0800889}