-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathext_rpc_service_stub.cc
More file actions
339 lines (293 loc) · 10.5 KB
/
Copy pathext_rpc_service_stub.cc
File metadata and controls
339 lines (293 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// Copyright (c) 2016 Baidu.com, Inc. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Author: zhangdi05@baidu.com (Zhangdi Di)
extern "C" {
#include <php.h>
#include <php_ini.h>
#include <ext/standard/info.h>
#include <ext/standard/php_string.h>
#include <Zend/zend_exceptions.h>
}
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef add_method
#undef add_method
#include <sofa/pbrpc/pbrpc.h>
#endif
#include "ext_rpc_service_stub.h"
#include "ext_rpc_service_stub_impl.h"
namespace sofa_php_ext
{
#define PB_CONSTANT(name) \
zend_declare_class_constant_long(rpc_service_stub_ce, #name, sizeof(#name) - 1, name TSRMLS_CC)
sofa::pbrpc::RpcClientOptions client_options;
sofa::pbrpc::RpcClient client_a(client_options);
sofa::pbrpc::RpcClient client_b(client_options);
zend_object_handlers rpc_service_stub_object_handlers;
struct rpc_service_stub_object
{
zend_object std;
PhpRpcServiceStubImpl* stub_impl;
};
ZEND_BEGIN_ARG_INFO_EX(rpc_service_stub_arginfo_initservice, 0, 0, 3)
ZEND_ARG_INFO(0, address_str)
ZEND_ARG_INFO(0, package_name)
ZEND_ARG_INFO(0, service_name)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(rpc_service_stub_arginfo_settimeout, 0, 0, 1)
ZEND_ARG_INFO(0, timeout)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(rpc_service_stub_arginfo_getfailed, 0, 0, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(rpc_service_stub_arginfo_geterrortext, 0, 0, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(rpc_service_stub_arginfo_registermethod, 0, 0, 3)
ZEND_ARG_INFO(0, method_name)
ZEND_ARG_INFO(0, request)
ZEND_ARG_INFO(0, response)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(rpc_service_stub_arginfo_initmethod, 0, 0, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(rpc_service_stub_arginfo_callmethod, 0, 0, 4)
ZEND_ARG_INFO(0, method_name)
ZEND_ARG_INFO(0, request)
ZEND_ARG_INFO(0, response)
ZEND_ARG_INFO(0, closure)
ZEND_END_ARG_INFO()
void rpc_service_stub_free_storage(void *object TSRMLS_DC)
{
rpc_service_stub_object *obj = (rpc_service_stub_object*)object;
delete obj->stub_impl;
zend_hash_destroy(obj->std.properties);
FREE_HASHTABLE(obj->std.properties);
efree(obj);
}
zend_object_value rpc_service_stub_create_handler(zend_class_entry* type TSRMLS_DC)
{
zval* tmp;
zend_object_value retval;
rpc_service_stub_object* obj
= (rpc_service_stub_object*)emalloc(sizeof(rpc_service_stub_object));
memset(obj, 0, sizeof(rpc_service_stub_object));
obj->std.ce = type;
ALLOC_HASHTABLE(obj->std.properties);
zend_hash_init(obj->std.properties, 0, NULL, ZVAL_PTR_DTOR, 0);
#if PHP_VERSION_ID < 50399
zend_hash_copy(obj->std.properties,
&(type->default_properties_table),
(copy_ctor_func_t)zval_add_ref,
(void*)&tmp,
sizeof(zval*));
#else
object_properties_init((zend_object*)obj, type);
#endif
retval.handle = zend_objects_store_put(obj,
NULL,
rpc_service_stub_free_storage,
NULL TSRMLS_CC);
retval.handlers = &rpc_service_stub_object_handlers;
return retval;
}
PHP_METHOD(PhpRpcServiceStub, InitService)
{
srand((unsigned)time(NULL));
zval* stub_zval = getThis();
char* address_str;
int address_str_len;
char* package_name;
int package_len;
char* service_name;
int service_len;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"sss",
&address_str,
&address_str_len,
&package_name,
&package_len,
&service_name,
&service_len) == FAILURE)
{
SLOG(ERROR, "create stub failed for bad parameters");
return;
}
std::string server_addr(address_str, address_str_len);
std::string package_name_str(package_name, package_len);
std::string service_name_str(service_name, service_len);
sofa::pbrpc::RpcChannelOptions channel_options;
sofa::pbrpc::RpcChannel* _channel = NULL;
int index = rand() % 2;
if (index)
{
_channel = new sofa::pbrpc::RpcChannel(&client_a, server_addr.c_str(), channel_options);
}
else
{
_channel = new sofa::pbrpc::RpcChannel(&client_b, server_addr.c_str(), channel_options);
}
sofa::pbrpc::RpcController* _cntl = new sofa::pbrpc::RpcController();
PhpRpcServiceStubImpl* _impl = new PhpRpcServiceStubImpl(_channel,
_cntl,
package_name_str,
service_name_str);
rpc_service_stub_object* stub_obj
= (rpc_service_stub_object*)zend_object_store_get_object(stub_zval TSRMLS_CC);
stub_obj->stub_impl = _impl;
}
PHP_METHOD(PhpRpcServiceStub, SetTimeout)
{
long timeout;
zval* stub_zval = getThis();
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &timeout) == FAILURE)
{
SLOG(ERROR, "set timeout failed for bad parameters");
return;
}
rpc_service_stub_object* stub_obj
= (rpc_service_stub_object*)zend_object_store_get_object(stub_zval TSRMLS_CC);
PhpRpcServiceStubImpl* _impl = stub_obj->stub_impl;
_impl->SetTimeout(timeout);
}
PHP_METHOD(PhpRpcServiceStub, GetFailed)
{
zval* stub_zval = getThis();
rpc_service_stub_object* stub_obj
= (rpc_service_stub_object*)zend_object_store_get_object(stub_zval TSRMLS_CC);
PhpRpcServiceStubImpl* _impl = stub_obj->stub_impl;
RETURN_BOOL(_impl->Failed());
}
PHP_METHOD(PhpRpcServiceStub, GetErrorText)
{
zval* stub_zval = getThis();
rpc_service_stub_object* stub_obj
= (rpc_service_stub_object*)zend_object_store_get_object(stub_zval TSRMLS_CC);
PhpRpcServiceStubImpl* _impl = stub_obj->stub_impl;
std::string err_text = _impl->ErrorText();
RETURN_STRINGL(err_text.c_str(), err_text.length(), 1);
}
PHP_METHOD(PhpRpcServiceStub, RegisterMethod)
{
char* method_name;
int method_length;
zval* request;
zval* response;
zval* stub_zval = getThis();
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"szz",
&method_name,
&method_length,
&request,
&response) == FAILURE)
{
SLOG(ERROR, "register method failed for bad parameters");
return;
}
std::string method_name_str(method_name, method_length);
rpc_service_stub_object* stub_obj
= (rpc_service_stub_object*)zend_object_store_get_object(stub_zval TSRMLS_CC);
PhpRpcServiceStubImpl* _impl = stub_obj->stub_impl;
_impl->RegisterMethod(method_name_str, request, response);
}
PHP_METHOD(PhpRpcServiceStub, InitMethods)
{
zval* stub_zval = getThis();
rpc_service_stub_object* stub_obj
= (rpc_service_stub_object*)zend_object_store_get_object(stub_zval TSRMLS_CC);
PhpRpcServiceStubImpl* _impl = stub_obj->stub_impl;
_impl->InitMethods();
}
PHP_METHOD(PhpRpcServiceStub, CallMethod)
{
char* method_name;
int method_length;
zval* request_zval;
zval* response_zval;
zval* controller_zval;
zval* closure_zval;
zval* stub_zval = getThis();
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
"szzz",
&method_name,
&method_length,
&request_zval,
&response_zval,
&closure_zval) == FAILURE)
{
SLOG(ERROR, "call method failed for bad parameters");
return;
}
rpc_service_stub_object* stub_obj
= (rpc_service_stub_object*)zend_object_store_get_object(stub_zval TSRMLS_CC);
PhpRpcServiceStubImpl* _impl = stub_obj->stub_impl;
std::string method_name_str(method_name, method_length);
_impl->CallMethod(method_name_str, request_zval, response_zval, closure_zval);
}
const zend_function_entry sofa_pbrpc_functions[] = {
PHP_ME(PhpRpcServiceStub, InitService, rpc_service_stub_arginfo_initservice, ZEND_ACC_PUBLIC)
PHP_ME(PhpRpcServiceStub, SetTimeout, rpc_service_stub_arginfo_settimeout, ZEND_ACC_PUBLIC)
PHP_ME(PhpRpcServiceStub, GetFailed, rpc_service_stub_arginfo_getfailed, ZEND_ACC_PUBLIC)
PHP_ME(PhpRpcServiceStub, GetErrorText, rpc_service_stub_arginfo_geterrortext, ZEND_ACC_PUBLIC)
PHP_ME(PhpRpcServiceStub, RegisterMethod, rpc_service_stub_arginfo_registermethod, ZEND_ACC_PUBLIC)
PHP_ME(PhpRpcServiceStub, InitMethods, rpc_service_stub_arginfo_initmethod, ZEND_ACC_PUBLIC)
PHP_ME(PhpRpcServiceStub, CallMethod, rpc_service_stub_arginfo_callmethod, ZEND_ACC_PUBLIC)
{NULL, NULL, NULL, 0, 0}
};
PHP_MINIT_FUNCTION(sofa_pbrpc)
{
zend_class_entry ce;
INIT_CLASS_ENTRY(ce, "PhpRpcServiceStub", sofa_pbrpc_functions);
rpc_service_stub_ce = zend_register_internal_class(&ce TSRMLS_CC);
rpc_service_stub_ce->create_object = rpc_service_stub_create_handler;
memcpy(&rpc_service_stub_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
rpc_service_stub_object_handlers.clone_obj = NULL;
PB_CONSTANT(PB_TYPE_DOUBLE);
PB_CONSTANT(PB_TYPE_FIXED32);
PB_CONSTANT(PB_TYPE_FIXED64);
PB_CONSTANT(PB_TYPE_FLOAT);
PB_CONSTANT(PB_TYPE_INT);
PB_CONSTANT(PB_TYPE_SIGNED_INT);
PB_CONSTANT(PB_TYPE_STRING);
PB_CONSTANT(PB_TYPE_BOOL);
return SUCCESS;
}
PHP_MSHUTDOWN_FUNCTION(sofa_pbrpc)
{
return SUCCESS;
}
PHP_RINIT_FUNCTION(sofa_pbrpc)
{
return SUCCESS;
}
PHP_RSHUTDOWN_FUNCTION(sofa_pbrpc)
{
return SUCCESS;
}
PHP_MINFO_FUNCTION(sofa_pbrpc)
{
php_info_print_table_start();
php_info_print_table_header(2, "sofa_pbrpc support", "enabled");
php_info_print_table_end();
}
zend_module_entry sofa_pbrpc_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
"sofa_pbrpc",
sofa_pbrpc_functions,
PHP_MINIT(sofa_pbrpc),
PHP_MSHUTDOWN(sofa_pbrpc),
PHP_RINIT(sofa_pbrpc),
PHP_RSHUTDOWN(sofa_pbrpc),
PHP_MINFO(sofa_pbrpc),
#if ZEND_MODULE_API_NO >= 20010901
"0.1",
#endif
STANDARD_MODULE_PROPERTIES
};
#ifdef COMPILE_DL_SOFA_PBRPC
ZEND_GET_MODULE(sofa_pbrpc)
#endif
}
/* vim: set ts=4 sw=4 sts=4 tw=100 */