• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
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
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  */
18 
19 #include "call.h"
20 
21 #include <ext/spl/spl_exceptions.h>
22 #include <zend_exceptions.h>
23 
24 #include <grpc/support/alloc.h>
25 
26 #include "call_credentials.h"
27 #include "completion_queue.h"
28 #include "timeval.h"
29 #include "channel.h"
30 #include "byte_buffer.h"
31 
32 zend_class_entry *grpc_ce_call;
33 PHP_GRPC_DECLARE_OBJECT_HANDLER(call_ce_handlers)
34 
35 /* Frees and destroys an instance of wrapped_grpc_call */
36 PHP_GRPC_FREE_WRAPPED_FUNC_START(wrapped_grpc_call)
37   if (p->owned && p->wrapped != NULL) {
38     grpc_call_unref(p->wrapped);
39   }
PHP_GRPC_FREE_WRAPPED_FUNC_END()40 PHP_GRPC_FREE_WRAPPED_FUNC_END()
41 
42 /* Initializes an instance of wrapped_grpc_call to be associated with an
43  * object of a class specified by class_type */
44 php_grpc_zend_object create_wrapped_grpc_call(zend_class_entry *class_type
45                                               TSRMLS_DC) {
46   PHP_GRPC_ALLOC_CLASS_OBJECT(wrapped_grpc_call);
47   zend_object_std_init(&intern->std, class_type TSRMLS_CC);
48   object_properties_init(&intern->std, class_type);
49   PHP_GRPC_FREE_CLASS_OBJECT(wrapped_grpc_call, call_ce_handlers);
50 }
51 
52 /* Creates and returns a PHP array object with the data in a
53  * grpc_metadata_array. Returns NULL on failure */
grpc_parse_metadata_array(grpc_metadata_array * metadata_array TSRMLS_DC)54 zval *grpc_parse_metadata_array(grpc_metadata_array
55                                 *metadata_array TSRMLS_DC) {
56   int count = metadata_array->count;
57   grpc_metadata *elements = metadata_array->metadata;
58   zval *array;
59   PHP_GRPC_MAKE_STD_ZVAL(array);
60   array_init(array);
61   int i;
62   HashTable *array_hash;
63   zval *inner_array;
64   char *str_key;
65   char *str_val;
66   size_t key_len;
67   zval *data = NULL;
68 
69   array_hash = Z_ARRVAL_P(array);
70   grpc_metadata *elem;
71   for (i = 0; i < count; i++) {
72     elem = &elements[i];
73     key_len = GRPC_SLICE_LENGTH(elem->key);
74     str_key = ecalloc(key_len + 1, sizeof(char));
75     memcpy(str_key, GRPC_SLICE_START_PTR(elem->key), key_len);
76     str_val = ecalloc(GRPC_SLICE_LENGTH(elem->value) + 1, sizeof(char));
77     memcpy(str_val, GRPC_SLICE_START_PTR(elem->value),
78            GRPC_SLICE_LENGTH(elem->value));
79     if (php_grpc_zend_hash_find(array_hash, str_key, key_len, (void **)&data)
80         == SUCCESS) {
81       if (Z_TYPE_P(data) != IS_ARRAY) {
82         zend_throw_exception(zend_exception_get_default(TSRMLS_C),
83                              "Metadata hash somehow contains wrong types.",
84                              1 TSRMLS_CC);
85         efree(str_key);
86         efree(str_val);
87         PHP_GRPC_FREE_STD_ZVAL(array);
88         return NULL;
89       }
90       php_grpc_add_next_index_stringl(data, str_val,
91                                       GRPC_SLICE_LENGTH(elem->value),
92                                       false);
93     } else {
94       PHP_GRPC_MAKE_STD_ZVAL(inner_array);
95       array_init(inner_array);
96       php_grpc_add_next_index_stringl(inner_array, str_val,
97                                       GRPC_SLICE_LENGTH(elem->value), false);
98       add_assoc_zval(array, str_key, inner_array);
99       PHP_GRPC_FREE_STD_ZVAL(inner_array);
100     }
101     efree(str_key);
102 #if PHP_MAJOR_VERSION >= 7
103     efree(str_val);
104 #endif
105   }
106   return array;
107 }
108 
109 /* Populates a grpc_metadata_array with the data in a PHP array object.
110    Returns true on success and false on failure */
create_metadata_array(zval * array,grpc_metadata_array * metadata)111 bool create_metadata_array(zval *array, grpc_metadata_array *metadata) {
112   HashTable *array_hash;
113   HashTable *inner_array_hash;
114   zval *value;
115   zval *inner_array;
116   grpc_metadata_array_init(metadata);
117   metadata->count = 0;
118   metadata->metadata = NULL;
119   if (Z_TYPE_P(array) != IS_ARRAY) {
120     return false;
121   }
122   array_hash = Z_ARRVAL_P(array);
123 
124   char *key;
125   int key_type;
126   PHP_GRPC_HASH_FOREACH_STR_KEY_VAL_START(array_hash, key, key_type,
127                                           inner_array)
128     if (key_type != HASH_KEY_IS_STRING || key == NULL) {
129       return false;
130     }
131     if (Z_TYPE_P(inner_array) != IS_ARRAY) {
132       return false;
133     }
134     inner_array_hash = Z_ARRVAL_P(inner_array);
135     metadata->capacity += zend_hash_num_elements(inner_array_hash);
136   PHP_GRPC_HASH_FOREACH_END()
137 
138   metadata->metadata = gpr_malloc(metadata->capacity * sizeof(grpc_metadata));
139 
140   char *key1 = NULL;
141   int key_type1;
142   PHP_GRPC_HASH_FOREACH_STR_KEY_VAL_START(array_hash, key1, key_type1,
143                                           inner_array)
144     if (key_type1 != HASH_KEY_IS_STRING) {
145       return false;
146     }
147     if (!grpc_header_key_is_legal(grpc_slice_from_static_string(key1))) {
148       return false;
149     }
150     inner_array_hash = Z_ARRVAL_P(inner_array);
151     PHP_GRPC_HASH_FOREACH_VAL_START(inner_array_hash, value)
152       if (Z_TYPE_P(value) != IS_STRING) {
153         return false;
154       }
155       metadata->metadata[metadata->count].key =
156         grpc_slice_from_copied_string(key1);
157       metadata->metadata[metadata->count].value =
158         grpc_slice_from_copied_buffer(Z_STRVAL_P(value), Z_STRLEN_P(value));
159       metadata->count += 1;
160     PHP_GRPC_HASH_FOREACH_END()
161   PHP_GRPC_HASH_FOREACH_END()
162   return true;
163 }
164 
grpc_php_metadata_array_destroy_including_entries(grpc_metadata_array * array)165 void grpc_php_metadata_array_destroy_including_entries(
166     grpc_metadata_array* array) {
167   size_t i;
168   if (array->metadata) {
169     for (i = 0; i < array->count; i++) {
170       grpc_slice_unref(array->metadata[i].key);
171       grpc_slice_unref(array->metadata[i].value);
172     }
173   }
174   grpc_metadata_array_destroy(array);
175 }
176 
177 /* Wraps a grpc_call struct in a PHP object. Owned indicates whether the
178    struct should be destroyed at the end of the object's lifecycle */
grpc_php_wrap_call(grpc_call * wrapped,bool owned TSRMLS_DC)179 zval *grpc_php_wrap_call(grpc_call *wrapped, bool owned TSRMLS_DC) {
180   zval *call_object;
181   PHP_GRPC_MAKE_STD_ZVAL(call_object);
182   object_init_ex(call_object, grpc_ce_call);
183   wrapped_grpc_call *call = PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_call,
184                                                         call_object);
185   call->wrapped = wrapped;
186   call->owned = owned;
187   return call_object;
188 }
189 
190 /**
191  * Constructs a new instance of the Call class.
192  * @param Channel $channel_obj The channel to associate the call with.
193  *                             Must not be closed.
194  * @param string $method The method to call
195  * @param Timeval $deadline_obj The deadline for completing the call
196  * @param string $host_override The host is set by user (optional)
197  */
PHP_METHOD(Call,__construct)198 PHP_METHOD(Call, __construct) {
199   zval *channel_obj;
200   char *method;
201   php_grpc_int method_len;
202   zval *deadline_obj;
203   char *host_override = NULL;
204   php_grpc_int host_override_len = 0;
205   wrapped_grpc_call *call = PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_call,
206                                                         getThis());
207 
208   /* "OsO|s" == 1 Object, 1 string, 1 Object, 1 optional string */
209   if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "OsO|s", &channel_obj,
210                             grpc_ce_channel, &method, &method_len,
211                             &deadline_obj, grpc_ce_timeval, &host_override,
212                             &host_override_len) == FAILURE) {
213     zend_throw_exception(spl_ce_InvalidArgumentException,
214                          "Call expects a Channel, a String, a Timeval and "
215                          "an optional String", 1 TSRMLS_CC);
216     return;
217   }
218   wrapped_grpc_channel *channel =
219     PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_channel, channel_obj);
220   gpr_mu_lock(&channel->wrapper->mu);
221   if (channel->wrapper == NULL || channel->wrapper->wrapped == NULL) {
222     zend_throw_exception(spl_ce_InvalidArgumentException,
223                          "Call cannot be constructed from a closed Channel",
224                          1 TSRMLS_CC);
225     gpr_mu_unlock(&channel->wrapper->mu);
226     return;
227   }
228   add_property_zval(getThis(), "channel", channel_obj);
229   wrapped_grpc_timeval *deadline =
230     PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_timeval, deadline_obj);
231   grpc_slice method_slice = grpc_slice_from_copied_string(method);
232   grpc_slice host_slice = host_override != NULL ?
233       grpc_slice_from_copied_string(host_override) : grpc_empty_slice();
234   call->wrapped =
235     grpc_channel_create_call(channel->wrapper->wrapped, NULL,
236                              GRPC_PROPAGATE_DEFAULTS,
237                              completion_queue, method_slice,
238                              host_override != NULL ? &host_slice : NULL,
239                              deadline->wrapped, NULL);
240   grpc_slice_unref(method_slice);
241   grpc_slice_unref(host_slice);
242   call->owned = true;
243   call->channel = channel;
244   gpr_mu_unlock(&channel->wrapper->mu);
245 }
246 
247 /**
248  * Start a batch of RPC actions.
249  * @param array $array Array of actions to take
250  * @return object Object with results of all actions
251  */
PHP_METHOD(Call,startBatch)252 PHP_METHOD(Call, startBatch) {
253   zval *result;
254   PHP_GRPC_MAKE_STD_ZVAL(result);
255   object_init(result);
256   php_grpc_ulong index;
257   zval *recv_status;
258   zval *value;
259   zval *inner_value;
260   zval *message_value;
261   zval *message_flags;
262   wrapped_grpc_call *call = PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_call,
263                                                         getThis());
264   if (call->channel) {
265     // startBatch in gRPC PHP server doesn't have channel in it.
266     if (call->channel->wrapper == NULL ||
267         call->channel->wrapper->wrapped == NULL) {
268       zend_throw_exception(spl_ce_RuntimeException,
269                            "startBatch Error. Channel is closed",
270                            1 TSRMLS_CC);
271     }
272   }
273 
274   grpc_op ops[8];
275   size_t op_num = 0;
276   zval *array;
277   HashTable *array_hash;
278   HashTable *status_hash;
279   HashTable *message_hash;
280 
281   grpc_metadata_array metadata;
282   grpc_metadata_array trailing_metadata;
283   grpc_metadata_array recv_metadata;
284   grpc_metadata_array recv_trailing_metadata;
285   grpc_status_code status;
286   grpc_slice recv_status_details = grpc_empty_slice();
287   grpc_slice send_status_details = grpc_empty_slice();
288   grpc_byte_buffer *message;
289   int cancelled;
290   grpc_call_error error;
291   char *message_str;
292   size_t message_len;
293 
294   grpc_metadata_array_init(&metadata);
295   grpc_metadata_array_init(&trailing_metadata);
296   grpc_metadata_array_init(&recv_metadata);
297   grpc_metadata_array_init(&recv_trailing_metadata);
298   memset(ops, 0, sizeof(ops));
299 
300   /* "a" == 1 array */
301   if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &array) ==
302       FAILURE) {
303     zend_throw_exception(spl_ce_InvalidArgumentException,
304                          "start_batch expects an array", 1 TSRMLS_CC);
305     goto cleanup;
306   }
307 
308   array_hash = Z_ARRVAL_P(array);
309 
310   char *key = NULL;
311   int key_type;
312   PHP_GRPC_HASH_FOREACH_LONG_KEY_VAL_START(array_hash, key, key_type, index,
313                                            value)
314     if (key_type != HASH_KEY_IS_LONG || key != NULL) {
315       zend_throw_exception(spl_ce_InvalidArgumentException,
316                            "batch keys must be integers", 1 TSRMLS_CC);
317       goto cleanup;
318     }
319 
320     ops[op_num].op = (grpc_op_type)index;
321     ops[op_num].flags = 0;
322     ops[op_num].reserved = NULL;
323 
324     switch(index) {
325     case GRPC_OP_SEND_INITIAL_METADATA:
326       if (!create_metadata_array(value, &metadata)) {
327         zend_throw_exception(spl_ce_InvalidArgumentException,
328                              "Bad metadata value given", 1 TSRMLS_CC);
329         goto cleanup;
330       }
331       ops[op_num].data.send_initial_metadata.count = metadata.count;
332       ops[op_num].data.send_initial_metadata.metadata = metadata.metadata;
333       break;
334     case GRPC_OP_SEND_MESSAGE:
335       if (Z_TYPE_P(value) != IS_ARRAY) {
336         zend_throw_exception(spl_ce_InvalidArgumentException,
337                              "Expected an array for send message",
338                              1 TSRMLS_CC);
339         goto cleanup;
340       }
341       message_hash = Z_ARRVAL_P(value);
342       if (php_grpc_zend_hash_find(message_hash, "flags", sizeof("flags"),
343                          (void **)&message_flags) == SUCCESS) {
344         if (Z_TYPE_P(message_flags) != IS_LONG) {
345           zend_throw_exception(spl_ce_InvalidArgumentException,
346                                "Expected an int for message flags",
347                                1 TSRMLS_CC);
348         }
349         ops[op_num].flags = Z_LVAL_P(message_flags) & GRPC_WRITE_USED_MASK;
350       }
351       if (php_grpc_zend_hash_find(message_hash, "message", sizeof("message"),
352                          (void **)&message_value) != SUCCESS ||
353           Z_TYPE_P(message_value) != IS_STRING) {
354         zend_throw_exception(spl_ce_InvalidArgumentException,
355                              "Expected a string for send message",
356                              1 TSRMLS_CC);
357         goto cleanup;
358       }
359       ops[op_num].data.send_message.send_message =
360           string_to_byte_buffer(Z_STRVAL_P(message_value),
361                                 Z_STRLEN_P(message_value));
362       break;
363     case GRPC_OP_SEND_CLOSE_FROM_CLIENT:
364       break;
365     case GRPC_OP_SEND_STATUS_FROM_SERVER:
366       status_hash = Z_ARRVAL_P(value);
367       if (php_grpc_zend_hash_find(status_hash, "metadata", sizeof("metadata"),
368                          (void **)&inner_value) == SUCCESS) {
369         if (!create_metadata_array(inner_value, &trailing_metadata)) {
370           zend_throw_exception(spl_ce_InvalidArgumentException,
371                                "Bad trailing metadata value given",
372                                1 TSRMLS_CC);
373           goto cleanup;
374         }
375         ops[op_num].data.send_status_from_server.trailing_metadata =
376             trailing_metadata.metadata;
377         ops[op_num].data.send_status_from_server.trailing_metadata_count =
378             trailing_metadata.count;
379       }
380       if (php_grpc_zend_hash_find(status_hash, "code", sizeof("code"),
381                          (void**)&inner_value) == SUCCESS) {
382         if (Z_TYPE_P(inner_value) != IS_LONG) {
383           zend_throw_exception(spl_ce_InvalidArgumentException,
384                                "Status code must be an integer",
385                                1 TSRMLS_CC);
386           goto cleanup;
387         }
388         ops[op_num].data.send_status_from_server.status =
389             Z_LVAL_P(inner_value);
390       } else {
391         zend_throw_exception(spl_ce_InvalidArgumentException,
392                              "Integer status code is required",
393                              1 TSRMLS_CC);
394         goto cleanup;
395       }
396       if (php_grpc_zend_hash_find(status_hash, "details", sizeof("details"),
397                          (void**)&inner_value) == SUCCESS) {
398         if (Z_TYPE_P(inner_value) != IS_STRING) {
399           zend_throw_exception(spl_ce_InvalidArgumentException,
400                                "Status details must be a string",
401                                1 TSRMLS_CC);
402           goto cleanup;
403         }
404         send_status_details = grpc_slice_from_copied_string(
405           Z_STRVAL_P(inner_value));
406         ops[op_num].data.send_status_from_server.status_details =
407           &send_status_details;
408       } else {
409         zend_throw_exception(spl_ce_InvalidArgumentException,
410                              "String status details is required",
411                              1 TSRMLS_CC);
412         goto cleanup;
413       }
414       break;
415     case GRPC_OP_RECV_INITIAL_METADATA:
416       ops[op_num].data.recv_initial_metadata.recv_initial_metadata =
417           &recv_metadata;
418       break;
419     case GRPC_OP_RECV_MESSAGE:
420       ops[op_num].data.recv_message.recv_message = &message;
421       break;
422     case GRPC_OP_RECV_STATUS_ON_CLIENT:
423       ops[op_num].data.recv_status_on_client.trailing_metadata =
424           &recv_trailing_metadata;
425       ops[op_num].data.recv_status_on_client.status = &status;
426       ops[op_num].data.recv_status_on_client.status_details =
427           &recv_status_details;
428       break;
429     case GRPC_OP_RECV_CLOSE_ON_SERVER:
430       ops[op_num].data.recv_close_on_server.cancelled = &cancelled;
431       break;
432     default:
433       zend_throw_exception(spl_ce_InvalidArgumentException,
434                            "Unrecognized key in batch", 1 TSRMLS_CC);
435       goto cleanup;
436     }
437     op_num++;
438   PHP_GRPC_HASH_FOREACH_END()
439 
440   error = grpc_call_start_batch(call->wrapped, ops, op_num, call->wrapped,
441                                 NULL);
442   if (error != GRPC_CALL_OK) {
443     zend_throw_exception(spl_ce_LogicException,
444                          "start_batch was called incorrectly",
445                          (long)error TSRMLS_CC);
446     goto cleanup;
447   }
448   grpc_completion_queue_pluck(completion_queue, call->wrapped,
449                               gpr_inf_future(GPR_CLOCK_REALTIME), NULL);
450 #if PHP_MAJOR_VERSION >= 7
451   zval *recv_md;
452 #endif
453   for (int i = 0; i < op_num; i++) {
454     switch(ops[i].op) {
455     case GRPC_OP_SEND_INITIAL_METADATA:
456       add_property_bool(result, "send_metadata", true);
457       break;
458     case GRPC_OP_SEND_MESSAGE:
459       add_property_bool(result, "send_message", true);
460       break;
461     case GRPC_OP_SEND_CLOSE_FROM_CLIENT:
462       add_property_bool(result, "send_close", true);
463       break;
464     case GRPC_OP_SEND_STATUS_FROM_SERVER:
465       add_property_bool(result, "send_status", true);
466       break;
467     case GRPC_OP_RECV_INITIAL_METADATA:
468 #if PHP_MAJOR_VERSION < 7
469       array = grpc_parse_metadata_array(&recv_metadata TSRMLS_CC);
470       add_property_zval(result, "metadata", array);
471 #else
472       recv_md = grpc_parse_metadata_array(&recv_metadata);
473       add_property_zval(result, "metadata", recv_md);
474       zval_ptr_dtor(recv_md);
475       PHP_GRPC_FREE_STD_ZVAL(recv_md);
476 #endif
477       PHP_GRPC_DELREF(array);
478       break;
479     case GRPC_OP_RECV_MESSAGE:
480       byte_buffer_to_string(message, &message_str, &message_len);
481       if (message_str == NULL) {
482         add_property_null(result, "message");
483       } else {
484         php_grpc_add_property_stringl(result, "message", message_str,
485                                       message_len, false);
486       }
487       break;
488     case GRPC_OP_RECV_STATUS_ON_CLIENT:
489       PHP_GRPC_MAKE_STD_ZVAL(recv_status);
490       object_init(recv_status);
491 #if PHP_MAJOR_VERSION < 7
492       array = grpc_parse_metadata_array(&recv_trailing_metadata TSRMLS_CC);
493       add_property_zval(recv_status, "metadata", array);
494 #else
495       recv_md = grpc_parse_metadata_array(&recv_trailing_metadata);
496       add_property_zval(recv_status, "metadata", recv_md);
497       zval_ptr_dtor(recv_md);
498       PHP_GRPC_FREE_STD_ZVAL(recv_md);
499 #endif
500       PHP_GRPC_DELREF(array);
501       add_property_long(recv_status, "code", status);
502       char *status_details_text = grpc_slice_to_c_string(recv_status_details);
503       php_grpc_add_property_string(recv_status, "details", status_details_text,
504                                    true);
505       gpr_free(status_details_text);
506       add_property_zval(result, "status", recv_status);
507 #if PHP_MAJOR_VERSION >= 7
508       zval_ptr_dtor(recv_status);
509 #endif
510       PHP_GRPC_DELREF(recv_status);
511       PHP_GRPC_FREE_STD_ZVAL(recv_status);
512       break;
513     case GRPC_OP_RECV_CLOSE_ON_SERVER:
514       add_property_bool(result, "cancelled", cancelled);
515       break;
516     default:
517       break;
518     }
519   }
520 
521 cleanup:
522   grpc_php_metadata_array_destroy_including_entries(&metadata);
523   grpc_php_metadata_array_destroy_including_entries(&trailing_metadata);
524   grpc_metadata_array_destroy(&recv_metadata);
525   grpc_metadata_array_destroy(&recv_trailing_metadata);
526   grpc_slice_unref(recv_status_details);
527   grpc_slice_unref(send_status_details);
528   for (int i = 0; i < op_num; i++) {
529     if (ops[i].op == GRPC_OP_SEND_MESSAGE) {
530       grpc_byte_buffer_destroy(ops[i].data.send_message.send_message);
531     }
532     if (ops[i].op == GRPC_OP_RECV_MESSAGE) {
533       grpc_byte_buffer_destroy(message);
534       PHP_GRPC_FREE_STD_ZVAL(message_str);
535     }
536   }
537   RETURN_DESTROY_ZVAL(result);
538 }
539 
540 /**
541  * Get the endpoint this call/stream is connected to
542  * @return string The URI of the endpoint
543  */
PHP_METHOD(Call,getPeer)544 PHP_METHOD(Call, getPeer) {
545   wrapped_grpc_call *call = PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_call,
546                                                         getThis());
547   char *peer = grpc_call_get_peer(call->wrapped);
548   PHP_GRPC_RETVAL_STRING(peer, 1);
549   gpr_free(peer);
550 }
551 
552 /**
553  * Cancel the call. This will cause the call to end with STATUS_CANCELLED
554  * if it has not already ended with another status.
555  * @return void
556  */
PHP_METHOD(Call,cancel)557 PHP_METHOD(Call, cancel) {
558   wrapped_grpc_call *call = PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_call,
559                                                         getThis());
560   grpc_call_cancel(call->wrapped, NULL);
561 }
562 
563 /**
564  * Set the CallCredentials for this call.
565  * @param CallCredentials $creds_obj The CallCredentials object
566  * @return int The error code
567  */
PHP_METHOD(Call,setCredentials)568 PHP_METHOD(Call, setCredentials) {
569   zval *creds_obj;
570 
571   /* "O" == 1 Object */
572   if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &creds_obj,
573                             grpc_ce_call_credentials) == FAILURE) {
574     zend_throw_exception(spl_ce_InvalidArgumentException,
575                          "setCredentials expects 1 CallCredentials",
576                          1 TSRMLS_CC);
577     return;
578   }
579 
580   wrapped_grpc_call_credentials *creds =
581     PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_call_credentials, creds_obj);
582   wrapped_grpc_call *call = PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_call,
583                                                         getThis());
584 
585   grpc_call_error error = GRPC_CALL_ERROR;
586   error = grpc_call_set_credentials(call->wrapped, creds->wrapped);
587   RETURN_LONG(error);
588 }
589 
590 ZEND_BEGIN_ARG_INFO_EX(arginfo_construct, 0, 0, 3)
591   ZEND_ARG_INFO(0, channel)
592   ZEND_ARG_INFO(0, method)
593   ZEND_ARG_INFO(0, deadline)
594   ZEND_ARG_INFO(0, host_override)
595 ZEND_END_ARG_INFO()
596 
597 ZEND_BEGIN_ARG_INFO_EX(arginfo_startBatch, 0, 0, 1)
598   ZEND_ARG_INFO(0, ops)
599 ZEND_END_ARG_INFO()
600 
601 ZEND_BEGIN_ARG_INFO_EX(arginfo_getPeer, 0, 0, 0)
602 ZEND_END_ARG_INFO()
603 
604 ZEND_BEGIN_ARG_INFO_EX(arginfo_cancel, 0, 0, 0)
605 ZEND_END_ARG_INFO()
606 
607 ZEND_BEGIN_ARG_INFO_EX(arginfo_setCredentials, 0, 0, 1)
608   ZEND_ARG_INFO(0, credentials)
609 ZEND_END_ARG_INFO()
610 
611 static zend_function_entry call_methods[] = {
612   PHP_ME(Call, __construct, arginfo_construct, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
613   PHP_ME(Call, startBatch, arginfo_startBatch, ZEND_ACC_PUBLIC)
614   PHP_ME(Call, getPeer, arginfo_getPeer, ZEND_ACC_PUBLIC)
615   PHP_ME(Call, cancel, arginfo_cancel, ZEND_ACC_PUBLIC)
616   PHP_ME(Call, setCredentials, arginfo_setCredentials, ZEND_ACC_PUBLIC)
617   PHP_FE_END
618 };
619 
grpc_init_call(TSRMLS_D)620 void grpc_init_call(TSRMLS_D) {
621   zend_class_entry ce;
622   INIT_CLASS_ENTRY(ce, "Grpc\\Call", call_methods);
623   ce.create_object = create_wrapped_grpc_call;
624   grpc_ce_call = zend_register_internal_class(&ce TSRMLS_CC);
625   PHP_GRPC_INIT_HANDLER(wrapped_grpc_call, call_ce_handlers);
626 }
627