Imported Upstream version 1.37.1
[platform/upstream/grpc.git] / src / php / ext / grpc / call.c
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 /**
20  * class Call
21  * @see https://github.com/grpc/grpc/tree/master/src/php/ext/grpc/call.c
22  */
23
24 #include "call.h"
25
26 #include <ext/spl/spl_exceptions.h>
27 #include <zend_exceptions.h>
28
29 #include <grpc/support/alloc.h>
30
31 #include "call_credentials.h"
32 #include "completion_queue.h"
33 #include "timeval.h"
34 #include "channel.h"
35 #include "byte_buffer.h"
36
37 zend_class_entry *grpc_ce_call;
38 PHP_GRPC_DECLARE_OBJECT_HANDLER(call_ce_handlers)
39
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);
44   }
45 PHP_GRPC_FREE_WRAPPED_FUNC_END()
46
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
50                                               TSRMLS_DC) {
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);
55 }
56
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;
63   zval *array;
64   PHP_GRPC_MAKE_STD_ZVAL(array);
65   array_init(array);
66   int i;
67   HashTable *array_hash;
68   zval *inner_array;
69   char *str_key;
70   char *str_val;
71   size_t key_len;
72   zval *data = NULL;
73
74   array_hash = Z_ARRVAL_P(array);
75   grpc_metadata *elem;
76   for (i = 0; i < count; i++) {
77     elem = &elements[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)
85         == SUCCESS) {
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.",
89                              1 TSRMLS_CC);
90         efree(str_key);
91         efree(str_val);
92         PHP_GRPC_FREE_STD_ZVAL(array);
93         return NULL;
94       }
95       php_grpc_add_next_index_stringl(data, str_val,
96                                       GRPC_SLICE_LENGTH(elem->value),
97                                       false);
98     } else {
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);
105     }
106     efree(str_key);
107     efree(str_val);
108   }
109   return array;
110 }
111
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;
117   zval *value;
118   zval *inner_array;
119   grpc_metadata_array_init(metadata);
120   metadata->count = 0;
121   metadata->metadata = NULL;
122   if (Z_TYPE_P(array) != IS_ARRAY) {
123     return false;
124   }
125   array_hash = Z_ARRVAL_P(array);
126
127   char *key;
128   int key_type;
129   PHP_GRPC_HASH_FOREACH_STR_KEY_VAL_START(array_hash, key, key_type,
130                                           inner_array)
131     if (key_type != HASH_KEY_IS_STRING || key == NULL) {
132       return false;
133     }
134     if (Z_TYPE_P(inner_array) != IS_ARRAY) {
135       return false;
136     }
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()
140
141   metadata->metadata = gpr_malloc(metadata->capacity * sizeof(grpc_metadata));
142
143   char *key1 = NULL;
144   int key_type1;
145   PHP_GRPC_HASH_FOREACH_STR_KEY_VAL_START(array_hash, key1, key_type1,
146                                           inner_array)
147     if (key_type1 != HASH_KEY_IS_STRING) {
148       return false;
149     }
150     if (!grpc_header_key_is_legal(grpc_slice_from_static_string(key1))) {
151       return false;
152     }
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) {
156         return false;
157       }
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()
165   return true;
166 }
167
168 void grpc_php_metadata_array_destroy_including_entries(
169     grpc_metadata_array* array) {
170   size_t i;
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);
175     }
176   }
177   grpc_metadata_array_destroy(array);
178 }
179
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) {
183   zval *call_object;
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,
187                                                         call_object);
188   call->wrapped = wrapped;
189   call->owned = owned;
190   return call_object;
191 }
192
193 /**
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)
200  */
201 PHP_METHOD(Call, __construct) {
202   zval *channel_obj;
203   char *method;
204   php_grpc_int method_len;
205   zval *deadline_obj;
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,
209                                                         getThis());
210
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);
219     return;
220   }
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",
226                          1 TSRMLS_CC);
227     return;
228   }
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",
233                          1 TSRMLS_CC);
234     gpr_mu_unlock(&channel->wrapper->mu);
235     return;
236   }
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();
243   call->wrapped =
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);
251   call->owned = true;
252   call->channel = channel;
253   gpr_mu_unlock(&channel->wrapper->mu);
254 }
255
256 /**
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
260  */
261 PHP_METHOD(Call, startBatch) {
262   zval *result;
263   PHP_GRPC_MAKE_STD_ZVAL(result);
264   object_init(result);
265   php_grpc_ulong index;
266   zval *recv_status;
267   zval *value;
268   zval *inner_value;
269   zval *message_value;
270   zval *message_flags;
271   wrapped_grpc_call *call = PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_call,
272                                                         getThis());
273   if (call->channel) {
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",
279                            1 TSRMLS_CC);
280     }
281   }
282   
283   grpc_op ops[8];
284   size_t op_num = 0;
285   zval *array;
286   HashTable *array_hash;
287   HashTable *status_hash;
288   HashTable *message_hash;
289
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;
298   int cancelled;
299   grpc_call_error error;
300
301   zend_string* zmessage = NULL;
302
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));
308   
309   /* "a" == 1 array */
310   if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &array) ==
311       FAILURE) {
312     zend_throw_exception(spl_ce_InvalidArgumentException,
313                          "start_batch expects an array", 1 TSRMLS_CC);
314     goto cleanup;
315   }
316
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);
320   srand(now.tv_nsec);
321
322   array_hash = Z_ARRVAL_P(array);
323
324   char *key = NULL;
325   int key_type;
326   PHP_GRPC_HASH_FOREACH_LONG_KEY_VAL_START(array_hash, key, key_type, index,
327                                            value)
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);
331       goto cleanup;
332     }
333
334     ops[op_num].op = (grpc_op_type)index;
335     ops[op_num].flags = 0;
336     ops[op_num].reserved = NULL;
337
338     switch(index) {
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);
343         goto cleanup;
344       }
345       ops[op_num].data.send_initial_metadata.count = metadata.count;
346       ops[op_num].data.send_initial_metadata.metadata = metadata.metadata;
347       break;
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",
352                              1 TSRMLS_CC);
353         goto cleanup;
354       }
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",
361                                1 TSRMLS_CC);
362         }
363         ops[op_num].flags = Z_LVAL_P(message_flags) & GRPC_WRITE_USED_MASK;
364       }
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",
370                              1 TSRMLS_CC);
371         goto cleanup;
372       }
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));
376       break;
377     case GRPC_OP_SEND_CLOSE_FROM_CLIENT:
378       break;
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",
386                                1 TSRMLS_CC);
387           goto cleanup;
388         }
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;
393       }
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",
399                                1 TSRMLS_CC);
400           goto cleanup;
401         }
402         ops[op_num].data.send_status_from_server.status =
403             Z_LVAL_P(inner_value);
404       } else {
405         zend_throw_exception(spl_ce_InvalidArgumentException,
406                              "Integer status code is required",
407                              1 TSRMLS_CC);
408         goto cleanup;
409       }
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",
415                                1 TSRMLS_CC);
416           goto cleanup;
417         }
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;
422       } else {
423         zend_throw_exception(spl_ce_InvalidArgumentException,
424                              "String status details is required",
425                              1 TSRMLS_CC);
426         goto cleanup;
427       }
428       break;
429     case GRPC_OP_RECV_INITIAL_METADATA:
430       ops[op_num].data.recv_initial_metadata.recv_initial_metadata =
431           &recv_metadata;
432       break;
433     case GRPC_OP_RECV_MESSAGE:
434       ops[op_num].data.recv_message.recv_message = &message;
435       break;
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;
442       break;
443     case GRPC_OP_RECV_CLOSE_ON_SERVER:
444       ops[op_num].data.recv_close_on_server.cancelled = &cancelled;
445       break;
446     default:
447       zend_throw_exception(spl_ce_InvalidArgumentException,
448                            "Unrecognized key in batch", 1 TSRMLS_CC);
449       goto cleanup;
450     }
451     op_num++;
452   PHP_GRPC_HASH_FOREACH_END()
453
454   error = grpc_call_start_batch(call->wrapped, ops, op_num, call->wrapped,
455                                 NULL);
456   if (error != GRPC_CALL_OK) {
457     zend_throw_exception(spl_ce_LogicException,
458                          "start_batch was called incorrectly",
459                          (long)error TSRMLS_CC);
460     goto cleanup;
461   }
462   grpc_completion_queue_pluck(completion_queue, call->wrapped,
463                               gpr_inf_future(GPR_CLOCK_REALTIME), NULL);
464   zval *recv_md;
465   for (int i = 0; i < op_num; i++) {
466     switch(ops[i].op) {
467     case GRPC_OP_SEND_INITIAL_METADATA:
468       add_property_bool(result, "send_metadata", true);
469       break;
470     case GRPC_OP_SEND_MESSAGE:
471       add_property_bool(result, "send_message", true);
472       break;
473     case GRPC_OP_SEND_CLOSE_FROM_CLIENT:
474       add_property_bool(result, "send_close", true);
475       break;
476     case GRPC_OP_SEND_STATUS_FROM_SERVER:
477       add_property_bool(result, "send_status", true);
478       break;
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);
485       break;
486     case GRPC_OP_RECV_MESSAGE:
487       zmessage = byte_buffer_to_zend_string(message);
488
489       if (zmessage == NULL) {
490         add_property_null(result, "message");
491       } else {
492         zval zmessage_val;
493         ZVAL_NEW_STR(&zmessage_val, zmessage);
494         add_property_zval(result, "message", &zmessage_val);
495         zval_ptr_dtor(&zmessage_val);
496       }
497       break;
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,
509                                    true);
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);
515       break;
516     case GRPC_OP_RECV_CLOSE_ON_SERVER:
517       add_property_bool(result, "cancelled", cancelled);
518       break;
519     default:
520       break;
521     }
522   }
523
524 cleanup:
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);
534     }
535     if (ops[i].op == GRPC_OP_RECV_MESSAGE) {
536       grpc_byte_buffer_destroy(message);
537     }
538   }
539   RETURN_DESTROY_ZVAL(result);
540 }
541
542 /**
543  * Get the endpoint this call/stream is connected to
544  * @return string The URI of the endpoint
545  */
546 PHP_METHOD(Call, getPeer) {
547   wrapped_grpc_call *call = PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_call,
548                                                         getThis());
549   char *peer = grpc_call_get_peer(call->wrapped);
550   PHP_GRPC_RETVAL_STRING(peer, 1);
551   gpr_free(peer);
552 }
553
554 /**
555  * Cancel the call. This will cause the call to end with STATUS_CANCELLED
556  * if it has not already ended with another status.
557  * @return void
558  */
559 PHP_METHOD(Call, cancel) {
560   wrapped_grpc_call *call = PHP_GRPC_GET_WRAPPED_OBJECT(wrapped_grpc_call,
561                                                         getThis());
562   grpc_call_cancel(call->wrapped, NULL);
563 }
564
565 /**
566  * Set the CallCredentials for this call.
567  * @param CallCredentials $creds_obj The CallCredentials object
568  * @return int The error code
569  */
570 PHP_METHOD(Call, setCredentials) {
571   zval *creds_obj;
572
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",
578                          1 TSRMLS_CC);
579     return;
580   }
581
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,
585                                                         getThis());
586
587   grpc_call_error error = GRPC_CALL_ERROR;
588   error = grpc_call_set_credentials(call->wrapped, creds->wrapped);
589   RETURN_LONG(error);
590 }
591
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)
597 ZEND_END_ARG_INFO()
598
599 ZEND_BEGIN_ARG_INFO_EX(arginfo_startBatch, 0, 0, 1)
600   ZEND_ARG_INFO(0, ops)
601 ZEND_END_ARG_INFO()
602
603 ZEND_BEGIN_ARG_INFO_EX(arginfo_getPeer, 0, 0, 0)
604 ZEND_END_ARG_INFO()
605
606 ZEND_BEGIN_ARG_INFO_EX(arginfo_cancel, 0, 0, 0)
607 ZEND_END_ARG_INFO()
608
609 ZEND_BEGIN_ARG_INFO_EX(arginfo_setCredentials, 0, 0, 1)
610   ZEND_ARG_INFO(0, credentials)
611 ZEND_END_ARG_INFO()
612
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)
619   PHP_FE_END
620 };
621
622 void grpc_init_call(TSRMLS_D) {
623   zend_class_entry ce;
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);
628 }