blob: ba1b2a407d3027a791bd3097cf6e25b9a41cc08d [file] [log] [blame]
Craig Tiller1a61b172015-02-16 11:53:47 -08001/*
2 *
3 * Copyright 2015, Google Inc.
4 * 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
40#include "php.h"
41#include "php_ini.h"
42#include "ext/standard/info.h"
43#include "ext/spl/spl_exceptions.h"
44#include "php_grpc.h"
45
46#include "zend_exceptions.h"
47#include "zend_hash.h"
48
49#include <stdbool.h>
50
51#include "grpc/support/log.h"
murgatroid99afd541c2015-03-03 18:16:09 -080052#include "grpc/support/alloc.h"
mlumishb892a272014-12-09 16:28:23 -080053#include "grpc/grpc.h"
54
55#include "timeval.h"
56#include "channel.h"
mlumishb892a272014-12-09 16:28:23 -080057#include "byte_buffer.h"
58
Xiaoguang Sun8a929a92015-03-13 14:22:31 +080059zend_class_entry *grpc_ce_call;
60
mlumishb892a272014-12-09 16:28:23 -080061/* Frees and destroys an instance of wrapped_grpc_call */
Craig Tillerb5dcec52015-01-13 11:13:42 -080062void free_wrapped_grpc_call(void *object TSRMLS_DC) {
63 wrapped_grpc_call *call = (wrapped_grpc_call *)object;
murgatroid99afd541c2015-03-03 18:16:09 -080064 grpc_event *event;
Craig Tillerb5dcec52015-01-13 11:13:42 -080065 if (call->owned && call->wrapped != NULL) {
murgatroid999fe516a2015-03-11 14:47:10 -070066 if (call->queue != NULL) {
67 grpc_completion_queue_shutdown(call->queue);
68 event = grpc_completion_queue_next(call->queue, gpr_inf_future);
69 while (event != NULL) {
70 if (event->type == GRPC_QUEUE_SHUTDOWN) {
71 break;
72 }
73 event = grpc_completion_queue_next(call->queue, gpr_inf_future);
74 }
75 grpc_completion_queue_destroy(call->queue);
76 }
mlumishb892a272014-12-09 16:28:23 -080077 grpc_call_destroy(call->wrapped);
78 }
79 efree(call);
80}
81
82/* Initializes an instance of wrapped_grpc_call to be associated with an object
83 * of a class specified by class_type */
Craig Tillerb5dcec52015-01-13 11:13:42 -080084zend_object_value create_wrapped_grpc_call(zend_class_entry *class_type
85 TSRMLS_DC) {
mlumishb892a272014-12-09 16:28:23 -080086 zend_object_value retval;
87 wrapped_grpc_call *intern;
88
Craig Tillerb5dcec52015-01-13 11:13:42 -080089 intern = (wrapped_grpc_call *)emalloc(sizeof(wrapped_grpc_call));
mlumishb892a272014-12-09 16:28:23 -080090 memset(intern, 0, sizeof(wrapped_grpc_call));
91
92 zend_object_std_init(&intern->std, class_type TSRMLS_CC);
93 object_properties_init(&intern->std, class_type);
94 retval.handle = zend_objects_store_put(
Craig Tillerb5dcec52015-01-13 11:13:42 -080095 intern, (zend_objects_store_dtor_t)zend_objects_destroy_object,
96 free_wrapped_grpc_call, NULL TSRMLS_CC);
mlumishb892a272014-12-09 16:28:23 -080097 retval.handlers = zend_get_std_object_handlers();
98 return retval;
99}
100
mlumish34cd1f02015-01-02 13:32:41 -0800101/* Wraps a grpc_call struct in a PHP object. Owned indicates whether the struct
102 should be destroyed at the end of the object's lifecycle */
murgatroid999fe516a2015-03-11 14:47:10 -0700103zval *grpc_php_wrap_call(grpc_call *wrapped, grpc_completion_queue *queue,
104 bool owned) {
mlumishb892a272014-12-09 16:28:23 -0800105 zval *call_object;
106 MAKE_STD_ZVAL(call_object);
107 object_init_ex(call_object, grpc_ce_call);
Craig Tillerb5dcec52015-01-13 11:13:42 -0800108 wrapped_grpc_call *call =
109 (wrapped_grpc_call *)zend_object_store_get_object(call_object TSRMLS_CC);
mlumishb892a272014-12-09 16:28:23 -0800110 call->wrapped = wrapped;
murgatroid999fe516a2015-03-11 14:47:10 -0700111 call->queue = queue;
mlumishb892a272014-12-09 16:28:23 -0800112 return call_object;
113}
114
murgatroid999c4425a2015-03-24 09:43:41 -0700115/* Creates and returns a PHP array object with the data in a
116 * grpc_metadata_array. Returns NULL on failure */
murgatroid99afd541c2015-03-03 18:16:09 -0800117zval *grpc_parse_metadata_array(grpc_metadata_array *metadata_array) {
118 int count = metadata_array->count;
119 grpc_metadata *elements = metadata_array->metadata;
mlumishb892a272014-12-09 16:28:23 -0800120 int i;
121 zval *array;
122 zval **data = NULL;
123 HashTable *array_hash;
124 zval *inner_array;
125 char *str_key;
126 char *str_val;
127 size_t key_len;
128 MAKE_STD_ZVAL(array);
129 array_init(array);
130 array_hash = Z_ARRVAL_P(array);
131 grpc_metadata *elem;
Craig Tillerb5dcec52015-01-13 11:13:42 -0800132 for (i = 0; i < count; i++) {
mlumishb892a272014-12-09 16:28:23 -0800133 elem = &elements[i];
134 key_len = strlen(elem->key);
Craig Tillerb5dcec52015-01-13 11:13:42 -0800135 str_key = ecalloc(key_len + 1, sizeof(char));
mlumishb892a272014-12-09 16:28:23 -0800136 memcpy(str_key, elem->key, key_len);
Craig Tillerb5dcec52015-01-13 11:13:42 -0800137 str_val = ecalloc(elem->value_length + 1, sizeof(char));
mlumishb892a272014-12-09 16:28:23 -0800138 memcpy(str_val, elem->value, elem->value_length);
Craig Tillerb5dcec52015-01-13 11:13:42 -0800139 if (zend_hash_find(array_hash, str_key, key_len, (void **)data) ==
140 SUCCESS) {
murgatroid995ca9f922015-02-03 11:21:11 -0800141 if (Z_TYPE_P(*data) != IS_ARRAY) {
142 zend_throw_exception(zend_exception_get_default(),
143 "Metadata hash somehow contains wrong types.",
144 1 TSRMLS_CC);
mlumishb892a272014-12-09 16:28:23 -0800145 efree(str_key);
146 efree(str_val);
147 return NULL;
148 }
murgatroid995ca9f922015-02-03 11:21:11 -0800149 add_next_index_stringl(*data, str_val, elem->value_length, false);
mlumishb892a272014-12-09 16:28:23 -0800150 } else {
murgatroid995ca9f922015-02-03 11:21:11 -0800151 MAKE_STD_ZVAL(inner_array);
152 array_init(inner_array);
153 add_next_index_stringl(inner_array, str_val, elem->value_length, false);
154 add_assoc_zval(array, str_key, inner_array);
mlumishb892a272014-12-09 16:28:23 -0800155 }
156 }
157 return array;
158}
159
murgatroid999c4425a2015-03-24 09:43:41 -0700160/* Populates a grpc_metadata_array with the data in a PHP array object.
161 Returns true on success and false on failure */
murgatroid99afd541c2015-03-03 18:16:09 -0800162bool create_metadata_array(zval *array, grpc_metadata_array *metadata) {
163 zval **inner_array;
164 zval **value;
165 HashTable *array_hash;
166 HashPosition array_pointer;
167 HashTable *inner_array_hash;
168 HashPosition inner_array_pointer;
169 char *key;
170 uint key_len;
171 ulong index;
172 if (Z_TYPE_P(array) != IS_ARRAY) {
173 return false;
174 }
175 grpc_metadata_array_init(metadata);
176 array_hash = Z_ARRVAL_P(array);
177 for (zend_hash_internal_pointer_reset_ex(array_hash, &array_pointer);
178 zend_hash_get_current_data_ex(array_hash, (void**)&inner_array,
179 &array_pointer) == SUCCESS;
180 zend_hash_move_forward_ex(array_hash, &array_pointer)) {
181 if (zend_hash_get_current_key_ex(array_hash, &key, &key_len, &index, 0,
182 &array_pointer) != HASH_KEY_IS_STRING) {
183 return false;
184 }
185 if (Z_TYPE_P(*inner_array) != IS_ARRAY) {
186 return false;
187 }
188 inner_array_hash = Z_ARRVAL_P(*inner_array);
189 metadata->capacity += zend_hash_num_elements(inner_array_hash);
190 }
191 metadata->metadata = gpr_malloc(metadata->capacity * sizeof(grpc_metadata));
192 for (zend_hash_internal_pointer_reset_ex(array_hash, &array_pointer);
193 zend_hash_get_current_data_ex(array_hash, (void**)&inner_array,
194 &array_pointer) == SUCCESS;
195 zend_hash_move_forward_ex(array_hash, &array_pointer)) {
196 if (zend_hash_get_current_key_ex(array_hash, &key, &key_len, &index, 0,
197 &array_pointer) != HASH_KEY_IS_STRING) {
198 return false;
199 }
200 inner_array_hash = Z_ARRVAL_P(*inner_array);
201 for (zend_hash_internal_pointer_reset_ex(inner_array_hash,
202 &inner_array_pointer);
203 zend_hash_get_current_data_ex(inner_array_hash, (void**)&value,
204 &inner_array_pointer) == SUCCESS;
205 zend_hash_move_forward_ex(inner_array_hash, &inner_array_pointer)) {
206 if (Z_TYPE_P(*value) != IS_STRING) {
207 return false;
208 }
209 metadata->metadata[metadata->count].key = key;
210 metadata->metadata[metadata->count].value = Z_STRVAL_P(*value);
211 metadata->metadata[metadata->count].value_length = Z_STRLEN_P(*value);
212 metadata->count += 1;
213 }
214 }
215 return true;
216}
217
mlumishb892a272014-12-09 16:28:23 -0800218/**
219 * Constructs a new instance of the Call class.
220 * @param Channel $channel The channel to associate the call with. Must not be
221 * closed.
222 * @param string $method The method to call
223 * @param Timeval $absolute_deadline The deadline for completing the call
224 */
Craig Tillerb5dcec52015-01-13 11:13:42 -0800225PHP_METHOD(Call, __construct) {
226 wrapped_grpc_call *call =
227 (wrapped_grpc_call *)zend_object_store_get_object(getThis() TSRMLS_CC);
mlumishb892a272014-12-09 16:28:23 -0800228 zval *channel_obj;
229 char *method;
230 int method_len;
231 zval *deadline_obj;
232 /* "OsO" == 1 Object, 1 string, 1 Object */
Craig Tillerb5dcec52015-01-13 11:13:42 -0800233 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "OsO", &channel_obj,
234 grpc_ce_channel, &method, &method_len,
235 &deadline_obj, grpc_ce_timeval) == FAILURE) {
murgatroid99afd541c2015-03-03 18:16:09 -0800236 zend_throw_exception(
237 spl_ce_InvalidArgumentException,
238 "Call expects a Channel, a String, and a Timeval",
239 1 TSRMLS_CC);
mlumishb892a272014-12-09 16:28:23 -0800240 return;
241 }
242 wrapped_grpc_channel *channel =
Craig Tillerb5dcec52015-01-13 11:13:42 -0800243 (wrapped_grpc_channel *)zend_object_store_get_object(
244 channel_obj TSRMLS_CC);
245 if (channel->wrapped == NULL) {
mlumishb892a272014-12-09 16:28:23 -0800246 zend_throw_exception(spl_ce_InvalidArgumentException,
247 "Call cannot be constructed from a closed Channel",
248 1 TSRMLS_CC);
249 return;
250 }
251 add_property_zval(getThis(), "channel", channel_obj);
252 wrapped_grpc_timeval *deadline =
Craig Tillerb5dcec52015-01-13 11:13:42 -0800253 (wrapped_grpc_timeval *)zend_object_store_get_object(
254 deadline_obj TSRMLS_CC);
murgatroid99afd541c2015-03-03 18:16:09 -0800255 call->queue = grpc_completion_queue_create();
256 call->wrapped = grpc_channel_create_call(
257 channel->wrapped, call->queue, method, channel->target,
258 deadline->wrapped);
mlumishb892a272014-12-09 16:28:23 -0800259}
260
261/**
murgatroid99afd541c2015-03-03 18:16:09 -0800262 * Start a batch of RPC actions.
263 * @param array batch Array of actions to take
264 * @return object Object with results of all actions
mlumishb892a272014-12-09 16:28:23 -0800265 */
murgatroid99afd541c2015-03-03 18:16:09 -0800266PHP_METHOD(Call, start_batch) {
Craig Tillerb5dcec52015-01-13 11:13:42 -0800267 wrapped_grpc_call *call =
268 (wrapped_grpc_call *)zend_object_store_get_object(getThis() TSRMLS_CC);
murgatroid99afd541c2015-03-03 18:16:09 -0800269 grpc_op ops[8];
270 size_t op_num = 0;
mlumishb892a272014-12-09 16:28:23 -0800271 zval *array;
murgatroid995ca9f922015-02-03 11:21:11 -0800272 zval **value;
murgatroid99afd541c2015-03-03 18:16:09 -0800273 zval **inner_value;
mlumishb892a272014-12-09 16:28:23 -0800274 HashTable *array_hash;
murgatroid995ca9f922015-02-03 11:21:11 -0800275 HashPosition array_pointer;
murgatroid99afd541c2015-03-03 18:16:09 -0800276 HashTable *status_hash;
murgatroid995ca9f922015-02-03 11:21:11 -0800277 char *key;
278 uint key_len;
279 ulong index;
murgatroid99afd541c2015-03-03 18:16:09 -0800280 grpc_metadata_array metadata;
281 grpc_metadata_array trailing_metadata;
282 grpc_metadata_array recv_metadata;
283 grpc_metadata_array recv_trailing_metadata;
284 grpc_status_code status;
285 char *status_details = NULL;
murgatroid999fe516a2015-03-11 14:47:10 -0700286 size_t status_details_capacity = 0;
murgatroid99afd541c2015-03-03 18:16:09 -0800287 grpc_byte_buffer *message;
288 int cancelled;
289 grpc_call_error error;
290 grpc_event *event;
291 zval *result;
292 char *message_str;
293 size_t message_len;
294 zval *recv_status;
295 grpc_metadata_array_init(&metadata);
296 grpc_metadata_array_init(&trailing_metadata);
murgatroid99d8bb9572015-03-11 09:18:06 -0700297 grpc_metadata_array_init(&recv_metadata);
298 grpc_metadata_array_init(&recv_trailing_metadata);
murgatroid99afd541c2015-03-03 18:16:09 -0800299 MAKE_STD_ZVAL(result);
300 object_init(result);
301 /* "a" == 1 array */
302 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &array) ==
Craig Tillerb5dcec52015-01-13 11:13:42 -0800303 FAILURE) {
mlumishb892a272014-12-09 16:28:23 -0800304 zend_throw_exception(spl_ce_InvalidArgumentException,
murgatroid99afd541c2015-03-03 18:16:09 -0800305 "start_batch expects an array", 1 TSRMLS_CC);
306 goto cleanup;
mlumishb892a272014-12-09 16:28:23 -0800307 }
308 array_hash = Z_ARRVAL_P(array);
murgatroid995ca9f922015-02-03 11:21:11 -0800309 for (zend_hash_internal_pointer_reset_ex(array_hash, &array_pointer);
murgatroid99afd541c2015-03-03 18:16:09 -0800310 zend_hash_get_current_data_ex(array_hash, (void**)&value,
murgatroid995ca9f922015-02-03 11:21:11 -0800311 &array_pointer) == SUCCESS;
312 zend_hash_move_forward_ex(array_hash, &array_pointer)) {
313 if (zend_hash_get_current_key_ex(array_hash, &key, &key_len, &index, 0,
murgatroid99afd541c2015-03-03 18:16:09 -0800314 &array_pointer) != HASH_KEY_IS_LONG) {
murgatroid995ca9f922015-02-03 11:21:11 -0800315 zend_throw_exception(spl_ce_InvalidArgumentException,
murgatroid99afd541c2015-03-03 18:16:09 -0800316 "batch keys must be integers", 1 TSRMLS_CC);
317 goto cleanup;
murgatroid995ca9f922015-02-03 11:21:11 -0800318 }
murgatroid99afd541c2015-03-03 18:16:09 -0800319 switch(index) {
320 case GRPC_OP_SEND_INITIAL_METADATA:
321 if (!create_metadata_array(*value, &metadata)) {
322 zend_throw_exception(spl_ce_InvalidArgumentException,
323 "Bad metadata value given", 1 TSRMLS_CC);
324 goto cleanup;
325 }
326 ops[op_num].data.send_initial_metadata.count =
327 metadata.count;
328 ops[op_num].data.send_initial_metadata.metadata =
329 metadata.metadata;
330 break;
331 case GRPC_OP_SEND_MESSAGE:
332 if (Z_TYPE_PP(value) != IS_STRING) {
333 zend_throw_exception(spl_ce_InvalidArgumentException,
334 "Expected a string for send message",
335 1 TSRMLS_CC);
336 }
337 ops[op_num].data.send_message =
338 string_to_byte_buffer(Z_STRVAL_PP(value), Z_STRLEN_PP(value));
339 break;
340 case GRPC_OP_SEND_CLOSE_FROM_CLIENT:
341 break;
342 case GRPC_OP_SEND_STATUS_FROM_SERVER:
343 status_hash = Z_ARRVAL_PP(value);
344 if (zend_hash_find(status_hash, "metadata", sizeof("metadata"),
345 (void **)&inner_value) == SUCCESS) {
346 if (!create_metadata_array(*inner_value, &trailing_metadata)) {
347 zend_throw_exception(spl_ce_InvalidArgumentException,
348 "Bad trailing metadata value given",
349 1 TSRMLS_CC);
350 goto cleanup;
351 }
352 ops[op_num].data.send_status_from_server.trailing_metadata =
353 trailing_metadata.metadata;
354 ops[op_num].data.send_status_from_server.trailing_metadata_count =
355 trailing_metadata.count;
356 }
357 if (zend_hash_find(status_hash, "code", sizeof("code"),
358 (void**)&inner_value) == SUCCESS) {
359 if (Z_TYPE_PP(inner_value) != IS_LONG) {
360 zend_throw_exception(spl_ce_InvalidArgumentException,
361 "Status code must be an integer",
362 1 TSRMLS_CC);
363 goto cleanup;
364 }
365 ops[op_num].data.send_status_from_server.status =
366 Z_LVAL_PP(inner_value);
367 } else {
368 zend_throw_exception(spl_ce_InvalidArgumentException,
369 "Integer status code is required",
370 1 TSRMLS_CC);
371 goto cleanup;
372 }
373 if (zend_hash_find(status_hash, "details", sizeof("details"),
374 (void**)&inner_value) == SUCCESS) {
375 if (Z_TYPE_PP(inner_value) != IS_STRING) {
376 zend_throw_exception(spl_ce_InvalidArgumentException,
377 "Status details must be a string",
378 1 TSRMLS_CC);
379 goto cleanup;
380 }
381 ops[op_num].data.send_status_from_server.status_details =
382 Z_STRVAL_PP(inner_value);
383 } else {
384 zend_throw_exception(spl_ce_InvalidArgumentException,
385 "String status details is required",
386 1 TSRMLS_CC);
387 goto cleanup;
388 }
389 break;
390 case GRPC_OP_RECV_INITIAL_METADATA:
391 ops[op_num].data.recv_initial_metadata = &recv_metadata;
392 break;
393 case GRPC_OP_RECV_MESSAGE:
394 ops[op_num].data.recv_message = &message;
395 break;
396 case GRPC_OP_RECV_STATUS_ON_CLIENT:
397 ops[op_num].data.recv_status_on_client.trailing_metadata =
398 &recv_trailing_metadata;
399 ops[op_num].data.recv_status_on_client.status = &status;
400 ops[op_num].data.recv_status_on_client.status_details =
401 &status_details;
402 ops[op_num].data.recv_status_on_client.status_details_capacity =
403 &status_details_capacity;
404 break;
405 case GRPC_OP_RECV_CLOSE_ON_SERVER:
406 ops[op_num].data.recv_close_on_server.cancelled = &cancelled;
407 break;
408 default:
murgatroid995ca9f922015-02-03 11:21:11 -0800409 zend_throw_exception(spl_ce_InvalidArgumentException,
murgatroid99afd541c2015-03-03 18:16:09 -0800410 "Unrecognized key in batch", 1 TSRMLS_CC);
411 goto cleanup;
412 }
413 ops[op_num].op = (grpc_op_type)index;
414 op_num++;
415 }
murgatroid999fe516a2015-03-11 14:47:10 -0700416 error = grpc_call_start_batch(call->wrapped, ops, op_num, call->wrapped);
murgatroid99afd541c2015-03-03 18:16:09 -0800417 if (error != GRPC_CALL_OK) {
418 zend_throw_exception(spl_ce_LogicException,
419 "start_batch was called incorrectly",
420 (long)error TSRMLS_CC);
421 goto cleanup;
422 }
murgatroid999fe516a2015-03-11 14:47:10 -0700423 event = grpc_completion_queue_pluck(call->queue, call->wrapped,
424 gpr_inf_future);
murgatroid99afd541c2015-03-03 18:16:09 -0800425 if (event->data.op_complete != GRPC_OP_OK) {
426 zend_throw_exception(spl_ce_LogicException,
427 "The batch failed for some reason",
428 1 TSRMLS_CC);
429 goto cleanup;
430 }
431 for (int i = 0; i < op_num; i++) {
432 switch(ops[i].op) {
433 case GRPC_OP_SEND_INITIAL_METADATA:
434 add_property_bool(result, "send_metadata", true);
435 break;
436 case GRPC_OP_SEND_MESSAGE:
437 add_property_bool(result, "send_message", true);
438 break;
439 case GRPC_OP_SEND_CLOSE_FROM_CLIENT:
440 add_property_bool(result, "send_close", true);
441 break;
442 case GRPC_OP_SEND_STATUS_FROM_SERVER:
443 add_property_bool(result, "send_status", true);
444 break;
445 case GRPC_OP_RECV_INITIAL_METADATA:
446 add_property_zval(result, "metadata",
447 grpc_parse_metadata_array(&recv_metadata));
448 break;
449 case GRPC_OP_RECV_MESSAGE:
450 byte_buffer_to_string(message, &message_str, &message_len);
murgatroid99c1da8f22015-03-25 11:33:05 -0700451 if (message_str == NULL) {
452 add_property_null(result, "message");
453 } else {
454 add_property_stringl(result, "message", message_str, message_len,
455 false);
456 }
murgatroid99afd541c2015-03-03 18:16:09 -0800457 break;
458 case GRPC_OP_RECV_STATUS_ON_CLIENT:
459 MAKE_STD_ZVAL(recv_status);
460 object_init(recv_status);
461 add_property_zval(recv_status, "metadata",
462 grpc_parse_metadata_array(&recv_trailing_metadata));
463 add_property_long(recv_status, "code", status);
murgatroid999fe516a2015-03-11 14:47:10 -0700464 add_property_string(recv_status, "details", status_details, true);
murgatroid99afd541c2015-03-03 18:16:09 -0800465 add_property_zval(result, "status", recv_status);
466 break;
467 case GRPC_OP_RECV_CLOSE_ON_SERVER:
468 add_property_bool(result, "cancelled", cancelled);
469 break;
470 default:
471 break;
murgatroid995ca9f922015-02-03 11:21:11 -0800472 }
473 }
murgatroid99afd541c2015-03-03 18:16:09 -0800474cleanup:
murgatroid99afd541c2015-03-03 18:16:09 -0800475 grpc_metadata_array_destroy(&metadata);
murgatroid99afd541c2015-03-03 18:16:09 -0800476 grpc_metadata_array_destroy(&trailing_metadata);
477 grpc_metadata_array_destroy(&recv_metadata);
478 grpc_metadata_array_destroy(&recv_trailing_metadata);
479 if (status_details != NULL) {
480 gpr_free(status_details);
mlumishdba87892015-01-02 13:27:28 -0800481 }
murgatroid99afd541c2015-03-03 18:16:09 -0800482 RETURN_DESTROY_ZVAL(result);
mlumishb892a272014-12-09 16:28:23 -0800483}
484
murgatroid99c1da8f22015-03-25 11:33:05 -0700485/**
486 * Cancel the call. This will cause the call to end with STATUS_CANCELLED if it
487 * has not already ended with another status.
488 */
489PHP_METHOD(Call, cancel) {
490 wrapped_grpc_call *call =
491 (wrapped_grpc_call *)zend_object_store_get_object(getThis() TSRMLS_CC);
492 grpc_call_cancel(call->wrapped);
493}
494
mlumishb892a272014-12-09 16:28:23 -0800495static zend_function_entry call_methods[] = {
Craig Tillerb5dcec52015-01-13 11:13:42 -0800496 PHP_ME(Call, __construct, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
murgatroid99c1da8f22015-03-25 11:33:05 -0700497 PHP_ME(Call, start_batch, NULL, ZEND_ACC_PUBLIC)
498 PHP_ME(Call, cancel, NULL, ZEND_ACC_PUBLIC) PHP_FE_END};
mlumishb892a272014-12-09 16:28:23 -0800499
Craig Tillerb5dcec52015-01-13 11:13:42 -0800500void grpc_init_call(TSRMLS_D) {
mlumishb892a272014-12-09 16:28:23 -0800501 zend_class_entry ce;
502 INIT_CLASS_ENTRY(ce, "Grpc\\Call", call_methods);
503 ce.create_object = create_wrapped_grpc_call;
504 grpc_ce_call = zend_register_internal_class(&ce TSRMLS_CC);
505}