mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 1 | #include "call.h" |
| 2 | |
| 3 | #ifdef HAVE_CONFIG_H |
| 4 | #include "config.h" |
| 5 | #endif |
| 6 | |
| 7 | #include "php.h" |
| 8 | #include "php_ini.h" |
| 9 | #include "ext/standard/info.h" |
| 10 | #include "ext/spl/spl_exceptions.h" |
| 11 | #include "php_grpc.h" |
| 12 | |
| 13 | #include "zend_exceptions.h" |
| 14 | |
| 15 | #include <stdbool.h> |
| 16 | |
| 17 | #include "grpc/grpc.h" |
| 18 | #include "grpc/support/log.h" |
| 19 | #include "grpc/grpc_security.h" |
| 20 | |
| 21 | #include "server.h" |
| 22 | #include "completion_queue.h" |
| 23 | #include "channel.h" |
| 24 | #include "server_credentials.h" |
| 25 | |
| 26 | /* Frees and destroys an instance of wrapped_grpc_server */ |
| 27 | void free_wrapped_grpc_server(void *object TSRMLS_DC){ |
| 28 | wrapped_grpc_server *server = (wrapped_grpc_server*)object; |
| 29 | if(server->wrapped != NULL){ |
| 30 | grpc_server_shutdown(server->wrapped); |
| 31 | grpc_server_destroy(server->wrapped); |
| 32 | } |
| 33 | efree(server); |
| 34 | } |
| 35 | |
| 36 | /* Initializes an instance of wrapped_grpc_call to be associated with an object |
| 37 | * of a class specified by class_type */ |
| 38 | zend_object_value create_wrapped_grpc_server( |
| 39 | zend_class_entry *class_type TSRMLS_DC){ |
| 40 | zend_object_value retval; |
| 41 | wrapped_grpc_server *intern; |
| 42 | |
| 43 | intern = (wrapped_grpc_server*)emalloc(sizeof(wrapped_grpc_server)); |
| 44 | memset(intern, 0, sizeof(wrapped_grpc_server)); |
| 45 | |
| 46 | zend_object_std_init(&intern->std, class_type TSRMLS_CC); |
| 47 | object_properties_init(&intern->std, class_type); |
| 48 | retval.handle = zend_objects_store_put( |
| 49 | intern, |
| 50 | (zend_objects_store_dtor_t) zend_objects_destroy_object, |
| 51 | free_wrapped_grpc_server, |
| 52 | NULL TSRMLS_CC); |
| 53 | retval.handlers = zend_get_std_object_handlers(); |
| 54 | return retval; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Constructs a new instance of the Server class |
| 59 | * @param CompletionQueue $queue The completion queue to use with the server |
| 60 | * @param array $args The arguments to pass to the server (optional) |
| 61 | */ |
| 62 | PHP_METHOD(Server, __construct){ |
| 63 | wrapped_grpc_server *server = |
| 64 | (wrapped_grpc_server*)zend_object_store_get_object(getThis() TSRMLS_CC); |
| 65 | zval *queue_obj; |
| 66 | zval *args_array = NULL; |
| 67 | grpc_channel_args args; |
| 68 | HashTable *array_hash; |
| 69 | zval **creds_obj = NULL; |
| 70 | wrapped_grpc_server_credentials *creds = NULL; |
| 71 | /* "O|a" == 1 Object, 1 optional array */ |
| 72 | if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, |
| 73 | "O|a", |
| 74 | &queue_obj, grpc_ce_completion_queue, |
| 75 | &args_array) == FAILURE){ |
| 76 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 77 | "Server expects a CompletionQueue and an array", |
| 78 | 1 TSRMLS_CC); |
| 79 | return; |
| 80 | } |
| 81 | add_property_zval(getThis(), "completion_queue", queue_obj); |
| 82 | wrapped_grpc_completion_queue *queue = |
| 83 | (wrapped_grpc_completion_queue*)zend_object_store_get_object( |
| 84 | queue_obj TSRMLS_CC); |
| 85 | if (args_array == NULL) { |
| 86 | server->wrapped = grpc_server_create(queue->wrapped, NULL); |
| 87 | } else { |
| 88 | array_hash = Z_ARRVAL_P(args_array); |
| 89 | if(zend_hash_find(array_hash, |
| 90 | "credentials", |
| 91 | sizeof("credentials"), |
| 92 | (void**)&creds_obj) == SUCCESS) { |
| 93 | if(zend_get_class_entry(*creds_obj TSRMLS_CC) != |
| 94 | grpc_ce_server_credentials) { |
| 95 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 96 | "credentials must be a ServerCredentials object", |
| 97 | 1 TSRMLS_CC); |
| 98 | return; |
| 99 | } |
| 100 | creds = (wrapped_grpc_server_credentials*)zend_object_store_get_object( |
| 101 | *creds_obj TSRMLS_CC); |
| 102 | zend_hash_del(array_hash, "credentials", sizeof("credentials")); |
| 103 | } |
| 104 | php_grpc_read_args_array(args_array, &args); |
| 105 | if (creds == NULL) { |
| 106 | server->wrapped = grpc_server_create(queue->wrapped, &args); |
| 107 | } else { |
| 108 | gpr_log(GPR_DEBUG, "Initialized secure server"); |
| 109 | server->wrapped = grpc_secure_server_create(creds->wrapped, |
| 110 | queue->wrapped, |
| 111 | &args); |
| 112 | } |
| 113 | efree(args.args); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Request a call on a server. Creates a single GRPC_SERVER_RPC_NEW event. |
| 119 | * @param long $tag_new The tag to associate with the new request |
| 120 | * @param long $tag_cancel The tag to use if the call is cancelled |
| 121 | * @return Void |
| 122 | */ |
| 123 | PHP_METHOD(Server, request_call){ |
mlumish | 8f91163 | 2015-01-02 13:34:14 -0800 | [diff] [blame^] | 124 | grpc_call_error error_code; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 125 | wrapped_grpc_server *server = |
| 126 | (wrapped_grpc_server*)zend_object_store_get_object(getThis() TSRMLS_CC); |
| 127 | long tag_new; |
| 128 | /* "l" == 1 long */ |
| 129 | if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, |
| 130 | "l", |
| 131 | &tag_new) == FAILURE){ |
| 132 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 133 | "request_call expects a long", |
| 134 | 1 TSRMLS_CC); |
| 135 | return; |
| 136 | } |
mlumish | 8f91163 | 2015-01-02 13:34:14 -0800 | [diff] [blame^] | 137 | error_code = grpc_server_request_call(server->wrapped, (void*)tag_new); |
| 138 | MAYBE_THROW_CALL_ERROR(request_call, error_code); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Add a http2 over tcp listener. |
| 143 | * @param string $addr The address to add |
| 144 | * @return true on success, false on failure |
| 145 | */ |
| 146 | PHP_METHOD(Server, add_http2_port){ |
| 147 | wrapped_grpc_server *server = |
| 148 | (wrapped_grpc_server*)zend_object_store_get_object(getThis() TSRMLS_CC); |
| 149 | const char *addr; |
| 150 | int addr_len; |
| 151 | /* "s" == 1 string */ |
| 152 | if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, |
| 153 | "s", |
| 154 | &addr, &addr_len) == FAILURE){ |
| 155 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 156 | "add_http2_port expects a string", |
| 157 | 1 TSRMLS_CC); |
| 158 | return; |
| 159 | } |
| 160 | RETURN_BOOL(grpc_server_add_http2_port(server->wrapped, addr)); |
| 161 | } |
| 162 | |
| 163 | PHP_METHOD(Server, add_secure_http2_port){ |
| 164 | wrapped_grpc_server *server = |
| 165 | (wrapped_grpc_server*)zend_object_store_get_object(getThis() TSRMLS_CC); |
| 166 | const char *addr; |
| 167 | int addr_len; |
| 168 | /* "s" == 1 string */ |
| 169 | if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, |
| 170 | "s", |
| 171 | &addr, &addr_len) == FAILURE){ |
| 172 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 173 | "add_http2_port expects a string", |
| 174 | 1 TSRMLS_CC); |
| 175 | return; |
| 176 | } |
| 177 | RETURN_BOOL(grpc_server_add_secure_http2_port(server->wrapped, addr)); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Start a server - tells all listeners to start listening |
| 182 | * @return Void |
| 183 | */ |
| 184 | PHP_METHOD(Server, start){ |
| 185 | wrapped_grpc_server *server = |
| 186 | (wrapped_grpc_server*)zend_object_store_get_object(getThis() TSRMLS_CC); |
| 187 | grpc_server_start(server->wrapped); |
| 188 | } |
| 189 | |
| 190 | static zend_function_entry server_methods[] = { |
| 191 | PHP_ME(Server, __construct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR) |
| 192 | PHP_ME(Server, request_call, NULL, ZEND_ACC_PUBLIC) |
| 193 | PHP_ME(Server, add_http2_port, NULL, ZEND_ACC_PUBLIC) |
| 194 | PHP_ME(Server, add_secure_http2_port, NULL, ZEND_ACC_PUBLIC) |
| 195 | PHP_ME(Server, start, NULL, ZEND_ACC_PUBLIC) |
| 196 | PHP_FE_END |
| 197 | }; |
| 198 | |
| 199 | void grpc_init_server(TSRMLS_D){ |
| 200 | zend_class_entry ce; |
| 201 | INIT_CLASS_ENTRY(ce, "Grpc\\Server", server_methods); |
| 202 | ce.create_object = create_wrapped_grpc_server; |
| 203 | grpc_ce_server = zend_register_internal_class(&ce TSRMLS_CC); |
| 204 | } |