/** * Checks whether the two times are within $threshold of each other * @param Timeval $a The first time to compare * @param Timeval $b The second time to compare * @param Timeval $threshold The threshold to check against * @return bool True if $a and $b are within $threshold, False otherwise */ PHP_METHOD(Timeval, similar) { zval *a_obj; zval *b_obj; zval *thresh_obj; /* "OOO" == 3 Objects */ #ifndef FAST_ZPP if (zend_parse_parameters(ZEND_NUM_ARGS(), "OOO", &a_obj, grpc_ce_timeval, &b_obj, grpc_ce_timeval, &thresh_obj, grpc_ce_timeval) == FAILURE) { zend_throw_exception(spl_ce_InvalidArgumentException, "compare expects three Timevals", 1); return; } #else ZEND_PARSE_PARAMETERS_START(3, 3) Z_PARAM_OBJECT_OF_CLASS(a_obj, grpc_ce_timeval) Z_PARAM_OBJECT_OF_CLASS(b_obj, grpc_ce_timeval) Z_PARAM_OBJECT_OF_CLASS(thresh_obj, grpc_ce_timeval) ZEND_PARSE_PARAMETERS_END(); #endif wrapped_grpc_timeval *a = Z_WRAPPED_GRPC_TIMEVAL_P(a_obj); wrapped_grpc_timeval *b = Z_WRAPPED_GRPC_TIMEVAL_P(b_obj); wrapped_grpc_timeval *thresh = Z_WRAPPED_GRPC_TIMEVAL_P(thresh_obj); int result = gpr_time_similar(a->wrapped, b->wrapped, thresh->wrapped); RETURN_BOOL(result); }
/** * Return negative, 0, or positive according to whether a < b, a == b, or a > b * respectively. * @param Timeval $a The first time to compare * @param Timeval $b The second time to compare * @return long */ PHP_METHOD(Timeval, compare) { zval *a_obj; zval *b_obj; /* "OO" == 2 Objects */ #ifndef FAST_ZPP if (zend_parse_parameters(ZEND_NUM_ARGS(), "OO", &a_obj, grpc_ce_timeval, &b_obj, grpc_ce_timeval) == FAILURE) { zend_throw_exception(spl_ce_InvalidArgumentException, "compare expects two Timevals", 1); return; } #else ZEND_PARSE_PARAMETERS_START(2, 2) Z_PARAM_OBJECT_OF_CLASS(a_obj, grpc_ce_timeval) Z_PARAM_OBJECT_OF_CLASS(b_obj, grpc_ce_timeval) ZEND_PARSE_PARAMETERS_END(); #endif wrapped_grpc_timeval *a = Z_WRAPPED_GRPC_TIMEVAL_P(a_obj); wrapped_grpc_timeval *b = Z_WRAPPED_GRPC_TIMEVAL_P(b_obj); long result = gpr_time_cmp(a->wrapped, b->wrapped); RETURN_LONG(result); }
/** * 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); }
/** * Adds another Timeval to this one and returns the sum. Calculations saturate * at infinities. * @param Timeval $other The other Timeval object to add * @return Timeval A new Timeval object containing the sum */ PHP_METHOD(Timeval, add) { zval *other_obj; /* "O" == 1 Object */ #ifndef FAST_ZPP if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &other_obj, grpc_ce_timeval) == FAILURE) { zend_throw_exception(spl_ce_InvalidArgumentException, "add expects a Timeval", 1); return; } #else ZEND_PARSE_PARAMETERS_START(1, 1) Z_PARAM_OBJECT_OF_CLASS(other_obj, grpc_ce_timeval) ZEND_PARSE_PARAMETERS_END(); #endif wrapped_grpc_timeval *self = Z_WRAPPED_GRPC_TIMEVAL_P(getThis()); wrapped_grpc_timeval *other = Z_WRAPPED_GRPC_TIMEVAL_P(other_obj); grpc_php_wrap_timeval(gpr_time_add(self->wrapped, other->wrapped), return_value); RETURN_DESTROY_ZVAL(return_value); }
/** * 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); }
/** * Constructs a new instance of the Timeval class * @param long $usec The number of microseconds in the interval */ PHP_METHOD(Timeval, __construct) { wrapped_grpc_timeval *timeval = Z_WRAPPED_GRPC_TIMEVAL_P(getThis()); zend_long microseconds; /* "l" == 1 long */ #ifndef FAST_ZPP if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", µseconds) == FAILURE) { zend_throw_exception(spl_ce_InvalidArgumentException, "Timeval expects a long", 1); return; } #else ZEND_PARSE_PARAMETERS_START(1, 1) Z_PARAM_LONG(microseconds) ZEND_PARSE_PARAMETERS_END(); #endif gpr_timespec time = gpr_time_from_micros(microseconds, GPR_TIMESPAN); memcpy(&timeval->wrapped, &time, sizeof(gpr_timespec)); }
void grpc_php_wrap_timeval(gpr_timespec wrapped, zval *timeval_object) { object_init_ex(timeval_object, grpc_ce_timeval); wrapped_grpc_timeval *timeval = Z_WRAPPED_GRPC_TIMEVAL_P(timeval_object); memcpy(&timeval->wrapped, &wrapped, sizeof(gpr_timespec)); }
/** * Sleep until this time, interpreted as an absolute timeout * @return void */ PHP_METHOD(Timeval, sleepUntil) { wrapped_grpc_timeval *this = Z_WRAPPED_GRPC_TIMEVAL_P(getThis()); gpr_sleep_until(this->wrapped); }