diff options
author | Max Kellermann <max@duempel.org> | 2008-09-07 13:24:51 +0200 |
---|---|---|
committer | Max Kellermann <max@duempel.org> | 2008-09-07 13:24:51 +0200 |
commit | 33aec0d6732a529342e59933b351f4e591d8f8f4 (patch) | |
tree | d0bb8a5729398cbf57d63058f6ecf6cd7736a731 /src/client.c | |
parent | a34e1d2b84c72b27212eed4e1483a20440da44ab (diff) | |
download | mpd-33aec0d6732a529342e59933b351f4e591d8f8f4.tar.gz mpd-33aec0d6732a529342e59933b351f4e591d8f8f4.tar.xz mpd-33aec0d6732a529342e59933b351f4e591d8f8f4.zip |
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.
Diffstat (limited to 'src/client.c')
-rw-r--r-- | src/client.c | 58 |
1 files changed, 34 insertions, 24 deletions
diff --git a/src/client.c b/src/client.c index 671337e81..e9cda12cb 100644 --- a/src/client.c +++ b/src/client.c @@ -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)); +} |