ServerSocket: expose the class
Eliminate the C wrappers.
This commit is contained in:
parent
39d56d6b65
commit
ad5eb2f8d6
@ -36,7 +36,7 @@
|
|||||||
|
|
||||||
#define DEFAULT_PORT 6600
|
#define DEFAULT_PORT 6600
|
||||||
|
|
||||||
static struct server_socket *listen_socket;
|
static ServerSocket *listen_socket;
|
||||||
int listen_port;
|
int listen_port;
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@ -55,13 +55,11 @@ listen_add_config_param(unsigned int port,
|
|||||||
assert(param != NULL);
|
assert(param != NULL);
|
||||||
|
|
||||||
if (0 == strcmp(param->value, "any")) {
|
if (0 == strcmp(param->value, "any")) {
|
||||||
return server_socket_add_port(listen_socket, port, error_r);
|
return listen_socket->AddPort(port, error_r);
|
||||||
} else if (param->value[0] == '/') {
|
} else if (param->value[0] == '/') {
|
||||||
return server_socket_add_path(listen_socket, param->value,
|
return listen_socket->AddPath(param->value, error_r);
|
||||||
error_r);
|
|
||||||
} else {
|
} else {
|
||||||
return server_socket_add_host(listen_socket, param->value,
|
return listen_socket->AddHost(param->value, port, error_r);
|
||||||
port, error_r);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -79,7 +77,7 @@ listen_systemd_activation(GError **error_r)
|
|||||||
|
|
||||||
for (int i = SD_LISTEN_FDS_START, end = SD_LISTEN_FDS_START + n;
|
for (int i = SD_LISTEN_FDS_START, end = SD_LISTEN_FDS_START + n;
|
||||||
i != end; ++i)
|
i != end; ++i)
|
||||||
if (!server_socket_add_fd(listen_socket, i, error_r))
|
if (!listen_socket->AddFD(i, error_r))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@ -100,7 +98,7 @@ listen_global_init(GError **error_r)
|
|||||||
bool success;
|
bool success;
|
||||||
GError *error = NULL;
|
GError *error = NULL;
|
||||||
|
|
||||||
listen_socket = server_socket_new(*main_loop, listen_callback, NULL);
|
listen_socket = new ServerSocket(*main_loop, listen_callback, nullptr);
|
||||||
|
|
||||||
if (listen_systemd_activation(&error))
|
if (listen_systemd_activation(&error))
|
||||||
return true;
|
return true;
|
||||||
@ -117,6 +115,7 @@ listen_global_init(GError **error_r)
|
|||||||
do {
|
do {
|
||||||
success = listen_add_config_param(port, param, &error);
|
success = listen_add_config_param(port, param, &error);
|
||||||
if (!success) {
|
if (!success) {
|
||||||
|
delete listen_socket;
|
||||||
g_propagate_prefixed_error(error_r, error,
|
g_propagate_prefixed_error(error_r, error,
|
||||||
"Failed to listen on %s (line %i): ",
|
"Failed to listen on %s (line %i): ",
|
||||||
param->value, param->line);
|
param->value, param->line);
|
||||||
@ -130,8 +129,9 @@ listen_global_init(GError **error_r)
|
|||||||
/* no "bind_to_address" configured, bind the
|
/* no "bind_to_address" configured, bind the
|
||||||
configured port on all interfaces */
|
configured port on all interfaces */
|
||||||
|
|
||||||
success = server_socket_add_port(listen_socket, port, error_r);
|
success = listen_socket->AddPort(port, error_r);
|
||||||
if (!success) {
|
if (!success) {
|
||||||
|
delete listen_socket;
|
||||||
g_propagate_prefixed_error(error_r, error,
|
g_propagate_prefixed_error(error_r, error,
|
||||||
"Failed to listen on *:%d: ",
|
"Failed to listen on *:%d: ",
|
||||||
port);
|
port);
|
||||||
@ -139,8 +139,10 @@ listen_global_init(GError **error_r)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!server_socket_open(listen_socket, error_r))
|
if (!listen_socket->Open(error_r)) {
|
||||||
|
delete listen_socket;
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
listen_port = port;
|
listen_port = port;
|
||||||
return true;
|
return true;
|
||||||
@ -152,5 +154,5 @@ void listen_global_finish(void)
|
|||||||
|
|
||||||
assert(listen_socket != NULL);
|
assert(listen_socket != NULL);
|
||||||
|
|
||||||
server_socket_free(listen_socket);
|
delete listen_socket;
|
||||||
}
|
}
|
||||||
|
@ -30,8 +30,6 @@
|
|||||||
#include "resolver.h"
|
#include "resolver.h"
|
||||||
#include "fd_util.h"
|
#include "fd_util.h"
|
||||||
|
|
||||||
#include <forward_list>
|
|
||||||
|
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
@ -55,8 +53,8 @@
|
|||||||
|
|
||||||
#define DEFAULT_PORT 6600
|
#define DEFAULT_PORT 6600
|
||||||
|
|
||||||
struct OneServerSocket final : private SocketMonitor {
|
class OneServerSocket final : private SocketMonitor {
|
||||||
const server_socket &parent;
|
const ServerSocket &parent;
|
||||||
|
|
||||||
const unsigned serial;
|
const unsigned serial;
|
||||||
|
|
||||||
@ -65,7 +63,8 @@ struct OneServerSocket final : private SocketMonitor {
|
|||||||
size_t address_length;
|
size_t address_length;
|
||||||
struct sockaddr *address;
|
struct sockaddr *address;
|
||||||
|
|
||||||
OneServerSocket(EventLoop &_loop, const server_socket &_parent,
|
public:
|
||||||
|
OneServerSocket(EventLoop &_loop, const ServerSocket &_parent,
|
||||||
unsigned _serial,
|
unsigned _serial,
|
||||||
const struct sockaddr *_address,
|
const struct sockaddr *_address,
|
||||||
size_t _address_length)
|
size_t _address_length)
|
||||||
@ -87,6 +86,16 @@ struct OneServerSocket final : private SocketMonitor {
|
|||||||
g_free(address);
|
g_free(address);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unsigned GetSerial() const {
|
||||||
|
return serial;
|
||||||
|
}
|
||||||
|
|
||||||
|
void SetPath(const char *_path) {
|
||||||
|
assert(path == nullptr);
|
||||||
|
|
||||||
|
path = g_strdup(_path);
|
||||||
|
}
|
||||||
|
|
||||||
bool Open(GError **error_r);
|
bool Open(GError **error_r);
|
||||||
|
|
||||||
using SocketMonitor::Close;
|
using SocketMonitor::Close;
|
||||||
@ -104,44 +113,12 @@ private:
|
|||||||
virtual bool OnSocketReady(unsigned flags) override;
|
virtual bool OnSocketReady(unsigned flags) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct server_socket {
|
|
||||||
EventLoop &loop;
|
|
||||||
|
|
||||||
server_socket_callback_t callback;
|
|
||||||
void *callback_ctx;
|
|
||||||
|
|
||||||
std::forward_list<OneServerSocket> sockets;
|
|
||||||
|
|
||||||
unsigned next_serial;
|
|
||||||
|
|
||||||
server_socket(EventLoop &_loop,
|
|
||||||
server_socket_callback_t _callback, void *_callback_ctx)
|
|
||||||
:loop(_loop),
|
|
||||||
callback(_callback), callback_ctx(_callback_ctx),
|
|
||||||
next_serial(1) {}
|
|
||||||
|
|
||||||
void Close();
|
|
||||||
};
|
|
||||||
|
|
||||||
static GQuark
|
static GQuark
|
||||||
server_socket_quark(void)
|
server_socket_quark(void)
|
||||||
{
|
{
|
||||||
return g_quark_from_static_string("server_socket");
|
return g_quark_from_static_string("server_socket");
|
||||||
}
|
}
|
||||||
|
|
||||||
struct server_socket *
|
|
||||||
server_socket_new(EventLoop &loop,
|
|
||||||
server_socket_callback_t callback, void *callback_ctx)
|
|
||||||
{
|
|
||||||
return new server_socket(loop, callback, callback_ctx);
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
server_socket_free(struct server_socket *ss)
|
|
||||||
{
|
|
||||||
delete ss;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Wraper for sockaddr_to_string() which never fails.
|
* Wraper for sockaddr_to_string() which never fails.
|
||||||
*/
|
*/
|
||||||
@ -236,25 +213,36 @@ OneServerSocket::Open(GError **error_r)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ServerSocket::ServerSocket(EventLoop &_loop,
|
||||||
|
server_socket_callback_t _callback,
|
||||||
|
void *_callback_ctx)
|
||||||
|
:loop(_loop),
|
||||||
|
callback(_callback), callback_ctx(_callback_ctx),
|
||||||
|
next_serial(1) {}
|
||||||
|
|
||||||
|
/* this is just here to allow the OneServerSocket forward
|
||||||
|
declaration */
|
||||||
|
ServerSocket::~ServerSocket() {}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
server_socket_open(struct server_socket *ss, GError **error_r)
|
ServerSocket::Open(GError **error_r)
|
||||||
{
|
{
|
||||||
struct OneServerSocket *good = nullptr, *bad = nullptr;
|
OneServerSocket *good = nullptr, *bad = nullptr;
|
||||||
GError *last_error = nullptr;
|
GError *last_error = nullptr;
|
||||||
|
|
||||||
for (auto &i : ss->sockets) {
|
for (auto &i : sockets) {
|
||||||
assert(i.serial > 0);
|
assert(i.GetSerial() > 0);
|
||||||
assert(good == nullptr || i.serial <= good->serial);
|
assert(good == nullptr || i.GetSerial() <= good->GetSerial());
|
||||||
|
|
||||||
if (bad != nullptr && i.serial != bad->serial) {
|
if (bad != nullptr && i.GetSerial() != bad->GetSerial()) {
|
||||||
server_socket_close(ss);
|
Close();
|
||||||
g_propagate_error(error_r, last_error);
|
g_propagate_error(error_r, last_error);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
GError *error = nullptr;
|
GError *error = nullptr;
|
||||||
if (!i.Open(&error)) {
|
if (!i.Open(&error)) {
|
||||||
if (good != nullptr && good->serial == i.serial) {
|
if (good != nullptr && good->GetSerial() == i.GetSerial()) {
|
||||||
char *address_string = i.ToString();
|
char *address_string = i.ToString();
|
||||||
char *good_string = good->ToString();
|
char *good_string = good->ToString();
|
||||||
g_warning("bind to '%s' failed: %s "
|
g_warning("bind to '%s' failed: %s "
|
||||||
@ -291,7 +279,7 @@ server_socket_open(struct server_socket *ss, GError **error_r)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (bad != nullptr) {
|
if (bad != nullptr) {
|
||||||
server_socket_close(ss);
|
Close();
|
||||||
g_propagate_error(error_r, last_error);
|
g_propagate_error(error_r, last_error);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -300,35 +288,24 @@ server_socket_open(struct server_socket *ss, GError **error_r)
|
|||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
server_socket::Close()
|
ServerSocket::Close()
|
||||||
{
|
{
|
||||||
for (auto &i : sockets)
|
for (auto &i : sockets)
|
||||||
i.Close();
|
i.Close();
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
OneServerSocket &
|
||||||
server_socket_close(struct server_socket *ss)
|
ServerSocket::AddAddress(const sockaddr &address, size_t address_length)
|
||||||
{
|
{
|
||||||
ss->Close();
|
sockets.emplace_front(loop, *this, next_serial,
|
||||||
}
|
&address, address_length);
|
||||||
|
|
||||||
static OneServerSocket &
|
return sockets.front();
|
||||||
server_socket_add_address(struct server_socket *ss,
|
|
||||||
const struct sockaddr *address,
|
|
||||||
size_t address_length)
|
|
||||||
{
|
|
||||||
assert(ss != nullptr);
|
|
||||||
|
|
||||||
ss->sockets.emplace_front(ss->loop, *ss, ss->next_serial,
|
|
||||||
address, address_length);
|
|
||||||
|
|
||||||
return ss->sockets.front();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
server_socket_add_fd(struct server_socket *ss, int fd, GError **error_r)
|
ServerSocket::AddFD(int fd, GError **error_r)
|
||||||
{
|
{
|
||||||
assert(ss != nullptr);
|
|
||||||
assert(fd >= 0);
|
assert(fd >= 0);
|
||||||
|
|
||||||
struct sockaddr_storage address;
|
struct sockaddr_storage address;
|
||||||
@ -340,9 +317,8 @@ server_socket_add_fd(struct server_socket *ss, int fd, GError **error_r)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
OneServerSocket &s =
|
OneServerSocket &s = AddAddress((const sockaddr &)address,
|
||||||
server_socket_add_address(ss, (struct sockaddr *)&address,
|
address_length);
|
||||||
address_length);
|
|
||||||
s.SetFD(fd);
|
s.SetFD(fd);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@ -350,13 +326,8 @@ server_socket_add_fd(struct server_socket *ss, int fd, GError **error_r)
|
|||||||
|
|
||||||
#ifdef HAVE_TCP
|
#ifdef HAVE_TCP
|
||||||
|
|
||||||
/**
|
inline void
|
||||||
* Add a listener on a port on all IPv4 interfaces.
|
ServerSocket::AddPortIPv4(unsigned port)
|
||||||
*
|
|
||||||
* @param port the TCP port
|
|
||||||
*/
|
|
||||||
static void
|
|
||||||
server_socket_add_port_ipv4(struct server_socket *ss, unsigned port)
|
|
||||||
{
|
{
|
||||||
struct sockaddr_in sin;
|
struct sockaddr_in sin;
|
||||||
memset(&sin, 0, sizeof(sin));
|
memset(&sin, 0, sizeof(sin));
|
||||||
@ -364,34 +335,26 @@ server_socket_add_port_ipv4(struct server_socket *ss, unsigned port)
|
|||||||
sin.sin_family = AF_INET;
|
sin.sin_family = AF_INET;
|
||||||
sin.sin_addr.s_addr = INADDR_ANY;
|
sin.sin_addr.s_addr = INADDR_ANY;
|
||||||
|
|
||||||
server_socket_add_address(ss, (const struct sockaddr *)&sin,
|
AddAddress((const sockaddr &)sin, sizeof(sin));
|
||||||
sizeof(sin));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef HAVE_IPV6
|
#ifdef HAVE_IPV6
|
||||||
/**
|
inline void
|
||||||
* Add a listener on a port on all IPv6 interfaces.
|
ServerSocket::AddPortIPv6(unsigned port)
|
||||||
*
|
|
||||||
* @param port the TCP port
|
|
||||||
*/
|
|
||||||
static void
|
|
||||||
server_socket_add_port_ipv6(struct server_socket *ss, unsigned port)
|
|
||||||
{
|
{
|
||||||
struct sockaddr_in6 sin;
|
struct sockaddr_in6 sin;
|
||||||
memset(&sin, 0, sizeof(sin));
|
memset(&sin, 0, sizeof(sin));
|
||||||
sin.sin6_port = htons(port);
|
sin.sin6_port = htons(port);
|
||||||
sin.sin6_family = AF_INET6;
|
sin.sin6_family = AF_INET6;
|
||||||
|
|
||||||
server_socket_add_address(ss, (const struct sockaddr *)&sin,
|
AddAddress((const sockaddr &)sin, sizeof(sin));
|
||||||
sizeof(sin));
|
|
||||||
}
|
}
|
||||||
#endif /* HAVE_IPV6 */
|
#endif /* HAVE_IPV6 */
|
||||||
|
|
||||||
#endif /* HAVE_TCP */
|
#endif /* HAVE_TCP */
|
||||||
|
|
||||||
bool
|
bool
|
||||||
server_socket_add_port(struct server_socket *ss, unsigned port,
|
ServerSocket::AddPort(unsigned port, GError **error_r)
|
||||||
GError **error_r)
|
|
||||||
{
|
{
|
||||||
#ifdef HAVE_TCP
|
#ifdef HAVE_TCP
|
||||||
if (port == 0 || port > 0xffff) {
|
if (port == 0 || port > 0xffff) {
|
||||||
@ -401,15 +364,14 @@ server_socket_add_port(struct server_socket *ss, unsigned port,
|
|||||||
}
|
}
|
||||||
|
|
||||||
#ifdef HAVE_IPV6
|
#ifdef HAVE_IPV6
|
||||||
server_socket_add_port_ipv6(ss, port);
|
AddPortIPv6(port);
|
||||||
#endif
|
#endif
|
||||||
server_socket_add_port_ipv4(ss, port);
|
AddPortIPv4(port);
|
||||||
|
|
||||||
++ss->next_serial;
|
++next_serial;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
#else /* HAVE_TCP */
|
#else /* HAVE_TCP */
|
||||||
(void)ss;
|
|
||||||
(void)port;
|
(void)port;
|
||||||
|
|
||||||
g_set_error(error_r, server_socket_quark(), 0,
|
g_set_error(error_r, server_socket_quark(), 0,
|
||||||
@ -419,8 +381,7 @@ server_socket_add_port(struct server_socket *ss, unsigned port,
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
server_socket_add_host(struct server_socket *ss, const char *hostname,
|
ServerSocket::AddHost(const char *hostname, unsigned port, GError **error_r)
|
||||||
unsigned port, GError **error_r)
|
|
||||||
{
|
{
|
||||||
#ifdef HAVE_TCP
|
#ifdef HAVE_TCP
|
||||||
struct addrinfo *ai = resolve_host_port(hostname, port,
|
struct addrinfo *ai = resolve_host_port(hostname, port,
|
||||||
@ -430,15 +391,14 @@ server_socket_add_host(struct server_socket *ss, const char *hostname,
|
|||||||
return false;
|
return false;
|
||||||
|
|
||||||
for (const struct addrinfo *i = ai; i != nullptr; i = i->ai_next)
|
for (const struct addrinfo *i = ai; i != nullptr; i = i->ai_next)
|
||||||
server_socket_add_address(ss, i->ai_addr, i->ai_addrlen);
|
AddAddress(*i->ai_addr, i->ai_addrlen);
|
||||||
|
|
||||||
freeaddrinfo(ai);
|
freeaddrinfo(ai);
|
||||||
|
|
||||||
++ss->next_serial;
|
++next_serial;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
#else /* HAVE_TCP */
|
#else /* HAVE_TCP */
|
||||||
(void)ss;
|
|
||||||
(void)hostname;
|
(void)hostname;
|
||||||
(void)port;
|
(void)port;
|
||||||
|
|
||||||
@ -449,8 +409,7 @@ server_socket_add_host(struct server_socket *ss, const char *hostname,
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
server_socket_add_path(struct server_socket *ss, const char *path,
|
ServerSocket::AddPath(const char *path, GError **error_r)
|
||||||
GError **error_r)
|
|
||||||
{
|
{
|
||||||
#ifdef HAVE_UN
|
#ifdef HAVE_UN
|
||||||
struct sockaddr_un s_un;
|
struct sockaddr_un s_un;
|
||||||
@ -467,14 +426,11 @@ server_socket_add_path(struct server_socket *ss, const char *path,
|
|||||||
s_un.sun_family = AF_UNIX;
|
s_un.sun_family = AF_UNIX;
|
||||||
memcpy(s_un.sun_path, path, path_length + 1);
|
memcpy(s_un.sun_path, path, path_length + 1);
|
||||||
|
|
||||||
OneServerSocket &s =
|
OneServerSocket &s = AddAddress((const sockaddr &)s_un, sizeof(s_un));
|
||||||
server_socket_add_address(ss, (const struct sockaddr *)&s_un,
|
s.SetPath(path);
|
||||||
sizeof(s_un));
|
|
||||||
s.path = g_strdup(path);
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
#else /* !HAVE_UN */
|
#else /* !HAVE_UN */
|
||||||
(void)ss;
|
|
||||||
(void)path;
|
(void)path;
|
||||||
|
|
||||||
g_set_error(error_r, server_socket_quark(), 0,
|
g_set_error(error_r, server_socket_quark(), 0,
|
||||||
|
@ -22,6 +22,8 @@
|
|||||||
|
|
||||||
#include "gerror.h"
|
#include "gerror.h"
|
||||||
|
|
||||||
|
#include <forward_list>
|
||||||
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
struct sockaddr;
|
struct sockaddr;
|
||||||
@ -32,63 +34,84 @@ typedef void (*server_socket_callback_t)(int fd,
|
|||||||
size_t address_length, int uid,
|
size_t address_length, int uid,
|
||||||
void *ctx);
|
void *ctx);
|
||||||
|
|
||||||
struct server_socket *
|
class OneServerSocket;
|
||||||
server_socket_new(EventLoop &loop,
|
|
||||||
server_socket_callback_t callback, void *callback_ctx);
|
|
||||||
|
|
||||||
void
|
class ServerSocket {
|
||||||
server_socket_free(struct server_socket *ss);
|
friend class OneServerSocket;
|
||||||
|
|
||||||
bool
|
EventLoop &loop;
|
||||||
server_socket_open(struct server_socket *ss, GError **error_r);
|
|
||||||
|
|
||||||
void
|
server_socket_callback_t callback;
|
||||||
server_socket_close(struct server_socket *ss);
|
void *callback_ctx;
|
||||||
|
|
||||||
/**
|
std::forward_list<OneServerSocket> sockets;
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
bool
|
|
||||||
server_socket_add_fd(struct server_socket *ss, int fd, GError **error_r);
|
|
||||||
|
|
||||||
/**
|
unsigned next_serial;
|
||||||
* Add a listener on a port on all interfaces.
|
|
||||||
*
|
|
||||||
* @param port the TCP port
|
|
||||||
* @param error_r location to store the error occurring, or NULL to
|
|
||||||
* ignore errors
|
|
||||||
* @return true on success
|
|
||||||
*/
|
|
||||||
bool
|
|
||||||
server_socket_add_port(struct server_socket *ss, unsigned port,
|
|
||||||
GError **error_r);
|
|
||||||
|
|
||||||
/**
|
public:
|
||||||
* Resolves a host name, and adds listeners on all addresses in the
|
ServerSocket(EventLoop &_loop,
|
||||||
* result set.
|
server_socket_callback_t _callback, void *_callback_ctx);
|
||||||
*
|
~ServerSocket();
|
||||||
* @param hostname the host name to be resolved
|
|
||||||
* @param port the TCP port
|
|
||||||
* @param error_r location to store the error occurring, or NULL to
|
|
||||||
* ignore errors
|
|
||||||
* @return true on success
|
|
||||||
*/
|
|
||||||
bool
|
|
||||||
server_socket_add_host(struct server_socket *ss, const char *hostname,
|
|
||||||
unsigned port, GError **error_r);
|
|
||||||
|
|
||||||
/**
|
private:
|
||||||
* Add a listener on a Unix domain socket.
|
OneServerSocket &AddAddress(const sockaddr &address, size_t length);
|
||||||
*
|
|
||||||
* @param path the absolute socket path
|
/**
|
||||||
* @param error_r location to store the error occurring, or NULL to
|
* Add a listener on a port on all IPv4 interfaces.
|
||||||
* ignore errors
|
*
|
||||||
* @return true on success
|
* @param port the TCP port
|
||||||
*/
|
*/
|
||||||
bool
|
void AddPortIPv4(unsigned port);
|
||||||
server_socket_add_path(struct server_socket *ss, const char *path,
|
|
||||||
GError **error_r);
|
/**
|
||||||
|
* 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
|
||||||
|
* @param error_r location to store the error occurring, or NULL to
|
||||||
|
* ignore errors
|
||||||
|
* @return true on success
|
||||||
|
*/
|
||||||
|
bool AddPort(unsigned port, GError **error_r);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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
|
||||||
|
* @param error_r location to store the error occurring, or NULL to
|
||||||
|
* ignore errors
|
||||||
|
* @return true on success
|
||||||
|
*/
|
||||||
|
bool AddHost(const char *hostname, unsigned port, GError **error_r);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a listener on a Unix domain socket.
|
||||||
|
*
|
||||||
|
* @param path the absolute socket path
|
||||||
|
* @param error_r location to store the error occurring, or NULL to
|
||||||
|
* ignore errors
|
||||||
|
* @return true on success
|
||||||
|
*/
|
||||||
|
bool AddPath(const char *path, GError **error_r);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
bool AddFD(int fd, GError **error_r);
|
||||||
|
|
||||||
|
bool Open(GError **error_r);
|
||||||
|
void Close();
|
||||||
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -36,6 +36,7 @@
|
|||||||
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
class ServerSocket;
|
||||||
class HttpdClient;
|
class HttpdClient;
|
||||||
|
|
||||||
struct HttpdOutput {
|
struct HttpdOutput {
|
||||||
@ -80,7 +81,7 @@ struct HttpdOutput {
|
|||||||
/**
|
/**
|
||||||
* The listener socket.
|
* The listener socket.
|
||||||
*/
|
*/
|
||||||
struct server_socket *server_socket;
|
ServerSocket *server_socket;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The header page, which is sent to every client on connect.
|
* The header page, which is sent to every client on connect.
|
||||||
|
@ -64,7 +64,7 @@ HttpdOutput::Bind(GError **error_r)
|
|||||||
open = false;
|
open = false;
|
||||||
|
|
||||||
const ScopeLock protect(mutex);
|
const ScopeLock protect(mutex);
|
||||||
return server_socket_open(server_socket, error_r);
|
return server_socket->Open(error_r);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void
|
inline void
|
||||||
@ -73,7 +73,7 @@ HttpdOutput::Unbind()
|
|||||||
assert(!open);
|
assert(!open);
|
||||||
|
|
||||||
const ScopeLock protect(mutex);
|
const ScopeLock protect(mutex);
|
||||||
server_socket_close(server_socket);
|
server_socket->Close();
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct audio_output *
|
static struct audio_output *
|
||||||
@ -112,16 +112,15 @@ httpd_output_init(const struct config_param *param,
|
|||||||
|
|
||||||
/* set up bind_to_address */
|
/* set up bind_to_address */
|
||||||
|
|
||||||
httpd->server_socket = server_socket_new(*main_loop,
|
httpd->server_socket = new ServerSocket(*main_loop,
|
||||||
httpd_listen_in_event, httpd);
|
httpd_listen_in_event, httpd);
|
||||||
|
|
||||||
const char *bind_to_address =
|
const char *bind_to_address =
|
||||||
config_get_block_string(param, "bind_to_address", NULL);
|
config_get_block_string(param, "bind_to_address", NULL);
|
||||||
bool success = bind_to_address != NULL &&
|
bool success = bind_to_address != NULL &&
|
||||||
strcmp(bind_to_address, "any") != 0
|
strcmp(bind_to_address, "any") != 0
|
||||||
? server_socket_add_host(httpd->server_socket, bind_to_address,
|
? httpd->server_socket->AddHost(bind_to_address, port, error)
|
||||||
port, error)
|
: httpd->server_socket->AddPort(port, error);
|
||||||
: server_socket_add_port(httpd->server_socket, port, error);
|
|
||||||
if (!success) {
|
if (!success) {
|
||||||
ao_base_finish(&httpd->base);
|
ao_base_finish(&httpd->base);
|
||||||
g_free(httpd);
|
g_free(httpd);
|
||||||
@ -159,7 +158,7 @@ httpd_output_finish(struct audio_output *ao)
|
|||||||
httpd->metadata->Unref();
|
httpd->metadata->Unref();
|
||||||
|
|
||||||
encoder_finish(httpd->encoder);
|
encoder_finish(httpd->encoder);
|
||||||
server_socket_free(httpd->server_socket);
|
delete httpd->server_socket;
|
||||||
ao_base_finish(&httpd->base);
|
ao_base_finish(&httpd->base);
|
||||||
delete httpd;
|
delete httpd;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user