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 <chen.foxe@gmail.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
This commit is contained in:
committed by
Christian Brabandt
parent
6cd6857cbe
commit
2035c74574
@ -6756,14 +6756,16 @@ RealWaitForChar(int fd, long msec, int *check_for_gpm UNUSED, int *interrupted)
|
|||||||
# endif
|
# endif
|
||||||
|
|
||||||
# ifdef FEAT_SOCKETSERVER
|
# ifdef FEAT_SOCKETSERVER
|
||||||
if (socket_server_fd != -1)
|
if (socket_server_idx >= 0)
|
||||||
{
|
{
|
||||||
if (fds[socket_server_idx].revents & POLLIN)
|
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))
|
else if (fds[socket_server_idx].revents & (POLLHUP | POLLERR))
|
||||||
socket_server_uninit();
|
socket_server_uninit();
|
||||||
}
|
}
|
||||||
|
|
||||||
# endif
|
# endif
|
||||||
|
|
||||||
# ifdef FEAT_WAYLAND_CLIPBOARD
|
# ifdef FEAT_WAYLAND_CLIPBOARD
|
||||||
@ -6966,13 +6968,10 @@ select_eintr:
|
|||||||
# endif
|
# endif
|
||||||
|
|
||||||
# ifdef FEAT_SOCKETSERVER
|
# ifdef FEAT_SOCKETSERVER
|
||||||
if (socket_server_fd != -1 && ret > 0)
|
if (ret > 0 && socket_server_fd != -1
|
||||||
{
|
&& FD_ISSET(socket_server_fd, &rfds)
|
||||||
if (FD_ISSET(socket_server_fd, &rfds))
|
&& socket_server_accept_client() == FAIL)
|
||||||
socket_server_accept_client();
|
socket_server_uninit();
|
||||||
else if (FD_ISSET(socket_server_fd, &efds))
|
|
||||||
socket_server_uninit();
|
|
||||||
}
|
|
||||||
# endif
|
# endif
|
||||||
|
|
||||||
# ifdef FEAT_WAYLAND_CLIPBOARD
|
# 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
|
* 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)
|
socket_server_accept_client(void)
|
||||||
{
|
{
|
||||||
int fd = accept(socket_server_fd, NULL, NULL);
|
int fd = accept(socket_server_fd, NULL, NULL);
|
||||||
ss_cmd_T cmd;
|
ss_cmd_T cmd;
|
||||||
|
|
||||||
if (fd == -1)
|
if (fd == -1)
|
||||||
return;
|
return FAIL;
|
||||||
|
|
||||||
if (socket_server_decode_cmd(&cmd, fd, 1000) == FAIL)
|
if (socket_server_decode_cmd(&cmd, fd, 1000) == FAIL)
|
||||||
goto exit;
|
goto exit;
|
||||||
@ -9460,6 +9460,7 @@ socket_server_accept_client(void)
|
|||||||
|
|
||||||
exit:
|
exit:
|
||||||
close(fd);
|
close(fd);
|
||||||
|
return OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -9545,8 +9546,9 @@ socket_server_send(
|
|||||||
size_t sz;
|
size_t sz;
|
||||||
char_u *final;
|
char_u *final;
|
||||||
char_u *path;
|
char_u *path;
|
||||||
struct timeval start, now;
|
#ifdef ELAPSED_FUNC
|
||||||
|
elapsed_T start_tv;
|
||||||
|
#endif
|
||||||
|
|
||||||
if (!socket_server_valid())
|
if (!socket_server_valid())
|
||||||
{
|
{
|
||||||
@ -9625,7 +9627,9 @@ socket_server_send(
|
|||||||
|
|
||||||
socket_server_init_pending_cmd(&pending);
|
socket_server_init_pending_cmd(&pending);
|
||||||
|
|
||||||
gettimeofday(&start, NULL);
|
#ifdef ELAPSED_FUNC
|
||||||
|
ELAPSED_INIT(start_tv);
|
||||||
|
#endif
|
||||||
|
|
||||||
// Wait for server to send back result
|
// Wait for server to send back result
|
||||||
while (socket_server_dispatch(500) >= 0)
|
while (socket_server_dispatch(500) >= 0)
|
||||||
@ -9633,11 +9637,7 @@ socket_server_send(
|
|||||||
if (pending.result != NULL)
|
if (pending.result != NULL)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
gettimeofday(&now, NULL);
|
if (ELAPSED_FUNC(start_tv) >= (timeout > 0 ? timeout : 1000))
|
||||||
|
|
||||||
if ((now.tv_sec * 1000000 + now.tv_usec) -
|
|
||||||
(start.tv_sec * 1000000 + start.tv_usec) >=
|
|
||||||
(timeout > 0 ? timeout * 1000 : 1000 * 1000))
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -9668,10 +9668,12 @@ socket_server_send(
|
|||||||
* success and FAIL on failure. Timeout is in milliseconds
|
* success and FAIL on failure. Timeout is in milliseconds
|
||||||
*/
|
*/
|
||||||
int
|
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;
|
ss_reply_T *reply = NULL;
|
||||||
struct timeval start, now;
|
#ifdef ELAPSED_FUNC
|
||||||
|
elapsed_T start_tv;
|
||||||
|
#endif
|
||||||
|
|
||||||
if (!socket_server_name_is_valid(client))
|
if (!socket_server_name_is_valid(client))
|
||||||
return -1;
|
return -1;
|
||||||
@ -9679,8 +9681,10 @@ socket_server_read_reply(char_u *client, char_u **str, int timeout)
|
|||||||
if (!socket_server_valid())
|
if (!socket_server_valid())
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
|
#ifdef ELAPSED_FUNC
|
||||||
if (timeout > 0)
|
if (timeout > 0)
|
||||||
gettimeofday(&start, NULL);
|
ELAPSED_INIT(start_tv);
|
||||||
|
#endif
|
||||||
|
|
||||||
// Try seeing if there already is a reply in the queue
|
// Try seeing if there already is a reply in the queue
|
||||||
goto get_reply;
|
goto get_reply;
|
||||||
@ -9689,13 +9693,10 @@ socket_server_read_reply(char_u *client, char_u **str, int timeout)
|
|||||||
{
|
{
|
||||||
int fd;
|
int fd;
|
||||||
|
|
||||||
if (timeout > 0)
|
#ifdef ELAPSED_FUNC
|
||||||
gettimeofday(&now, NULL);
|
if (timeout > 0 && ELAPSED_FUNC(start_tv) >= timeout)
|
||||||
|
break;
|
||||||
if (timeout > 0)
|
#endif
|
||||||
if ((now.tv_sec * 1000000 + now.tv_usec) -
|
|
||||||
(start.tv_sec * 1000000 + start.tv_usec) >= timeout * 1000)
|
|
||||||
break;
|
|
||||||
|
|
||||||
get_reply:
|
get_reply:
|
||||||
reply = socket_server_get_reply(client, NULL);
|
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;
|
size_t total_r = 0;
|
||||||
char_u *buf;
|
char_u *buf;
|
||||||
char_u *cur;
|
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
|
// We also poll the socket server listening file descriptor to handle
|
||||||
// recursive remote calls between Vim instances, such as when one Vim
|
// 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.
|
// want to free an uninitialized pointer.
|
||||||
memset(cmd, 0, sizeof(*cmd));
|
memset(cmd, 0, sizeof(*cmd));
|
||||||
|
|
||||||
gettimeofday(&start, NULL);
|
#ifdef ELAPSED_FUNC
|
||||||
|
ELAPSED_INIT(start_tv);
|
||||||
|
#endif
|
||||||
|
|
||||||
while (TRUE)
|
while (TRUE)
|
||||||
{
|
{
|
||||||
@ -10125,11 +10130,10 @@ socket_server_decode_cmd(ss_cmd_T *cmd, int socket_fd, int timeout)
|
|||||||
total_r += r;
|
total_r += r;
|
||||||
|
|
||||||
continue_loop:
|
continue_loop:
|
||||||
gettimeofday(&now, NULL);
|
#ifdef ELAPSED_FUNC
|
||||||
|
if (ELAPSED_FUNC(start_tv) >= timeout)
|
||||||
if ((now.tv_sec * 1000000 + now.tv_usec) -
|
|
||||||
(start.tv_sec * 1000000 + start.tv_usec) >= timeout * 1000)
|
|
||||||
goto fail;
|
goto fail;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse message data
|
// 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;
|
char_u *cur = data;
|
||||||
size_t total_w = 0;
|
size_t total_w = 0;
|
||||||
struct timeval start, now;
|
#ifdef ELAPSED_FUNC
|
||||||
|
elapsed_T start_tv;
|
||||||
|
#endif
|
||||||
#ifndef HAVE_SELECT
|
#ifndef HAVE_SELECT
|
||||||
struct pollfd pfd;
|
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);
|
FD_SET(socket_fd, &wfds);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
gettimeofday(&start, NULL);
|
#ifdef ELAPSED_FUNC
|
||||||
|
ELAPSED_INIT(start_tv);
|
||||||
|
#endif
|
||||||
|
|
||||||
while (total_w < sz)
|
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;
|
total_w += written;
|
||||||
|
|
||||||
|
|
||||||
continue_loop:
|
continue_loop:
|
||||||
gettimeofday(&now, NULL);
|
#ifdef ELAPSED_FUNC
|
||||||
|
if (ELAPSED_FUNC(start_tv) >= timeout)
|
||||||
if ((now.tv_sec * 1000000 + now.tv_usec) -
|
|
||||||
(start.tv_sec * 1000000 + start.tv_usec) >= timeout * 1000)
|
|
||||||
return FAIL;
|
return FAIL;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
return OK;
|
return OK;
|
||||||
|
|||||||
@ -97,7 +97,7 @@ int mch_create_anon_file(void);
|
|||||||
int socket_server_init(char_u *name);
|
int socket_server_init(char_u *name);
|
||||||
void socket_server_uninit(void);
|
void socket_server_uninit(void);
|
||||||
char_u *socket_server_list_sockets(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_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_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);
|
int socket_server_read_reply(char_u *client, char_u **str, int timeout);
|
||||||
|
|||||||
@ -724,6 +724,8 @@ static char *(features[]) =
|
|||||||
|
|
||||||
static int included_patches[] =
|
static int included_patches[] =
|
||||||
{ /* Add new patch number below this line */
|
{ /* Add new patch number below this line */
|
||||||
|
/**/
|
||||||
|
1719,
|
||||||
/**/
|
/**/
|
||||||
1718,
|
1718,
|
||||||
/**/
|
/**/
|
||||||
|
|||||||
Reference in New Issue
Block a user