aboutsummaryrefslogtreecommitdiffstats
path: root/src/ClientRead.cxx
diff options
context:
space:
mode:
authorMax Kellermann <max@duempel.org>2013-01-14 23:42:06 +0100
committerMax Kellermann <max@duempel.org>2013-01-15 12:15:33 +0100
commit39439b80f5d1574e67f337a047869cf067c396b7 (patch)
tree5e28e61d4895da86189d14cde9f1e238c18079a8 /src/ClientRead.cxx
parent396480cf94fbeda581acb6a78c42c7ec610d04a4 (diff)
downloadmpd-39439b80f5d1574e67f337a047869cf067c396b7.tar.gz
mpd-39439b80f5d1574e67f337a047869cf067c396b7.tar.xz
mpd-39439b80f5d1574e67f337a047869cf067c396b7.zip
Client: rebase on the new BufferedSocket class
Diffstat (limited to 'src/ClientRead.cxx')
-rw-r--r--src/ClientRead.cxx95
1 files changed, 26 insertions, 69 deletions
diff --git a/src/ClientRead.cxx b/src/ClientRead.cxx
index e58c73dea..363363d1f 100644
--- a/src/ClientRead.cxx
+++ b/src/ClientRead.cxx
@@ -19,91 +19,48 @@
#include "config.h"
#include "ClientInternal.hxx"
-#include "util/fifo_buffer.h"
+#include "Main.hxx"
+#include "event/Loop.hxx"
#include <assert.h>
#include <string.h>
-static char *
-client_read_line(Client *client)
+BufferedSocket::InputResult
+Client::OnSocketInput(const void *data, size_t length)
{
- size_t length;
- const char *p = (const char *)fifo_buffer_read(client->input, &length);
- if (p == NULL)
- return NULL;
+ g_timer_start(last_activity);
+ const char *p = (const char *)data;
const char *newline = (const char *)memchr(p, '\n', length);
if (newline == NULL)
- return NULL;
+ return InputResult::MORE;
char *line = g_strndup(p, newline - p);
- fifo_buffer_consume(client->input, newline - p + 1);
+ BufferedSocket::ConsumeInput(newline + 1 - p);
- return g_strchomp(line);
-}
-
-static enum command_return
-client_input_received(Client *client, size_t bytesRead)
-{
- char *line;
-
- fifo_buffer_append(client->input, bytesRead);
-
- /* process all lines */
-
- while ((line = client_read_line(client)) != NULL) {
- enum command_return ret = client_process_line(client, line);
- g_free(line);
-
- if (ret == COMMAND_RETURN_KILL ||
- ret == COMMAND_RETURN_CLOSE)
- return ret;
- if (client->IsExpired())
- return COMMAND_RETURN_CLOSE;
- }
-
- return COMMAND_RETURN_OK;
-}
+ enum command_return result = client_process_line(this, line);
+ g_free(line);
-enum command_return
-client_read(Client *client)
-{
- GError *error = NULL;
- GIOStatus status;
- gsize bytes_read;
+ switch (result) {
+ case COMMAND_RETURN_OK:
+ case COMMAND_RETURN_IDLE:
+ case COMMAND_RETURN_ERROR:
+ break;
- assert(client != NULL);
- assert(client->channel != NULL);
+ case COMMAND_RETURN_KILL:
+ Close();
+ main_loop->Break();
+ return InputResult::CLOSED;
- size_t max_length;
- char *p = (char *)fifo_buffer_write(client->input, &max_length);
- if (p == NULL) {
- g_warning("[%u] buffer overflow", client->num);
- return COMMAND_RETURN_CLOSE;
+ case COMMAND_RETURN_CLOSE:
+ Close();
+ return InputResult::CLOSED;
}
- status = g_io_channel_read_chars(client->channel, p, max_length,
- &bytes_read, &error);
- switch (status) {
- case G_IO_STATUS_NORMAL:
- return client_input_received(client, bytes_read);
-
- case G_IO_STATUS_AGAIN:
- /* try again later, after select() */
- return COMMAND_RETURN_OK;
-
- case G_IO_STATUS_EOF:
- /* peer disconnected */
- return COMMAND_RETURN_CLOSE;
-
- case G_IO_STATUS_ERROR:
- /* I/O error */
- g_warning("failed to read from client %d: %s",
- client->num, error->message);
- g_error_free(error);
- return COMMAND_RETURN_CLOSE;
+ if (IsExpired()) {
+ Close();
+ return InputResult::CLOSED;
}
- /* unreachable */
- return COMMAND_RETURN_CLOSE;
+ return InputResult::AGAIN;
}