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.
This commit is contained in:
parent
d15e1e09a2
commit
a091c148e6
34
src/client.c
34
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;
|
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;
|
size_t copylen;
|
||||||
struct client *client;
|
struct client *client;
|
||||||
|
|
||||||
assert(fd >= 0);
|
assert(fd >= 0);
|
||||||
|
|
||||||
if (i >= client_max_connections ||
|
client = client_by_fd(fd);
|
||||||
clients[i].fd < 0 || clients[i].fd != fd) {
|
if (client == NULL)
|
||||||
for (i = 0; i < client_max_connections; i++) {
|
return -1;
|
||||||
if (clients[i].fd == fd)
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (i == client_max_connections)
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* if fd isn't found or client is going to be closed, do nothing */
|
/* if fd isn't found or client is going to be closed, do nothing */
|
||||||
if (clients[i].expired)
|
if (client->expired)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
client = clients + i;
|
|
||||||
|
|
||||||
while (buflen > 0 && !client->expired) {
|
while (buflen > 0 && !client->expired) {
|
||||||
size_t left;
|
size_t left;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user