client: added client_write() and client_puts()

client_write() writes a buffer to the client and buffers it if
required.  client_puts() does the same for a C string.  The next patch
will add more tools which will replace fdprintf() later.
This commit is contained in:
Max Kellermann 2008-09-07 13:24:51 +02:00
parent a34e1d2b84
commit 33aec0d673
3 changed files with 45 additions and 26 deletions

View File

@ -692,7 +692,6 @@ static struct client *client_by_fd(int fd)
int client_print(int fd, const char *buffer, size_t buflen)
{
size_t copylen;
struct client *client;
assert(fd >= 0);
@ -701,26 +700,7 @@ int client_print(int fd, const char *buffer, size_t buflen)
if (client == NULL)
return -1;
/* if fd isn't found or client is going to be closed, do nothing */
if (client_is_expired(client))
return 0;
while (buflen > 0 && !client_is_expired(client)) {
size_t left;
assert(client->send_buf_size >= client->send_buf_used);
left = client->send_buf_size - client->send_buf_used;
copylen = buflen > left ? left : buflen;
memcpy(client->send_buf + client->send_buf_used, buffer,
copylen);
buflen -= copylen;
client->send_buf_used += copylen;
buffer += copylen;
if (client->send_buf_used >= client->send_buf_size)
client_write_output(client);
}
client_write(client, buffer, buflen);
return 0;
}
@ -749,8 +729,8 @@ static void client_defer_output(struct client *client,
*buf_r = new_sllnode(data, length);
}
static void client_write(struct client *client,
const char *data, size_t length)
static void client_write_direct(struct client *client,
const char *data, size_t length)
{
ssize_t ret;
@ -782,8 +762,38 @@ static void client_write_output(struct client *client)
client_defer_output(client, client->send_buf,
client->send_buf_used);
else
client_write(client, client->send_buf, client->send_buf_used);
client_write_direct(client, client->send_buf,
client->send_buf_used);
client->send_buf_used = 0;
}
void client_write(struct client *client, const char *buffer, size_t buflen)
{
size_t copylen;
/* if the client is going to be closed, do nothing */
if (client_is_expired(client))
return;
while (buflen > 0 && !client_is_expired(client)) {
size_t left;
assert(client->send_buf_size >= client->send_buf_used);
left = client->send_buf_size - client->send_buf_used;
copylen = buflen > left ? left : buflen;
memcpy(client->send_buf + client->send_buf_used, buffer,
copylen);
buflen -= copylen;
client->send_buf_used += copylen;
buffer += copylen;
if (client->send_buf_used >= client->send_buf_size)
client_write_output(client);
}
}
void client_puts(struct client *client, const char *s)
{
client_write(client, s, strlen(s));
}

View File

@ -39,6 +39,16 @@ int client_get_fd(struct client *client);
int client_is_expired(const struct client *client);
/**
* Write a block of data to the client.
*/
void client_write(struct client *client, const char *data, size_t length);
/**
* Write a C string to the client.
*/
void client_puts(struct client *client, const char *s);
int client_print(int fd, const char *buffer, size_t len);
#endif

View File

@ -1239,7 +1239,6 @@ static int processCommandInternal(struct client *client,
int processListOfCommands(struct client *client, int *permission,
int listOK, struct strnode *list)
{
int fd = client_get_fd(client);
struct strnode *cur = list;
int ret = 0;
@ -1253,7 +1252,7 @@ int processListOfCommands(struct client *client, int *permission,
if (ret != 0 || client_is_expired(client))
goto out;
else if (listOK)
fdprintf(fd, "list_OK\n");
client_puts(client, "list_OK\n");
command_listNum++;
cur = cur->next;
}