aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMax Kellermann <max@duempel.org>2008-09-07 13:24:51 +0200
committerMax Kellermann <max@duempel.org>2008-09-07 13:24:51 +0200
commit33aec0d6732a529342e59933b351f4e591d8f8f4 (patch)
treed0bb8a5729398cbf57d63058f6ecf6cd7736a731
parenta34e1d2b84c72b27212eed4e1483a20440da44ab (diff)
downloadmpd-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.
-rw-r--r--src/client.c58
-rw-r--r--src/client.h10
-rw-r--r--src/command.c3
3 files changed, 45 insertions, 26 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));
+}
diff --git a/src/client.h b/src/client.h
index dc67c044f..500e47b8f 100644
--- a/src/client.h
+++ b/src/client.h
@@ -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
diff --git a/src/command.c b/src/command.c
index 834fb6420..faa68e8d0 100644
--- a/src/command.c
+++ b/src/command.c
@@ -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;
}