From 7c5a476927734aec99ba87d744ac46ff7e890dd5 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Thu, 28 Aug 2008 20:03:06 +0200 Subject: client: added function client_by_fd() The code becomes less complex and more readable when we move this linear search into a separate mini function. --- src/client.c | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/client.c b/src/client.c index 54c62bc83..c4693193f 100644 --- a/src/client.c +++ b/src/client.c @@ -686,30 +686,38 @@ static void client_write_deferred(struct client *client) } } -int client_print(int fd, const char *buffer, size_t buflen) +static struct client *client_by_fd(int fd) { static unsigned int i; + + assert(fd >= 0); + + if (i < client_max_connections && clients[i].fd >= 0 && + clients[i].fd == fd) + return &clients[i]; + + for (i = 0; i < client_max_connections; i++) + if (clients[i].fd == fd) + return &clients[i]; + + return NULL; +} + +int client_print(int fd, const char *buffer, size_t buflen) +{ size_t copylen; struct client *client; assert(fd >= 0); - if (i >= client_max_connections || - clients[i].fd < 0 || clients[i].fd != fd) { - for (i = 0; i < client_max_connections; i++) { - if (clients[i].fd == fd) - break; - } - if (i == client_max_connections) - return -1; - } + client = client_by_fd(fd); + if (client == NULL) + return -1; /* if fd isn't found or client is going to be closed, do nothing */ - if (clients[i].expired) + if (client->expired) return 0; - client = clients + i; - while (buflen > 0 && !client->expired) { size_t left; -- cgit v1.2.3