ServerSocket.hxx 2.78 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 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 EventLoop;
28
class Error;
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 74

	/**
	 * 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.
	 *
	 * @param port the TCP port
Max Kellermann's avatar
Max Kellermann committed
75
	 * @param error location to store the error occurring
76 77
	 * @return true on success
	 */
78
	bool AddPort(unsigned port, Error &error);
79 80 81 82 83 84 85

	/**
	 * Resolves a host name, and adds listeners on all addresses in the
	 * result set.
	 *
	 * @param hostname the host name to be resolved
	 * @param port the TCP port
Max Kellermann's avatar
Max Kellermann committed
86
	 * @param error location to store the error occurring
87 88
	 * @return true on success
	 */
89
	bool AddHost(const char *hostname, unsigned port, Error &error);
90 91 92 93 94

	/**
	 * Add a listener on a Unix domain socket.
	 *
	 * @param path the absolute socket path
Max Kellermann's avatar
Max Kellermann committed
95
	 * @param error location to store the error occurring
96 97
	 * @return true on success
	 */
98
	bool AddPath(AllocatedPath &&path, Error &error);
99 100 101 102 103 104

	/**
	 * 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.
	 */
105
	bool AddFD(int fd, Error &error);
106

107
	bool Open(Error &error);
108
	void Close();
109 110

protected:
111
	virtual void OnAccept(int fd, SocketAddress address, int uid) = 0;
112
};
113 114

#endif