2010-10-05 20:29:41 +02:00
|
|
|
/*
|
2018-10-31 17:54:59 +01:00
|
|
|
* Copyright 2003-2018 The Music Player Daemon Project
|
2010-10-05 20:29:41 +02:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2013-01-15 21:42:55 +01:00
|
|
|
#ifndef MPD_SERVER_SOCKET_HXX
|
|
|
|
#define MPD_SERVER_SOCKET_HXX
|
2010-10-05 20:29:41 +02:00
|
|
|
|
2013-11-04 20:10:46 +01:00
|
|
|
#include <list>
|
2013-01-30 12:56:23 +01:00
|
|
|
|
2015-02-10 20:30:10 +01:00
|
|
|
class SocketAddress;
|
2015-07-22 11:05:08 +02:00
|
|
|
class AllocatedSocketAddress;
|
2017-08-10 19:13:18 +02:00
|
|
|
class UniqueSocketDescriptor;
|
2013-01-15 22:50:49 +01:00
|
|
|
class EventLoop;
|
2013-10-19 17:40:56 +02:00
|
|
|
class AllocatedPath;
|
2010-10-05 20:29:41 +02:00
|
|
|
|
2013-11-24 19:28:04 +01:00
|
|
|
/**
|
|
|
|
* A socket that accepts incoming stream connections (e.g. TCP).
|
|
|
|
*/
|
2013-01-30 12:56:23 +01:00
|
|
|
class ServerSocket {
|
2018-10-30 20:16:26 +01:00
|
|
|
class OneServerSocket;
|
2010-10-05 20:29:41 +02:00
|
|
|
|
2013-01-30 12:56:23 +01:00
|
|
|
EventLoop &loop;
|
2010-10-05 20:29:41 +02:00
|
|
|
|
2013-11-04 20:10:46 +01:00
|
|
|
std::list<OneServerSocket> sockets;
|
2012-02-13 20:58:57 +01:00
|
|
|
|
2018-10-30 20:11:58 +01:00
|
|
|
unsigned next_serial = 1;
|
2010-10-05 20:29:41 +02:00
|
|
|
|
2013-01-30 12:56:23 +01:00
|
|
|
public:
|
2018-10-30 19:57:39 +01:00
|
|
|
ServerSocket(EventLoop &_loop) noexcept;
|
|
|
|
~ServerSocket() noexcept;
|
2010-10-05 20:29:41 +02:00
|
|
|
|
2018-10-30 19:57:39 +01:00
|
|
|
EventLoop &GetEventLoop() const noexcept {
|
2013-01-30 14:22:58 +01:00
|
|
|
return loop;
|
|
|
|
}
|
|
|
|
|
2013-01-30 12:56:23 +01:00
|
|
|
private:
|
2018-10-30 20:43:52 +01:00
|
|
|
template<typename A>
|
|
|
|
OneServerSocket &AddAddress(A &&address) noexcept;
|
2013-01-30 12:56:23 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a listener on a port on all IPv4 interfaces.
|
|
|
|
*
|
|
|
|
* @param port the TCP port
|
|
|
|
*/
|
2018-10-30 19:57:39 +01:00
|
|
|
void AddPortIPv4(unsigned port) noexcept;
|
2013-01-30 12:56:23 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a listener on a port on all IPv6 interfaces.
|
|
|
|
*
|
|
|
|
* @param port the TCP port
|
|
|
|
*/
|
2018-10-30 19:57:39 +01:00
|
|
|
void AddPortIPv6(unsigned port) noexcept;
|
2013-01-30 12:56:23 +01:00
|
|
|
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Add a listener on a port on all interfaces.
|
|
|
|
*
|
2016-10-28 10:36:05 +02:00
|
|
|
* Throws #std::runtime_error on error.
|
|
|
|
*
|
2013-01-30 12:56:23 +01:00
|
|
|
* @param port the TCP port
|
2015-03-17 11:21:29 +01:00
|
|
|
* @param error location to store the error occurring
|
2013-01-30 12:56:23 +01:00
|
|
|
*/
|
2016-10-28 10:36:05 +02:00
|
|
|
void AddPort(unsigned port);
|
2013-01-30 12:56:23 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolves a host name, and adds listeners on all addresses in the
|
|
|
|
* result set.
|
|
|
|
*
|
2016-10-28 10:36:05 +02:00
|
|
|
* Throws #std::runtime_error on error.
|
|
|
|
*
|
2013-01-30 12:56:23 +01:00
|
|
|
* @param hostname the host name to be resolved
|
|
|
|
* @param port the TCP port
|
2015-03-17 11:21:29 +01:00
|
|
|
* @param error location to store the error occurring
|
2013-01-30 12:56:23 +01:00
|
|
|
*/
|
2016-10-28 10:36:05 +02:00
|
|
|
void AddHost(const char *hostname, unsigned port);
|
2013-01-30 12:56:23 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a listener on a Unix domain socket.
|
|
|
|
*
|
2016-10-28 10:36:05 +02:00
|
|
|
* Throws #std::runtime_error on error.
|
|
|
|
*
|
2013-01-30 12:56:23 +01:00
|
|
|
* @param path the absolute socket path
|
2015-03-17 11:21:29 +01:00
|
|
|
* @param error location to store the error occurring
|
2013-01-30 12:56:23 +01:00
|
|
|
*/
|
2016-10-28 10:36:05 +02:00
|
|
|
void AddPath(AllocatedPath &&path);
|
2013-01-30 12:56:23 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
2016-10-28 10:36:05 +02:00
|
|
|
*
|
|
|
|
* Throws #std::runtime_error on error.
|
|
|
|
*/
|
2018-10-30 20:05:57 +01:00
|
|
|
void AddFD(UniqueSocketDescriptor fd);
|
2016-10-28 10:36:05 +02:00
|
|
|
|
2018-10-30 20:44:56 +01:00
|
|
|
void AddFD(UniqueSocketDescriptor fd,
|
|
|
|
AllocatedSocketAddress &&address) noexcept;
|
|
|
|
|
2018-07-15 21:35:14 +02:00
|
|
|
bool IsEmpty() const noexcept {
|
|
|
|
return sockets.empty();
|
|
|
|
}
|
|
|
|
|
2016-10-28 10:36:05 +02:00
|
|
|
/**
|
|
|
|
* Throws #std::runtime_error on error.
|
2013-01-30 12:56:23 +01:00
|
|
|
*/
|
2016-10-28 10:36:05 +02:00
|
|
|
void Open();
|
2013-01-30 12:56:23 +01:00
|
|
|
|
2018-10-30 19:57:39 +01:00
|
|
|
void Close() noexcept;
|
2013-01-30 13:20:27 +01:00
|
|
|
|
|
|
|
protected:
|
2017-11-10 20:43:14 +01:00
|
|
|
virtual void OnAccept(UniqueSocketDescriptor fd,
|
2018-10-30 20:13:07 +01:00
|
|
|
SocketAddress address, int uid) noexcept = 0;
|
2013-01-30 12:56:23 +01:00
|
|
|
};
|
2010-10-05 20:29:41 +02:00
|
|
|
|
|
|
|
#endif
|