summaryrefslogtreecommitdiffstats
path: root/src/pulsecore/socket-client.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2007-10-28 19:13:50 +0000
committerLennart Poettering <lennart@poettering.net>2007-10-28 19:13:50 +0000
commita67c21f093202f142438689d3f7cfbdf4ea82eea (patch)
tree5c3295037f033904bc11ab8b3adae5b7331101e9 /src/pulsecore/socket-client.c
parent6687dd013169fd8436aa1b45ccdacff074a40d05 (diff)
merge 'lennart' branch back into trunk.
git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@1971 fefdeb5f-60dc-0310-8127-8f9354f1896f
Diffstat (limited to 'src/pulsecore/socket-client.c')
-rw-r--r--src/pulsecore/socket-client.c180
1 files changed, 110 insertions, 70 deletions
diff --git a/src/pulsecore/socket-client.c b/src/pulsecore/socket-client.c
index b99c8025..5b5bc5ca 100644
--- a/src/pulsecore/socket-client.c
+++ b/src/pulsecore/socket-client.c
@@ -1,25 +1,25 @@
/* $Id$ */
/***
- This file is part of PulseAudio.
+ This file is part of PulseAudio.
- Copyright 2004-2006 Lennart Poettering
- Copyright 2006-2007 Pierre Ossman <ossman@cendio.se> for Cendio AB
+ Copyright 2004-2006 Lennart Poettering
+ Copyright 2006-2007 Pierre Ossman <ossman@cendio.se> for Cendio AB
- PulseAudio is free software; you can redistribute it and/or modify
- it under the terms of the GNU Lesser General Public License as
- published by the Free Software Foundation; either version 2.1 of the
- License, or (at your option) any later version.
+ PulseAudio is free software; you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as
+ published by the Free Software Foundation; either version 2.1 of the
+ License, or (at your option) any later version.
- PulseAudio 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
- Lesser General Public License for more details.
+ PulseAudio 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
+ Lesser General Public License for more details.
- You should have received a copy of the GNU Lesser General Public
- License along with PulseAudio; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
- USA.
+ You should have received a copy of the GNU Lesser General Public
+ License along with PulseAudio; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
***/
#ifdef HAVE_CONFIG_H
@@ -32,7 +32,6 @@
#include <stdio.h>
#include <errno.h>
#include <string.h>
-#include <assert.h>
#include <stdlib.h>
#ifdef HAVE_SYS_SOCKET_H
@@ -55,23 +54,24 @@
#include <asyncns.h>
#endif
-#include "winsock.h"
-
#include <pulse/timeval.h>
#include <pulse/xmalloc.h>
+#include <pulsecore/winsock.h>
#include <pulsecore/core-error.h>
#include <pulsecore/socket-util.h>
#include <pulsecore/core-util.h>
#include <pulsecore/log.h>
#include <pulsecore/parseaddr.h>
+#include <pulsecore/macro.h>
+#include <pulsecore/refcnt.h>
#include "socket-client.h"
#define CONNECT_TIMEOUT 5
struct pa_socket_client {
- int ref;
+ PA_REFCNT_DECLARE;
pa_mainloop_api *mainloop;
int fd;
pa_io_event *io_event;
@@ -89,10 +89,10 @@ struct pa_socket_client {
static pa_socket_client*pa_socket_client_new(pa_mainloop_api *m) {
pa_socket_client *c;
- assert(m);
+ pa_assert(m);
- c = pa_xmalloc(sizeof(pa_socket_client));
- c->ref = 1;
+ c = pa_xnew(pa_socket_client, 1);
+ PA_REFCNT_INIT(c);
c->mainloop = m;
c->fd = -1;
c->io_event = NULL;
@@ -112,7 +112,7 @@ static pa_socket_client*pa_socket_client_new(pa_mainloop_api *m) {
}
static void free_events(pa_socket_client *c) {
- assert(c);
+ pa_assert(c);
if (c->io_event) {
c->mainloop->io_free(c->io_event);
@@ -134,7 +134,10 @@ static void do_call(pa_socket_client *c) {
pa_iochannel *io = NULL;
int error;
socklen_t lerror;
- assert(c && c->callback);
+
+ pa_assert(c);
+ pa_assert(PA_REFCNT_VALUE(c) >= 1);
+ pa_assert(c->callback);
pa_socket_client_ref(c);
@@ -153,13 +156,13 @@ static void do_call(pa_socket_client *c) {
}
if (error != 0) {
- pa_log_debug("connect(): %s", pa_cstrerror(errno));
+ pa_log_debug("connect(): %s", pa_cstrerror(error));
errno = error;
goto finish;
}
io = pa_iochannel_new(c->mainloop, c->fd, c->fd);
- assert(io);
+ pa_assert(io);
finish:
if (!io && c->fd >= 0)
@@ -168,7 +171,7 @@ finish:
free_events(c);
- assert(c->callback);
+ pa_assert(c->callback);
c->callback(c, io, c->userdata);
pa_socket_client_unref(c);
@@ -176,21 +179,36 @@ finish:
static void connect_fixed_cb(pa_mainloop_api *m, pa_defer_event *e, void *userdata) {
pa_socket_client *c = userdata;
- assert(m && c && c->defer_event == e);
+
+ pa_assert(m);
+ pa_assert(c);
+ pa_assert(PA_REFCNT_VALUE(c) >= 1);
+ pa_assert(c->defer_event == e);
+
do_call(c);
}
static void connect_io_cb(pa_mainloop_api*m, pa_io_event *e, int fd, PA_GCC_UNUSED pa_io_event_flags_t f, void *userdata) {
pa_socket_client *c = userdata;
- assert(m && c && c->io_event == e && fd >= 0);
+
+ pa_assert(m);
+ pa_assert(c);
+ pa_assert(PA_REFCNT_VALUE(c) >= 1);
+ pa_assert(c->io_event == e);
+ pa_assert(fd >= 0);
+
do_call(c);
}
static int do_connect(pa_socket_client *c, const struct sockaddr *sa, socklen_t len) {
int r;
- assert(c && sa && len);
- pa_make_nonblock_fd(c->fd);
+ pa_assert(c);
+ pa_assert(PA_REFCNT_VALUE(c) >= 1);
+ pa_assert(sa);
+ pa_assert(len > 0);
+
+ pa_make_fd_nonblock(c->fd);
if ((r = connect(c->fd, sa, len)) < 0) {
#ifdef OS_IS_WIN32
@@ -203,19 +221,18 @@ static int do_connect(pa_socket_client *c, const struct sockaddr *sa, socklen_t
return -1;
}
- c->io_event = c->mainloop->io_new(c->mainloop, c->fd, PA_IO_EVENT_OUTPUT, connect_io_cb, c);
- assert(c->io_event);
- } else {
- c->defer_event = c->mainloop->defer_new(c->mainloop, connect_fixed_cb, c);
- assert(c->defer_event);
- }
+ pa_assert_se(c->io_event = c->mainloop->io_new(c->mainloop, c->fd, PA_IO_EVENT_OUTPUT, connect_io_cb, c));
+ } else
+ pa_assert_se(c->defer_event = c->mainloop->defer_new(c->mainloop, connect_fixed_cb, c));
return 0;
}
pa_socket_client* pa_socket_client_new_ipv4(pa_mainloop_api *m, uint32_t address, uint16_t port) {
struct sockaddr_in sa;
- assert(m && port > 0);
+
+ pa_assert(m);
+ pa_assert(port > 0);
memset(&sa, 0, sizeof(sa));
sa.sin_family = AF_INET;
@@ -229,7 +246,9 @@ pa_socket_client* pa_socket_client_new_ipv4(pa_mainloop_api *m, uint32_t address
pa_socket_client* pa_socket_client_new_unix(pa_mainloop_api *m, const char *filename) {
struct sockaddr_un sa;
- assert(m && filename);
+
+ pa_assert(m);
+ pa_assert(filename);
memset(&sa, 0, sizeof(sa));
sa.sun_family = AF_UNIX;
@@ -248,9 +267,9 @@ pa_socket_client* pa_socket_client_new_unix(pa_mainloop_api *m, const char *file
#endif /* HAVE_SYS_UN_H */
static int sockaddr_prepare(pa_socket_client *c, const struct sockaddr *sa, size_t salen) {
- assert(c);
- assert(sa);
- assert(salen);
+ pa_assert(c);
+ pa_assert(sa);
+ pa_assert(salen);
switch (sa->sa_family) {
case AF_UNIX:
@@ -274,11 +293,11 @@ static int sockaddr_prepare(pa_socket_client *c, const struct sockaddr *sa, size
return -1;
}
- pa_fd_set_cloexec(c->fd, 1);
+ pa_make_fd_cloexec(c->fd);
if (sa->sa_family == AF_INET || sa->sa_family == AF_INET6)
- pa_socket_tcp_low_delay(c->fd);
+ pa_make_tcp_socket_low_delay(c->fd);
else
- pa_socket_low_delay(c->fd);
+ pa_make_socket_low_delay(c->fd);
if (do_connect(c, sa, salen) < 0)
return -1;
@@ -288,9 +307,12 @@ static int sockaddr_prepare(pa_socket_client *c, const struct sockaddr *sa, size
pa_socket_client* pa_socket_client_new_sockaddr(pa_mainloop_api *m, const struct sockaddr *sa, size_t salen) {
pa_socket_client *c;
- assert(m && sa);
- c = pa_socket_client_new(m);
- assert(c);
+
+ pa_assert(m);
+ pa_assert(sa);
+ pa_assert(salen > 0);
+
+ pa_assert_se(c = pa_socket_client_new(m));
if (sockaddr_prepare(c, sa, salen) < 0)
goto fail;
@@ -300,12 +322,11 @@ pa_socket_client* pa_socket_client_new_sockaddr(pa_mainloop_api *m, const struct
fail:
pa_socket_client_unref(c);
return NULL;
-
}
static void socket_client_free(pa_socket_client *c) {
- assert(c && c->mainloop);
-
+ pa_assert(c);
+ pa_assert(c->mainloop);
free_events(c);
@@ -325,20 +346,25 @@ static void socket_client_free(pa_socket_client *c) {
}
void pa_socket_client_unref(pa_socket_client *c) {
- assert(c && c->ref >= 1);
+ pa_assert(c);
+ pa_assert(PA_REFCNT_VALUE(c) >= 1);
- if (!(--(c->ref)))
+ if (PA_REFCNT_DEC(c) <= 0)
socket_client_free(c);
}
pa_socket_client* pa_socket_client_ref(pa_socket_client *c) {
- assert(c && c->ref >= 1);
- c->ref++;
+ pa_assert(c);
+ pa_assert(PA_REFCNT_VALUE(c) >= 1);
+
+ PA_REFCNT_INC(c);
return c;
}
void pa_socket_client_set_callback(pa_socket_client *c, void (*on_connection)(pa_socket_client *c, pa_iochannel*io, void *userdata), void *userdata) {
- assert(c);
+ pa_assert(c);
+ pa_assert(PA_REFCNT_VALUE(c) >= 1);
+
c->callback = on_connection;
c->userdata = userdata;
}
@@ -346,6 +372,10 @@ void pa_socket_client_set_callback(pa_socket_client *c, void (*on_connection)(pa
pa_socket_client* pa_socket_client_new_ipv6(pa_mainloop_api *m, uint8_t address[16], uint16_t port) {
struct sockaddr_in6 sa;
+ pa_assert(m);
+ pa_assert(address);
+ pa_assert(port > 0);
+
memset(&sa, 0, sizeof(sa));
sa.sin6_family = AF_INET6;
sa.sin6_port = htons(port);
@@ -360,7 +390,12 @@ static void asyncns_cb(pa_mainloop_api*m, pa_io_event *e, int fd, PA_GCC_UNUSED
pa_socket_client *c = userdata;
struct addrinfo *res = NULL;
int ret;
- assert(m && c && c->asyncns_io_event == e && fd >= 0);
+
+ pa_assert(m);
+ pa_assert(c);
+ pa_assert(PA_REFCNT_VALUE(c) >= 1);
+ pa_assert(c->asyncns_io_event == e);
+ pa_assert(fd >= 0);
if (asyncns_wait(c->asyncns, 0) < 0)
goto fail;
@@ -397,10 +432,11 @@ fail:
static void timeout_cb(pa_mainloop_api *m, pa_time_event *e, const struct timeval *tv, void *userdata) {
pa_socket_client *c = userdata;
- assert(m);
- assert(e);
- assert(tv);
- assert(c);
+
+ pa_assert(m);
+ pa_assert(e);
+ pa_assert(tv);
+ pa_assert(c);
if (c->fd >= 0) {
pa_close(c->fd);
@@ -413,8 +449,8 @@ static void timeout_cb(pa_mainloop_api *m, pa_time_event *e, const struct timeva
static void start_timeout(pa_socket_client *c) {
struct timeval tv;
- assert(c);
- assert(!c->timeout_event);
+ pa_assert(c);
+ pa_assert(!c->timeout_event);
pa_gettimeofday(&tv);
pa_timeval_add(&tv, CONNECT_TIMEOUT * 1000000);
@@ -424,7 +460,9 @@ static void start_timeout(pa_socket_client *c) {
pa_socket_client* pa_socket_client_new_string(pa_mainloop_api *m, const char*name, uint16_t default_port) {
pa_socket_client *c = NULL;
pa_parsed_address a;
- assert(m && name);
+
+ pa_assert(m);
+ pa_assert(name);
if (pa_parse_address(name, &a) < 0)
return NULL;
@@ -435,7 +473,7 @@ pa_socket_client* pa_socket_client_new_string(pa_mainloop_api *m, const char*nam
switch (a.type) {
case PA_PARSED_ADDRESS_UNIX:
if ((c = pa_socket_client_new_unix(m, a.path_or_host)))
- start_timeout(c);
+ start_timeout(c);
break;
case PA_PARSED_ADDRESS_TCP4: /* Fallthrough */
@@ -445,7 +483,7 @@ pa_socket_client* pa_socket_client_new_string(pa_mainloop_api *m, const char*nam
struct addrinfo hints;
char port[12];
- snprintf(port, sizeof(port), "%u", (unsigned) a.port);
+ pa_snprintf(port, sizeof(port), "%u", (unsigned) a.port);
memset(&hints, 0, sizeof(hints));
hints.ai_family = a.type == PA_PARSED_ADDRESS_TCP4 ? PF_INET : (a.type == PA_PARSED_ADDRESS_TCP6 ? PF_INET6 : PF_UNSPEC);
@@ -462,7 +500,7 @@ pa_socket_client* pa_socket_client_new_string(pa_mainloop_api *m, const char*nam
c->asyncns = asyncns;
c->asyncns_io_event = m->io_new(m, asyncns_fd(c->asyncns), PA_IO_EVENT_INPUT, asyncns_cb, c);
c->asyncns_query = asyncns_getaddrinfo(c->asyncns, a.path_or_host, port, &hints);
- assert(c->asyncns_query);
+ pa_assert(c->asyncns_query);
start_timeout(c);
}
#else /* HAVE_LIBASYNCNS */
@@ -479,7 +517,7 @@ pa_socket_client* pa_socket_client_new_string(pa_mainloop_api *m, const char*nam
if (res->ai_addr) {
if ((c = pa_socket_client_new_sockaddr(m, res->ai_addr, res->ai_addrlen)))
start_timeout(c);
- }
+ }
freeaddrinfo(res);
#else /* HAVE_GETADDRINFO */
@@ -507,7 +545,7 @@ pa_socket_client* pa_socket_client_new_string(pa_mainloop_api *m, const char*nam
s.sin_port = htons(a.port);
if ((c = pa_socket_client_new_sockaddr(m, (struct sockaddr*)&s, sizeof(s))))
- start_timeout(c);
+ start_timeout(c);
#endif /* HAVE_GETADDRINFO */
}
#endif /* HAVE_LIBASYNCNS */
@@ -524,6 +562,8 @@ finish:
local. "local" means UNIX socket or TCP socket on localhost. Other
local IP addresses are not considered local. */
int pa_socket_client_is_local(pa_socket_client *c) {
- assert(c);
+ pa_assert(c);
+ pa_assert(PA_REFCNT_VALUE(c) >= 1);
+
return c->local;
}