blob: a5cfd952871798c0281f7767d00a624ca10ed6f4 [file] [log] [blame]
Craig Tiller1a61b172015-02-16 11:53:47 -08001/*
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
mlumishb892a272014-12-09 16:28:23 -080034#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 Sun8a929a92015-03-13 14:22:31 +080059zend_class_entry *grpc_ce_server;
60
mlumishb892a272014-12-09 16:28:23 -080061/* Frees and destroys an instance of wrapped_grpc_server */
Craig Tillerb5dcec52015-01-13 11:13:42 -080062void free_wrapped_grpc_server(void *object TSRMLS_DC) {
63 wrapped_grpc_server *server = (wrapped_grpc_server *)object;
64 if (server->wrapped != NULL) {
mlumishb892a272014-12-09 16:28:23 -080065 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 Tillerb5dcec52015-01-13 11:13:42 -080073zend_object_value create_wrapped_grpc_server(zend_class_entry *class_type
74 TSRMLS_DC) {
mlumishb892a272014-12-09 16:28:23 -080075 zend_object_value retval;
76 wrapped_grpc_server *intern;
77
Craig Tillerb5dcec52015-01-13 11:13:42 -080078 intern = (wrapped_grpc_server *)emalloc(sizeof(wrapped_grpc_server));
mlumishb892a272014-12-09 16:28:23 -080079 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 Tillerb5dcec52015-01-13 11:13:42 -080084 intern, (zend_objects_store_dtor_t)zend_objects_destroy_object,
85 free_wrapped_grpc_server, NULL TSRMLS_CC);
mlumishb892a272014-12-09 16:28:23 -080086 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 Tillerb5dcec52015-01-13 11:13:42 -080095PHP_METHOD(Server, __construct) {
mlumishb892a272014-12-09 16:28:23 -080096 wrapped_grpc_server *server =
Craig Tillerb5dcec52015-01-13 11:13:42 -080097 (wrapped_grpc_server *)zend_object_store_get_object(getThis() TSRMLS_CC);
mlumishb892a272014-12-09 16:28:23 -080098 zval *queue_obj;
99 zval *args_array = NULL;
100 grpc_channel_args args;
mlumishb892a272014-12-09 16:28:23 -0800101 /* "O|a" == 1 Object, 1 optional array */
Craig Tillerb5dcec52015-01-13 11:13:42 -0800102 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O|a", &queue_obj,
103 grpc_ce_completion_queue, &args_array) == FAILURE) {
mlumishb892a272014-12-09 16:28:23 -0800104 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 Tillerb5dcec52015-01-13 11:13:42 -0800111 (wrapped_grpc_completion_queue *)zend_object_store_get_object(
112 queue_obj TSRMLS_CC);
mlumishb892a272014-12-09 16:28:23 -0800113 if (args_array == NULL) {
114 server->wrapped = grpc_server_create(queue->wrapped, NULL);
115 } else {
mlumishb892a272014-12-09 16:28:23 -0800116 php_grpc_read_args_array(args_array, &args);
murgatroid998b87e842015-03-04 12:14:53 -0800117 server->wrapped = grpc_server_create(queue->wrapped, &args);
mlumishb892a272014-12-09 16:28:23 -0800118 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 Tillerb5dcec52015-01-13 11:13:42 -0800128PHP_METHOD(Server, request_call) {
mlumish8f911632015-01-02 13:34:14 -0800129 grpc_call_error error_code;
mlumishb892a272014-12-09 16:28:23 -0800130 wrapped_grpc_server *server =
Craig Tillerb5dcec52015-01-13 11:13:42 -0800131 (wrapped_grpc_server *)zend_object_store_get_object(getThis() TSRMLS_CC);
mlumishb892a272014-12-09 16:28:23 -0800132 long tag_new;
133 /* "l" == 1 long */
Craig Tillerb5dcec52015-01-13 11:13:42 -0800134 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &tag_new) ==
135 FAILURE) {
mlumishb892a272014-12-09 16:28:23 -0800136 zend_throw_exception(spl_ce_InvalidArgumentException,
Craig Tillerb5dcec52015-01-13 11:13:42 -0800137 "request_call expects a long", 1 TSRMLS_CC);
mlumishb892a272014-12-09 16:28:23 -0800138 return;
139 }
Craig Tillerc4f0ebe2015-02-02 10:16:30 -0800140 error_code = grpc_server_request_call_old(server->wrapped, (void *)tag_new);
mlumish8f911632015-01-02 13:34:14 -0800141 MAYBE_THROW_CALL_ERROR(request_call, error_code);
mlumishb892a272014-12-09 16:28:23 -0800142}
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 Tillerb5dcec52015-01-13 11:13:42 -0800149PHP_METHOD(Server, add_http2_port) {
mlumishb892a272014-12-09 16:28:23 -0800150 wrapped_grpc_server *server =
Craig Tillerb5dcec52015-01-13 11:13:42 -0800151 (wrapped_grpc_server *)zend_object_store_get_object(getThis() TSRMLS_CC);
mlumishb892a272014-12-09 16:28:23 -0800152 const char *addr;
153 int addr_len;
154 /* "s" == 1 string */
Craig Tillerb5dcec52015-01-13 11:13:42 -0800155 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &addr, &addr_len) ==
156 FAILURE) {
mlumishb892a272014-12-09 16:28:23 -0800157 zend_throw_exception(spl_ce_InvalidArgumentException,
Craig Tillerb5dcec52015-01-13 11:13:42 -0800158 "add_http2_port expects a string", 1 TSRMLS_CC);
mlumishb892a272014-12-09 16:28:23 -0800159 return;
160 }
murgatroid99d96db792015-02-03 17:44:50 -0800161 RETURN_LONG(grpc_server_add_http2_port(server->wrapped, addr));
mlumishb892a272014-12-09 16:28:23 -0800162}
163
Craig Tillerb5dcec52015-01-13 11:13:42 -0800164PHP_METHOD(Server, add_secure_http2_port) {
mlumishb892a272014-12-09 16:28:23 -0800165 wrapped_grpc_server *server =
Craig Tillerb5dcec52015-01-13 11:13:42 -0800166 (wrapped_grpc_server *)zend_object_store_get_object(getThis() TSRMLS_CC);
mlumishb892a272014-12-09 16:28:23 -0800167 const char *addr;
168 int addr_len;
murgatroid998b87e842015-03-04 12:14:53 -0800169 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 Tillerb5dcec52015-01-13 11:13:42 -0800173 FAILURE) {
murgatroid998b87e842015-03-04 12:14:53 -0800174 zend_throw_exception(
175 spl_ce_InvalidArgumentException,
176 "add_http2_port expects a string and a ServerCredentials", 1 TSRMLS_CC);
mlumishb892a272014-12-09 16:28:23 -0800177 return;
178 }
murgatroid998b87e842015-03-04 12:14:53 -0800179 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));
mlumishb892a272014-12-09 16:28:23 -0800184}
185
186/**
187 * Start a server - tells all listeners to start listening
188 * @return Void
189 */
Craig Tillerb5dcec52015-01-13 11:13:42 -0800190PHP_METHOD(Server, start) {
mlumishb892a272014-12-09 16:28:23 -0800191 wrapped_grpc_server *server =
Craig Tillerb5dcec52015-01-13 11:13:42 -0800192 (wrapped_grpc_server *)zend_object_store_get_object(getThis() TSRMLS_CC);
mlumishb892a272014-12-09 16:28:23 -0800193 grpc_server_start(server->wrapped);
194}
195
196static zend_function_entry server_methods[] = {
Craig Tillerb5dcec52015-01-13 11:13:42 -0800197 PHP_ME(Server, __construct, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
Craig Tillerecd49342015-01-18 14:36:47 -0800198 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};
mlumishb892a272014-12-09 16:28:23 -0800202
Craig Tillerb5dcec52015-01-13 11:13:42 -0800203void grpc_init_server(TSRMLS_D) {
mlumishb892a272014-12-09 16:28:23 -0800204 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}