From 2035c745747ba78e740a20fefc9885b2463df5e6 Mon Sep 17 00:00:00 2001 From: Foxe Chen Date: Sun, 31 Aug 2025 19:37:40 +0200 Subject: [PATCH] patch 9.1.1719: socket server code can be improved Problem: socket server code can be improved Solution: Refactor code, get rid of gettimeofday() and use ELAPSED_ macros (Foxe Chen) closes: #18147 Signed-off-by: Foxe Chen Signed-off-by: Christian Brabandt --- src/os_unix.c | 96 +++++++++++++++++++++++-------------------- src/proto/os_unix.pro | 2 +- src/version.c | 2 + 3 files changed, 54 insertions(+), 46 deletions(-) diff --git a/src/os_unix.c b/src/os_unix.c index cf195e62e1..074a9fed34 100644 --- a/src/os_unix.c +++ b/src/os_unix.c @@ -6756,14 +6756,16 @@ RealWaitForChar(int fd, long msec, int *check_for_gpm UNUSED, int *interrupted) # endif # ifdef FEAT_SOCKETSERVER - if (socket_server_fd != -1) + if (socket_server_idx >= 0) { if (fds[socket_server_idx].revents & POLLIN) - socket_server_accept_client(); + { + if (socket_server_accept_client() == FAIL) + socket_server_uninit(); + } else if (fds[socket_server_idx].revents & (POLLHUP | POLLERR)) socket_server_uninit(); } - # endif # ifdef FEAT_WAYLAND_CLIPBOARD @@ -6966,13 +6968,10 @@ select_eintr: # endif # ifdef FEAT_SOCKETSERVER - if (socket_server_fd != -1 && ret > 0) - { - if (FD_ISSET(socket_server_fd, &rfds)) - socket_server_accept_client(); - else if (FD_ISSET(socket_server_fd, &efds)) - socket_server_uninit(); - } + if (ret > 0 && socket_server_fd != -1 + && FD_ISSET(socket_server_fd, &rfds) + && socket_server_accept_client() == FAIL) + socket_server_uninit(); # endif # ifdef FEAT_WAYLAND_CLIPBOARD @@ -9437,16 +9436,17 @@ socket_server_list_sockets(void) /* * Called when the server has received a new command. If so, parse it and do the - * stuff it says, and possibly send back a reply. + * stuff it says, and possibly send back a reply. Returns OK if client was + * accepted, else FAIL. */ - void + int socket_server_accept_client(void) { int fd = accept(socket_server_fd, NULL, NULL); ss_cmd_T cmd; if (fd == -1) - return; + return FAIL; if (socket_server_decode_cmd(&cmd, fd, 1000) == FAIL) goto exit; @@ -9460,6 +9460,7 @@ socket_server_accept_client(void) exit: close(fd); + return OK; } /* @@ -9545,8 +9546,9 @@ socket_server_send( size_t sz; char_u *final; char_u *path; - struct timeval start, now; - +#ifdef ELAPSED_FUNC + elapsed_T start_tv; +#endif if (!socket_server_valid()) { @@ -9625,7 +9627,9 @@ socket_server_send( socket_server_init_pending_cmd(&pending); - gettimeofday(&start, NULL); +#ifdef ELAPSED_FUNC + ELAPSED_INIT(start_tv); +#endif // Wait for server to send back result while (socket_server_dispatch(500) >= 0) @@ -9633,11 +9637,7 @@ socket_server_send( if (pending.result != NULL) break; - gettimeofday(&now, NULL); - - if ((now.tv_sec * 1000000 + now.tv_usec) - - (start.tv_sec * 1000000 + start.tv_usec) >= - (timeout > 0 ? timeout * 1000 : 1000 * 1000)) + if (ELAPSED_FUNC(start_tv) >= (timeout > 0 ? timeout : 1000)) break; } @@ -9668,10 +9668,12 @@ socket_server_send( * success and FAIL on failure. Timeout is in milliseconds */ int -socket_server_read_reply(char_u *client, char_u **str, int timeout) +socket_server_read_reply(char_u *client, char_u **str, int timeout UNUSED) { - ss_reply_T *reply = NULL; - struct timeval start, now; + ss_reply_T *reply = NULL; +#ifdef ELAPSED_FUNC + elapsed_T start_tv; +#endif if (!socket_server_name_is_valid(client)) return -1; @@ -9679,8 +9681,10 @@ socket_server_read_reply(char_u *client, char_u **str, int timeout) if (!socket_server_valid()) return -1; +#ifdef ELAPSED_FUNC if (timeout > 0) - gettimeofday(&start, NULL); + ELAPSED_INIT(start_tv); +#endif // Try seeing if there already is a reply in the queue goto get_reply; @@ -9689,13 +9693,10 @@ socket_server_read_reply(char_u *client, char_u **str, int timeout) { int fd; - if (timeout > 0) - gettimeofday(&now, NULL); - - if (timeout > 0) - if ((now.tv_sec * 1000000 + now.tv_usec) - - (start.tv_sec * 1000000 + start.tv_usec) >= timeout * 1000) - break; +#ifdef ELAPSED_FUNC + if (timeout > 0 && ELAPSED_FUNC(start_tv) >= timeout) + break; +#endif get_reply: reply = socket_server_get_reply(client, NULL); @@ -10023,7 +10024,9 @@ socket_server_decode_cmd(ss_cmd_T *cmd, int socket_fd, int timeout) size_t total_r = 0; char_u *buf; char_u *cur; - struct timeval start, now; +#ifdef ELAPSED_FUNC + elapsed_T start_tv; +#endif // We also poll the socket server listening file descriptor to handle // recursive remote calls between Vim instances, such as when one Vim @@ -10051,7 +10054,9 @@ socket_server_decode_cmd(ss_cmd_T *cmd, int socket_fd, int timeout) // want to free an uninitialized pointer. memset(cmd, 0, sizeof(*cmd)); - gettimeofday(&start, NULL); +#ifdef ELAPSED_FUNC + ELAPSED_INIT(start_tv); +#endif while (TRUE) { @@ -10125,11 +10130,10 @@ socket_server_decode_cmd(ss_cmd_T *cmd, int socket_fd, int timeout) total_r += r; continue_loop: - gettimeofday(&now, NULL); - - if ((now.tv_sec * 1000000 + now.tv_usec) - - (start.tv_sec * 1000000 + start.tv_usec) >= timeout * 1000) +#ifdef ELAPSED_FUNC + if (ELAPSED_FUNC(start_tv) >= timeout) goto fail; +#endif } // Parse message data @@ -10172,7 +10176,9 @@ socket_server_write(int socket_fd, char_u *data, size_t sz, int timeout) { char_u *cur = data; size_t total_w = 0; - struct timeval start, now; +#ifdef ELAPSED_FUNC + elapsed_T start_tv; +#endif #ifndef HAVE_SELECT struct pollfd pfd; @@ -10186,7 +10192,9 @@ socket_server_write(int socket_fd, char_u *data, size_t sz, int timeout) FD_SET(socket_fd, &wfds); #endif - gettimeofday(&start, NULL); +#ifdef ELAPSED_FUNC + ELAPSED_INIT(start_tv); +#endif while (total_w < sz) { @@ -10213,13 +10221,11 @@ socket_server_write(int socket_fd, char_u *data, size_t sz, int timeout) total_w += written; - continue_loop: - gettimeofday(&now, NULL); - - if ((now.tv_sec * 1000000 + now.tv_usec) - - (start.tv_sec * 1000000 + start.tv_usec) >= timeout * 1000) +#ifdef ELAPSED_FUNC + if (ELAPSED_FUNC(start_tv) >= timeout) return FAIL; +#endif } return OK; diff --git a/src/proto/os_unix.pro b/src/proto/os_unix.pro index 8f623c612b..41efd0f2cb 100644 --- a/src/proto/os_unix.pro +++ b/src/proto/os_unix.pro @@ -97,7 +97,7 @@ int mch_create_anon_file(void); int socket_server_init(char_u *name); void socket_server_uninit(void); char_u *socket_server_list_sockets(void); -void socket_server_accept_client(void); +int socket_server_accept_client(void); int socket_server_valid(void); int socket_server_send(char_u *name, char_u *str, char_u **result, char_u **receiver, int is_expr, int timeout, int silent); int socket_server_read_reply(char_u *client, char_u **str, int timeout); diff --git a/src/version.c b/src/version.c index 1ca0bcc281..fe65617e1a 100644 --- a/src/version.c +++ b/src/version.c @@ -724,6 +724,8 @@ static char *(features[]) = static int included_patches[] = { /* Add new patch number below this line */ +/**/ + 1719, /**/ 1718, /**/