ClientRead.cxx 1.9 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 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.
 */

Max Kellermann's avatar
Max Kellermann committed
20
#include "ClientInternal.hxx"
21 22
#include "Partition.hxx"
#include "Instance.hxx"
23
#include "event/Loop.hxx"
24
#include "util/StringStrip.hxx"
25 26 27

#include <string.h>

28
BufferedSocket::InputResult
29
Client::OnSocketInput(void *data, size_t length) noexcept
30
{
31 32
	char *p = (char *)data;
	char *newline = (char *)memchr(p, '\n', length);
33
	if (newline == nullptr)
34
		return InputResult::MORE;
35

36
	timeout_event.Schedule(client_timeout);
37

38 39 40
	BufferedSocket::ConsumeInput(newline + 1 - p);

	/* skip whitespace at the end of the line */
41
	char *end = StripRight(p, newline);
42

43
	/* terminate the string at the end of the line */
44
	*end = 0;
45

46
	CommandResult result = client_process_line(*this, p);
47
	switch (result) {
48 49 50
	case CommandResult::OK:
	case CommandResult::IDLE:
	case CommandResult::ERROR:
51
		break;
52

53
	case CommandResult::KILL:
54
		partition->instance.Break();
55
		Close();
56
		return InputResult::CLOSED;
57

58 59 60 61 62
	case CommandResult::FINISH:
		if (Flush())
			Close();
		return InputResult::CLOSED;

63
	case CommandResult::CLOSE:
64 65
		Close();
		return InputResult::CLOSED;
66 67
	}

68 69 70
	if (IsExpired()) {
		Close();
		return InputResult::CLOSED;
71 72
	}

73
	return InputResult::AGAIN;
74
}