New.cxx 2.61 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
#include "Domain.hxx"
23
#include "List.hxx"
24
#include "BackgroundCommand.hxx"
25
#include "Partition.hxx"
26
#include "Instance.hxx"
27
#include "net/UniqueSocketDescriptor.hxx"
28
#include "net/SocketAddress.hxx"
29
#include "net/ToString.hxx"
30
#include "Log.hxx"
31
#include "Version.h"
32

33
#include <cassert>
34

35
static constexpr char GREETING[] = "OK MPD " PROTOCOL_VERSION "\n";
36

37
Client::Client(EventLoop &_loop, Partition &_partition,
38 39 40
	       UniqueSocketDescriptor _fd,
	       int _uid, unsigned _permission,
	       int _num) noexcept
41 42
	:FullyBufferedSocket(_fd.Release(), _loop,
			     16384, client_max_output_buffer_size),
43
	 timeout_event(_loop, BIND_THIS_METHOD(OnTimeout)),
44
	 partition(&_partition),
45
	 permission(_permission),
46
	 uid(_uid),
47
	 num(_num)
48
{
49
	timeout_event.Schedule(client_timeout);
50 51
}

52
void
53
client_new(EventLoop &loop, Partition &partition,
54 55
	   UniqueSocketDescriptor fd, SocketAddress address, int uid,
	   unsigned permission) noexcept
56 57
{
	static unsigned int next_client_num;
58
	const auto remote = ToString(address);
59

60
	assert(fd.IsDefined());
61

62 63
	ClientList &client_list = *partition.instance.client_list;
	if (client_list.IsFull()) {
64
		LogWarning(client_domain, "Max connections reached");
65 66 67
		return;
	}

68
	(void)fd.Write(GREETING, sizeof(GREETING) - 1);
69

70
	const unsigned num = next_client_num++;
Max Kellermann's avatar
Max Kellermann committed
71
	auto *client = new Client(loop, partition, std::move(fd), uid,
72
				    permission,
73
				    num);
74

75
	client_list.Add(*client);
76
	partition.clients.push_back(*client);
77

78
	FormatInfo(client_domain, "[%u] opened from %s",
79
		   num, remote.c_str());
80 81 82
}

void
83
Client::Close() noexcept
84
{
85
	partition->instance.client_list->Remove(*this);
86
	partition->clients.erase(partition->clients.iterator_to(*this));
87

88 89
	if (FullyBufferedSocket::IsDefined())
		FullyBufferedSocket::Close();
90

91
	FormatInfo(client_domain, "[%u] closed", num);
92
	delete this;
93
}