/** * Close the channel */ PHP_METHOD(Channel, close) { wrapped_grpc_channel *channel = Z_WRAPPED_GRPC_CHANNEL_P(getThis()); if (channel->wrapped != NULL) { grpc_channel_destroy(channel->wrapped); channel->wrapped = NULL; } }
/** * Watch the connectivity state of the channel until it changed * @param long The previous connectivity state of the channel * @param Timeval The deadline this function should wait until * @return bool If the connectivity state changes from last_state * before deadline */ PHP_METHOD(Channel, watchConnectivityState) { wrapped_grpc_channel *channel = Z_WRAPPED_GRPC_CHANNEL_P(getThis()); zend_long last_state; zval *deadline_obj; /* "lO" == 1 long 1 object */ #ifndef FAST_ZPP if (zend_parse_parameters(ZEND_NUM_ARGS(), "lO", &last_state, &deadline_obj, grpc_ce_timeval) == FAILURE) { zend_throw_exception(spl_ce_InvalidArgumentException, "watchConnectivityState expects 1 long 1 timeval", 1); return; } #else ZEND_PARSE_PARAMETERS_START(2, 2) Z_PARAM_LONG(last_state) Z_PARAM_OBJECT_OF_CLASS(deadline_obj, grpc_ce_timeval) ZEND_PARSE_PARAMETERS_END(); #endif wrapped_grpc_timeval *deadline = Z_WRAPPED_GRPC_TIMEVAL_P(deadline_obj); grpc_channel_watch_connectivity_state( channel->wrapped, (grpc_connectivity_state)last_state, deadline->wrapped, completion_queue, NULL); grpc_event event = grpc_completion_queue_pluck( completion_queue, NULL, gpr_inf_future(GPR_CLOCK_REALTIME), NULL); RETURN_BOOL(event.success); }
/** * Construct an instance of the Channel class. If the $args array contains a * "credentials" key mapping to a ChannelCredentials object, a secure channel * will be created with those credentials. * @param string $target The hostname to associate with this channel * @param array $args The arguments to pass to the Channel (optional) */ PHP_METHOD(Channel, __construct) { wrapped_grpc_channel *channel = Z_WRAPPED_GRPC_CHANNEL_P(getThis()); zend_string *target; zval *args_array = NULL; grpc_channel_args args; HashTable *array_hash; zval *creds_obj = NULL; wrapped_grpc_channel_credentials *creds = NULL; /* "Sa" == 1 string, 1 array */ #ifndef FAST_ZPP if (zend_parse_parameters(ZEND_NUM_ARGS(), "Sa", &target, &args_array) == FAILURE) { zend_throw_exception(spl_ce_InvalidArgumentException, "Channel expects a string and an array", 1); return; } #else ZEND_PARSE_PARAMETERS_START(2, 2) Z_PARAM_STR(target) Z_PARAM_ARRAY(args_array) ZEND_PARSE_PARAMETERS_END(); #endif array_hash = HASH_OF(args_array); if ((creds_obj = zend_hash_str_find(array_hash, "credentials", sizeof("credentials") - 1)) != NULL) { if (Z_TYPE_P(creds_obj) == IS_NULL) { creds = NULL; zend_hash_str_del(array_hash, "credentials", sizeof("credentials") - 1); } else if (Z_OBJ_P(creds_obj)->ce != grpc_ce_channel_credentials) { zend_throw_exception(spl_ce_InvalidArgumentException, "credentials must be a ChannelCredentials object", 1); return; } else { creds = Z_WRAPPED_GRPC_CHANNEL_CREDS_P(creds_obj); zend_hash_str_del(array_hash, "credentials", sizeof("credentials") - 1); } } php_grpc_read_args_array(args_array, &args); if (creds == NULL) { channel->wrapped = grpc_insecure_channel_create(ZSTR_VAL(target), &args, NULL); } else { channel->wrapped = grpc_secure_channel_create(creds->wrapped, ZSTR_VAL(target), &args, NULL); } efree(args.args); }
/** * Constructs a new instance of the Call class. * @param Channel $channel The channel to associate the call with. Must not be * closed. * @param string $method The method to call * @param Timeval $absolute_deadline The deadline for completing the call */ PHP_METHOD(Call, __construct) { wrapped_grpc_call *call = Z_WRAPPED_GRPC_CALL_P(getThis()); zval *channel_obj; //char *method; //size_t method_len; zend_string *method; zval *deadline_obj; //char *host_override = NULL; //size_t host_override_len = 0; zend_string *host_override; /* "OSO|S" == 1 Object, 1 string, 1 Object, 1 optional string */ #ifndef FAST_ZPP if (zend_parse_parameters(ZEND_NUM_ARGS(), "OSO|S", &channel_obj, grpc_ce_channel, &method, &deadline_obj, grpc_ce_timeval, &host_override) == FAILURE) { zend_throw_exception( spl_ce_InvalidArgumentException, "Call expects a Channel, a String, a Timeval and an optional String", 1); return; } #else ZEND_PARSE_PARAMETERS_START(3, 4) Z_PARAM_OBJECT_OF_CLASS(channel_obj, grpc_ce_channel) Z_PARAM_STR(method) Z_PARAM_OBJECT_OF_CLASS(deadline_obj, grpc_ce_timeval) Z_PARAM_OPTIONAL Z_PARAM_STR(host_override) ZEND_PARSE_PARAMETERS_END(); #endif wrapped_grpc_channel *channel = Z_WRAPPED_GRPC_CHANNEL_P(channel_obj); if (channel->wrapped == NULL) { zend_throw_exception(spl_ce_InvalidArgumentException, "Call cannot be constructed from a closed Channel", 1); return; } add_property_zval(getThis(), "channel", channel_obj); wrapped_grpc_timeval *deadline = Z_WRAPPED_GRPC_TIMEVAL_P(deadline_obj); call->wrapped = grpc_channel_create_call( channel->wrapped, NULL, GRPC_PROPAGATE_DEFAULTS, completion_queue, ZSTR_VAL(method), ZSTR_VAL(host_override), deadline->wrapped, NULL); }
/** * Get the connectivity state of the channel * @param bool (optional) try to connect on the channel * @return long The grpc connectivity state */ PHP_METHOD(Channel, getConnectivityState) { wrapped_grpc_channel *channel = Z_WRAPPED_GRPC_CHANNEL_P(getThis()); zend_bool try_to_connect = 0; /* "|b" == 1 optional bool */ #ifndef FAST_ZPP if (zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &try_to_connect) == FAILURE) { zend_throw_exception(spl_ce_InvalidArgumentException, "getConnectivityState expects a bool", 1); return; } #else ZEND_PARSE_PARAMETERS_START(0, 1) Z_PARAM_OPTIONAL Z_PARAM_BOOL(try_to_connect) ZEND_PARSE_PARAMETERS_END(); #endif RETURN_LONG(grpc_channel_check_connectivity_state(channel->wrapped, (int)try_to_connect)); }
/** * Get the endpoint this call/stream is connected to * @return string The URI of the endpoint */ PHP_METHOD(Channel, getTarget) { wrapped_grpc_channel *channel = Z_WRAPPED_GRPC_CHANNEL_P(getThis()); RETURN_STRING(grpc_channel_get_target(channel->wrapped)); }