ClientRead.cxx 1.73 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2013 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * http://www.musicpd.org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

20
#include "config.h"
Max Kellermann's avatar
Max Kellermann committed
21
#include "ClientInternal.hxx"
22 23
#include "Main.hxx"
#include "event/Loop.hxx"
24 25 26 27

#include <assert.h>
#include <string.h>

28 29
BufferedSocket::InputResult
Client::OnSocketInput(const void *data, size_t length)
30
{
31
	const char *p = (const char *)data;
Max Kellermann's avatar
Max Kellermann committed
32
	const char *newline = (const char *)memchr(p, '\n', length);
33
	if (newline == NULL)
34
		return InputResult::MORE;
35

36 37
	TimeoutMonitor::ScheduleSeconds(client_timeout);

Max Kellermann's avatar
Max Kellermann committed
38
	char *line = g_strndup(p, newline - p);
39
	BufferedSocket::ConsumeInput(newline + 1 - p);
40

41 42
	enum command_return result = client_process_line(this, line);
	g_free(line);
43

44 45 46 47 48
	switch (result) {
	case COMMAND_RETURN_OK:
	case COMMAND_RETURN_IDLE:
	case COMMAND_RETURN_ERROR:
		break;
49

50 51 52 53
	case COMMAND_RETURN_KILL:
		Close();
		main_loop->Break();
		return InputResult::CLOSED;
54

55 56 57
	case COMMAND_RETURN_CLOSE:
		Close();
		return InputResult::CLOSED;
58 59
	}

60 61 62
	if (IsExpired()) {
		Close();
		return InputResult::CLOSED;
63 64
	}

65
	return InputResult::AGAIN;
66
}