aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMax Kellermann <max@duempel.org>2008-08-28 20:03:06 +0200
committerMax Kellermann <max@duempel.org>2008-08-28 20:03:06 +0200
commita091c148e60a13c646ba562a53324604d6ad98b7 (patch)
treeb48bddbcd0220de092dc1e3c6099e36500eb9927
parentd15e1e09a231f25d5bb8bac2922192560e3ea515 (diff)
downloadmpd-a091c148e60a13c646ba562a53324604d6ad98b7.tar.gz
mpd-a091c148e60a13c646ba562a53324604d6ad98b7.tar.xz
mpd-a091c148e60a13c646ba562a53324604d6ad98b7.zip
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.
Diffstat (limited to '')
-rw-r--r--src/client.c34
1 files changed, 21 insertions, 13 deletions
diff --git a/src/client.c b/src/client.c
index 67417d181..9c18ca40e 100644
--- a/src/client.c
+++ b/src/client.c
@@ -689,30 +689,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;