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