Skip to content

Commit 553a165

Browse files
committed
Optimize code
1 parent a385082 commit 553a165

File tree

5 files changed

+78
-222
lines changed

5 files changed

+78
-222
lines changed

src/php/api.cc

Lines changed: 47 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@
33
#include <cstring>
44

55
#include "phpx.h"
6-
#include "php_streams.h"
76
#include "ext/swoole/include/swoole_api.h"
87
#include "ext/swoole/include/swoole_coroutine_c_api.h"
98
#include "zookeeper.h"
109
#include "zklib.h"
11-
#include "api_x_arginfo.h"
12-
#include "../../vendor/swoole/phpx/include/phpx.h"
10+
#include "api_arginfo.h"
1311

1412
#define EXT_NAME "swoole_zookeeper"
1513

@@ -20,9 +18,9 @@ using namespace zookeeper;
2018
zhandle_t *handle = nullptr;
2119
int connected = 0;
2220
int expired = 0;
23-
static struct timeval startTime;
21+
static timeval startTime;
2422

25-
const bool debug = false;
23+
constexpr bool debug = false;
2624

2725
struct QueryResult {
2826
QueryResult() : retval(nullptr) {
@@ -35,7 +33,7 @@ struct QueryResult {
3533
Variant retval;
3634
};
3735

38-
static void dump_stat(const struct Stat *stat) {
36+
static void dump_stat(const Stat *stat) {
3937
char tctimes[40];
4038
char tmtimes[40];
4139
time_t tctime;
@@ -65,9 +63,9 @@ static void dump_stat(const struct Stat *stat) {
6563
stat->ephemeralOwner);
6664
}
6765

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) {
6967
int fd, rc, events = ZOOKEEPER_READ;
70-
struct timeval tv;
68+
timeval tv{};
7169
fd_set rfds, wfds, efds;
7270
if (!swoole_coroutine_is_in()) {
7371
FD_ZERO(&rfds);
@@ -146,14 +144,12 @@ static void my_string_completion_free_data(int rc, const char *name, const void
146144
my_string_completion(rc, name, data);
147145
}
148146

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);
157153
fprintf(stderr, "%s: rc = %d\n", (char *) data, rc);
158154
if (value) {
159155
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
163159
free((void *) data);
164160
}
165161

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;
168164
result->error = rc;
169165
if (rc == ZOK) {
170166
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
174170
result->running = false;
175171
}
176172

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;
179175
result->error = rc;
180176
if (rc == ZOK) {
181177
Array array;
182178
if (strings) {
183-
int i;
184-
for (i = 0; i < strings->count; i++) {
179+
for (int i = 0; i < strings->count; i++) {
185180
array.append(strings->data[i]);
186181
}
187182
}
@@ -193,16 +188,13 @@ void my_strings_completion(int rc, const struct String_vector *strings, const vo
193188
result->running = false;
194189
}
195190

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) {
200192
my_strings_completion(rc, strings, data);
201193
dump_stat(stat);
202194
}
203195

204196
void my_void_completion(int rc, const void *data) {
205-
QueryResult *result = (QueryResult *) data;
197+
auto *result = (QueryResult *) data;
206198
result->error = rc;
207199
if (rc == ZOK) {
208200
result->retval = true;
@@ -212,13 +204,13 @@ void my_void_completion(int rc, const void *data) {
212204
result->running = false;
213205
}
214206

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) {
216208
if (debug) {
217209
std::printf("my_stat_completion rc=%d\n", rc);
218210
dump_stat(stat);
219211
}
220212

221-
QueryResult *result = (QueryResult *) data;
213+
auto *result = (QueryResult *) data;
222214
result->error = rc;
223215
if (rc == ZOK) {
224216
result->retval = true;
@@ -228,8 +220,8 @@ void my_stat_completion(int rc, const struct Stat *stat, const void *data) {
228220
result->running = false;
229221
}
230222

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;
233225
result->error = rc;
234226
if (rc == ZOK) {
235227
result->retval = true;
@@ -239,14 +231,14 @@ void my_silent_stat_completion(int rc, const struct Stat *stat, const void *data
239231
result->running = false;
240232
}
241233

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;
244236
result->error = rc;
245237
if (rc == ZOK) {
246238
Array res;
247-
//把acl转化到数组
239+
// 把acl转化到数组
248240
convert_acl_to_array(&res, acl);
249-
//把stat转化到数组
241+
// 把stat转化到数组
250242
convert_stat_to_array(&res, stat);
251243
result->retval = res;
252244
} else {
@@ -256,7 +248,7 @@ void my_acl_stat_completion(int rc, struct ACL_vector *acl, struct Stat *stat, c
256248
}
257249

258250
void my_set_acl_completion(int rc, const void *data) {
259-
QueryResult *result = (QueryResult *) data;
251+
auto *result = (QueryResult *) data;
260252
result->error = rc;
261253
if (rc == ZOK) {
262254
result->retval = true;
@@ -268,13 +260,12 @@ void my_set_acl_completion(int rc, const void *data) {
268260

269261
/**
270262
* 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
272264
* @param aclv
273265
* @param array
274266
*/
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++) {
278269
Array temp;
279270
temp.set("perms", aclv->data[i].perms);
280271
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) {
288279
* @param stat
289280
* @param array
290281
*/
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) {
292283
array.clean();
293284
array.set("czxid", (long) stat->czxid);
294285
array.set("mzxid", (long) stat->mzxid);
@@ -310,8 +301,8 @@ static void php_stat_to_array(const struct Stat *stat, Array &array) {
310301
* @param stat
311302
* @param data
312303
*/
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;
315306
result->error = rc;
316307
if (rc == ZOK) {
317308
Array _array(result->retval);
@@ -334,7 +325,7 @@ void my_acl_completion(int rc, struct ACL_vector *acl, struct Stat *stat, const
334325
static inline zhandle_t *get_class_handle(Object &_this) {
335326
auto zh = _this.oGet<zhandle_t>("handle", "zhandle_t");
336327
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");
338329
return nullptr;
339330
} else {
340331
return zh;
@@ -343,14 +334,14 @@ static inline zhandle_t *get_class_handle(Object &_this) {
343334

344335
PHPX_METHOD(Swoole_ZooKeeper, __construct) {
345336
auto host = args[0];
346-
double recv_timeout = args[1].toInt();
337+
double recv_timeout = args[1].toFloat();
347338
int recv_timeout_ms = recv_timeout * 1000;
348339
zoo_deterministic_conn_order(1);
349340

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);
351342
if (!zh) {
352343
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);
354345
} else {
355346
_this.oSet<zhandle_t>("handle", "zhandle_t", zh);
356347
}
@@ -540,15 +531,14 @@ PHPX_METHOD(Swoole_ZooKeeper, getState) {
540531
}
541532

542533
PHPX_METHOD(Swoole_ZooKeeper, getClientId) {
543-
const clientid_t *cid;
544534
zhandle_t *zh = get_class_handle(_this);
545535
if (!zh) {
546536
return nullptr;
547537
}
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);
552542
return rv;
553543
}
554544

@@ -577,7 +567,7 @@ PHPX_METHOD(Swoole_ZooKeeper, setLogStream) {
577567
z_stream = args[0].ptr();
578568

579569
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) {
581571
goto _return_null;
582572
}
583573

@@ -615,7 +605,7 @@ void zookeeper_dtor(zend_resource *res) {
615605
}
616606

617607
PHPX_METHOD(Swoole_ZooKeeper, setAcl) {
618-
struct ACL_vector *zookeeper_acl;
608+
ACL_vector *zookeeper_acl;
619609
QueryResult result;
620610
long version = -1;
621611
// 至少有一个参数
@@ -703,14 +693,13 @@ PHPX_METHOD(Swoole_ZooKeeper, watchChildren) {
703693

704694
PHPX_METHOD(Swoole_ZooKeeper, wait) {
705695
zhandle_t *zh = get_class_handle(_this);
706-
QueryResult result;
707696

708697
if (!zh) {
709698
return nullptr;
710699
}
711700

712-
int fd, rc, events = ZOOKEEPER_READ;
713-
struct timeval tv;
701+
int fd, events = ZOOKEEPER_READ;
702+
timeval tv{};
714703
fd_set rfds, wfds, efds;
715704
if (!swoole_coroutine_is_in()) {
716705
FD_ZERO(&rfds);
@@ -719,6 +708,7 @@ PHPX_METHOD(Swoole_ZooKeeper, wait) {
719708
}
720709

721710
while (true) {
711+
QueryResult result;
722712
int rc = zookeeper_interest(zh, &fd, &events, &tv);
723713
if (rc) {
724714
_error:

0 commit comments

Comments
 (0)