New.cxx 2.6 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 32 33

#include <assert.h>

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

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

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

59
	assert(fd.IsDefined());
60

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

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

69
	const unsigned num = next_client_num++;
70
	Client *client = new Client(loop, partition, std::move(fd), uid,
71
				    permission,
72
				    num);
73

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

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

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

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

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