Skip to content

Commit b0146b8

Browse files
committed
Optimize code
1 parent 270f0a1 commit b0146b8

File tree

4 files changed

+27
-13
lines changed

4 files changed

+27
-13
lines changed

ext-src/php_swoole_library.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Generated by build-library.php, Please DO NOT modify!
33
*/
44

5-
/* $Id: b591f704aaab92336e8906d2584b7030fde90e87 */
5+
/* $Id: e07037baef45ea64d334257e203fe2ca82914c93 */
66

77
static const char* swoole_library_source_constants =
88
"\n"
@@ -618,7 +618,7 @@ static const char* swoole_library_source_core_string_object =
618618
" /**\n"
619619
" * @return static\n"
620620
" */\n"
621-
" public function substr(int $offset, ?int $length = null): self\n"
621+
" public function substr(int $offset, ?int $length = null)\n"
622622
" {\n"
623623
" return new static(substr($this->string, ...func_get_args()));\n"
624624
" }\n"
@@ -693,7 +693,7 @@ static const char* swoole_library_source_core_string_object =
693693
" */\n"
694694
" public function chunkSplit(int $chunkLength = 76, string $chunkEnd = ''): self\n"
695695
" {\n"
696-
" return new static(chunk_split($this->string, $chunkLength, $chunkEnd));\n"
696+
" return new static(chunk_split($this->string, ...func_get_args()));\n"
697697
" }\n"
698698
"\n"
699699
" public function chunk(int $splitLength = 1): ArrayObject\n"

ext-src/swoole_server.cc

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1872,8 +1872,8 @@ void php_swoole_server_onClose(Server *serv, DataHead *info) {
18721872
}
18731873
if (conn->websocket_status != WEBSOCKET_STATUS_ACTIVE) {
18741874
ListenPort *port = serv->get_port_by_server_fd(info->server_fd);
1875-
if (port && port->open_websocket_protocol
1876-
&& php_swoole_server_isset_callback(serv, port, SW_SERVER_CB_onDisconnect)) {
1875+
if (port && port->open_websocket_protocol &&
1876+
php_swoole_server_isset_callback(serv, port, SW_SERVER_CB_onDisconnect)) {
18771877
fci_cache = php_swoole_server_get_fci_cache(serv, info->server_fd, SW_SERVER_CB_onDisconnect);
18781878
}
18791879
}
@@ -3078,11 +3078,15 @@ static PHP_METHOD(swoole_server, heartbeat) {
30783078
if (conn->protect || conn->last_recv_time == 0 || conn->last_recv_time > checktime) {
30793079
return;
30803080
}
3081+
SessionId session_id = conn->session_id;
3082+
if (session_id <= 0) {
3083+
return;
3084+
}
30813085
if (close_connection) {
30823086
conn->close_force = 1;
3083-
serv->close(conn->fd, false);
3087+
serv->close(session_id, false);
30843088
}
3085-
add_next_index_long(return_value, conn->session_id);
3089+
add_next_index_long(return_value, session_id);
30863090
});
30873091
}
30883092

@@ -3713,7 +3717,11 @@ static PHP_METHOD(swoole_server, getClientList) {
37133717
swTrace("maxfd=%d, fd=%d, find_count=%ld, start_fd=%ld", serv_max_fd, fd, find_count, start_session_id);
37143718
Connection *conn = serv->get_connection_for_iterator(fd);
37153719
if (conn) {
3716-
add_next_index_long(return_value, conn->session_id);
3720+
SessionId session_id = conn->session_id;
3721+
if (session_id <= 0) {
3722+
continue;
3723+
}
3724+
add_next_index_long(return_value, session_id);
37173725
find_count--;
37183726
}
37193727
// finish fetch
@@ -3931,10 +3939,12 @@ static PHP_METHOD(swoole_connection_iterator, valid) {
39313939
if (!conn) {
39323940
continue;
39333941
}
3934-
if (iterator->port && (iterator->port->get_fd() < 0 || conn->server_fd != iterator->port->get_fd())) {
3942+
SessionId session_id = conn->session_id;
3943+
if (session_id <= 0 ||
3944+
(iterator->port && (iterator->port->get_fd() < 0 || conn->server_fd != iterator->port->get_fd()))) {
39353945
continue;
39363946
}
3937-
iterator->session_id = conn->session_id;
3947+
iterator->session_id = session_id;
39383948
iterator->current_fd = fd;
39393949
iterator->index++;
39403950
RETURN_TRUE;

include/swoole_server.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1158,7 +1158,7 @@ class Server {
11581158

11591159
inline Connection *get_connection_for_iterator(int fd) {
11601160
Connection *conn = get_connection(fd);
1161-
if (conn && conn->active && !conn->closed && conn->session_id > 0) {
1161+
if (conn && conn->active && !conn->closed) {
11621162
#ifdef SW_USE_OPENSSL
11631163
if (conn->ssl && !conn->ssl_ready) {
11641164
return nullptr;

src/server/reactor_thread.cc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,11 +1091,15 @@ void Server::start_heartbeat_thread() {
10911091
if (conn->protect || conn->last_recv_time == 0 || conn->last_recv_time > checktime) {
10921092
return;
10931093
}
1094+
SessionId session_id = conn->session_id;
1095+
if (session_id <= 0) {
1096+
return;
1097+
}
10941098
DataHead ev{};
10951099
ev.type = SW_SERVER_EVENT_CLOSE_FORCE;
10961100
// convert fd to session_id, in order to verify the connection before the force close connection
1097-
ev.fd = conn->session_id;
1098-
Socket *_pipe_sock = get_reactor_thread_pipe(conn->session_id, conn->reactor_id);
1101+
ev.fd = session_id;
1102+
Socket *_pipe_sock = get_reactor_thread_pipe(session_id, conn->reactor_id);
10991103
_pipe_sock->send_blocking((void *) &ev, sizeof(ev));
11001104
});
11011105
sleep(heartbeat_check_interval);

0 commit comments

Comments
 (0)