Craig Tiller | 1a61b17 | 2015-02-16 11:53:47 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2015, Google Inc. |
| 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 34 | #include "call.h" |
| 35 | |
| 36 | #ifdef HAVE_CONFIG_H |
| 37 | #include "config.h" |
| 38 | #endif |
| 39 | |
| 40 | #include "php.h" |
| 41 | #include "php_ini.h" |
| 42 | #include "ext/standard/info.h" |
| 43 | #include "ext/spl/spl_exceptions.h" |
| 44 | #include "php_grpc.h" |
| 45 | |
| 46 | #include "zend_exceptions.h" |
| 47 | |
| 48 | #include <stdbool.h> |
| 49 | |
| 50 | #include "grpc/grpc.h" |
| 51 | #include "grpc/support/log.h" |
| 52 | #include "grpc/grpc_security.h" |
| 53 | |
| 54 | #include "server.h" |
| 55 | #include "completion_queue.h" |
| 56 | #include "channel.h" |
| 57 | #include "server_credentials.h" |
| 58 | |
Xiaoguang Sun | 8a929a9 | 2015-03-13 14:22:31 +0800 | [diff] [blame] | 59 | zend_class_entry *grpc_ce_server; |
| 60 | |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 61 | /* Frees and destroys an instance of wrapped_grpc_server */ |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 62 | void free_wrapped_grpc_server(void *object TSRMLS_DC) { |
| 63 | wrapped_grpc_server *server = (wrapped_grpc_server *)object; |
| 64 | if (server->wrapped != NULL) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 65 | grpc_server_shutdown(server->wrapped); |
| 66 | grpc_server_destroy(server->wrapped); |
| 67 | } |
| 68 | efree(server); |
| 69 | } |
| 70 | |
| 71 | /* Initializes an instance of wrapped_grpc_call to be associated with an object |
| 72 | * of a class specified by class_type */ |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 73 | zend_object_value create_wrapped_grpc_server(zend_class_entry *class_type |
| 74 | TSRMLS_DC) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 75 | zend_object_value retval; |
| 76 | wrapped_grpc_server *intern; |
| 77 | |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 78 | intern = (wrapped_grpc_server *)emalloc(sizeof(wrapped_grpc_server)); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 79 | memset(intern, 0, sizeof(wrapped_grpc_server)); |
| 80 | |
| 81 | zend_object_std_init(&intern->std, class_type TSRMLS_CC); |
| 82 | object_properties_init(&intern->std, class_type); |
| 83 | retval.handle = zend_objects_store_put( |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 84 | intern, (zend_objects_store_dtor_t)zend_objects_destroy_object, |
| 85 | free_wrapped_grpc_server, NULL TSRMLS_CC); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 86 | retval.handlers = zend_get_std_object_handlers(); |
| 87 | return retval; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Constructs a new instance of the Server class |
| 92 | * @param CompletionQueue $queue The completion queue to use with the server |
| 93 | * @param array $args The arguments to pass to the server (optional) |
| 94 | */ |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 95 | PHP_METHOD(Server, __construct) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 96 | wrapped_grpc_server *server = |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 97 | (wrapped_grpc_server *)zend_object_store_get_object(getThis() TSRMLS_CC); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 98 | zval *queue_obj; |
| 99 | zval *args_array = NULL; |
| 100 | grpc_channel_args args; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 101 | /* "O|a" == 1 Object, 1 optional array */ |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 102 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O|a", &queue_obj, |
| 103 | grpc_ce_completion_queue, &args_array) == FAILURE) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 104 | zend_throw_exception(spl_ce_InvalidArgumentException, |
| 105 | "Server expects a CompletionQueue and an array", |
| 106 | 1 TSRMLS_CC); |
| 107 | return; |
| 108 | } |
| 109 | add_property_zval(getThis(), "completion_queue", queue_obj); |
| 110 | wrapped_grpc_completion_queue *queue = |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 111 | (wrapped_grpc_completion_queue *)zend_object_store_get_object( |
| 112 | queue_obj TSRMLS_CC); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 113 | if (args_array == NULL) { |
| 114 | server->wrapped = grpc_server_create(queue->wrapped, NULL); |
| 115 | } else { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 116 | php_grpc_read_args_array(args_array, &args); |
murgatroid99 | 8b87e84 | 2015-03-04 12:14:53 -0800 | [diff] [blame] | 117 | server->wrapped = grpc_server_create(queue->wrapped, &args); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 118 | efree(args.args); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Request a call on a server. Creates a single GRPC_SERVER_RPC_NEW event. |
| 124 | * @param long $tag_new The tag to associate with the new request |
| 125 | * @param long $tag_cancel The tag to use if the call is cancelled |
| 126 | * @return Void |
| 127 | */ |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 128 | PHP_METHOD(Server, request_call) { |
mlumish | 8f91163 | 2015-01-02 13:34:14 -0800 | [diff] [blame] | 129 | grpc_call_error error_code; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 130 | wrapped_grpc_server *server = |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 131 | (wrapped_grpc_server *)zend_object_store_get_object(getThis() TSRMLS_CC); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 132 | long tag_new; |
| 133 | /* "l" == 1 long */ |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 134 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &tag_new) == |
| 135 | FAILURE) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 136 | zend_throw_exception(spl_ce_InvalidArgumentException, |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 137 | "request_call expects a long", 1 TSRMLS_CC); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 138 | return; |
| 139 | } |
Craig Tiller | c4f0ebe | 2015-02-02 10:16:30 -0800 | [diff] [blame] | 140 | error_code = grpc_server_request_call_old(server->wrapped, (void *)tag_new); |
mlumish | 8f91163 | 2015-01-02 13:34:14 -0800 | [diff] [blame] | 141 | MAYBE_THROW_CALL_ERROR(request_call, error_code); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Add a http2 over tcp listener. |
| 146 | * @param string $addr The address to add |
| 147 | * @return true on success, false on failure |
| 148 | */ |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 149 | PHP_METHOD(Server, add_http2_port) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 150 | wrapped_grpc_server *server = |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 151 | (wrapped_grpc_server *)zend_object_store_get_object(getThis() TSRMLS_CC); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 152 | const char *addr; |
| 153 | int addr_len; |
| 154 | /* "s" == 1 string */ |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 155 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &addr, &addr_len) == |
| 156 | FAILURE) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 157 | zend_throw_exception(spl_ce_InvalidArgumentException, |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 158 | "add_http2_port expects a string", 1 TSRMLS_CC); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 159 | return; |
| 160 | } |
murgatroid99 | d96db79 | 2015-02-03 17:44:50 -0800 | [diff] [blame] | 161 | RETURN_LONG(grpc_server_add_http2_port(server->wrapped, addr)); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 162 | } |
| 163 | |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 164 | PHP_METHOD(Server, add_secure_http2_port) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 165 | wrapped_grpc_server *server = |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 166 | (wrapped_grpc_server *)zend_object_store_get_object(getThis() TSRMLS_CC); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 167 | const char *addr; |
| 168 | int addr_len; |
murgatroid99 | 8b87e84 | 2015-03-04 12:14:53 -0800 | [diff] [blame] | 169 | zval *creds_obj; |
| 170 | /* "sO" == 1 string, 1 object */ |
| 171 | if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &addr, &addr_len, |
| 172 | &creds_obj, grpc_ce_server_credentials) == |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 173 | FAILURE) { |
murgatroid99 | 8b87e84 | 2015-03-04 12:14:53 -0800 | [diff] [blame] | 174 | zend_throw_exception( |
| 175 | spl_ce_InvalidArgumentException, |
| 176 | "add_http2_port expects a string and a ServerCredentials", 1 TSRMLS_CC); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 177 | return; |
| 178 | } |
murgatroid99 | 8b87e84 | 2015-03-04 12:14:53 -0800 | [diff] [blame] | 179 | wrapped_grpc_server_credentials *creds = |
| 180 | (wrapped_grpc_server_credentials *)zend_object_store_get_object( |
| 181 | creds_obj TSRMLS_CC); |
| 182 | RETURN_LONG(grpc_server_add_secure_http2_port(server->wrapped, addr, |
| 183 | creds->wrapped)); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Start a server - tells all listeners to start listening |
| 188 | * @return Void |
| 189 | */ |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 190 | PHP_METHOD(Server, start) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 191 | wrapped_grpc_server *server = |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 192 | (wrapped_grpc_server *)zend_object_store_get_object(getThis() TSRMLS_CC); |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 193 | grpc_server_start(server->wrapped); |
| 194 | } |
| 195 | |
| 196 | static zend_function_entry server_methods[] = { |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 197 | PHP_ME(Server, __construct, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR) |
Craig Tiller | ecd4934 | 2015-01-18 14:36:47 -0800 | [diff] [blame] | 198 | PHP_ME(Server, request_call, NULL, ZEND_ACC_PUBLIC) |
| 199 | PHP_ME(Server, add_http2_port, NULL, ZEND_ACC_PUBLIC) |
| 200 | PHP_ME(Server, add_secure_http2_port, NULL, ZEND_ACC_PUBLIC) |
| 201 | PHP_ME(Server, start, NULL, ZEND_ACC_PUBLIC) PHP_FE_END}; |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 202 | |
Craig Tiller | b5dcec5 | 2015-01-13 11:13:42 -0800 | [diff] [blame] | 203 | void grpc_init_server(TSRMLS_D) { |
mlumish | b892a27 | 2014-12-09 16:28:23 -0800 | [diff] [blame] | 204 | zend_class_entry ce; |
| 205 | INIT_CLASS_ENTRY(ce, "Grpc\\Server", server_methods); |
| 206 | ce.create_object = create_wrapped_grpc_server; |
| 207 | grpc_ce_server = zend_register_internal_class(&ce TSRMLS_CC); |
| 208 | } |