Switched events to use native objects instead of wrappers
Change on 2015/01/02 by mlumish <mlumish@google.com>
-------------
Created by MOE: http://code.google.com/p/moe-java
MOE_MIGRATED_REVID=83146045
diff --git a/src/php/ext/grpc/event.c b/src/php/ext/grpc/event.c
index 23ce2c8..c15f479 100755
--- a/src/php/ext/grpc/event.c
+++ b/src/php/ext/grpc/event.c
@@ -17,86 +17,10 @@
#include "call.h"
#include "timeval.h"
-/* Frees and finishes a wrapped instance of grpc_event */
-void free_wrapped_grpc_event(void *object TSRMLS_DC){
- wrapped_grpc_event *event = (wrapped_grpc_event*)object;
- if(event->wrapped != NULL){
- grpc_event_finish(event->wrapped);
- }
- efree(event);
-}
-
-/* Initializes an instance of wrapped_grpc_channel to be associated with an
- * object of a class specified by class_type */
-zend_object_value create_wrapped_grpc_event(
- zend_class_entry *class_type TSRMLS_DC){
- zend_object_value retval;
- wrapped_grpc_event *intern;
- intern = (wrapped_grpc_event*)emalloc(sizeof(wrapped_grpc_event));
- memset(intern, 0, sizeof(wrapped_grpc_event));
- zend_object_std_init(&intern->std, class_type TSRMLS_CC);
- object_properties_init(&intern->std, class_type);
- retval.handle = zend_objects_store_put(
- intern,
- (zend_objects_store_dtor_t)zend_objects_destroy_object,
- free_wrapped_grpc_event,
- NULL TSRMLS_CC);
- retval.handlers = zend_get_std_object_handlers();
- return retval;
-}
-
-zval *grpc_php_wrap_event(grpc_event *wrapped){
- zval *event_object;
- MAKE_STD_ZVAL(event_object);
-
- object_init_ex(event_object, grpc_ce_event);
- wrapped_grpc_event *event = (wrapped_grpc_event*)zend_object_store_get_object(
- event_object TSRMLS_CC);
- event->wrapped = wrapped;
- return event_object;
-}
-
-/**
- * Get the type of the event
- * @return long Integer representing the type
- */
-PHP_METHOD(Event, get_type){
- wrapped_grpc_event *event = (wrapped_grpc_event*)zend_object_store_get_object(
- getThis() TSRMLS_CC);
- RETURN_LONG((long)(event->wrapped->type));
-}
-
-/**
- * Get the tag of the event
- * @return long The event's tag
- */
-PHP_METHOD(Event, get_tag){
- wrapped_grpc_event *event = (wrapped_grpc_event*)zend_object_store_get_object(
- getThis() TSRMLS_CC);
- RETURN_LONG((long)(event->wrapped->tag));
-}
-
-/**
- * Get the call associated with the event
- * @return Call The call
- */
-PHP_METHOD(Event, get_call){
- wrapped_grpc_event *event = (wrapped_grpc_event*)zend_object_store_get_object(
- getThis() TSRMLS_CC);
- zval *call_obj = grpc_php_wrap_call(event->wrapped->call);
- RETURN_DESTROY_ZVAL(call_obj);
-}
-
-/**
- * Get the data associated with the event
- * @return object The data, with type depending on the type field
- */
-PHP_METHOD(Event, get_data){
- zval *retval;
- wrapped_grpc_event *wrapped_event =
- (wrapped_grpc_event*)zend_object_store_get_object(
- getThis() TSRMLS_CC);
- grpc_event *event = wrapped_event->wrapped;
+/* Create a new PHP object containing the event data in the event struct.
+ event must not be used after this function is called */
+zval *grpc_php_convert_event(grpc_event *event) {
+ zval *data_object;
char *detail_string;
size_t detail_len;
char *method_string;
@@ -106,89 +30,103 @@
char *read_string;
size_t read_len;
+ zval *event_object;
+
+ if(event == NULL) {
+ return NULL;
+ }
+
+ MAKE_STD_ZVAL(event_object);
+ object_init(event_object);
+
+ add_property_zval(event_object,
+ "call",
+ grpc_php_wrap_call(event->call,
+ event->type==GRPC_SERVER_RPC_NEW));
+ add_property_long(event_object, "type", event->type);
+ add_property_long(event_object, "tag", (long)event->tag);
+
switch(event->type){
- case GRPC_QUEUE_SHUTDOWN: RETURN_NULL(); break;
+ case GRPC_QUEUE_SHUTDOWN: add_property_null(event_object, "data"); break;
case GRPC_READ:
if(event->data.read == NULL){
- RETURN_NULL();
+ add_property_null(event_object, "data");
} else {
byte_buffer_to_string(event->data.read, &read_string, &read_len);
- RETURN_STRINGL(read_string, read_len, true);
+ add_property_stringl(event_object, "data", read_string, read_len, true);
}
break;
case GRPC_INVOKE_ACCEPTED:
- RETURN_LONG((long)event->data.invoke_accepted); break;
+ add_property_long(event_object,
+ "data",
+ (long)event->data.invoke_accepted);
+ break;
case GRPC_WRITE_ACCEPTED:
- RETURN_LONG((long)event->data.write_accepted); break;
+ add_property_long(event_object, "data", (long)event->data.write_accepted);
+ break;
case GRPC_FINISH_ACCEPTED:
- RETURN_LONG((long)event->data.finish_accepted); break;
+ add_property_long(event_object,
+ "data",
+ (long)event->data.finish_accepted);
+ break;
case GRPC_CLIENT_METADATA_READ:
- retval = grpc_call_create_metadata_array(
+ data_object = grpc_call_create_metadata_array(
event->data.client_metadata_read.count,
event->data.client_metadata_read.elements);
+ add_property_zval(event_object, "data", data_object);
break;
case GRPC_FINISHED:
- MAKE_STD_ZVAL(retval);
- object_init(retval);
- add_property_long(retval, "code", event->data.finished.status);
+ MAKE_STD_ZVAL(data_object);
+ object_init(data_object);
+ add_property_long(data_object, "code", event->data.finished.status);
if(event->data.finished.details == NULL){
- add_property_null(retval, "details");
+ add_property_null(data_object, "details");
} else {
detail_len = strlen(event->data.finished.details);
detail_string = ecalloc(detail_len+1, sizeof(char));
memcpy(detail_string, event->data.finished.details, detail_len);
- add_property_string(retval,
+ add_property_string(data_object,
"details",
detail_string,
true);
}
- add_property_zval(retval, "metadata", grpc_call_create_metadata_array(
- event->data.finished.metadata_count,
- event->data.finished.metadata_elements));
+ add_property_zval(data_object,
+ "metadata",
+ grpc_call_create_metadata_array(
+ event->data.finished.metadata_count,
+ event->data.finished.metadata_elements));
+ add_property_zval(event_object, "data", data_object);
break;
case GRPC_SERVER_RPC_NEW:
- MAKE_STD_ZVAL(retval);
- object_init(retval);
+ MAKE_STD_ZVAL(data_object);
+ object_init(data_object);
method_len = strlen(event->data.server_rpc_new.method);
method_string = ecalloc(method_len+1, sizeof(char));
memcpy(method_string, event->data.server_rpc_new.method, method_len);
- add_property_string(retval,
+ add_property_string(data_object,
"method",
method_string,
false);
host_len = strlen(event->data.server_rpc_new.host);
host_string = ecalloc(host_len+1, sizeof(char));
memcpy(host_string, event->data.server_rpc_new.host, host_len);
- add_property_string(retval,
+ add_property_string(data_object,
"host",
host_string,
false);
- add_property_zval(retval,
+ add_property_zval(data_object,
"absolute_timeout",
grpc_php_wrap_timeval(
event->data.server_rpc_new.deadline));
- add_property_zval(retval,
+ add_property_zval(data_object,
"metadata",
grpc_call_create_metadata_array(
event->data.server_rpc_new.metadata_count,
event->data.server_rpc_new.metadata_elements));
+ add_property_zval(event_object, "data", data_object);
break;
- default: RETURN_NULL(); break;
+ default: add_property_null(event_object, "data"); break;
}
- RETURN_DESTROY_ZVAL(retval);
-}
-
-static zend_function_entry event_methods[] = {
- PHP_ME(Event, get_call, NULL, ZEND_ACC_PUBLIC)
- PHP_ME(Event, get_data, NULL, ZEND_ACC_PUBLIC)
- PHP_ME(Event, get_tag, NULL, ZEND_ACC_PUBLIC)
- PHP_ME(Event, get_type, NULL, ZEND_ACC_PUBLIC)
- PHP_FE_END
-};
-
-void grpc_init_event(TSRMLS_D){
- zend_class_entry ce;
- INIT_CLASS_ENTRY(ce, "Grpc\\Event", event_methods);
- ce.create_object = create_wrapped_grpc_event;
- grpc_ce_event = zend_register_internal_class(&ce TSRMLS_CC);
+ grpc_event_finish(event);
+ return event_object;
}