Read.cxx 1.96 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2020 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 21
#include "Client.hxx"
#include "Config.hxx"
22 23
#include "Partition.hxx"
#include "Instance.hxx"
24
#include "util/StringStrip.hxx"
25

Rosen Penev's avatar
Rosen Penev committed
26
#include <cstring>
27

28
BufferedSocket::InputResult
29
Client::OnSocketInput(void *data, size_t length) noexcept
30
{
31 32 33
	if (background_command)
		return InputResult::PAUSE;

34
	char *p = (char *)data;
Rosen Penev's avatar
Rosen Penev committed
35
	char *newline = (char *)std::memchr(p, '\n', length);
36
	if (newline == nullptr)
37
		return InputResult::MORE;
38

39
	timeout_event.Schedule(client_timeout);
40

41 42 43
	BufferedSocket::ConsumeInput(newline + 1 - p);

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

46
	/* terminate the string at the end of the line */
47
	*end = 0;
48

49
	CommandResult result = ProcessLine(p);
50
	switch (result) {
51 52
	case CommandResult::OK:
	case CommandResult::IDLE:
53
	case CommandResult::BACKGROUND:
54
	case CommandResult::ERROR:
55
		break;
56

57
	case CommandResult::KILL:
58
		partition->instance.Break();
59
		Close();
60
		return InputResult::CLOSED;
61

62 63 64 65 66
	case CommandResult::FINISH:
		if (Flush())
			Close();
		return InputResult::CLOSED;

67
	case CommandResult::CLOSE:
68 69
		Close();
		return InputResult::CLOSED;
70 71
	}

72 73 74 75 76
	if (IsExpired()) {
		Close();
		return InputResult::CLOSED;
	}

77
	return InputResult::AGAIN;
78
}