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);
112 /* Populates a grpc_metadata_array with the data in a PHP array object.
113 Returns true on success and false on failure */
114 bool create_metadata_array(zval *array, grpc_metadata_array *metadata) {
115 HashTable *array_hash;
116 HashTable *inner_array_hash;
119 grpc_metadata_array_init(metadata);
121 metadata->metadata = NULL;
122 if (Z_TYPE_P(array) != IS_ARRAY) {
125 array_hash = Z_ARRVAL_P(array);
129 PHP_GRPC_HASH_FOREACH_STR_KEY_VAL_START(array_hash, key, key_type,
131 if (key_type != HASH_KEY_IS_STRING || key == NULL) {
134 if (Z_TYPE_P(inner_array) != IS_ARRAY) {
137 inner_array_hash = Z_ARRVAL_P(inner_array);
138 metadata->capacity += zend_hash_num_elements(inner_array_hash);
139 PHP_GRPC_HASH_FOREACH_END()
141 metadata->metadata = gpr_malloc(metadata->capacity * sizeof(grpc_metadata));
145 PHP_GRPC_HASH_FOREACH_STR_KEY_VAL_START(array_hash, key1, key_type1,
147 if (key_type1 != HASH_KEY_IS_STRING) {
150 if (!grpc_header_key_is_legal(grpc_slice_from_static_string(key1))) {
153 inner_array_hash = Z_ARRVAL_P(inner_array);
154 PHP_GRPC_HASH_FOREACH_VAL_START(inner_array_hash, value)
155 if (Z_TYPE_P(value) != IS_STRING) {
158 metadata->metadata[metadata->count].key =
159 grpc_slice_from_copied_string(key1);
160 metadata->metadata[metadata->count].value =
161 grpc_slice_from_copied_buffer(Z_STRVAL_P(value), Z_STRLEN_P(value));
162 metadata->count += 1;
163 PHP_GRPC_HASH_FOREACH_END()
164 PHP_GRPC_HASH_FOREACH_END()
168 void grpc_php_metadata_array_destroy_including_entries(
169 grpc_metadata_array* array) {
171 if (array->metadata) {
172 for (i = 0; i < array->count; i++) {
173 grpc_slice_unref(array->metadata[i].key);
174 grpc_slice_unref(array->metadata[i].value);
177 grpc_metadata_array_destroy(array);
180 /* Wraps a grpc_call struct in a PHP object. Owned indicates whether the
181 struct should be destroyed at the end of the object's lifecycle */
182 zval *grpc_php_wrap_call(grpc_call *wrapped, bool owned TSRMLS_DC) {
184 PHP_GRPC_MAKE_STD_ZVAL(call_object);
185 object_init_ex(call_object, grpc_ce_call);
186 wrapped_grpc_call *call = PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_call,
188 call->wrapped = wrapped;
194 * Constructs a new instance of the Call class.
195 * @param Channel $channel_obj The channel to associate the call with.
196 * Must not be closed.
197 * @param string $method The method to call
198 * @param Timeval $deadline_obj The deadline for completing the call
199 * @param string $host_override = "" The host is set by user (optional)
201 PHP_METHOD(Call, __construct) {
204 php_grpc_int method_len;
206 char *host_override = NULL;
207 php_grpc_int host_override_len = 0;
208 wrapped_grpc_call *call = PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_call,
211 /* "OsO|s" == 1 Object, 1 string, 1 Object, 1 optional string */
212 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "OsO|s", &channel_obj,
213 grpc_ce_channel, &method, &method_len,
214 &deadline_obj, grpc_ce_timeval, &host_override,
215 &host_override_len) == FAILURE) {
216 zend_throw_exception(spl_ce_InvalidArgumentException,
217 "Call expects a Channel, a String, a Timeval and "
218 "an optional String", 1 TSRMLS_CC);
221 wrapped_grpc_channel *channel =
222 PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_channel, channel_obj);
223 if (channel->wrapper == NULL) {
224 zend_throw_exception(spl_ce_InvalidArgumentException,
225 "Call cannot be constructed from a closed Channel",
229 gpr_mu_lock(&channel->wrapper->mu);
230 if (channel->wrapper == NULL || channel->wrapper->wrapped == NULL) {
231 zend_throw_exception(spl_ce_InvalidArgumentException,
232 "Call cannot be constructed from a closed Channel",
234 gpr_mu_unlock(&channel->wrapper->mu);
237 add_property_zval(getThis(), "channel", channel_obj);
238 wrapped_grpc_timeval *deadline =
239 PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_timeval, deadline_obj);
240 grpc_slice method_slice = grpc_slice_from_copied_string(method);
241 grpc_slice host_slice = host_override != NULL ?
242 grpc_slice_from_copied_string(host_override) : grpc_empty_slice();
244 grpc_channel_create_call(channel->wrapper->wrapped, NULL,
245 GRPC_PROPAGATE_DEFAULTS,
246 completion_queue, method_slice,
247 host_override != NULL ? &host_slice : NULL,
248 deadline->wrapped, NULL);
249 grpc_slice_unref(method_slice);
250 grpc_slice_unref(host_slice);
252 call->channel = channel;
253 gpr_mu_unlock(&channel->wrapper->mu);
257 * Start a batch of RPC actions.
258 * @param array $array Array of actions to take
259 * @return object Object with results of all actions
261 PHP_METHOD(Call, startBatch) {
263 PHP_GRPC_MAKE_STD_ZVAL(result);
265 php_grpc_ulong index;
271 wrapped_grpc_call *call = PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_call,
274 // startBatch in gRPC PHP server doesn't have channel in it.
275 if (call->channel->wrapper == NULL ||
276 call->channel->wrapper->wrapped == NULL) {
277 zend_throw_exception(spl_ce_RuntimeException,
278 "startBatch Error. Channel is closed",
286 HashTable *array_hash;
287 HashTable *status_hash;
288 HashTable *message_hash;
290 grpc_metadata_array metadata;
291 grpc_metadata_array trailing_metadata;
292 grpc_metadata_array recv_metadata;
293 grpc_metadata_array recv_trailing_metadata;
294 grpc_status_code status;
295 grpc_slice recv_status_details = grpc_empty_slice();
296 grpc_slice send_status_details = grpc_empty_slice();
297 grpc_byte_buffer *message = NULL;
299 grpc_call_error error;
301 zend_string* zmessage = NULL;
303 grpc_metadata_array_init(&metadata);
304 grpc_metadata_array_init(&trailing_metadata);
305 grpc_metadata_array_init(&recv_metadata);
306 grpc_metadata_array_init(&recv_trailing_metadata);
307 memset(ops, 0, sizeof(ops));
310 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &array) ==
312 zend_throw_exception(spl_ce_InvalidArgumentException,
313 "start_batch expects an array", 1 TSRMLS_CC);
317 // c-core may call rand(). If we don't call srand() here, all the
318 // random numbers being returned would be the same.
319 gpr_timespec now = gpr_now(GPR_CLOCK_REALTIME);
322 array_hash = Z_ARRVAL_P(array);
326 PHP_GRPC_HASH_FOREACH_LONG_KEY_VAL_START(array_hash, key, key_type, index,
328 if (key_type != HASH_KEY_IS_LONG || key != NULL) {
329 zend_throw_exception(spl_ce_InvalidArgumentException,
330 "batch keys must be integers", 1 TSRMLS_CC);
334 ops[op_num].op = (grpc_op_type)index;
335 ops[op_num].flags = 0;
336 ops[op_num].reserved = NULL;
339 case GRPC_OP_SEND_INITIAL_METADATA:
340 if (!create_metadata_array(value, &metadata)) {
341 zend_throw_exception(spl_ce_InvalidArgumentException,
342 "Bad metadata value given", 1 TSRMLS_CC);
345 ops[op_num].data.send_initial_metadata.count = metadata.count;
346 ops[op_num].data.send_initial_metadata.metadata = metadata.metadata;
348 case GRPC_OP_SEND_MESSAGE:
349 if (Z_TYPE_P(value) != IS_ARRAY) {
350 zend_throw_exception(spl_ce_InvalidArgumentException,
351 "Expected an array for send message",
355 message_hash = Z_ARRVAL_P(value);
356 if (php_grpc_zend_hash_find(message_hash, "flags", sizeof("flags"),
357 (void **)&message_flags) == SUCCESS) {
358 if (Z_TYPE_P(message_flags) != IS_LONG) {
359 zend_throw_exception(spl_ce_InvalidArgumentException,
360 "Expected an int for message flags",
363 ops[op_num].flags = Z_LVAL_P(message_flags) & GRPC_WRITE_USED_MASK;
365 if (php_grpc_zend_hash_find(message_hash, "message", sizeof("message"),
366 (void **)&message_value) != SUCCESS ||
367 Z_TYPE_P(message_value) != IS_STRING) {
368 zend_throw_exception(spl_ce_InvalidArgumentException,
369 "Expected a string for send message",
373 ops[op_num].data.send_message.send_message =
374 string_to_byte_buffer(Z_STRVAL_P(message_value),
375 Z_STRLEN_P(message_value));
377 case GRPC_OP_SEND_CLOSE_FROM_CLIENT:
379 case GRPC_OP_SEND_STATUS_FROM_SERVER:
380 status_hash = Z_ARRVAL_P(value);
381 if (php_grpc_zend_hash_find(status_hash, "metadata", sizeof("metadata"),
382 (void **)&inner_value) == SUCCESS) {
383 if (!create_metadata_array(inner_value, &trailing_metadata)) {
384 zend_throw_exception(spl_ce_InvalidArgumentException,
385 "Bad trailing metadata value given",
389 ops[op_num].data.send_status_from_server.trailing_metadata =
390 trailing_metadata.metadata;
391 ops[op_num].data.send_status_from_server.trailing_metadata_count =
392 trailing_metadata.count;
394 if (php_grpc_zend_hash_find(status_hash, "code", sizeof("code"),
395 (void**)&inner_value) == SUCCESS) {
396 if (Z_TYPE_P(inner_value) != IS_LONG) {
397 zend_throw_exception(spl_ce_InvalidArgumentException,
398 "Status code must be an integer",
402 ops[op_num].data.send_status_from_server.status =
403 Z_LVAL_P(inner_value);
405 zend_throw_exception(spl_ce_InvalidArgumentException,
406 "Integer status code is required",
410 if (php_grpc_zend_hash_find(status_hash, "details", sizeof("details"),
411 (void**)&inner_value) == SUCCESS) {
412 if (Z_TYPE_P(inner_value) != IS_STRING) {
413 zend_throw_exception(spl_ce_InvalidArgumentException,
414 "Status details must be a string",
418 send_status_details = grpc_slice_from_copied_string(
419 Z_STRVAL_P(inner_value));
420 ops[op_num].data.send_status_from_server.status_details =
421 &send_status_details;
423 zend_throw_exception(spl_ce_InvalidArgumentException,
424 "String status details is required",
429 case GRPC_OP_RECV_INITIAL_METADATA:
430 ops[op_num].data.recv_initial_metadata.recv_initial_metadata =
433 case GRPC_OP_RECV_MESSAGE:
434 ops[op_num].data.recv_message.recv_message = &message;
436 case GRPC_OP_RECV_STATUS_ON_CLIENT:
437 ops[op_num].data.recv_status_on_client.trailing_metadata =
438 &recv_trailing_metadata;
439 ops[op_num].data.recv_status_on_client.status = &status;
440 ops[op_num].data.recv_status_on_client.status_details =
441 &recv_status_details;
443 case GRPC_OP_RECV_CLOSE_ON_SERVER:
444 ops[op_num].data.recv_close_on_server.cancelled = &cancelled;
447 zend_throw_exception(spl_ce_InvalidArgumentException,
448 "Unrecognized key in batch", 1 TSRMLS_CC);
452 PHP_GRPC_HASH_FOREACH_END()
454 error = grpc_call_start_batch(call->wrapped, ops, op_num, call->wrapped,
456 if (error != GRPC_CALL_OK) {
457 zend_throw_exception(spl_ce_LogicException,
458 "start_batch was called incorrectly",
459 (long)error TSRMLS_CC);
462 grpc_completion_queue_pluck(completion_queue, call->wrapped,
463 gpr_inf_future(GPR_CLOCK_REALTIME), NULL);
465 for (int i = 0; i < op_num; i++) {
467 case GRPC_OP_SEND_INITIAL_METADATA:
468 add_property_bool(result, "send_metadata", true);
470 case GRPC_OP_SEND_MESSAGE:
471 add_property_bool(result, "send_message", true);
473 case GRPC_OP_SEND_CLOSE_FROM_CLIENT:
474 add_property_bool(result, "send_close", true);
476 case GRPC_OP_SEND_STATUS_FROM_SERVER:
477 add_property_bool(result, "send_status", true);
479 case GRPC_OP_RECV_INITIAL_METADATA:
480 recv_md = grpc_parse_metadata_array(&recv_metadata);
481 add_property_zval(result, "metadata", recv_md);
482 zval_ptr_dtor(recv_md);
483 PHP_GRPC_FREE_STD_ZVAL(recv_md);
484 PHP_GRPC_DELREF(array);
486 case GRPC_OP_RECV_MESSAGE:
487 zmessage = byte_buffer_to_zend_string(message);
489 if (zmessage == NULL) {
490 add_property_null(result, "message");
493 ZVAL_NEW_STR(&zmessage_val, zmessage);
494 add_property_zval(result, "message", &zmessage_val);
495 zval_ptr_dtor(&zmessage_val);
498 case GRPC_OP_RECV_STATUS_ON_CLIENT:
499 PHP_GRPC_MAKE_STD_ZVAL(recv_status);
500 object_init(recv_status);
501 recv_md = grpc_parse_metadata_array(&recv_trailing_metadata);
502 add_property_zval(recv_status, "metadata", recv_md);
503 zval_ptr_dtor(recv_md);
504 PHP_GRPC_FREE_STD_ZVAL(recv_md);
505 PHP_GRPC_DELREF(array);
506 add_property_long(recv_status, "code", status);
507 char *status_details_text = grpc_slice_to_c_string(recv_status_details);
508 php_grpc_add_property_string(recv_status, "details", status_details_text,
510 gpr_free(status_details_text);
511 add_property_zval(result, "status", recv_status);
512 zval_ptr_dtor(recv_status);
513 PHP_GRPC_DELREF(recv_status);
514 PHP_GRPC_FREE_STD_ZVAL(recv_status);
516 case GRPC_OP_RECV_CLOSE_ON_SERVER:
517 add_property_bool(result, "cancelled", cancelled);
525 grpc_php_metadata_array_destroy_including_entries(&metadata);
526 grpc_php_metadata_array_destroy_including_entries(&trailing_metadata);
527 grpc_metadata_array_destroy(&recv_metadata);
528 grpc_metadata_array_destroy(&recv_trailing_metadata);
529 grpc_slice_unref(recv_status_details);
530 grpc_slice_unref(send_status_details);
531 for (int i = 0; i < op_num; i++) {
532 if (ops[i].op == GRPC_OP_SEND_MESSAGE) {
533 grpc_byte_buffer_destroy(ops[i].data.send_message.send_message);
535 if (ops[i].op == GRPC_OP_RECV_MESSAGE) {
536 grpc_byte_buffer_destroy(message);
539 RETURN_DESTROY_ZVAL(result);
543 * Get the endpoint this call/stream is connected to
544 * @return string The URI of the endpoint
546 PHP_METHOD(Call, getPeer) {
547 wrapped_grpc_call *call = PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_call,
549 char *peer = grpc_call_get_peer(call->wrapped);
550 PHP_GRPC_RETVAL_STRING(peer, 1);
555 * Cancel the call. This will cause the call to end with STATUS_CANCELLED
556 * if it has not already ended with another status.
559 PHP_METHOD(Call, cancel) {
560 wrapped_grpc_call *call = PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_call,
562 grpc_call_cancel(call->wrapped, NULL);
566 * Set the CallCredentials for this call.
567 * @param CallCredentials $creds_obj The CallCredentials object
568 * @return int The error code
570 PHP_METHOD(Call, setCredentials) {
573 /* "O" == 1 Object */
574 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &creds_obj,
575 grpc_ce_call_credentials) == FAILURE) {
576 zend_throw_exception(spl_ce_InvalidArgumentException,
577 "setCredentials expects 1 CallCredentials",
582 wrapped_grpc_call_credentials *creds =
583 PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_call_credentials, creds_obj);
584 wrapped_grpc_call *call = PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_call,
587 grpc_call_error error = GRPC_CALL_ERROR;
588 error = grpc_call_set_credentials(call->wrapped, creds->wrapped);
592 ZEND_BEGIN_ARG_INFO_EX(arginfo_construct, 0, 0, 3)
593 ZEND_ARG_INFO(0, channel)
594 ZEND_ARG_INFO(0, method)
595 ZEND_ARG_INFO(0, deadline)
596 ZEND_ARG_INFO(0, host_override)
599 ZEND_BEGIN_ARG_INFO_EX(arginfo_startBatch, 0, 0, 1)
600 ZEND_ARG_INFO(0, ops)
603 ZEND_BEGIN_ARG_INFO_EX(arginfo_getPeer, 0, 0, 0)
606 ZEND_BEGIN_ARG_INFO_EX(arginfo_cancel, 0, 0, 0)
609 ZEND_BEGIN_ARG_INFO_EX(arginfo_setCredentials, 0, 0, 1)
610 ZEND_ARG_INFO(0, credentials)
613 static zend_function_entry call_methods[] = {
614 PHP_ME(Call, __construct, arginfo_construct, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
615 PHP_ME(Call, startBatch, arginfo_startBatch, ZEND_ACC_PUBLIC)
616 PHP_ME(Call, getPeer, arginfo_getPeer, ZEND_ACC_PUBLIC)
617 PHP_ME(Call, cancel, arginfo_cancel, ZEND_ACC_PUBLIC)
618 PHP_ME(Call, setCredentials, arginfo_setCredentials, ZEND_ACC_PUBLIC)
622 void grpc_init_call(TSRMLS_D) {
624 INIT_CLASS_ENTRY(ce, "Grpc\\Call", call_methods);
625 ce.create_object = create_wrapped_grpc_call;
626 grpc_ce_call = zend_register_internal_class(&ce TSRMLS_CC);
627 PHP_GRPC_INIT_HANDLER(wrapped_grpc_call, call_ce_handlers);