ServerSocket.hxx 2.91 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 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
class OneServerSocket;
31

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

38
	EventLoop &loop;
39

40
	std::list<OneServerSocket> sockets;
41

42
	unsigned next_serial;
43

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

48 49 50 51
	EventLoop &GetEventLoop() {
		return loop;
	}

52
private:
53
	OneServerSocket &AddAddress(SocketAddress address);
54
	OneServerSocket &AddAddress(AllocatedSocketAddress &&address);
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73

	/**
	 * Add a listener on a port on all IPv4 interfaces.
	 *
	 * @param port the TCP port
	 */
	void AddPortIPv4(unsigned port);

	/**
	 * Add a listener on a port on all IPv6 interfaces.
	 *
	 * @param port the TCP port
	 */
	void AddPortIPv6(unsigned port);

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

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

	/**
	 * Add a listener on a Unix domain socket.
	 *
96 97
	 * Throws #std::runtime_error on error.
	 *
98
	 * @param path the absolute socket path
Max Kellermann's avatar
Max Kellermann committed
99
	 * @param error location to store the error occurring
100
	 */
101
	void AddPath(AllocatedPath &&path);
102 103 104 105 106

	/**
	 * 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.
107 108 109 110 111 112 113
	 *
	 * Throws #std::runtime_error on error.
	 */
	void AddFD(int fd);

	/**
	 * Throws #std::runtime_error on error.
114
	 */
115
	void Open();
116 117

	void Close();
118 119

protected:
120 121
	virtual void OnAccept(UniqueSocketDescriptor &&fd,
			      SocketAddress address, int uid) = 0;
122
};
123 124

#endif