3
3
#include < cstring>
4
4
5
5
#include " phpx.h"
6
- #include " php_streams.h"
7
6
#include " ext/swoole/include/swoole_api.h"
8
7
#include " ext/swoole/include/swoole_coroutine_c_api.h"
9
8
#include " zookeeper.h"
10
9
#include " zklib.h"
11
- #include " api_x_arginfo.h"
12
- #include " ../../vendor/swoole/phpx/include/phpx.h"
10
+ #include " api_arginfo.h"
13
11
14
12
#define EXT_NAME " swoole_zookeeper"
15
13
@@ -20,9 +18,9 @@ using namespace zookeeper;
20
18
zhandle_t *handle = nullptr ;
21
19
int connected = 0 ;
22
20
int expired = 0 ;
23
- static struct timeval startTime;
21
+ static timeval startTime;
24
22
25
- const bool debug = false ;
23
+ constexpr bool debug = false ;
26
24
27
25
struct QueryResult {
28
26
QueryResult () : retval(nullptr ) {
@@ -35,7 +33,7 @@ struct QueryResult {
35
33
Variant retval;
36
34
};
37
35
38
- static void dump_stat (const struct Stat *stat) {
36
+ static void dump_stat (const Stat *stat) {
39
37
char tctimes[40 ];
40
38
char tmtimes[40 ];
41
39
time_t tctime;
@@ -65,9 +63,9 @@ static void dump_stat(const struct Stat *stat) {
65
63
stat->ephemeralOwner );
66
64
}
67
65
68
- static void zk_dispatch (Object &_this, zhandle_t *zh, QueryResult &result) {
66
+ static void zk_dispatch (const Object &_this, zhandle_t *zh, QueryResult &result) {
69
67
int fd, rc, events = ZOOKEEPER_READ;
70
- struct timeval tv;
68
+ timeval tv{} ;
71
69
fd_set rfds, wfds, efds;
72
70
if (!swoole_coroutine_is_in ()) {
73
71
FD_ZERO (&rfds);
@@ -146,14 +144,12 @@ static void my_string_completion_free_data(int rc, const char *name, const void
146
144
my_string_completion (rc, name, data);
147
145
}
148
146
149
- void my_data_completion (int rc, const char *value, int value_len, const struct Stat *stat, const void *data) {
150
- struct timeval tv;
151
- int sec;
152
- int usec;
153
- gettimeofday (&tv, 0 );
154
- sec = tv.tv_sec - startTime.tv_sec ;
155
- usec = tv.tv_usec - startTime.tv_usec ;
156
- fprintf (stderr, " time = %d msec\n " , sec * 1000 + usec / 1000 );
147
+ void my_data_completion (int rc, const char *value, int value_len, const Stat *stat, const void *data) {
148
+ timeval tv{};
149
+ gettimeofday (&tv, nullptr );
150
+ const long sec = tv.tv_sec - startTime.tv_sec ;
151
+ const long usec = tv.tv_usec - startTime.tv_usec ;
152
+ fprintf (stderr, " time = %ld msec\n " , sec * 1000 + usec / 1000 );
157
153
fprintf (stderr, " %s: rc = %d\n " , (char *) data, rc);
158
154
if (value) {
159
155
fprintf (stderr, " value_len = %d\n " , value_len);
@@ -163,8 +159,8 @@ void my_data_completion(int rc, const char *value, int value_len, const struct S
163
159
free ((void *) data);
164
160
}
165
161
166
- void my_silent_data_completion (int rc, const char *value, int value_len, const struct Stat *stat, const void *data) {
167
- QueryResult *result = (QueryResult *) data;
162
+ void my_silent_data_completion (int rc, const char *value, int value_len, const Stat *stat, const void *data) {
163
+ auto *result = (QueryResult *) data;
168
164
result->error = rc;
169
165
if (rc == ZOK) {
170
166
result->retval = Variant (value, value_len);
@@ -174,14 +170,13 @@ void my_silent_data_completion(int rc, const char *value, int value_len, const s
174
170
result->running = false ;
175
171
}
176
172
177
- void my_strings_completion (int rc, const struct String_vector *strings, const void *data) {
178
- QueryResult *result = (QueryResult *) data;
173
+ void my_strings_completion (int rc, const String_vector *strings, const void *data) {
174
+ auto *result = (QueryResult *) data;
179
175
result->error = rc;
180
176
if (rc == ZOK) {
181
177
Array array;
182
178
if (strings) {
183
- int i;
184
- for (i = 0 ; i < strings->count ; i++) {
179
+ for (int i = 0 ; i < strings->count ; i++) {
185
180
array.append (strings->data [i]);
186
181
}
187
182
}
@@ -193,16 +188,13 @@ void my_strings_completion(int rc, const struct String_vector *strings, const vo
193
188
result->running = false ;
194
189
}
195
190
196
- void my_strings_stat_completion (int rc,
197
- const struct String_vector *strings,
198
- const struct Stat *stat,
199
- const void *data) {
191
+ void my_strings_stat_completion (int rc, const String_vector *strings, const Stat *stat, const void *data) {
200
192
my_strings_completion (rc, strings, data);
201
193
dump_stat (stat);
202
194
}
203
195
204
196
void my_void_completion (int rc, const void *data) {
205
- QueryResult *result = (QueryResult *) data;
197
+ auto *result = (QueryResult *) data;
206
198
result->error = rc;
207
199
if (rc == ZOK) {
208
200
result->retval = true ;
@@ -212,13 +204,13 @@ void my_void_completion(int rc, const void *data) {
212
204
result->running = false ;
213
205
}
214
206
215
- void my_stat_completion (int rc, const struct Stat *stat, const void *data) {
207
+ void my_stat_completion (int rc, const Stat *stat, const void *data) {
216
208
if (debug) {
217
209
std::printf (" my_stat_completion rc=%d\n " , rc);
218
210
dump_stat (stat);
219
211
}
220
212
221
- QueryResult *result = (QueryResult *) data;
213
+ auto *result = (QueryResult *) data;
222
214
result->error = rc;
223
215
if (rc == ZOK) {
224
216
result->retval = true ;
@@ -228,8 +220,8 @@ void my_stat_completion(int rc, const struct Stat *stat, const void *data) {
228
220
result->running = false ;
229
221
}
230
222
231
- void my_silent_stat_completion (int rc, const struct Stat *stat, const void *data) {
232
- QueryResult *result = (QueryResult *) data;
223
+ void my_silent_stat_completion (int rc, const Stat *stat, const void *data) {
224
+ auto *result = (QueryResult *) data;
233
225
result->error = rc;
234
226
if (rc == ZOK) {
235
227
result->retval = true ;
@@ -239,14 +231,14 @@ void my_silent_stat_completion(int rc, const struct Stat *stat, const void *data
239
231
result->running = false ;
240
232
}
241
233
242
- void my_acl_stat_completion (int rc, struct ACL_vector *acl, struct Stat *stat, const void *data) {
243
- QueryResult *result = (QueryResult *) data;
234
+ void my_acl_stat_completion (int rc, ACL_vector *acl, Stat *stat, const void *data) {
235
+ auto *result = (QueryResult *) data;
244
236
result->error = rc;
245
237
if (rc == ZOK) {
246
238
Array res;
247
- // 把acl转化到数组
239
+ // 把acl转化到数组
248
240
convert_acl_to_array (&res, acl);
249
- // 把stat转化到数组
241
+ // 把stat转化到数组
250
242
convert_stat_to_array (&res, stat);
251
243
result->retval = res;
252
244
} else {
@@ -256,7 +248,7 @@ void my_acl_stat_completion(int rc, struct ACL_vector *acl, struct Stat *stat, c
256
248
}
257
249
258
250
void my_set_acl_completion (int rc, const void *data) {
259
- QueryResult *result = (QueryResult *) data;
251
+ auto *result = (QueryResult *) data;
260
252
result->error = rc;
261
253
if (rc == ZOK) {
262
254
result->retval = true ;
@@ -268,13 +260,12 @@ void my_set_acl_completion(int rc, const void *data) {
268
260
269
261
/* *
270
262
* C acl信息转换为php数组信息
271
- * 参考https ://github.com/andreiz/php-zookeeper/blob/master/php_zookeeper.c#L922
263
+ * 参考 https ://github.com/andreiz/php-zookeeper/blob/master/php_zookeeper.c#L922
272
264
* @param aclv
273
265
* @param array
274
266
*/
275
- static void php_aclv_to_array (const struct ACL_vector *aclv, Array &array) {
276
- int i;
277
- for (i = 0 ; i < aclv->count ; i++) {
267
+ static void php_aclv_to_array (const ACL_vector *aclv, Array &array) {
268
+ for (int i = 0 ; i < aclv->count ; i++) {
278
269
Array temp;
279
270
temp.set (" perms" , aclv->data [i].perms );
280
271
temp.set (" scheme" , aclv->data [i].id .scheme );
@@ -288,7 +279,7 @@ static void php_aclv_to_array(const struct ACL_vector *aclv, Array &array) {
288
279
* @param stat
289
280
* @param array
290
281
*/
291
- static void php_stat_to_array (const struct Stat *stat, Array &array) {
282
+ static void php_stat_to_array (const Stat *stat, Array &array) {
292
283
array.clean ();
293
284
array.set (" czxid" , (long ) stat->czxid );
294
285
array.set (" mzxid" , (long ) stat->mzxid );
@@ -310,8 +301,8 @@ static void php_stat_to_array(const struct Stat *stat, Array &array) {
310
301
* @param stat
311
302
* @param data
312
303
*/
313
- void my_acl_completion (int rc, struct ACL_vector *acl, struct Stat *stat, const void *data) {
314
- QueryResult *result = (QueryResult *) data;
304
+ void my_acl_completion (int rc, ACL_vector *acl, Stat *stat, const void *data) {
305
+ auto *result = (QueryResult *) data;
315
306
result->error = rc;
316
307
if (rc == ZOK) {
317
308
Array _array (result->retval );
@@ -334,7 +325,7 @@ void my_acl_completion(int rc, struct ACL_vector *acl, struct Stat *stat, const
334
325
static inline zhandle_t *get_class_handle (Object &_this) {
335
326
auto zh = _this.oGet <zhandle_t >(" handle" , " zhandle_t" );
336
327
if (zh == nullptr ) {
337
- zend_throw_exception_ex (NULL , 0 , " Could not get zookeeper handle" );
328
+ zend_throw_exception_ex (nullptr , 0 , " Could not get zookeeper handle" );
338
329
return nullptr ;
339
330
} else {
340
331
return zh;
@@ -343,14 +334,14 @@ static inline zhandle_t *get_class_handle(Object &_this) {
343
334
344
335
PHPX_METHOD (Swoole_ZooKeeper, __construct) {
345
336
auto host = args[0 ];
346
- double recv_timeout = args[1 ].toInt ();
337
+ double recv_timeout = args[1 ].toFloat ();
347
338
int recv_timeout_ms = recv_timeout * 1000 ;
348
339
zoo_deterministic_conn_order (1 );
349
340
350
- zhandle_t *zh = zookeeper_init (host.toCString (), nullptr , recv_timeout_ms, 0 , NULL , 0 );
341
+ zhandle_t *zh = zookeeper_init (host.toCString (), nullptr , recv_timeout_ms, nullptr , nullptr , 0 );
351
342
if (!zh) {
352
343
zend_throw_exception (nullptr , " connect zookeeper of server failed" , 0 );
353
- _this.oSet <zhandle_t >(" handle" , " zhandle_t" , NULL );
344
+ _this.oSet <zhandle_t >(" handle" , " zhandle_t" , nullptr );
354
345
} else {
355
346
_this.oSet <zhandle_t >(" handle" , " zhandle_t" , zh);
356
347
}
@@ -540,15 +531,14 @@ PHPX_METHOD(Swoole_ZooKeeper, getState) {
540
531
}
541
532
542
533
PHPX_METHOD (Swoole_ZooKeeper, getClientId) {
543
- const clientid_t *cid;
544
534
zhandle_t *zh = get_class_handle (_this);
545
535
if (!zh) {
546
536
return nullptr ;
547
537
}
548
- cid = zoo_client_id (zh);
549
- Array rv = Array () ;
550
- rv.append (( long ) cid->client_id );
551
- rv.append (( char *) cid->passwd );
538
+ const clientid_t * cid = zoo_client_id (zh);
539
+ Array rv;
540
+ rv.append (cid->client_id );
541
+ rv.append (cid->passwd );
552
542
return rv;
553
543
}
554
544
@@ -577,7 +567,7 @@ PHPX_METHOD(Swoole_ZooKeeper, setLogStream) {
577
567
z_stream = args[0 ].ptr ();
578
568
579
569
stream = (php_stream *) zend_fetch_resource (Z_RES_P (z_stream), " stream" , Z_RES_P (z_stream)->type );
580
- if (NULL == stream) {
570
+ if (nullptr == stream) {
581
571
goto _return_null;
582
572
}
583
573
@@ -615,7 +605,7 @@ void zookeeper_dtor(zend_resource *res) {
615
605
}
616
606
617
607
PHPX_METHOD (Swoole_ZooKeeper, setAcl) {
618
- struct ACL_vector *zookeeper_acl;
608
+ ACL_vector *zookeeper_acl;
619
609
QueryResult result;
620
610
long version = -1 ;
621
611
// 至少有一个参数
@@ -703,14 +693,13 @@ PHPX_METHOD(Swoole_ZooKeeper, watchChildren) {
703
693
704
694
PHPX_METHOD (Swoole_ZooKeeper, wait) {
705
695
zhandle_t *zh = get_class_handle (_this);
706
- QueryResult result;
707
696
708
697
if (!zh) {
709
698
return nullptr ;
710
699
}
711
700
712
- int fd, rc, events = ZOOKEEPER_READ;
713
- struct timeval tv;
701
+ int fd, events = ZOOKEEPER_READ;
702
+ timeval tv{} ;
714
703
fd_set rfds, wfds, efds;
715
704
if (!swoole_coroutine_is_in ()) {
716
705
FD_ZERO (&rfds);
@@ -719,6 +708,7 @@ PHPX_METHOD(Swoole_ZooKeeper, wait) {
719
708
}
720
709
721
710
while (true ) {
711
+ QueryResult result;
722
712
int rc = zookeeper_interest (zh, &fd, &events, &tv);
723
713
if (rc) {
724
714
_error:
0 commit comments