ServerSocket.hxx 3.29 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
#ifndef MPD_SERVER_SOCKET_HXX
#define MPD_SERVER_SOCKET_HXX
22

23
#include <list>
24

25
class SocketAddress;
26
class AllocatedSocketAddress;
27
class UniqueSocketDescriptor;
28
class EventLoop;
29
class AllocatedPath;
30

31 32 33
/**
 * A socket that accepts incoming stream connections (e.g. TCP).
 */
34
class ServerSocket {
35
	class OneServerSocket;
36

37
	EventLoop &loop;
38

39
	std::list<OneServerSocket> sockets;
40

41
	unsigned next_serial = 1;
42

43
public:
44 45
	ServerSocket(EventLoop &_loop) noexcept;
	~ServerSocket() noexcept;
46

47
	EventLoop &GetEventLoop() const noexcept {
48 49 50
		return loop;
	}

51
private:
52 53
	template<typename A>
	OneServerSocket &AddAddress(A &&address) noexcept;
54 55 56 57 58 59

	/**
	 * Add a listener on a port on all IPv4 interfaces.
	 *
	 * @param port the TCP port
	 */
60
	void AddPortIPv4(unsigned port) noexcept;
61 62 63 64 65 66

	/**
	 * Add a listener on a port on all IPv6 interfaces.
	 *
	 * @param port the TCP port
	 */
67
	void AddPortIPv6(unsigned port) noexcept;
68 69 70 71 72

public:
	/**
	 * Add a listener on a port on all interfaces.
	 *
73 74
	 * Throws #std::runtime_error on error.
	 *
75
	 * @param port the TCP port
Max Kellermann's avatar
Max Kellermann committed
76
	 * @param error location to store the error occurring
77
	 */
78
	void AddPort(unsigned port);
79 80 81 82 83

	/**
	 * Resolves a host name, and adds listeners on all addresses in the
	 * result set.
	 *
84 85
	 * Throws #std::runtime_error on error.
	 *
86 87
	 * @param hostname the host name to be resolved
	 * @param port the TCP port
Max Kellermann's avatar
Max Kellermann committed
88
	 * @param error location to store the error occurring
89
	 */
90
	void AddHost(const char *hostname, unsigned port);
91 92

	/**
93
	 * Add a listener on a local socket.
94
	 *
95 96
	 * Throws #std::runtime_error on error.
	 *
97
	 * @param path the absolute socket path
Max Kellermann's avatar
Max Kellermann committed
98
	 * @param error location to store the error occurring
99
	 */
100
	void AddPath(AllocatedPath &&path);
101

102 103 104 105 106 107 108 109 110 111
	/**
	 * Add a listener on an abstract local socket (Linux specific).
	 *
	 * Throws on error.
	 *
	 * @param name the abstract socket name, starting with a '@'
	 * instead of a null byte
	 */
	void AddAbstract(const char *name);

112 113 114 115
	/**
	 * Add a socket descriptor that is accepting connections.  After this
	 * has been called, don't call server_socket_open(), because the
	 * socket is already open.
116 117 118
	 *
	 * Throws #std::runtime_error on error.
	 */
119
	void AddFD(UniqueSocketDescriptor fd);
120

121 122 123
	void AddFD(UniqueSocketDescriptor fd,
		   AllocatedSocketAddress &&address) noexcept;

124 125 126 127
	bool IsEmpty() const noexcept {
		return sockets.empty();
	}

128 129
	/**
	 * Throws #std::runtime_error on error.
130
	 */
131
	void Open();
132

133
	void Close() noexcept;
134 135

protected:
136
	virtual void OnAccept(UniqueSocketDescriptor fd,
137
			      SocketAddress address, int uid) noexcept = 0;
138
};
139 140

#endif