3 * Copyright 2015 gRPC authors.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
21 * @see https://github.com/grpc/grpc/tree/master/src/php/ext/grpc/call.c
26 #include <ext/spl/spl_exceptions.h>
27 #include <zend_exceptions.h>
29 #include <grpc/support/alloc.h>
31 #include "call_credentials.h"
32 #include "completion_queue.h"
35 #include "byte_buffer.h"
37 zend_class_entry *grpc_ce_call;
38 PHP_GRPC_DECLARE_OBJECT_HANDLER(call_ce_handlers)
40 /* Frees and destroys an instance of wrapped_grpc_call */
41 PHP_GRPC_FREE_WRAPPED_FUNC_START(wrapped_grpc_call)
42 if (p->owned && p->wrapped != NULL) {
43 grpc_call_unref(p->wrapped);
45 PHP_GRPC_FREE_WRAPPED_FUNC_END()
47 /* Initializes an instance of wrapped_grpc_call to be associated with an
48 * object of a class specified by class_type */
49 php_grpc_zend_object create_wrapped_grpc_call(zend_class_entry *class_type
51 PHP_GRPC_ALLOC_CLASS_OBJECT(wrapped_grpc_call);
52 zend_object_std_init(&intern->std, class_type TSRMLS_CC);
53 object_properties_init(&intern->std, class_type);
54 PHP_GRPC_FREE_CLASS_OBJECT(wrapped_grpc_call, call_ce_handlers);
57 /* Creates and returns a PHP array object with the data in a
58 * grpc_metadata_array. Returns NULL on failure */
59 zval *grpc_parse_metadata_array(grpc_metadata_array
60 *metadata_array TSRMLS_DC) {
61 int count = metadata_array->count;
62 grpc_metadata *elements = metadata_array->metadata;
64 PHP_GRPC_MAKE_STD_ZVAL(array);
67 HashTable *array_hash;
74 array_hash = Z_ARRVAL_P(array);
76 for (i = 0; i < count; i++) {
78 key_len = GRPC_SLICE_LENGTH(elem->key);
79 str_key = ecalloc(key_len + 1, sizeof(char));
80 memcpy(str_key, GRPC_SLICE_START_PTR(elem->key), key_len);
81 str_val = ecalloc(GRPC_SLICE_LENGTH(elem->value) + 1, sizeof(char));
82 memcpy(str_val, GRPC_SLICE_START_PTR(elem->value),
83 GRPC_SLICE_LENGTH(elem->value));
84 if (php_grpc_zend_hash_find(array_hash, str_key, key_len, (void **)&data)
86 if (Z_TYPE_P(data) != IS_ARRAY) {
87 zend_throw_exception(zend_exception_get_default(TSRMLS_C),
88 "Metadata hash somehow contains wrong types.",
92 PHP_GRPC_FREE_STD_ZVAL(array);
95 php_grpc_add_next_index_stringl(data, str_val,
96 GRPC_SLICE_LENGTH(elem->value),
99 PHP_GRPC_MAKE_STD_ZVAL(inner_array);
100 array_init(inner_array);
101 php_grpc_add_next_index_stringl(inner_array, str_val,
102 GRPC_SLICE_LENGTH(elem->value), false);
103 add_assoc_zval(array, str_key, inner_array);
104 PHP_GRPC_FREE_STD_ZVAL(inner_array);
107 #if PHP_MAJOR_VERSION >= 7
114 /* Populates a grpc_metadata_array with the data in a PHP array object.
115 Returns true on success and false on failure */
116 bool create_metadata_array(zval *array, grpc_metadata_array *metadata) {
117 HashTable *array_hash;
118 HashTable *inner_array_hash;
121 grpc_metadata_array_init(metadata);
123 metadata->metadata = NULL;
124 if (Z_TYPE_P(array) != IS_ARRAY) {
127 array_hash = Z_ARRVAL_P(array);
131 PHP_GRPC_HASH_FOREACH_STR_KEY_VAL_START(array_hash, key, key_type,
133 if (key_type != HASH_KEY_IS_STRING || key == NULL) {
136 if (Z_TYPE_P(inner_array) != IS_ARRAY) {
139 inner_array_hash = Z_ARRVAL_P(inner_array);
140 metadata->capacity += zend_hash_num_elements(inner_array_hash);
141 PHP_GRPC_HASH_FOREACH_END()
143 metadata->metadata = gpr_malloc(metadata->capacity * sizeof(grpc_metadata));
147 PHP_GRPC_HASH_FOREACH_STR_KEY_VAL_START(array_hash, key1, key_type1,
149 if (key_type1 != HASH_KEY_IS_STRING) {
152 if (!grpc_header_key_is_legal(grpc_slice_from_static_string(key1))) {
155 inner_array_hash = Z_ARRVAL_P(inner_array);
156 PHP_GRPC_HASH_FOREACH_VAL_START(inner_array_hash, value)
157 if (Z_TYPE_P(value) != IS_STRING) {
160 metadata->metadata[metadata->count].key =
161 grpc_slice_from_copied_string(key1);
162 metadata->metadata[metadata->count].value =
163 grpc_slice_from_copied_buffer(Z_STRVAL_P(value), Z_STRLEN_P(value));
164 metadata->count += 1;
165 PHP_GRPC_HASH_FOREACH_END()
166 PHP_GRPC_HASH_FOREACH_END()
170 void grpc_php_metadata_array_destroy_including_entries(
171 grpc_metadata_array* array) {
173 if (array->metadata) {
174 for (i = 0; i < array->count; i++) {
175 grpc_slice_unref(array->metadata[i].key);
176 grpc_slice_unref(array->metadata[i].value);
179 grpc_metadata_array_destroy(array);
182 /* Wraps a grpc_call struct in a PHP object. Owned indicates whether the
183 struct should be destroyed at the end of the object's lifecycle */
184 zval *grpc_php_wrap_call(grpc_call *wrapped, bool owned TSRMLS_DC) {
186 PHP_GRPC_MAKE_STD_ZVAL(call_object);
187 object_init_ex(call_object, grpc_ce_call);
188 wrapped_grpc_call *call = PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_call,
190 call->wrapped = wrapped;
196 * Constructs a new instance of the Call class.
197 * @param Channel $channel_obj The channel to associate the call with.
198 * Must not be closed.
199 * @param string $method The method to call
200 * @param Timeval $deadline_obj The deadline for completing the call
201 * @param string $host_override = "" The host is set by user (optional)
203 PHP_METHOD(Call, __construct) {
206 php_grpc_int method_len;
208 char *host_override = NULL;
209 php_grpc_int host_override_len = 0;
210 wrapped_grpc_call *call = PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_call,
213 /* "OsO|s" == 1 Object, 1 string, 1 Object, 1 optional string */
214 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "OsO|s", &channel_obj,
215 grpc_ce_channel, &method, &method_len,
216 &deadline_obj, grpc_ce_timeval, &host_override,
217 &host_override_len) == FAILURE) {
218 zend_throw_exception(spl_ce_InvalidArgumentException,
219 "Call expects a Channel, a String, a Timeval and "
220 "an optional String", 1 TSRMLS_CC);
223 wrapped_grpc_channel *channel =
224 PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_channel, channel_obj);
225 if (channel->wrapper == NULL) {
226 zend_throw_exception(spl_ce_InvalidArgumentException,
227 "Call cannot be constructed from a closed Channel",
231 gpr_mu_lock(&channel->wrapper->mu);
232 if (channel->wrapper == NULL || channel->wrapper->wrapped == NULL) {
233 zend_throw_exception(spl_ce_InvalidArgumentException,
234 "Call cannot be constructed from a closed Channel",
236 gpr_mu_unlock(&channel->wrapper->mu);
239 add_property_zval(getThis(), "channel", channel_obj);
240 wrapped_grpc_timeval *deadline =
241 PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_timeval, deadline_obj);
242 grpc_slice method_slice = grpc_slice_from_copied_string(method);
243 grpc_slice host_slice = host_override != NULL ?
244 grpc_slice_from_copied_string(host_override) : grpc_empty_slice();
246 grpc_channel_create_call(channel->wrapper->wrapped, NULL,
247 GRPC_PROPAGATE_DEFAULTS,
248 completion_queue, method_slice,
249 host_override != NULL ? &host_slice : NULL,
250 deadline->wrapped, NULL);
251 grpc_slice_unref(method_slice);
252 grpc_slice_unref(host_slice);
254 call->channel = channel;
255 gpr_mu_unlock(&channel->wrapper->mu);
259 * Start a batch of RPC actions.
260 * @param array $array Array of actions to take
261 * @return object Object with results of all actions
263 PHP_METHOD(Call, startBatch) {
265 PHP_GRPC_MAKE_STD_ZVAL(result);
267 php_grpc_ulong index;
273 wrapped_grpc_call *call = PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_call,
276 // startBatch in gRPC PHP server doesn't have channel in it.
277 if (call->channel->wrapper == NULL ||
278 call->channel->wrapper->wrapped == NULL) {
279 zend_throw_exception(spl_ce_RuntimeException,
280 "startBatch Error. Channel is closed",
288 HashTable *array_hash;
289 HashTable *status_hash;
290 HashTable *message_hash;
292 grpc_metadata_array metadata;
293 grpc_metadata_array trailing_metadata;
294 grpc_metadata_array recv_metadata;
295 grpc_metadata_array recv_trailing_metadata;
296 grpc_status_code status;
297 grpc_slice recv_status_details = grpc_empty_slice();
298 grpc_slice send_status_details = grpc_empty_slice();
299 grpc_byte_buffer *message;
301 grpc_call_error error;
303 #if PHP_MAJOR_VERSION < 7
307 zend_string* zmessage = NULL;
308 #endif // PHP_MAJOR_VERSION < 7
310 grpc_metadata_array_init(&metadata);
311 grpc_metadata_array_init(&trailing_metadata);
312 grpc_metadata_array_init(&recv_metadata);
313 grpc_metadata_array_init(&recv_trailing_metadata);
314 memset(ops, 0, sizeof(ops));
317 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &array) ==
319 zend_throw_exception(spl_ce_InvalidArgumentException,
320 "start_batch expects an array", 1 TSRMLS_CC);
324 array_hash = Z_ARRVAL_P(array);
328 PHP_GRPC_HASH_FOREACH_LONG_KEY_VAL_START(array_hash, key, key_type, index,
330 if (key_type != HASH_KEY_IS_LONG || key != NULL) {
331 zend_throw_exception(spl_ce_InvalidArgumentException,
332 "batch keys must be integers", 1 TSRMLS_CC);
336 ops[op_num].op = (grpc_op_type)index;
337 ops[op_num].flags = 0;
338 ops[op_num].reserved = NULL;
341 case GRPC_OP_SEND_INITIAL_METADATA:
342 if (!create_metadata_array(value, &metadata)) {
343 zend_throw_exception(spl_ce_InvalidArgumentException,
344 "Bad metadata value given", 1 TSRMLS_CC);
347 ops[op_num].data.send_initial_metadata.count = metadata.count;
348 ops[op_num].data.send_initial_metadata.metadata = metadata.metadata;
350 case GRPC_OP_SEND_MESSAGE:
351 if (Z_TYPE_P(value) != IS_ARRAY) {
352 zend_throw_exception(spl_ce_InvalidArgumentException,
353 "Expected an array for send message",
357 message_hash = Z_ARRVAL_P(value);
358 if (php_grpc_zend_hash_find(message_hash, "flags", sizeof("flags"),
359 (void **)&message_flags) == SUCCESS) {
360 if (Z_TYPE_P(message_flags) != IS_LONG) {
361 zend_throw_exception(spl_ce_InvalidArgumentException,
362 "Expected an int for message flags",
365 ops[op_num].flags = Z_LVAL_P(message_flags) & GRPC_WRITE_USED_MASK;
367 if (php_grpc_zend_hash_find(message_hash, "message", sizeof("message"),
368 (void **)&message_value) != SUCCESS ||
369 Z_TYPE_P(message_value) != IS_STRING) {
370 zend_throw_exception(spl_ce_InvalidArgumentException,
371 "Expected a string for send message",
375 ops[op_num].data.send_message.send_message =
376 string_to_byte_buffer(Z_STRVAL_P(message_value),
377 Z_STRLEN_P(message_value));
379 case GRPC_OP_SEND_CLOSE_FROM_CLIENT:
381 case GRPC_OP_SEND_STATUS_FROM_SERVER:
382 status_hash = Z_ARRVAL_P(value);
383 if (php_grpc_zend_hash_find(status_hash, "metadata", sizeof("metadata"),
384 (void **)&inner_value) == SUCCESS) {
385 if (!create_metadata_array(inner_value, &trailing_metadata)) {
386 zend_throw_exception(spl_ce_InvalidArgumentException,
387 "Bad trailing metadata value given",
391 ops[op_num].data.send_status_from_server.trailing_metadata =
392 trailing_metadata.metadata;
393 ops[op_num].data.send_status_from_server.trailing_metadata_count =
394 trailing_metadata.count;
396 if (php_grpc_zend_hash_find(status_hash, "code", sizeof("code"),
397 (void**)&inner_value) == SUCCESS) {
398 if (Z_TYPE_P(inner_value) != IS_LONG) {
399 zend_throw_exception(spl_ce_InvalidArgumentException,
400 "Status code must be an integer",
404 ops[op_num].data.send_status_from_server.status =
405 Z_LVAL_P(inner_value);
407 zend_throw_exception(spl_ce_InvalidArgumentException,
408 "Integer status code is required",
412 if (php_grpc_zend_hash_find(status_hash, "details", sizeof("details"),
413 (void**)&inner_value) == SUCCESS) {
414 if (Z_TYPE_P(inner_value) != IS_STRING) {
415 zend_throw_exception(spl_ce_InvalidArgumentException,
416 "Status details must be a string",
420 send_status_details = grpc_slice_from_copied_string(
421 Z_STRVAL_P(inner_value));
422 ops[op_num].data.send_status_from_server.status_details =
423 &send_status_details;
425 zend_throw_exception(spl_ce_InvalidArgumentException,
426 "String status details is required",
431 case GRPC_OP_RECV_INITIAL_METADATA:
432 ops[op_num].data.recv_initial_metadata.recv_initial_metadata =
435 case GRPC_OP_RECV_MESSAGE:
436 ops[op_num].data.recv_message.recv_message = &message;
438 case GRPC_OP_RECV_STATUS_ON_CLIENT:
439 ops[op_num].data.recv_status_on_client.trailing_metadata =
440 &recv_trailing_metadata;
441 ops[op_num].data.recv_status_on_client.status = &status;
442 ops[op_num].data.recv_status_on_client.status_details =
443 &recv_status_details;
445 case GRPC_OP_RECV_CLOSE_ON_SERVER:
446 ops[op_num].data.recv_close_on_server.cancelled = &cancelled;
449 zend_throw_exception(spl_ce_InvalidArgumentException,
450 "Unrecognized key in batch", 1 TSRMLS_CC);
454 PHP_GRPC_HASH_FOREACH_END()
456 error = grpc_call_start_batch(call->wrapped, ops, op_num, call->wrapped,
458 if (error != GRPC_CALL_OK) {
459 zend_throw_exception(spl_ce_LogicException,
460 "start_batch was called incorrectly",
461 (long)error TSRMLS_CC);
464 grpc_completion_queue_pluck(completion_queue, call->wrapped,
465 gpr_inf_future(GPR_CLOCK_REALTIME), NULL);
466 #if PHP_MAJOR_VERSION >= 7
469 for (int i = 0; i < op_num; i++) {
471 case GRPC_OP_SEND_INITIAL_METADATA:
472 add_property_bool(result, "send_metadata", true);
474 case GRPC_OP_SEND_MESSAGE:
475 add_property_bool(result, "send_message", true);
477 case GRPC_OP_SEND_CLOSE_FROM_CLIENT:
478 add_property_bool(result, "send_close", true);
480 case GRPC_OP_SEND_STATUS_FROM_SERVER:
481 add_property_bool(result, "send_status", true);
483 case GRPC_OP_RECV_INITIAL_METADATA:
484 #if PHP_MAJOR_VERSION < 7
485 array = grpc_parse_metadata_array(&recv_metadata TSRMLS_CC);
486 add_property_zval(result, "metadata", array);
488 recv_md = grpc_parse_metadata_array(&recv_metadata);
489 add_property_zval(result, "metadata", recv_md);
490 zval_ptr_dtor(recv_md);
491 PHP_GRPC_FREE_STD_ZVAL(recv_md);
493 PHP_GRPC_DELREF(array);
495 case GRPC_OP_RECV_MESSAGE:
496 #if PHP_MAJOR_VERSION < 7
497 byte_buffer_to_string(message, &message_str, &message_len);
499 zmessage = byte_buffer_to_zend_string(message);
500 #endif // PHP_MAJOR_VERSION < 7
502 #if PHP_MAJOR_VERSION < 7
503 if (message_str == NULL) {
505 if (zmessage == NULL) {
506 #endif // PHP_MAJOR_VERSION < 7
507 add_property_null(result, "message");
509 #if PHP_MAJOR_VERSION < 7
510 php_grpc_add_property_stringl(result, "message", message_str,
514 ZVAL_NEW_STR(&zmessage_val, zmessage);
515 add_property_zval(result, "message", &zmessage_val);
516 zval_ptr_dtor(&zmessage_val);
517 #endif // PHP_MAJOR_VERSION < 7
520 case GRPC_OP_RECV_STATUS_ON_CLIENT:
521 PHP_GRPC_MAKE_STD_ZVAL(recv_status);
522 object_init(recv_status);
523 #if PHP_MAJOR_VERSION < 7
524 array = grpc_parse_metadata_array(&recv_trailing_metadata TSRMLS_CC);
525 add_property_zval(recv_status, "metadata", array);
527 recv_md = grpc_parse_metadata_array(&recv_trailing_metadata);
528 add_property_zval(recv_status, "metadata", recv_md);
529 zval_ptr_dtor(recv_md);
530 PHP_GRPC_FREE_STD_ZVAL(recv_md);
532 PHP_GRPC_DELREF(array);
533 add_property_long(recv_status, "code", status);
534 char *status_details_text = grpc_slice_to_c_string(recv_status_details);
535 php_grpc_add_property_string(recv_status, "details", status_details_text,
537 gpr_free(status_details_text);
538 add_property_zval(result, "status", recv_status);
539 #if PHP_MAJOR_VERSION >= 7
540 zval_ptr_dtor(recv_status);
542 PHP_GRPC_DELREF(recv_status);
543 PHP_GRPC_FREE_STD_ZVAL(recv_status);
545 case GRPC_OP_RECV_CLOSE_ON_SERVER:
546 add_property_bool(result, "cancelled", cancelled);
554 grpc_php_metadata_array_destroy_including_entries(&metadata);
555 grpc_php_metadata_array_destroy_including_entries(&trailing_metadata);
556 grpc_metadata_array_destroy(&recv_metadata);
557 grpc_metadata_array_destroy(&recv_trailing_metadata);
558 grpc_slice_unref(recv_status_details);
559 grpc_slice_unref(send_status_details);
560 for (int i = 0; i < op_num; i++) {
561 if (ops[i].op == GRPC_OP_SEND_MESSAGE) {
562 grpc_byte_buffer_destroy(ops[i].data.send_message.send_message);
564 if (ops[i].op == GRPC_OP_RECV_MESSAGE) {
565 grpc_byte_buffer_destroy(message);
566 #if PHP_MAJOR_VERSION < 7
567 PHP_GRPC_FREE_STD_ZVAL(message_str);
568 #endif // PHP_MAJOR_VERSION < 7
571 RETURN_DESTROY_ZVAL(result);
575 * Get the endpoint this call/stream is connected to
576 * @return string The URI of the endpoint
578 PHP_METHOD(Call, getPeer) {
579 wrapped_grpc_call *call = PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_call,
581 char *peer = grpc_call_get_peer(call->wrapped);
582 PHP_GRPC_RETVAL_STRING(peer, 1);
587 * Cancel the call. This will cause the call to end with STATUS_CANCELLED
588 * if it has not already ended with another status.
591 PHP_METHOD(Call, cancel) {
592 wrapped_grpc_call *call = PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_call,
594 grpc_call_cancel(call->wrapped, NULL);
598 * Set the CallCredentials for this call.
599 * @param CallCredentials $creds_obj The CallCredentials object
600 * @return int The error code
602 PHP_METHOD(Call, setCredentials) {
605 /* "O" == 1 Object */
606 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &creds_obj,
607 grpc_ce_call_credentials) == FAILURE) {
608 zend_throw_exception(spl_ce_InvalidArgumentException,
609 "setCredentials expects 1 CallCredentials",
614 wrapped_grpc_call_credentials *creds =
615 PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_call_credentials, creds_obj);
616 wrapped_grpc_call *call = PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_call,
619 grpc_call_error error = GRPC_CALL_ERROR;
620 error = grpc_call_set_credentials(call->wrapped, creds->wrapped);
624 ZEND_BEGIN_ARG_INFO_EX(arginfo_construct, 0, 0, 3)
625 ZEND_ARG_INFO(0, channel)
626 ZEND_ARG_INFO(0, method)
627 ZEND_ARG_INFO(0, deadline)
628 ZEND_ARG_INFO(0, host_override)
631 ZEND_BEGIN_ARG_INFO_EX(arginfo_startBatch, 0, 0, 1)
632 ZEND_ARG_INFO(0, ops)
635 ZEND_BEGIN_ARG_INFO_EX(arginfo_getPeer, 0, 0, 0)
638 ZEND_BEGIN_ARG_INFO_EX(arginfo_cancel, 0, 0, 0)
641 ZEND_BEGIN_ARG_INFO_EX(arginfo_setCredentials, 0, 0, 1)
642 ZEND_ARG_INFO(0, credentials)
645 static zend_function_entry call_methods[] = {
646 PHP_ME(Call, __construct, arginfo_construct, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
647 PHP_ME(Call, startBatch, arginfo_startBatch, ZEND_ACC_PUBLIC)
648 PHP_ME(Call, getPeer, arginfo_getPeer, ZEND_ACC_PUBLIC)
649 PHP_ME(Call, cancel, arginfo_cancel, ZEND_ACC_PUBLIC)
650 PHP_ME(Call, setCredentials, arginfo_setCredentials, ZEND_ACC_PUBLIC)
654 void grpc_init_call(TSRMLS_D) {
656 INIT_CLASS_ENTRY(ce, "Grpc\\Call", call_methods);
657 ce.create_object = create_wrapped_grpc_call;
658 grpc_ce_call = zend_register_internal_class(&ce TSRMLS_CC);
659 PHP_GRPC_INIT_HANDLER(wrapped_grpc_call, call_ce_handlers);