Skip to content

Commit 23388ce

Browse files
committed
php#53: Fix async stream event optimization for TCP/UDP socket connections
Add missing asynchronous connection support for TCP/UDP sockets in php_tcp_sockop_connect. Previously only Unix domain sockets used async when ZEND_ASYNC_IS_ACTIVE, while TCP/UDP always used synchronous php_network_connect_socket_to_host. Changes: - Add php_stream parameter to php_network_connect_socket_to_host() - Use network_async_connect_socket() when stream provided and ZEND_ASYNC_IS_ACTIVE - Update FTP extension call (pass NULL to remain synchronous) - Update socket streams call (pass stream to enable async)
1 parent 860c75c commit 23388ce

File tree

4 files changed

+16
-7
lines changed

4 files changed

+16
-7
lines changed

ext/ftp/ftp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ ftpbuf_t* ftp_open(const char *host, short port, zend_long timeout_sec)
123123

124124
ftp->fd = php_network_connect_socket_to_host(host,
125125
(unsigned short) (port ? port : 21), SOCK_STREAM,
126-
0, &tv, NULL, NULL, NULL, 0, STREAM_SOCKOP_NONE);
126+
0, &tv, NULL, NULL, NULL, 0, STREAM_SOCKOP_NONE, NULL);
127127
if (ftp->fd == -1) {
128128
goto bail;
129129
}

main/network.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -834,7 +834,8 @@ PHPAPI php_socket_t php_network_accept_incoming(php_socket_t srvsock,
834834
/* {{{ php_network_connect_socket_to_host */
835835
php_socket_t php_network_connect_socket_to_host(const char *host, unsigned short port,
836836
int socktype, int asynchronous, struct timeval *timeout, zend_string **error_string,
837-
int *error_code, const char *bindto, unsigned short bindport, long sockopts
837+
int *error_code, const char *bindto, unsigned short bindport, long sockopts,
838+
php_stream *stream
838839
)
839840
{
840841
int num_addrs, n, fatal = 0;
@@ -958,9 +959,15 @@ php_socket_t php_network_connect_socket_to_host(const char *host, unsigned short
958959
}
959960
}
960961
#endif
961-
n = php_network_connect_socket(sock, sa, socklen, asynchronous,
962-
timeout ? &working_timeout : NULL,
963-
error_string, error_code);
962+
if (ZEND_ASYNC_IS_ACTIVE && stream != NULL) {
963+
n = network_async_connect_socket(stream, sock, sa, socklen, asynchronous,
964+
timeout ? &working_timeout : NULL,
965+
error_string, error_code);
966+
} else {
967+
n = php_network_connect_socket(sock, sa, socklen, asynchronous,
968+
timeout ? &working_timeout : NULL,
969+
error_string, error_code);
970+
}
964971

965972
if (n != -1) {
966973
goto connected;

main/php_network.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,8 @@ PHPAPI void php_network_freeaddresses(struct sockaddr **sal);
280280

281281
PHPAPI php_socket_t php_network_connect_socket_to_host(const char *host, unsigned short port,
282282
int socktype, int asynchronous, struct timeval *timeout, zend_string **error_string,
283-
int *error_code, const char *bindto, unsigned short bindport, long sockopts
283+
int *error_code, const char *bindto, unsigned short bindport, long sockopts,
284+
php_stream *stream
284285
);
285286

286287
PHPAPI int php_network_connect_socket(php_socket_t sockfd,

main/streams/xp_socket.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -980,7 +980,8 @@ static inline int php_tcp_sockop_connect(php_stream *stream, php_netstream_data_
980980
&err,
981981
bindto,
982982
bindport,
983-
sockopts
983+
sockopts,
984+
stream
984985
);
985986

986987
ret = sock->socket == -1 ? -1 : 0;

0 commit comments

Comments
 (0)